blob: f68b4308418743ddaeed2631d88a7ed3ca9de756 [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
Romain Guye3a9b242013-01-08 11:15:30 -080080 void render(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
Romain Guye3a9b242013-01-08 11:15:30 -080083 void render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -070084 int numGlyphs, SkPath* path, float hOffset, float vOffset);
85
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
109 void precache(SkPaint* paint, const char* text, int numGlyphs);
110
111 void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
112 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
113 uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions);
114
115 void measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
116 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
120 CachedGlyphInfo* cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching);
Victoria Lease2ee2d592013-12-17 13:54:29 -0800121 void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, SkGlyphCache* skiaGlyphCache,
122 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
139 CachedGlyphInfo* getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching = false);
140
141 FontRenderer* mState;
Romain Guye3a9b242013-01-08 11:15:30 -0800142 FontDescription mDescription;
143
144 // Cache of glyphs
145 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
Romain Guyc74f45a2013-02-26 19:10:14 -0800146
Romain Guy624234f2013-03-05 16:43:31 -0800147 bool mIdentityTransform;
Romain Guy9f5dab32012-09-04 12:55:44 -0700148};
149
Romain Guye3a9b242013-01-08 11:15:30 -0800150inline int strictly_order_type(const Font::FontDescription& lhs,
151 const Font::FontDescription& rhs) {
152 return Font::FontDescription::compare(lhs, rhs) < 0;
153}
154
155inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
156 return Font::FontDescription::compare(lhs, rhs);
157}
158
159inline hash_t hash_type(const Font::FontDescription& entry) {
160 return entry.hash();
161}
162
Romain Guy9f5dab32012-09-04 12:55:44 -0700163}; // namespace uirenderer
164}; // namespace android
165
166#endif // ANDROID_HWUI_FONT_H