blob: 85221bc069f1ebaaf72870142b011e7bace94c4d [file] [log] [blame]
Romain Guy9f5dab32012-09-04 12:55:44 -07001/*
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 Craik59744b72014-07-01 17:56:52 -070020#include <vector>
21
Romain Guy9f5dab32012-09-04 12:55:44 -070022#include <utils/KeyedVector.h>
23
Romain Guy9f5dab32012-09-04 12:55:44 -070024#include <SkPaint.h>
25#include <SkPathMeasure.h>
John Reck1bcacfd2017-11-03 10:12:19 -070026#include <SkScalar.h>
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050027#include <SkTypeface.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070028
Romain Guye3a9b242013-01-08 11:15:30 -080029#include "../Matrix.h"
John Reck1bcacfd2017-11-03 10:12:19 -070030#include "../Rect.h"
31#include "FontUtil.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070032
Leon Scroggins IIIee708fa2016-12-12 15:31:39 -050033class SkGlyphCache;
34
Romain Guy9f5dab32012-09-04 12:55:44 -070035namespace android {
36namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
39// Font
40///////////////////////////////////////////////////////////////////////////////
41
Chris Craike84a2082014-12-22 14:28:49 -080042struct CachedGlyphInfo;
Tom Hudson2dc236b2014-10-15 15:46:42 -040043class CacheTexture;
Romain Guy9f5dab32012-09-04 12:55:44 -070044class 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 */
50class Font {
51public:
John Reck1bcacfd2017-11-03 10:12:19 -070052 enum Style { kFakeBold = 1 };
Romain Guy9f5dab32012-09-04 12:55:44 -070053
Romain Guye3a9b242013-01-08 11:15:30 -080054 struct FontDescription {
Chris Craik59744b72014-07-01 17:56:52 -070055 FontDescription(const SkPaint* paint, const SkMatrix& matrix);
Romain Guye3a9b242013-01-08 11:15:30 -080056
57 static int compare(const FontDescription& lhs, const FontDescription& rhs);
58
59 hash_t hash() const;
60
John Reck1bcacfd2017-11-03 10:12:19 -070061 bool operator==(const FontDescription& other) const { return compare(*this, other) == 0; }
Romain Guye3a9b242013-01-08 11:15:30 -080062
John Reck1bcacfd2017-11-03 10:12:19 -070063 bool operator!=(const FontDescription& other) const { return compare(*this, other) != 0; }
Romain Guye3a9b242013-01-08 11:15:30 -080064
65 SkFontID mFontId;
66 float mFontSize;
67 int mFlags;
68 float mItalicStyle;
69 float mScaleX;
70 uint8_t mStyle;
71 float mStrokeWidth;
Romain Guyb969a0d2013-02-05 14:38:40 -080072 bool mAntiAliasing;
Romain Guy2d5945e2013-06-18 12:59:25 -070073 uint8_t mHinting;
Romain Guyc74f45a2013-02-26 19:10:14 -080074 SkMatrix mLookupTransform;
Romain Guy874f5c62013-03-01 18:07:35 -080075 SkMatrix mInverseLookupTransform;
Romain Guye3a9b242013-01-08 11:15:30 -080076 };
77
Romain Guy9f5dab32012-09-04 12:55:44 -070078 ~Font();
79
John Reck1bcacfd2017-11-03 10:12:19 -070080 void render(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, int x, int y,
81 const float* positions);
Romain Guy9f5dab32012-09-04 12:55:44 -070082
John Reck1bcacfd2017-11-03 10:12:19 -070083 void render(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, const SkPath* path,
84 float hOffset, float vOffset);
Romain Guy9f5dab32012-09-04 12:55:44 -070085
John Reck1bcacfd2017-11-03 10:12:19 -070086 const Font::FontDescription& getDescription() const { return mDescription; }
Romain Guye3a9b242013-01-08 11:15:30 -080087
Romain Guy9f5dab32012-09-04 12:55:44 -070088 /**
89 * Creates a new font associated with the specified font state.
90 */
Chris Craik59744b72014-07-01 17:56:52 -070091 static Font* create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix);
Romain Guy9f5dab32012-09-04 12:55:44 -070092
93private:
94 friend class FontRenderer;
Romain Guye3a9b242013-01-08 11:15:30 -080095
96 Font(FontRenderer* state, const Font::FontDescription& desc);
97
John Reck1bcacfd2017-11-03 10:12:19 -070098 typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*, uint32_t, uint32_t,
99 Rect*, const float*);
Romain Guy9f5dab32012-09-04 12:55:44 -0700100
101 enum RenderMode {
102 FRAMEBUFFER,
103 BITMAP,
104 MEASURE,
105 };
106
Chris Craike8c3c812016-02-05 20:10:50 -0800107 void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs);
Romain Guy9f5dab32012-09-04 12:55:44 -0700108
John Reck1bcacfd2017-11-03 10:12:19 -0700109 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 Guy9f5dab32012-09-04 12:55:44 -0700112
John Reck1bcacfd2017-11-03 10:12:19 -0700113 void measure(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, Rect* bounds,
114 const float* positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700115
Chris Craike84a2082014-12-22 14:28:49 -0800116 void invalidateTextureCache(CacheTexture* cacheTexture = nullptr);
Romain Guy9f5dab32012-09-04 12:55:44 -0700117
Chris Craikd218a922014-01-02 17:13:34 -0800118 CachedGlyphInfo* cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching);
119 void updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph,
John Reck1bcacfd2017-11-03 10:12:19 -0700120 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700121
John Reck1bcacfd2017-11-03 10:12:19 -0700122 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 Guy9f5dab32012-09-04 12:55:44 -0700131 void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
John Reck1bcacfd2017-11-03 10:12:19 -0700132 SkPathMeasure& measure, SkPoint* position, SkVector* tangent);
Romain Guy9f5dab32012-09-04 12:55:44 -0700133
Chris Craikd218a922014-01-02 17:13:34 -0800134 CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit,
John Reck1bcacfd2017-11-03 10:12:19 -0700135 bool precaching = false);
Romain Guy9f5dab32012-09-04 12:55:44 -0700136
137 FontRenderer* mState;
Romain Guye3a9b242013-01-08 11:15:30 -0800138 FontDescription mDescription;
139
140 // Cache of glyphs
141 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
Romain Guyc74f45a2013-02-26 19:10:14 -0800142
Romain Guy624234f2013-03-05 16:43:31 -0800143 bool mIdentityTransform;
Romain Guy9f5dab32012-09-04 12:55:44 -0700144};
145
John Reck1bcacfd2017-11-03 10:12:19 -0700146inline int strictly_order_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
Romain Guye3a9b242013-01-08 11:15:30 -0800147 return Font::FontDescription::compare(lhs, rhs) < 0;
148}
149
150inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
151 return Font::FontDescription::compare(lhs, rhs);
152}
153
154inline hash_t hash_type(const Font::FontDescription& entry) {
155 return entry.hash();
156}
157
John Reck1bcacfd2017-11-03 10:12:19 -0700158}; // namespace uirenderer
159}; // namespace android
Romain Guy9f5dab32012-09-04 12:55:44 -0700160
John Reck1bcacfd2017-11-03 10:12:19 -0700161#endif // ANDROID_HWUI_FONT_H