blob: 504dabb5bcc0f104c18e340ff74cf6ddfcd934f0 [file] [log] [blame]
Romain Guy9f5dab32012-09-04 12:55:44 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HWUI_FONT_H
18#define ANDROID_HWUI_FONT_H
19
Chris Craik59744b72014-07-01 17:56:52 -070020#include <vector>
21
Romain Guy9f5dab32012-09-04 12:55:44 -070022#include <utils/KeyedVector.h>
23
Victoria Leaseb66270e2014-04-22 15:00:31 -070024#include <SkScalar.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070025#include <SkPaint.h>
26#include <SkPathMeasure.h>
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050027#include <SkTypeface.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070028
Tom Hudson2dc236b2014-10-15 15:46:42 -040029#include "FontUtil.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070030#include "../Rect.h"
Romain Guye3a9b242013-01-08 11:15:30 -080031#include "../Matrix.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070032
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050033class SkGlyphCache;
34
Romain Guy9f5dab32012-09-04 12:55:44 -070035namespace android {
36namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
39// Font
40///////////////////////////////////////////////////////////////////////////////
41
Chris Craike84a2082014-12-22 14:28:49 -080042struct CachedGlyphInfo;
Tom Hudson2dc236b2014-10-15 15:46:42 -040043class CacheTexture;
Romain Guy9f5dab32012-09-04 12:55:44 -070044class FontRenderer;
45
46/**
47 * Represents a font, defined by a Skia font id and a font size. A font is used
48 * to generate glyphs and cache them in the FontState.
49 */
50class Font {
51public:
52 enum Style {
53 kFakeBold = 1
54 };
55
Romain Guye3a9b242013-01-08 11:15:30 -080056 struct FontDescription {
Chris Craik59744b72014-07-01 17:56:52 -070057 FontDescription(const SkPaint* paint, const SkMatrix& matrix);
Romain Guye3a9b242013-01-08 11:15:30 -080058
59 static int compare(const FontDescription& lhs, const FontDescription& rhs);
60
61 hash_t hash() const;
62
63 bool operator==(const FontDescription& other) const {
64 return compare(*this, other) == 0;
65 }
66
67 bool operator!=(const FontDescription& other) const {
68 return compare(*this, other) != 0;
69 }
70
71 SkFontID mFontId;
72 float mFontSize;
73 int mFlags;
74 float mItalicStyle;
75 float mScaleX;
76 uint8_t mStyle;
77 float mStrokeWidth;
Romain Guyb969a0d2013-02-05 14:38:40 -080078 bool mAntiAliasing;
Romain Guy2d5945e2013-06-18 12:59:25 -070079 uint8_t mHinting;
Romain Guyc74f45a2013-02-26 19:10:14 -080080 SkMatrix mLookupTransform;
Romain Guy874f5c62013-03-01 18:07:35 -080081 SkMatrix mInverseLookupTransform;
Romain Guye3a9b242013-01-08 11:15:30 -080082 };
83
Romain Guy9f5dab32012-09-04 12:55:44 -070084 ~Font();
85
Chris Craike8c3c812016-02-05 20:10:50 -080086 void render(const SkPaint* paint, const glyph_t* glyphs,
Romain Guy9f5dab32012-09-04 12:55:44 -070087 int numGlyphs, int x, int y, const float* positions);
88
Chris Craike8c3c812016-02-05 20:10:50 -080089 void render(const SkPaint* paint, const glyph_t* glyphs,
Chris Craikd218a922014-01-02 17:13:34 -080090 int numGlyphs, const SkPath* path, float hOffset, float vOffset);
Romain Guy9f5dab32012-09-04 12:55:44 -070091
Romain Guye3a9b242013-01-08 11:15:30 -080092 const Font::FontDescription& getDescription() const {
93 return mDescription;
94 }
95
Romain Guy9f5dab32012-09-04 12:55:44 -070096 /**
97 * Creates a new font associated with the specified font state.
98 */
Chris Craik59744b72014-07-01 17:56:52 -070099 static Font* create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix);
Romain Guy9f5dab32012-09-04 12:55:44 -0700100
101private:
102 friend class FontRenderer;
Romain Guye3a9b242013-01-08 11:15:30 -0800103
104 Font(FontRenderer* state, const Font::FontDescription& desc);
105
Romain Guy9f5dab32012-09-04 12:55:44 -0700106 typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*,
107 uint32_t, uint32_t, Rect*, const float*);
108
109 enum RenderMode {
110 FRAMEBUFFER,
111 BITMAP,
112 MEASURE,
113 };
114
Chris Craike8c3c812016-02-05 20:10:50 -0800115 void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs);
Romain Guy9f5dab32012-09-04 12:55:44 -0700116
Chris Craike8c3c812016-02-05 20:10:50 -0800117 void render(const SkPaint* paint, const glyph_t* glyphs,
Romain Guy9f5dab32012-09-04 12:55:44 -0700118 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
119 uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions);
120
Chris Craike8c3c812016-02-05 20:10:50 -0800121 void measure(const SkPaint* paint, const glyph_t* glyphs,
Romain Guy9f5dab32012-09-04 12:55:44 -0700122 int numGlyphs, Rect *bounds, const float* positions);
123
Chris Craike84a2082014-12-22 14:28:49 -0800124 void invalidateTextureCache(CacheTexture* cacheTexture = nullptr);
Romain Guy9f5dab32012-09-04 12:55:44 -0700125
Chris Craikd218a922014-01-02 17:13:34 -0800126 CachedGlyphInfo* cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching);
127 void updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph,
128 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700129
130 void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
131 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
132 Rect* bounds, const float* pos);
133 void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
134 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
135 Rect* bounds, const float* pos);
Romain Guy624234f2013-03-05 16:43:31 -0800136 void drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Romain Guya4adcf02013-02-28 12:15:35 -0800137 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
138 Rect* bounds, const float* pos);
Romain Guy9f5dab32012-09-04 12:55:44 -0700139 void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
140 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
141 Rect* bounds, const float* pos);
142 void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
143 SkPathMeasure& measure, SkPoint* position, SkVector* tangent);
144
Chris Craikd218a922014-01-02 17:13:34 -0800145 CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit,
146 bool precaching = false);
Romain Guy9f5dab32012-09-04 12:55:44 -0700147
148 FontRenderer* mState;
Romain Guye3a9b242013-01-08 11:15:30 -0800149 FontDescription mDescription;
150
151 // Cache of glyphs
152 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
Romain Guyc74f45a2013-02-26 19:10:14 -0800153
Romain Guy624234f2013-03-05 16:43:31 -0800154 bool mIdentityTransform;
Romain Guy9f5dab32012-09-04 12:55:44 -0700155};
156
Romain Guye3a9b242013-01-08 11:15:30 -0800157inline int strictly_order_type(const Font::FontDescription& lhs,
158 const Font::FontDescription& rhs) {
159 return Font::FontDescription::compare(lhs, rhs) < 0;
160}
161
162inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
163 return Font::FontDescription::compare(lhs, rhs);
164}
165
166inline hash_t hash_type(const Font::FontDescription& entry) {
167 return entry.hash();
168}
169
Romain Guy9f5dab32012-09-04 12:55:44 -0700170}; // namespace uirenderer
171}; // namespace android
172
173#endif // ANDROID_HWUI_FONT_H