blob: 52cca1c370f3971d4656a5becf2561770b1722eb [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
22#include <SkScalerContext.h>
23#include <SkPaint.h>
24#include <SkPathMeasure.h>
25
26#include "CachedGlyphInfo.h"
27#include "../Rect.h"
Romain Guye3a9b242013-01-08 11:15:30 -080028#include "../Matrix.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070029
30namespace android {
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Font
35///////////////////////////////////////////////////////////////////////////////
36
37class FontRenderer;
38
39/**
40 * Represents a font, defined by a Skia font id and a font size. A font is used
41 * to generate glyphs and cache them in the FontState.
42 */
43class Font {
44public:
45 enum Style {
46 kFakeBold = 1
47 };
48
Romain Guye3a9b242013-01-08 11:15:30 -080049 struct FontDescription {
50 FontDescription(const SkPaint* paint, const mat4& matrix);
51
52 static int compare(const FontDescription& lhs, const FontDescription& rhs);
53
54 hash_t hash() const;
55
56 bool operator==(const FontDescription& other) const {
57 return compare(*this, other) == 0;
58 }
59
60 bool operator!=(const FontDescription& other) const {
61 return compare(*this, other) != 0;
62 }
63
64 SkFontID mFontId;
65 float mFontSize;
66 int mFlags;
67 float mItalicStyle;
68 float mScaleX;
69 uint8_t mStyle;
70 float mStrokeWidth;
Romain Guyb969a0d2013-02-05 14:38:40 -080071 bool mAntiAliasing;
Romain Guyc74f45a2013-02-26 19:10:14 -080072 SkMatrix mLookupTransform;
Romain Guy874f5c62013-03-01 18:07:35 -080073 SkMatrix mInverseLookupTransform;
Romain Guye3a9b242013-01-08 11:15:30 -080074 };
75
Romain Guy9f5dab32012-09-04 12:55:44 -070076 ~Font();
77
Romain Guye3a9b242013-01-08 11:15:30 -080078 void render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -070079 int numGlyphs, int x, int y, const float* positions);
80
Romain Guye3a9b242013-01-08 11:15:30 -080081 void render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -070082 int numGlyphs, SkPath* path, float hOffset, float vOffset);
83
Romain Guye3a9b242013-01-08 11:15:30 -080084 const Font::FontDescription& getDescription() const {
85 return mDescription;
86 }
87
Romain Guy9f5dab32012-09-04 12:55:44 -070088 /**
89 * Creates a new font associated with the specified font state.
90 */
Romain Guye3a9b242013-01-08 11:15:30 -080091 static Font* create(FontRenderer* state, const SkPaint* paint, const mat4& 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
Romain Guy9f5dab32012-09-04 12:55:44 -070098 typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*,
99 uint32_t, uint32_t, Rect*, const float*);
100
101 enum RenderMode {
102 FRAMEBUFFER,
103 BITMAP,
104 MEASURE,
105 };
106
107 void precache(SkPaint* paint, const char* text, int numGlyphs);
108
109 void render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
110 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
111 uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions);
112
113 void measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
114 int numGlyphs, Rect *bounds, const float* positions);
115
Romain Guy80872462012-09-04 16:42:01 -0700116 void invalidateTextureCache(CacheTexture* cacheTexture = NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700117
118 CachedGlyphInfo* cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching);
119 void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
120 bool precaching);
121
122 void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
123 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
124 Rect* bounds, const float* pos);
125 void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
126 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
127 Rect* bounds, const float* pos);
Romain Guy624234f2013-03-05 16:43:31 -0800128 void drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Romain Guya4adcf02013-02-28 12:15:35 -0800129 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
130 Rect* bounds, const float* pos);
Romain Guy9f5dab32012-09-04 12:55:44 -0700131 void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
132 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
133 Rect* bounds, const float* pos);
134 void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
135 SkPathMeasure& measure, SkPoint* position, SkVector* tangent);
136
137 CachedGlyphInfo* getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching = false);
138
139 FontRenderer* mState;
Romain Guye3a9b242013-01-08 11:15:30 -0800140 FontDescription mDescription;
141
142 // Cache of glyphs
143 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
Romain Guyc74f45a2013-02-26 19:10:14 -0800144
Romain Guy624234f2013-03-05 16:43:31 -0800145 bool mIdentityTransform;
Romain Guy9f5dab32012-09-04 12:55:44 -0700146};
147
Romain Guye3a9b242013-01-08 11:15:30 -0800148inline int strictly_order_type(const Font::FontDescription& lhs,
149 const Font::FontDescription& rhs) {
150 return Font::FontDescription::compare(lhs, rhs) < 0;
151}
152
153inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
154 return Font::FontDescription::compare(lhs, rhs);
155}
156
157inline hash_t hash_type(const Font::FontDescription& entry) {
158 return entry.hash();
159}
160
Romain Guy9f5dab32012-09-04 12:55:44 -0700161}; // namespace uirenderer
162}; // namespace android
163
164#endif // ANDROID_HWUI_FONT_H