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 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 24 | #include <SkPaint.h> |
| 25 | #include <SkPathMeasure.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 26 | #include <SkScalar.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 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 29 | #include "../Matrix.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 30 | #include "../Rect.h" |
| 31 | #include "FontUtil.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: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 52 | enum Style { kFakeBold = 1 }; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 53 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 54 | struct FontDescription { |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 55 | FontDescription(const SkPaint* paint, const SkMatrix& matrix); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 56 | |
| 57 | static int compare(const FontDescription& lhs, const FontDescription& rhs); |
| 58 | |
| 59 | hash_t hash() const; |
| 60 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 61 | bool operator==(const FontDescription& other) const { return compare(*this, other) == 0; } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 62 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 63 | bool operator!=(const FontDescription& other) const { return compare(*this, other) != 0; } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 80 | void render(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, int x, int y, |
| 81 | const float* positions); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 82 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 83 | void render(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, const SkPath* path, |
| 84 | float hOffset, float vOffset); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 85 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 86 | const Font::FontDescription& getDescription() const { return mDescription; } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 87 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Creates a new font associated with the specified font state. |
| 90 | */ |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 91 | static Font* create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 92 | |
| 93 | private: |
| 94 | friend class FontRenderer; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 95 | |
| 96 | Font(FontRenderer* state, const Font::FontDescription& desc); |
| 97 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 98 | typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*, uint32_t, uint32_t, |
| 99 | Rect*, const float*); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 100 | |
| 101 | enum RenderMode { |
| 102 | FRAMEBUFFER, |
| 103 | BITMAP, |
| 104 | MEASURE, |
| 105 | }; |
| 106 | |
Chris Craik | e8c3c81 | 2016-02-05 20:10:50 -0800 | [diff] [blame] | 107 | void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 108 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 112 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 113 | void measure(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, Rect* bounds, |
| 114 | const float* positions); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 115 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 116 | void invalidateTextureCache(CacheTexture* cacheTexture = nullptr); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 117 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 118 | CachedGlyphInfo* cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching); |
| 119 | void updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 120 | SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 121 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 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); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 131 | void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 132 | SkPathMeasure& measure, SkPoint* position, SkVector* tangent); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 133 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 134 | CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 135 | bool precaching = false); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 136 | |
| 137 | FontRenderer* mState; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 138 | FontDescription mDescription; |
| 139 | |
| 140 | // Cache of glyphs |
| 141 | DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs; |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 142 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 143 | bool mIdentityTransform; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 146 | inline int strictly_order_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 147 | return Font::FontDescription::compare(lhs, rhs) < 0; |
| 148 | } |
| 149 | |
| 150 | inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) { |
| 151 | return Font::FontDescription::compare(lhs, rhs); |
| 152 | } |
| 153 | |
| 154 | inline hash_t hash_type(const Font::FontDescription& entry) { |
| 155 | return entry.hash(); |
| 156 | } |
| 157 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 158 | }; // namespace uirenderer |
| 159 | }; // namespace android |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 160 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 161 | #endif // ANDROID_HWUI_FONT_H |