blob: 288f73361bbcc5ceaaffc7703783ec13a3bfd4f1 [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
Victoria Leaseb66270e2014-04-22 15:00:31 -070024#include <SkScalar.h>
Victoria Lease2ee2d592013-12-17 13:54:29 -080025#include <SkGlyphCache.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070026#include <SkPaint.h>
27#include <SkPathMeasure.h>
28
Tom Hudson2dc236b2014-10-15 15:46:42 -040029#include "FontUtil.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070030#include "../Rect.h"
Romain Guye3a9b242013-01-08 11:15:30 -080031#include "../Matrix.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070032
33namespace android {
34namespace uirenderer {
35
36///////////////////////////////////////////////////////////////////////////////
37// Font
38///////////////////////////////////////////////////////////////////////////////
39
Chris Craike84a2082014-12-22 14:28:49 -080040struct CachedGlyphInfo;
Tom Hudson2dc236b2014-10-15 15:46:42 -040041class CacheTexture;
Romain Guy9f5dab32012-09-04 12:55:44 -070042class FontRenderer;
43
44/**
45 * Represents a font, defined by a Skia font id and a font size. A font is used
46 * to generate glyphs and cache them in the FontState.
47 */
48class Font {
49public:
50 enum Style {
51 kFakeBold = 1
52 };
53
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
61 bool operator==(const FontDescription& other) const {
62 return compare(*this, other) == 0;
63 }
64
65 bool operator!=(const FontDescription& other) const {
66 return compare(*this, other) != 0;
67 }
68
69 SkFontID mFontId;
70 float mFontSize;
71 int mFlags;
72 float mItalicStyle;
73 float mScaleX;
74 uint8_t mStyle;
75 float mStrokeWidth;
Romain Guyb969a0d2013-02-05 14:38:40 -080076 bool mAntiAliasing;
Romain Guy2d5945e2013-06-18 12:59:25 -070077 uint8_t mHinting;
Romain Guyc74f45a2013-02-26 19:10:14 -080078 SkMatrix mLookupTransform;
Romain Guy874f5c62013-03-01 18:07:35 -080079 SkMatrix mInverseLookupTransform;
Romain Guye3a9b242013-01-08 11:15:30 -080080 };
81
Romain Guy9f5dab32012-09-04 12:55:44 -070082 ~Font();
83
Chris Craike8c3c812016-02-05 20:10:50 -080084 void render(const SkPaint* paint, const glyph_t* glyphs,
Romain Guy9f5dab32012-09-04 12:55:44 -070085 int numGlyphs, int x, int y, const float* positions);
86
Chris Craike8c3c812016-02-05 20:10:50 -080087 void render(const SkPaint* paint, const glyph_t* glyphs,
Chris Craikd218a922014-01-02 17:13:34 -080088 int numGlyphs, const SkPath* path, float hOffset, float vOffset);
Romain Guy9f5dab32012-09-04 12:55:44 -070089
Romain Guye3a9b242013-01-08 11:15:30 -080090 const Font::FontDescription& getDescription() const {
91 return mDescription;
92 }
93
Romain Guy9f5dab32012-09-04 12:55:44 -070094 /**
95 * Creates a new font associated with the specified font state.
96 */
Chris Craik59744b72014-07-01 17:56:52 -070097 static Font* create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix);
Romain Guy9f5dab32012-09-04 12:55:44 -070098
99private:
100 friend class FontRenderer;
Romain Guye3a9b242013-01-08 11:15:30 -0800101
102 Font(FontRenderer* state, const Font::FontDescription& desc);
103
Romain Guy9f5dab32012-09-04 12:55:44 -0700104 typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*,
105 uint32_t, uint32_t, Rect*, const float*);
106
107 enum RenderMode {
108 FRAMEBUFFER,
109 BITMAP,
110 MEASURE,
111 };
112
Chris Craike8c3c812016-02-05 20:10:50 -0800113 void precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs);
Romain Guy9f5dab32012-09-04 12:55:44 -0700114
Chris Craike8c3c812016-02-05 20:10:50 -0800115 void render(const SkPaint* paint, const glyph_t* glyphs,
Romain Guy9f5dab32012-09-04 12:55:44 -0700116 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
117 uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions);
118
Chris Craike8c3c812016-02-05 20:10:50 -0800119 void measure(const SkPaint* paint, const glyph_t* glyphs,
Romain Guy9f5dab32012-09-04 12:55:44 -0700120 int numGlyphs, Rect *bounds, const float* positions);
121
Chris Craike84a2082014-12-22 14:28:49 -0800122 void invalidateTextureCache(CacheTexture* cacheTexture = nullptr);
Romain Guy9f5dab32012-09-04 12:55:44 -0700123
Chris Craikd218a922014-01-02 17:13:34 -0800124 CachedGlyphInfo* cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching);
125 void updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph,
126 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700127
128 void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
129 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
130 Rect* bounds, const float* pos);
131 void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
132 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
133 Rect* bounds, const float* pos);
Romain Guy624234f2013-03-05 16:43:31 -0800134 void drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Romain Guya4adcf02013-02-28 12:15:35 -0800135 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
136 Rect* bounds, const float* pos);
Romain Guy9f5dab32012-09-04 12:55:44 -0700137 void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
138 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
139 Rect* bounds, const float* pos);
140 void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
141 SkPathMeasure& measure, SkPoint* position, SkVector* tangent);
142
Chris Craikd218a922014-01-02 17:13:34 -0800143 CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit,
144 bool precaching = false);
Romain Guy9f5dab32012-09-04 12:55:44 -0700145
146 FontRenderer* mState;
Romain Guye3a9b242013-01-08 11:15:30 -0800147 FontDescription mDescription;
148
149 // Cache of glyphs
150 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
Romain Guyc74f45a2013-02-26 19:10:14 -0800151
Romain Guy624234f2013-03-05 16:43:31 -0800152 bool mIdentityTransform;
Romain Guy9f5dab32012-09-04 12:55:44 -0700153};
154
Romain Guye3a9b242013-01-08 11:15:30 -0800155inline int strictly_order_type(const Font::FontDescription& lhs,
156 const Font::FontDescription& rhs) {
157 return Font::FontDescription::compare(lhs, rhs) < 0;
158}
159
160inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
161 return Font::FontDescription::compare(lhs, rhs);
162}
163
164inline hash_t hash_type(const Font::FontDescription& entry) {
165 return entry.hash();
166}
167
Romain Guy9f5dab32012-09-04 12:55:44 -0700168}; // namespace uirenderer
169}; // namespace android
170
171#endif // ANDROID_HWUI_FONT_H