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