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