blob: d54bc4408e5879fc68470970ca1d540fb8aeda2b [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 <SkScalerContext.h>
27#include <SkPaint.h>
28#include <SkPathMeasure.h>
29
Tom Hudson2dc236b2014-10-15 15:46:42 -040030#include "FontUtil.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070031#include "../Rect.h"
Romain Guye3a9b242013-01-08 11:15:30 -080032#include "../Matrix.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070033
34namespace android {
35namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
38// Font
39///////////////////////////////////////////////////////////////////////////////
40
Tom Hudson2dc236b2014-10-15 15:46:42 -040041class CachedGlyphInfo;
42class CacheTexture;
Romain Guy9f5dab32012-09-04 12:55:44 -070043class FontRenderer;
44
45/**
46 * Represents a font, defined by a Skia font id and a font size. A font is used
47 * to generate glyphs and cache them in the FontState.
48 */
49class Font {
50public:
51 enum Style {
52 kFakeBold = 1
53 };
54
Romain Guye3a9b242013-01-08 11:15:30 -080055 struct FontDescription {
Chris Craik59744b72014-07-01 17:56:52 -070056 FontDescription(const SkPaint* paint, const SkMatrix& matrix);
Romain Guye3a9b242013-01-08 11:15:30 -080057
58 static int compare(const FontDescription& lhs, const FontDescription& rhs);
59
60 hash_t hash() const;
61
62 bool operator==(const FontDescription& other) const {
63 return compare(*this, other) == 0;
64 }
65
66 bool operator!=(const FontDescription& other) const {
67 return compare(*this, other) != 0;
68 }
69
70 SkFontID mFontId;
71 float mFontSize;
72 int mFlags;
73 float mItalicStyle;
74 float mScaleX;
75 uint8_t mStyle;
76 float mStrokeWidth;
Romain Guyb969a0d2013-02-05 14:38:40 -080077 bool mAntiAliasing;
Romain Guy2d5945e2013-06-18 12:59:25 -070078 uint8_t mHinting;
Romain Guyc74f45a2013-02-26 19:10:14 -080079 SkMatrix mLookupTransform;
Romain Guy874f5c62013-03-01 18:07:35 -080080 SkMatrix mInverseLookupTransform;
Romain Guye3a9b242013-01-08 11:15:30 -080081 };
82
Romain Guy9f5dab32012-09-04 12:55:44 -070083 ~Font();
84
Chris Craikd218a922014-01-02 17:13:34 -080085 void render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -070086 int numGlyphs, int x, int y, const float* positions);
87
Chris Craikd218a922014-01-02 17:13:34 -080088 void render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
89 int numGlyphs, const SkPath* path, float hOffset, float vOffset);
Romain Guy9f5dab32012-09-04 12:55:44 -070090
Romain Guye3a9b242013-01-08 11:15:30 -080091 const Font::FontDescription& getDescription() const {
92 return mDescription;
93 }
94
Romain Guy9f5dab32012-09-04 12:55:44 -070095 /**
96 * Creates a new font associated with the specified font state.
97 */
Chris Craik59744b72014-07-01 17:56:52 -070098 static Font* create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix);
Romain Guy9f5dab32012-09-04 12:55:44 -070099
100private:
101 friend class FontRenderer;
Romain Guye3a9b242013-01-08 11:15:30 -0800102
103 Font(FontRenderer* state, const Font::FontDescription& desc);
104
Romain Guy9f5dab32012-09-04 12:55:44 -0700105 typedef void (Font::*RenderGlyph)(CachedGlyphInfo*, int, int, uint8_t*,
106 uint32_t, uint32_t, Rect*, const float*);
107
108 enum RenderMode {
109 FRAMEBUFFER,
110 BITMAP,
111 MEASURE,
112 };
113
Chris Craikd218a922014-01-02 17:13:34 -0800114 void precache(const SkPaint* paint, const char* text, int numGlyphs);
Romain Guy9f5dab32012-09-04 12:55:44 -0700115
Chris Craikd218a922014-01-02 17:13:34 -0800116 void render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700117 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
118 uint32_t bitmapW, uint32_t bitmapH, Rect *bounds, const float* positions);
119
Chris Craikd218a922014-01-02 17:13:34 -0800120 void measure(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700121 int numGlyphs, Rect *bounds, const float* positions);
122
Romain Guy80872462012-09-04 16:42:01 -0700123 void invalidateTextureCache(CacheTexture* cacheTexture = NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700124
Chris Craikd218a922014-01-02 17:13:34 -0800125 CachedGlyphInfo* cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching);
126 void updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph,
127 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700128
129 void measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
130 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
131 Rect* bounds, const float* pos);
132 void drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
133 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
134 Rect* bounds, const float* pos);
Romain Guy624234f2013-03-05 16:43:31 -0800135 void drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Romain Guya4adcf02013-02-28 12:15:35 -0800136 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
137 Rect* bounds, const float* pos);
Romain Guy9f5dab32012-09-04 12:55:44 -0700138 void drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
139 uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH,
140 Rect* bounds, const float* pos);
141 void drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
142 SkPathMeasure& measure, SkPoint* position, SkVector* tangent);
143
Chris Craikd218a922014-01-02 17:13:34 -0800144 CachedGlyphInfo* getCachedGlyph(const SkPaint* paint, glyph_t textUnit,
145 bool precaching = false);
Romain Guy9f5dab32012-09-04 12:55:44 -0700146
147 FontRenderer* mState;
Romain Guye3a9b242013-01-08 11:15:30 -0800148 FontDescription mDescription;
149
150 // Cache of glyphs
151 DefaultKeyedVector<glyph_t, CachedGlyphInfo*> mCachedGlyphs;
Romain Guyc74f45a2013-02-26 19:10:14 -0800152
Romain Guy624234f2013-03-05 16:43:31 -0800153 bool mIdentityTransform;
Romain Guy9f5dab32012-09-04 12:55:44 -0700154};
155
Romain Guye3a9b242013-01-08 11:15:30 -0800156inline int strictly_order_type(const Font::FontDescription& lhs,
157 const Font::FontDescription& rhs) {
158 return Font::FontDescription::compare(lhs, rhs) < 0;
159}
160
161inline int compare_type(const Font::FontDescription& lhs, const Font::FontDescription& rhs) {
162 return Font::FontDescription::compare(lhs, rhs);
163}
164
165inline hash_t hash_type(const Font::FontDescription& entry) {
166 return entry.hash();
167}
168
Romain Guy9f5dab32012-09-04 12:55:44 -0700169}; // namespace uirenderer
170}; // namespace android
171
172#endif // ANDROID_HWUI_FONT_H