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 | |
Victoria Lease | 2ee2d59 | 2013-12-17 13:54:29 -0800 | [diff] [blame] | 22 | #include <SkGlyphCache.h> |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 23 | #include <SkScalerContext.h> |
| 24 | #include <SkPaint.h> |
| 25 | #include <SkPathMeasure.h> |
| 26 | |
| 27 | #include "CachedGlyphInfo.h" |
| 28 | #include "../Rect.h" |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 29 | #include "../Matrix.h" |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 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 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 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; |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 72 | bool mAntiAliasing; |
Romain Guy | 2d5945e | 2013-06-18 12:59:25 -0700 | [diff] [blame] | 73 | uint8_t mHinting; |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 74 | SkMatrix mLookupTransform; |
Romain Guy | 874f5c6 | 2013-03-01 18:07:35 -0800 | [diff] [blame] | 75 | SkMatrix mInverseLookupTransform; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 78 | ~Font(); |
| 79 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 80 | void render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 81 | int numGlyphs, int x, int y, const float* positions); |
| 82 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 83 | void render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
| 84 | int numGlyphs, const SkPath* path, float hOffset, float vOffset); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 85 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 86 | const Font::FontDescription& getDescription() const { |
| 87 | return mDescription; |
| 88 | } |
| 89 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Creates a new font associated with the specified font state. |
| 92 | */ |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 93 | static Font* create(FontRenderer* state, const SkPaint* paint, const mat4& matrix); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | friend class FontRenderer; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 97 | |
| 98 | Font(FontRenderer* state, const Font::FontDescription& desc); |
| 99 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 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 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 109 | void precache(const SkPaint* paint, const char* text, int numGlyphs); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 110 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 111 | void render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 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 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 115 | void measure(const SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 116 | int numGlyphs, Rect *bounds, const float* positions); |
| 117 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 118 | void invalidateTextureCache(CacheTexture* cacheTexture = NULL); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 119 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 120 | CachedGlyphInfo* cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching); |
| 121 | void updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph, |
| 122 | SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 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); |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 130 | void drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y, |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 131 | uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH, |
| 132 | Rect* bounds, const float* pos); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 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 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 139 | CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit, |
| 140 | bool precaching = false); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 141 | |
| 142 | FontRenderer* mState; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 143 | FontDescription mDescription; |
| 144 | |
| 145 | // Cache of glyphs |
| 146 | DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs; |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 147 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 148 | bool mIdentityTransform; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 151 | inline int strictly_order_type(const Font::FontDescription& lhs, |
| 152 | const Font::FontDescription& rhs) { |
| 153 | return Font::FontDescription::compare(lhs, rhs) < 0; |
| 154 | } |
| 155 | |
| 156 | inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) { |
| 157 | return Font::FontDescription::compare(lhs, rhs); |
| 158 | } |
| 159 | |
| 160 | inline hash_t hash_type(const Font::FontDescription& entry) { |
| 161 | return entry.hash(); |
| 162 | } |
| 163 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 164 | }; // namespace uirenderer |
| 165 | }; // namespace android |
| 166 | |
| 167 | #endif // ANDROID_HWUI_FONT_H |