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