1 /*
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
20 #include <vector>
21
22 #include <utils/KeyedVector.h>
23
24 #include <SkScalar.h>
25 #include <SkGlyphCache.h>
26 #include <SkScalerContext.h>
27 #include <SkPaint.h>
28 #include <SkPathMeasure.h>
29
30 #include "FontUtil.h"
31 #include "../Rect.h"
32 #include "../Matrix.h"
33
34 namespace android {
35 namespace uirenderer {
36
37 ///////////////////////////////////////////////////////////////////////////////
38 // Font
39 ///////////////////////////////////////////////////////////////////////////////
40
41 struct CachedGlyphInfo;
42 class CacheTexture;
43 class FontRenderer;
44
45 /**
46 * Represents a font, defined by a Skia font id and a font size. A font is used
47 * to generate glyphs and cache them in the FontState.
48 */
49 class Font {
50 public:
51 enum Style {
52 kFakeBold = 1
53 };
54
55 struct FontDescription {
56 FontDescription(const SkPaint* paint, const SkMatrix& matrix);
57
58 static int compare(const FontDescription& lhs, const FontDescription& rhs);
59
60 hash_t hash() const;
61
62 bool operator==(const FontDescription& other) const {
63 return compare(*this, other) == 0;
64 }
65
66 bool operator!=(const FontDescription& other) const {
67 return compare(*this, other) != 0;
68 }
69
70 SkFontID mFontId;
71 float mFontSize;
72 int mFlags;
73 float mItalicStyle;
74 float mScaleX;
75 uint8_t mStyle;
76 float mStrokeWidth;
77 bool mAntiAliasing;
78 uint8_t mHinting;
79 SkMatrix mLookupTransform;
80 SkMatrix mInverseLookupTransform;
81 };
82
83 ~Font();
84
85 void render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
86 int numGlyphs, int x, int y, const float* positions);
87
88 void render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
89 int numGlyphs, const SkPath* path, float hOffset, float vOffset);
90
getDescription()91 const Font::FontDescription& getDescription() const {
92 return mDescription;
93 }
94
95 /**
96 * Creates a new font associated with the specified font state.
97 */
98 static Font* create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix);
99
100 private:
101 friend class FontRenderer;
102
103 Font(FontRenderer* state, const Font::FontDescription& desc);
104
105 typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*,
106 uint32_t, uint32_t, Rect*, const float*);
107
108 enum RenderMode {
109 FRAMEBUFFER,
110 BITMAP,
111 MEASURE,
112 };
113
114 void precache(const SkPaint* paint, const char* text, int numGlyphs);
115
116 void render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
117 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
118 uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions);
119
120 void measure(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
121 int numGlyphs, Rect *bounds, const float* positions);
122
123 void invalidateTextureCache(CacheTexture* cacheTexture = nullptr);
124
125 CachedGlyphInfo* cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching);
126 void updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph,
127 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching);
128
129 void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
130 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
131 Rect* bounds, const float* pos);
132 void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
133 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
134 Rect* bounds, const float* pos);
135 void drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
136 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
137 Rect* bounds, const float* pos);
138 void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
139 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
140 Rect* bounds, const float* pos);
141 void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
142 SkPathMeasure& measure, SkPoint* position, SkVector* tangent);
143
144 CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit,
145 bool precaching = false);
146
147 FontRenderer* mState;
148 FontDescription mDescription;
149
150 // Cache of glyphs
151 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
152
153 bool mIdentityTransform;
154 };
155
strictly_order_type(const Font::FontDescription & lhs,const Font::FontDescription & rhs)156 inline int strictly_order_type(const Font::FontDescription& lhs,
157 const Font::FontDescription& rhs) {
158 return Font::FontDescription::compare(lhs, rhs) < 0;
159 }
160
compare_type(const Font::FontDescription & lhs,const Font::FontDescription & rhs)161 inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
162 return Font::FontDescription::compare(lhs, rhs);
163 }
164
hash_type(const Font::FontDescription & entry)165 inline hash_t hash_type(const Font::FontDescription& entry) {
166 return entry.hash();
167 }
168
169 }; // namespace uirenderer
170 }; // namespace android
171
172 #endif // ANDROID_HWUI_FONT_H
173