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