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