blob: 02197bcf5b584a416496ea1fc13ab9cb0ffd2cbd [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
20#include <utils/KeyedVector.h>
21
Victoria Lease2ee2d592013-12-17 13:54:29 -080022#include <SkGlyphCache.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070023#include <SkScalerContext.h>
24#include <SkPaint.h>
25#include <SkPathMeasure.h>
26
27#include "CachedGlyphInfo.h"
28#include "../Rect.h"
Romain Guye3a9b242013-01-08 11:15:30 -080029#include "../Matrix.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070030
31namespace android {
32namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Font
36///////////////////////////////////////////////////////////////////////////////
37
38class 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 */
44class Font {
45public:
46 enum Style {
47 kFakeBold = 1
48 };
49
Romain Guye3a9b242013-01-08 11:15:30 -080050 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 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
Chris Craikd218a922014-01-02 17:13:34 -080080 void render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -070081 int numGlyphs, int x, int y, const float* positions);
82
Chris Craikd218a922014-01-02 17:13:34 -080083 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 Guy9f5dab32012-09-04 12:55:44 -070085
Romain Guye3a9b242013-01-08 11:15:30 -080086 const Font::FontDescription& getDescription() const {
87 return mDescription;
88 }
89
Romain Guy9f5dab32012-09-04 12:55:44 -070090 /**
91 * Creates a new font associated with the specified font state.
92 */
Romain Guye3a9b242013-01-08 11:15:30 -080093 static Font* create(FontRenderer* state, const SkPaint* paint, const mat4& matrix);
Romain Guy9f5dab32012-09-04 12:55:44 -070094
95private:
96 friend class FontRenderer;
Romain Guye3a9b242013-01-08 11:15:30 -080097
98 Font(FontRenderer* state, const Font::FontDescription& desc);
99
Romain Guy9f5dab32012-09-04 12:55:44 -0700100 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 Craikd218a922014-01-02 17:13:34 -0800109 void precache(const SkPaint* paint, const char* text, int numGlyphs);
Romain Guy9f5dab32012-09-04 12:55:44 -0700110
Chris Craikd218a922014-01-02 17:13:34 -0800111 void render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700112 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 Craikd218a922014-01-02 17:13:34 -0800115 void measure(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700116 int numGlyphs, Rect *bounds, const float* positions);
117
Romain Guy80872462012-09-04 16:42:01 -0700118 void invalidateTextureCache(CacheTexture* cacheTexture = NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700119
Chris Craikd218a922014-01-02 17:13:34 -0800120 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 Guy9f5dab32012-09-04 12:55:44 -0700123
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 Guy624234f2013-03-05 16:43:31 -0800130 void drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Romain Guya4adcf02013-02-28 12:15:35 -0800131 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
132 Rect* bounds, const float* pos);
Romain Guy9f5dab32012-09-04 12:55:44 -0700133 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 Craikd218a922014-01-02 17:13:34 -0800139 CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit,
140 bool precaching = false);
Romain Guy9f5dab32012-09-04 12:55:44 -0700141
142 FontRenderer* mState;
Romain Guye3a9b242013-01-08 11:15:30 -0800143 FontDescription mDescription;
144
145 // Cache of glyphs
146 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
Romain Guyc74f45a2013-02-26 19:10:14 -0800147
Romain Guy624234f2013-03-05 16:43:31 -0800148 bool mIdentityTransform;
Romain Guy9f5dab32012-09-04 12:55:44 -0700149};
150
Romain Guye3a9b242013-01-08 11:15:30 -0800151inline int strictly_order_type(const Font::FontDescription& lhs,
152 const Font::FontDescription& rhs) {
153 return Font::FontDescription::compare(lhs, rhs) < 0;
154}
155
156inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
157 return Font::FontDescription::compare(lhs, rhs);
158}
159
160inline hash_t hash_type(const Font::FontDescription& entry) {
161 return entry.hash();
162}
163
Romain Guy9f5dab32012-09-04 12:55:44 -0700164}; // namespace uirenderer
165}; // namespace android
166
167#endif // ANDROID_HWUI_FONT_H