blob: c1cd7fb695a391248f9ab306c1751c11ae5fd67a [file] [log] [blame]
Romain Guy694b5192010-07-21 21:33:20 -07001/*
2 * Copyright (C) 2010 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_UI_FONT_RENDERER_H
18#define ANDROID_UI_FONT_RENDERER_H
19
20#include <utils/String8.h>
21#include <utils/Vector.h>
22#include <utils/KeyedVector.h>
23
24#include <SkScalerContext.h>
25#include <SkPaint.h>
26
27#include <GLES2/gl2.h>
28
Romain Guy09147fb2010-07-22 13:08:20 -070029#include "Rect.h"
Romain Guy51769a62010-07-23 00:28:00 -070030#include "Properties.h"
Romain Guy09147fb2010-07-22 13:08:20 -070031
Romain Guy694b5192010-07-21 21:33:20 -070032namespace android {
33namespace uirenderer {
34
35class FontRenderer;
36
Romain Guy51769a62010-07-23 00:28:00 -070037/**
38 * Represents a font, defined by a Skia font id and a font size. A font is used
39 * to generate glyphs and cache them in the FontState.
40 */
Romain Guy694b5192010-07-21 21:33:20 -070041class Font {
42public:
43 ~Font();
44
Romain Guy51769a62010-07-23 00:28:00 -070045 /**
46 * Renders the specified string of text.
47 */
48 void renderUTF(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Romain Guy694b5192010-07-21 21:33:20 -070049 int numGlyphs, int x, int y);
Romain Guy51769a62010-07-23 00:28:00 -070050 /**
51 * Creates a new font associated with the specified font state.
52 */
Romain Guy694b5192010-07-21 21:33:20 -070053 static Font* create(FontRenderer* state, uint32_t fontId, float fontSize);
54
55protected:
Romain Guy694b5192010-07-21 21:33:20 -070056 friend class FontRenderer;
57
Romain Guy694b5192010-07-21 21:33:20 -070058 struct CachedGlyphInfo {
59 // Has the cache been invalidated?
60 bool mIsValid;
61 // Location of the cached glyph in the bitmap
62 // in case we need to resize the texture
63 uint32_t mBitmapWidth;
64 uint32_t mBitmapHeight;
65 // Also cache texture coords for the quad
66 float mBitmapMinU;
67 float mBitmapMinV;
68 float mBitmapMaxU;
69 float mBitmapMaxV;
70 // Minimize how much we call freetype
71 uint32_t mGlyphIndex;
72 uint32_t mAdvanceX;
73 uint32_t mAdvanceY;
74 // Values below contain a glyph's origin in the bitmap
75 uint32_t mBitmapLeft;
76 uint32_t mBitmapTop;
77 };
78
Romain Guy694b5192010-07-21 21:33:20 -070079 Font(FontRenderer* state, uint32_t fontId, float fontSize);
80
81 DefaultKeyedVector<int32_t, CachedGlyphInfo*> mCachedGlyphs;
82
Romain Guybd0e6aa2010-07-22 18:50:12 -070083 void invalidateTextureCache();
84
Romain Guy694b5192010-07-21 21:33:20 -070085 CachedGlyphInfo *cacheGlyph(SkPaint* paint, int32_t glyph);
86 void updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo *glyph);
87 void drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y);
Romain Guybd0e6aa2010-07-22 18:50:12 -070088
89 FontRenderer* mState;
90 uint32_t mFontId;
91 float mFontSize;
Romain Guy694b5192010-07-21 21:33:20 -070092};
93
94class FontRenderer {
95public:
96 FontRenderer();
97 ~FontRenderer();
98
99 void init();
100 void deinit();
101
102 void setFont(uint32_t fontId, float fontSize);
Romain Guy51769a62010-07-23 00:28:00 -0700103 void renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
104 uint32_t len, int numGlyphs, int x, int y);
Romain Guy694b5192010-07-21 21:33:20 -0700105
106 GLuint getTexture() {
107 checkInit();
108 return mTextureId;
109 }
110
111protected:
112 friend class Font;
113
114 struct CacheTextureLine {
115 uint16_t mMaxHeight;
116 uint16_t mMaxWidth;
117 uint32_t mCurrentRow;
118 uint32_t mCurrentCol;
119
Romain Guy51769a62010-07-23 00:28:00 -0700120 CacheTextureLine(uint16_t maxWidth, uint16_t maxHeight, uint32_t currentRow,
Romain Guy694b5192010-07-21 21:33:20 -0700121 uint32_t currentCol):
Romain Guy51769a62010-07-23 00:28:00 -0700122 mMaxHeight(maxHeight),
123 mMaxWidth(maxWidth),
124 mCurrentRow(currentRow),
125 mCurrentCol(currentCol) {
Romain Guy694b5192010-07-21 21:33:20 -0700126 }
127
128 bool fitBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY) {
129 if (glyph.fHeight > mMaxHeight) {
130 return false;
131 }
132
133 if (mCurrentCol + glyph.fWidth < mMaxWidth) {
134 *retOriginX = mCurrentCol;
135 *retOriginY = mCurrentRow;
136 mCurrentCol += glyph.fWidth;
137 return true;
138 }
139
140 return false;
141 }
142 };
143
144 uint32_t getCacheWidth() const {
145 return mCacheWidth;
146 }
147
148 uint32_t getCacheHeight() const {
149 return mCacheHeight;
150 }
151
152 void initTextTexture();
Romain Guy694b5192010-07-21 21:33:20 -0700153 bool cacheBitmap(const SkGlyph& glyph, uint32_t *retOriginX, uint32_t *retOriginY);
154
155 void flushAllAndInvalidate();
156 void initVertexArrayBuffers();
157
158 void checkInit();
159
160 void issueDrawCommand();
Romain Guy694b5192010-07-21 21:33:20 -0700161 void appendMeshQuad(float x1, float y1, float z1, float u1, float v1, float x2, float y2,
162 float z2, float u2, float v2, float x3, float y3, float z3, float u3, float v3,
163 float x4, float y4, float z4, float u4, float v4);
164
165 uint32_t mCacheWidth;
166 uint32_t mCacheHeight;
167
Romain Guy694b5192010-07-21 21:33:20 -0700168 Vector<CacheTextureLine*> mCacheLines;
169
Romain Guy09147fb2010-07-22 13:08:20 -0700170 Font* mCurrentFont;
Romain Guy694b5192010-07-21 21:33:20 -0700171 Vector<Font*> mActiveFonts;
172
173 // Texture to cache glyph bitmaps
174 unsigned char* mTextTexture;
175 GLuint mTextureId;
176 bool mUploadTexture;
177
178 // Pointer to vertex data to speed up frame to frame work
179 float *mTextMeshPtr;
180 uint32_t mCurrentQuadIndex;
181 uint32_t mMaxNumberOfQuads;
182
183 uint32_t mIndexBufferID;
184
Romain Guy09147fb2010-07-22 13:08:20 -0700185 const Rect* mClip;
186
Romain Guy694b5192010-07-21 21:33:20 -0700187 bool mInitialized;
188};
189
190}; // namespace uirenderer
191}; // namespace android
192
193#endif // ANDROID_UI_FONT_RENDERER_H