blob: 1a75ea8d272f66fb902a7369ddc02f1f0e32a7a8 [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
Romain Guye3a9b242013-01-08 11:15:30 -080021#include <utils/JenkinsHash.h>
22
Romain Guy14c40b42013-01-08 18:03:07 -080023#include <SkGlyph.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070024#include <SkUtils.h>
25
26#include "Debug.h"
27#include "FontUtil.h"
28#include "Font.h"
29#include "FontRenderer.h"
30#include "Properties.h"
31
32namespace android {
33namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Font
37///////////////////////////////////////////////////////////////////////////////
38
Romain Guye3a9b242013-01-08 11:15:30 -080039Font::Font(FontRenderer* state, const Font::FontDescription& desc) :
40 mState(state), mDescription(desc) {
Romain Guy9f5dab32012-09-04 12:55:44 -070041}
42
Romain Guye3a9b242013-01-08 11:15:30 -080043Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) {
44 mFontId = SkTypeface::UniqueID(paint->getTypeface());
45 mFontSize = paint->getTextSize();
46 mFlags = 0;
47 if (paint->isFakeBoldText()) {
48 mFlags |= Font::kFakeBold;
49 }
50 mItalicStyle = paint->getTextSkewX();
51 mScaleX = paint->getTextScaleX();
52 mStyle = paint->getStyle();
53 mStrokeWidth = paint->getStrokeWidth();
Romain Guyb969a0d2013-02-05 14:38:40 -080054 mAntiAliasing = paint->isAntiAlias();
Romain Guye3a9b242013-01-08 11:15:30 -080055}
Romain Guy9f5dab32012-09-04 12:55:44 -070056
57Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070058 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070059
60 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
61 delete mCachedGlyphs.valueAt(i);
62 }
63}
64
Romain Guye3a9b242013-01-08 11:15:30 -080065hash_t Font::FontDescription::hash() const {
66 uint32_t hash = JenkinsHashMix(0, mFontId);
67 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
68 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
69 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
70 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
71 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
72 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080073 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guye3a9b242013-01-08 11:15:30 -080074 return JenkinsHashWhiten(hash);
75}
76
77int Font::FontDescription::compare(const Font::FontDescription& lhs,
78 const Font::FontDescription& rhs) {
79 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
80 if (deltaInt != 0) return deltaInt;
81
82 if (lhs.mFontSize < rhs.mFontSize) return -1;
83 if (lhs.mFontSize > rhs.mFontSize) return +1;
84
85 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
86 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
87
88 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
89 if (deltaInt != 0) return deltaInt;
90
91 if (lhs.mScaleX < rhs.mScaleX) return -1;
92 if (lhs.mScaleX > rhs.mScaleX) return +1;
93
94 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
95 if (deltaInt != 0) return deltaInt;
96
97 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
98 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
99
Romain Guyb969a0d2013-02-05 14:38:40 -0800100 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
101 if (deltaInt != 0) return deltaInt;
102
Romain Guye3a9b242013-01-08 11:15:30 -0800103 return 0;
104}
105
Romain Guy80872462012-09-04 16:42:01 -0700106void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700107 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
108 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700109 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700110 cachedGlyph->mIsValid = false;
111 }
112 }
113}
114
115void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
116 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
117 int nPenX = x + glyph->mBitmapLeft;
118 int nPenY = y + glyph->mBitmapTop;
119
120 int width = (int) glyph->mBitmapWidth;
121 int height = (int) glyph->mBitmapHeight;
122
123 if (bounds->bottom > nPenY) {
124 bounds->bottom = nPenY;
125 }
126 if (bounds->left > nPenX) {
127 bounds->left = nPenX;
128 }
129 if (bounds->right < nPenX + width) {
130 bounds->right = nPenX + width;
131 }
132 if (bounds->top < nPenY + height) {
133 bounds->top = nPenY + height;
134 }
135}
136
137void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
138 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800139 float nPenX = x + glyph->mBitmapLeft;
140 float nPenY = y + (glyph->mBitmapTop + glyph->mBitmapHeight);
141
142 float width = (float) glyph->mBitmapWidth;
143 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700144
145 float u1 = glyph->mBitmapMinU;
146 float u2 = glyph->mBitmapMaxU;
147 float v1 = glyph->mBitmapMinV;
148 float v2 = glyph->mBitmapMaxV;
149
Romain Guy9f5dab32012-09-04 12:55:44 -0700150 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
151 nPenX + width, nPenY, u2, v2,
152 nPenX + width, nPenY - height, u2, v1,
153 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
154}
155
156void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
157 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
158 int nPenX = x + glyph->mBitmapLeft;
159 int nPenY = y + glyph->mBitmapTop;
160
161 uint32_t endX = glyph->mStartX + glyph->mBitmapWidth;
162 uint32_t endY = glyph->mStartY + glyph->mBitmapHeight;
163
Romain Guy80872462012-09-04 16:42:01 -0700164 CacheTexture* cacheTexture = glyph->mCacheTexture;
165 uint32_t cacheWidth = cacheTexture->getWidth();
166 const uint8_t* cacheBuffer = cacheTexture->getTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -0700167
168 uint32_t cacheX = 0, cacheY = 0;
169 int32_t bX = 0, bY = 0;
170 for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) {
171 for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) {
172#if DEBUG_FONT_RENDERER
173 if (bX < 0 || bY < 0 || bX >= (int32_t) bitmapW || bY >= (int32_t) bitmapH) {
174 ALOGE("Skipping invalid index");
175 continue;
176 }
177#endif
178 uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX];
179 bitmap[bY * bitmapW + bX] = tempCol;
180 }
181 }
182}
183
184void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
185 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
186 const float halfWidth = glyph->mBitmapWidth * 0.5f;
187 const float height = glyph->mBitmapHeight;
188
189 vOffset += glyph->mBitmapTop + height;
190
191 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800192 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
193 if (!ok) {
194 ALOGW("The path for drawTextOnPath is empty or null");
195 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700196
197 // Move along the tangent and offset by the normal
198 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
199 -tangent->fY * halfWidth + tangent->fX * vOffset);
200 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
201 tangent->fY * halfWidth + tangent->fX * vOffset);
202 destination[2].set(destination[1].fX + tangent->fY * height,
203 destination[1].fY - tangent->fX * height);
204 destination[3].set(destination[0].fX + tangent->fY * height,
205 destination[0].fY - tangent->fX * height);
206
207 const float u1 = glyph->mBitmapMinU;
208 const float u2 = glyph->mBitmapMaxU;
209 const float v1 = glyph->mBitmapMinV;
210 const float v2 = glyph->mBitmapMaxV;
211
212 mState->appendRotatedMeshQuad(
213 position->fX + destination[0].fX,
214 position->fY + destination[0].fY, u1, v2,
215 position->fX + destination[1].fX,
216 position->fY + destination[1].fY, u2, v2,
217 position->fX + destination[2].fX,
218 position->fY + destination[2].fY, u2, v1,
219 position->fX + destination[3].fX,
220 position->fY + destination[3].fY, u1, v1,
221 glyph->mCacheTexture);
222}
223
224CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
225 CachedGlyphInfo* cachedGlyph = NULL;
226 ssize_t index = mCachedGlyphs.indexOfKey(textUnit);
227 if (index >= 0) {
228 cachedGlyph = mCachedGlyphs.valueAt(index);
229 } else {
230 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
231 }
232
233 // Is the glyph still in texture cache?
234 if (!cachedGlyph->mIsValid) {
Romain Guye3a9b242013-01-08 11:15:30 -0800235 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit, NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700236 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
237 }
238
239 return cachedGlyph;
240}
241
Romain Guy9f5dab32012-09-04 12:55:44 -0700242void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
243 int numGlyphs, int x, int y, const float* positions) {
244 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
245 0, 0, NULL, positions);
246}
247
248void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
249 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
250 if (numGlyphs == 0 || text == NULL || len == 0) {
251 return;
252 }
253
254 text += start;
255
256 int glyphsCount = 0;
257 SkFixed prevRsbDelta = 0;
258
259 float penX = 0.0f;
260
261 SkPoint position;
262 SkVector tangent;
263
264 SkPathMeasure measure(*path, false);
265 float pathLength = SkScalarToFloat(measure.getLength());
266
267 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
268 float textWidth = SkScalarToFloat(paint->measureText(text, len));
269 float pathOffset = pathLength;
270 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
271 textWidth *= 0.5f;
272 pathOffset *= 0.5f;
273 }
274 penX += pathOffset - textWidth;
275 }
276
277 while (glyphsCount < numGlyphs && penX < pathLength) {
278 glyph_t glyph = GET_GLYPH(text);
279
280 if (IS_END_OF_STRING(glyph)) {
281 break;
282 }
283
284 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
285 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
286 prevRsbDelta = cachedGlyph->mRsbDelta;
287
288 if (cachedGlyph->mIsValid) {
289 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
290 }
291
292 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
293
294 glyphsCount++;
295 }
296}
297
298void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
299 int numGlyphs, Rect *bounds, const float* positions) {
300 if (bounds == NULL) {
301 ALOGE("No return rectangle provided to measure text");
302 return;
303 }
304 bounds->set(1e6, -1e6, -1e6, 1e6);
305 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
306}
307
308void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
309
310 if (numGlyphs == 0 || text == NULL) {
311 return;
312 }
313 int glyphsCount = 0;
314
315 while (glyphsCount < numGlyphs) {
316 glyph_t glyph = GET_GLYPH(text);
317
318 // Reached the end of the string
319 if (IS_END_OF_STRING(glyph)) {
320 break;
321 }
322
323 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
324
325 glyphsCount++;
326 }
327}
328
329void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
330 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
331 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
332 if (numGlyphs == 0 || text == NULL || len == 0) {
333 return;
334 }
335
336 static RenderGlyph gRenderGlyph[] = {
337 &android::uirenderer::Font::drawCachedGlyph,
338 &android::uirenderer::Font::drawCachedGlyphBitmap,
339 &android::uirenderer::Font::measureCachedGlyph
340 };
341 RenderGlyph render = gRenderGlyph[mode];
342
343 text += start;
344 int glyphsCount = 0;
345
Romain Guye3a9b242013-01-08 11:15:30 -0800346 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700347
Romain Guye3a9b242013-01-08 11:15:30 -0800348 while (glyphsCount < numGlyphs) {
349 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700350
Romain Guye3a9b242013-01-08 11:15:30 -0800351 // Reached the end of the string
352 if (IS_END_OF_STRING(glyph)) {
353 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700354 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700355
Romain Guye3a9b242013-01-08 11:15:30 -0800356 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700357
Romain Guye3a9b242013-01-08 11:15:30 -0800358 // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
359 if (cachedGlyph->mIsValid) {
360 int penX = x + positions[(glyphsCount << 1)];
361 int penY = y + positions[(glyphsCount << 1) + 1];
362
363 switch (align) {
364 case SkPaint::kRight_Align:
365 penX -= SkFixedToFloat(cachedGlyph->mAdvanceX);
366 penY -= SkFixedToFloat(cachedGlyph->mAdvanceY);
367 break;
368 case SkPaint::kCenter_Align:
369 penX -= SkFixedToFloat(cachedGlyph->mAdvanceX >> 1);
370 penY -= SkFixedToFloat(cachedGlyph->mAdvanceY >> 1);
371 default:
372 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700373 }
374
Romain Guye3a9b242013-01-08 11:15:30 -0800375 (*this.*render)(cachedGlyph, penX, penY,
376 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700377 }
Romain Guye3a9b242013-01-08 11:15:30 -0800378
379 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700380 }
381}
382
383void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
384 bool precaching) {
385 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
386 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
387 glyph->mBitmapLeft = skiaGlyph.fLeft;
388 glyph->mBitmapTop = skiaGlyph.fTop;
389 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
390 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
391
392 uint32_t startX = 0;
393 uint32_t startY = 0;
394
395 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800396 if (!skiaGlyph.fImage) {
397 paint->findImage(skiaGlyph, NULL);
398 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700399 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
400
401 if (!glyph->mIsValid) {
402 return;
403 }
404
405 uint32_t endX = startX + skiaGlyph.fWidth;
406 uint32_t endY = startY + skiaGlyph.fHeight;
407
408 glyph->mStartX = startX;
409 glyph->mStartY = startY;
410 glyph->mBitmapWidth = skiaGlyph.fWidth;
411 glyph->mBitmapHeight = skiaGlyph.fHeight;
412
Romain Guy80872462012-09-04 16:42:01 -0700413 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
414 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700415
416 glyph->mBitmapMinU = startX / (float) cacheWidth;
417 glyph->mBitmapMinV = startY / (float) cacheHeight;
418 glyph->mBitmapMaxU = endX / (float) cacheWidth;
419 glyph->mBitmapMaxV = endY / (float) cacheHeight;
420
Romain Guy9b1204b2012-09-04 15:22:57 -0700421 mState->setTextureDirty();
Romain Guy9f5dab32012-09-04 12:55:44 -0700422}
423
424CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
425 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
426 mCachedGlyphs.add(glyph, newGlyph);
427
Romain Guye3a9b242013-01-08 11:15:30 -0800428 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700429 newGlyph->mGlyphIndex = skiaGlyph.fID;
430 newGlyph->mIsValid = false;
431
432 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
433
434 return newGlyph;
435}
436
Romain Guye3a9b242013-01-08 11:15:30 -0800437Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
438 FontDescription description(paint, matrix);
439 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700440
Romain Guye3a9b242013-01-08 11:15:30 -0800441 if (font) {
442 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700443 }
444
Romain Guye3a9b242013-01-08 11:15:30 -0800445 Font* newFont = new Font(state, description);
446 state->mActiveFonts.put(description, newFont);
Romain Guy9f5dab32012-09-04 12:55:44 -0700447 return newFont;
448}
449
450}; // namespace uirenderer
451}; // namespace android