blob: 9068b8a4bbb7ed540aaf93831f2109b3461d42fc [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
Romain Guya3dc55f2012-09-28 13:55:44 -070017#define LOG_TAG "OpenGLRenderer"
18
Romain Guy9f5dab32012-09-04 12:55:44 -070019#include <cutils/compiler.h>
20
Derek Sollenbergerca79cf62012-08-14 16:44:52 -040021#include <SkGlyph.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070022#include <SkUtils.h>
23
24#include "Debug.h"
25#include "FontUtil.h"
26#include "Font.h"
27#include "FontRenderer.h"
28#include "Properties.h"
29
30namespace android {
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Font
35///////////////////////////////////////////////////////////////////////////////
36
37Font::Font(FontRenderer* state, uint32_t fontId, float fontSize,
38 int flags, uint32_t italicStyle, uint32_t scaleX,
39 SkPaint::Style style, uint32_t strokeWidth) :
40 mState(state), mFontId(fontId), mFontSize(fontSize),
41 mFlags(flags), mItalicStyle(italicStyle), mScaleX(scaleX),
42 mStyle(style), mStrokeWidth(mStrokeWidth) {
43}
44
45
46Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070047 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070048
49 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
50 delete mCachedGlyphs.valueAt(i);
51 }
52}
53
Romain Guy80872462012-09-04 16:42:01 -070054void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -070055 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
56 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -070057 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -070058 cachedGlyph->mIsValid = false;
59 }
60 }
61}
62
63void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
64 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
65 int nPenX = x + glyph->mBitmapLeft;
66 int nPenY = y + glyph->mBitmapTop;
67
68 int width = (int) glyph->mBitmapWidth;
69 int height = (int) glyph->mBitmapHeight;
70
71 if (bounds->bottom > nPenY) {
72 bounds->bottom = nPenY;
73 }
74 if (bounds->left > nPenX) {
75 bounds->left = nPenX;
76 }
77 if (bounds->right < nPenX + width) {
78 bounds->right = nPenX + width;
79 }
80 if (bounds->top < nPenY + height) {
81 bounds->top = nPenY + height;
82 }
83}
84
85void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
86 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
87 int nPenX = x + glyph->mBitmapLeft;
88 int nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight;
89
90 float u1 = glyph->mBitmapMinU;
91 float u2 = glyph->mBitmapMaxU;
92 float v1 = glyph->mBitmapMinV;
93 float v2 = glyph->mBitmapMaxV;
94
95 int width = (int) glyph->mBitmapWidth;
96 int height = (int) glyph->mBitmapHeight;
97
98 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
99 nPenX + width, nPenY, u2, v2,
100 nPenX + width, nPenY - height, u2, v1,
101 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
102}
103
104void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
105 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
106 int nPenX = x + glyph->mBitmapLeft;
107 int nPenY = y + glyph->mBitmapTop;
108
109 uint32_t endX = glyph->mStartX + glyph->mBitmapWidth;
110 uint32_t endY = glyph->mStartY + glyph->mBitmapHeight;
111
Romain Guy80872462012-09-04 16:42:01 -0700112 CacheTexture* cacheTexture = glyph->mCacheTexture;
113 uint32_t cacheWidth = cacheTexture->getWidth();
114 const uint8_t* cacheBuffer = cacheTexture->getTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -0700115
116 uint32_t cacheX = 0, cacheY = 0;
117 int32_t bX = 0, bY = 0;
118 for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) {
119 for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) {
120#if DEBUG_FONT_RENDERER
121 if (bX < 0 || bY < 0 || bX >= (int32_t) bitmapW || bY >= (int32_t) bitmapH) {
122 ALOGE("Skipping invalid index");
123 continue;
124 }
125#endif
126 uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX];
127 bitmap[bY * bitmapW + bX] = tempCol;
128 }
129 }
130}
131
132void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
133 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
134 const float halfWidth = glyph->mBitmapWidth * 0.5f;
135 const float height = glyph->mBitmapHeight;
136
137 vOffset += glyph->mBitmapTop + height;
138
139 SkPoint destination[4];
140 measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
141
142 // Move along the tangent and offset by the normal
143 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
144 -tangent->fY * halfWidth + tangent->fX * vOffset);
145 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
146 tangent->fY * halfWidth + tangent->fX * vOffset);
147 destination[2].set(destination[1].fX + tangent->fY * height,
148 destination[1].fY - tangent->fX * height);
149 destination[3].set(destination[0].fX + tangent->fY * height,
150 destination[0].fY - tangent->fX * height);
151
152 const float u1 = glyph->mBitmapMinU;
153 const float u2 = glyph->mBitmapMaxU;
154 const float v1 = glyph->mBitmapMinV;
155 const float v2 = glyph->mBitmapMaxV;
156
157 mState->appendRotatedMeshQuad(
158 position->fX + destination[0].fX,
159 position->fY + destination[0].fY, u1, v2,
160 position->fX + destination[1].fX,
161 position->fY + destination[1].fY, u2, v2,
162 position->fX + destination[2].fX,
163 position->fY + destination[2].fY, u2, v1,
164 position->fX + destination[3].fX,
165 position->fY + destination[3].fY, u1, v1,
166 glyph->mCacheTexture);
167}
168
169CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
170 CachedGlyphInfo* cachedGlyph = NULL;
171 ssize_t index = mCachedGlyphs.indexOfKey(textUnit);
172 if (index >= 0) {
173 cachedGlyph = mCachedGlyphs.valueAt(index);
174 } else {
175 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
176 }
177
178 // Is the glyph still in texture cache?
179 if (!cachedGlyph->mIsValid) {
180 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit);
181 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
182 }
183
184 return cachedGlyph;
185}
186
187void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
188 int numGlyphs, int x, int y, uint8_t *bitmap, uint32_t bitmapW, uint32_t bitmapH) {
189 if (bitmap != NULL && bitmapW > 0 && bitmapH > 0) {
190 render(paint, text, start, len, numGlyphs, x, y, BITMAP, bitmap,
191 bitmapW, bitmapH, NULL, NULL);
192 } else {
193 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
194 0, 0, NULL, NULL);
195 }
196}
197
198void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
199 int numGlyphs, int x, int y, const float* positions) {
200 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
201 0, 0, NULL, positions);
202}
203
204void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
205 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
206 if (numGlyphs == 0 || text == NULL || len == 0) {
207 return;
208 }
209
210 text += start;
211
212 int glyphsCount = 0;
213 SkFixed prevRsbDelta = 0;
214
215 float penX = 0.0f;
216
217 SkPoint position;
218 SkVector tangent;
219
220 SkPathMeasure measure(*path, false);
221 float pathLength = SkScalarToFloat(measure.getLength());
222
223 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
224 float textWidth = SkScalarToFloat(paint->measureText(text, len));
225 float pathOffset = pathLength;
226 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
227 textWidth *= 0.5f;
228 pathOffset *= 0.5f;
229 }
230 penX += pathOffset - textWidth;
231 }
232
233 while (glyphsCount < numGlyphs && penX < pathLength) {
234 glyph_t glyph = GET_GLYPH(text);
235
236 if (IS_END_OF_STRING(glyph)) {
237 break;
238 }
239
240 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
241 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
242 prevRsbDelta = cachedGlyph->mRsbDelta;
243
244 if (cachedGlyph->mIsValid) {
245 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
246 }
247
248 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
249
250 glyphsCount++;
251 }
252}
253
254void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
255 int numGlyphs, Rect *bounds, const float* positions) {
256 if (bounds == NULL) {
257 ALOGE("No return rectangle provided to measure text");
258 return;
259 }
260 bounds->set(1e6, -1e6, -1e6, 1e6);
261 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
262}
263
264void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
265
266 if (numGlyphs == 0 || text == NULL) {
267 return;
268 }
269 int glyphsCount = 0;
270
271 while (glyphsCount < numGlyphs) {
272 glyph_t glyph = GET_GLYPH(text);
273
274 // Reached the end of the string
275 if (IS_END_OF_STRING(glyph)) {
276 break;
277 }
278
279 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
280
281 glyphsCount++;
282 }
283}
284
285void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
286 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
287 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
288 if (numGlyphs == 0 || text == NULL || len == 0) {
289 return;
290 }
291
292 static RenderGlyph gRenderGlyph[] = {
293 &android::uirenderer::Font::drawCachedGlyph,
294 &android::uirenderer::Font::drawCachedGlyphBitmap,
295 &android::uirenderer::Font::measureCachedGlyph
296 };
297 RenderGlyph render = gRenderGlyph[mode];
298
299 text += start;
300 int glyphsCount = 0;
301
302 if (CC_LIKELY(positions == NULL)) {
303 SkFixed prevRsbDelta = 0;
304
305 float penX = x + 0.5f;
306 int penY = y;
307
308 while (glyphsCount < numGlyphs) {
309 glyph_t glyph = GET_GLYPH(text);
310
311 // Reached the end of the string
312 if (IS_END_OF_STRING(glyph)) {
313 break;
314 }
315
316 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
317 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
318 prevRsbDelta = cachedGlyph->mRsbDelta;
319
320 // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
321 if (cachedGlyph->mIsValid) {
322 (*this.*render)(cachedGlyph, (int) floorf(penX), penY,
323 bitmap, bitmapW, bitmapH, bounds, positions);
324 }
325
326 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
327
328 glyphsCount++;
329 }
330 } else {
331 const SkPaint::Align align = paint->getTextAlign();
332
333 // This is for renderPosText()
334 while (glyphsCount < numGlyphs) {
335 glyph_t glyph = GET_GLYPH(text);
336
337 // Reached the end of the string
338 if (IS_END_OF_STRING(glyph)) {
339 break;
340 }
341
342 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
343
344 // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
345 if (cachedGlyph->mIsValid) {
346 int penX = x + positions[(glyphsCount << 1)];
347 int penY = y + positions[(glyphsCount << 1) + 1];
348
349 switch (align) {
350 case SkPaint::kRight_Align:
351 penX -= SkFixedToFloat(cachedGlyph->mAdvanceX);
352 penY -= SkFixedToFloat(cachedGlyph->mAdvanceY);
353 break;
354 case SkPaint::kCenter_Align:
355 penX -= SkFixedToFloat(cachedGlyph->mAdvanceX >> 1);
356 penY -= SkFixedToFloat(cachedGlyph->mAdvanceY >> 1);
357 default:
358 break;
359 }
360
361 (*this.*render)(cachedGlyph, penX, penY,
362 bitmap, bitmapW, bitmapH, bounds, positions);
363 }
364
365 glyphsCount++;
366 }
367 }
368}
369
370void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
371 bool precaching) {
372 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
373 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
374 glyph->mBitmapLeft = skiaGlyph.fLeft;
375 glyph->mBitmapTop = skiaGlyph.fTop;
376 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
377 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
378
379 uint32_t startX = 0;
380 uint32_t startY = 0;
381
382 // Get the bitmap for the glyph
383 paint->findImage(skiaGlyph);
384 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
385
386 if (!glyph->mIsValid) {
387 return;
388 }
389
390 uint32_t endX = startX + skiaGlyph.fWidth;
391 uint32_t endY = startY + skiaGlyph.fHeight;
392
393 glyph->mStartX = startX;
394 glyph->mStartY = startY;
395 glyph->mBitmapWidth = skiaGlyph.fWidth;
396 glyph->mBitmapHeight = skiaGlyph.fHeight;
397
Romain Guy80872462012-09-04 16:42:01 -0700398 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
399 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700400
401 glyph->mBitmapMinU = startX / (float) cacheWidth;
402 glyph->mBitmapMinV = startY / (float) cacheHeight;
403 glyph->mBitmapMaxU = endX / (float) cacheWidth;
404 glyph->mBitmapMaxV = endY / (float) cacheHeight;
405
Romain Guy9b1204b2012-09-04 15:22:57 -0700406 mState->setTextureDirty();
Romain Guy9f5dab32012-09-04 12:55:44 -0700407}
408
409CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
410 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
411 mCachedGlyphs.add(glyph, newGlyph);
412
413 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph);
414 newGlyph->mGlyphIndex = skiaGlyph.fID;
415 newGlyph->mIsValid = false;
416
417 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
418
419 return newGlyph;
420}
421
422Font* Font::create(FontRenderer* state, uint32_t fontId, float fontSize,
423 int flags, uint32_t italicStyle, uint32_t scaleX,
424 SkPaint::Style style, uint32_t strokeWidth) {
425 Vector<Font*> &activeFonts = state->mActiveFonts;
426
427 for (uint32_t i = 0; i < activeFonts.size(); i++) {
428 Font* font = activeFonts[i];
429 if (font->mFontId == fontId && font->mFontSize == fontSize &&
430 font->mFlags == flags && font->mItalicStyle == italicStyle &&
431 font->mScaleX == scaleX && font->mStyle == style &&
432 (style == SkPaint::kFill_Style || font->mStrokeWidth == strokeWidth)) {
433 return font;
434 }
435 }
436
437 Font* newFont = new Font(state, fontId, fontSize, flags, italicStyle,
438 scaleX, style, strokeWidth);
439 activeFonts.push(newFont);
440 return newFont;
441}
442
443}; // namespace uirenderer
444}; // namespace android