blob: 8c5a8ff90b223cb40eff674985cc728434c42146 [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 Guy9f5dab32012-09-04 12:55:44 -070023#include <SkUtils.h>
24
25#include "Debug.h"
26#include "FontUtil.h"
27#include "Font.h"
28#include "FontRenderer.h"
29#include "Properties.h"
30
31namespace android {
32namespace uirenderer {
33
34///////////////////////////////////////////////////////////////////////////////
35// Font
36///////////////////////////////////////////////////////////////////////////////
37
Romain Guye3a9b242013-01-08 11:15:30 -080038Font::Font(FontRenderer* state, const Font::FontDescription& desc) :
39 mState(state), mDescription(desc) {
Romain Guy9f5dab32012-09-04 12:55:44 -070040}
41
Romain Guye3a9b242013-01-08 11:15:30 -080042Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) {
43 mFontId = SkTypeface::UniqueID(paint->getTypeface());
44 mFontSize = paint->getTextSize();
45 mFlags = 0;
46 if (paint->isFakeBoldText()) {
47 mFlags |= Font::kFakeBold;
48 }
49 mItalicStyle = paint->getTextSkewX();
50 mScaleX = paint->getTextScaleX();
51 mStyle = paint->getStyle();
52 mStrokeWidth = paint->getStrokeWidth();
Romain Guyb969a0d2013-02-05 14:38:40 -080053 mAntiAliasing = paint->isAntiAlias();
Romain Guye3a9b242013-01-08 11:15:30 -080054}
Romain Guy9f5dab32012-09-04 12:55:44 -070055
56Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070057 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070058
59 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
60 delete mCachedGlyphs.valueAt(i);
61 }
62}
63
Romain Guye3a9b242013-01-08 11:15:30 -080064hash_t Font::FontDescription::hash() const {
65 uint32_t hash = JenkinsHashMix(0, mFontId);
66 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
67 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
68 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
69 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
70 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
71 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080072 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guye3a9b242013-01-08 11:15:30 -080073 return JenkinsHashWhiten(hash);
74}
75
76int Font::FontDescription::compare(const Font::FontDescription& lhs,
77 const Font::FontDescription& rhs) {
78 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
79 if (deltaInt != 0) return deltaInt;
80
81 if (lhs.mFontSize < rhs.mFontSize) return -1;
82 if (lhs.mFontSize > rhs.mFontSize) return +1;
83
84 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
85 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
86
87 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
88 if (deltaInt != 0) return deltaInt;
89
90 if (lhs.mScaleX < rhs.mScaleX) return -1;
91 if (lhs.mScaleX > rhs.mScaleX) return +1;
92
93 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
94 if (deltaInt != 0) return deltaInt;
95
96 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
97 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
98
Romain Guyb969a0d2013-02-05 14:38:40 -080099 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
100 if (deltaInt != 0) return deltaInt;
101
Romain Guye3a9b242013-01-08 11:15:30 -0800102 return 0;
103}
104
Romain Guy80872462012-09-04 16:42:01 -0700105void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700106 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
107 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700108 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700109 cachedGlyph->mIsValid = false;
110 }
111 }
112}
113
114void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
115 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
116 int nPenX = x + glyph->mBitmapLeft;
117 int nPenY = y + glyph->mBitmapTop;
118
119 int width = (int) glyph->mBitmapWidth;
120 int height = (int) glyph->mBitmapHeight;
121
122 if (bounds->bottom > nPenY) {
123 bounds->bottom = nPenY;
124 }
125 if (bounds->left > nPenX) {
126 bounds->left = nPenX;
127 }
128 if (bounds->right < nPenX + width) {
129 bounds->right = nPenX + width;
130 }
131 if (bounds->top < nPenY + height) {
132 bounds->top = nPenY + height;
133 }
134}
135
136void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
137 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800138 float nPenX = x + glyph->mBitmapLeft;
139 float nPenY = y + (glyph->mBitmapTop + glyph->mBitmapHeight);
140
141 float width = (float) glyph->mBitmapWidth;
142 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700143
144 float u1 = glyph->mBitmapMinU;
145 float u2 = glyph->mBitmapMaxU;
146 float v1 = glyph->mBitmapMinV;
147 float v2 = glyph->mBitmapMaxV;
148
Romain Guy9f5dab32012-09-04 12:55:44 -0700149 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
150 nPenX + width, nPenY, u2, v2,
151 nPenX + width, nPenY - height, u2, v1,
152 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
153}
154
155void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
156 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
157 int nPenX = x + glyph->mBitmapLeft;
158 int nPenY = y + glyph->mBitmapTop;
159
160 uint32_t endX = glyph->mStartX + glyph->mBitmapWidth;
161 uint32_t endY = glyph->mStartY + glyph->mBitmapHeight;
162
Romain Guy80872462012-09-04 16:42:01 -0700163 CacheTexture* cacheTexture = glyph->mCacheTexture;
164 uint32_t cacheWidth = cacheTexture->getWidth();
165 const uint8_t* cacheBuffer = cacheTexture->getTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -0700166
167 uint32_t cacheX = 0, cacheY = 0;
168 int32_t bX = 0, bY = 0;
169 for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) {
170 for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) {
171#if DEBUG_FONT_RENDERER
172 if (bX < 0 || bY < 0 || bX >= (int32_t) bitmapW || bY >= (int32_t) bitmapH) {
173 ALOGE("Skipping invalid index");
174 continue;
175 }
176#endif
177 uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX];
178 bitmap[bY * bitmapW + bX] = tempCol;
179 }
180 }
181}
182
183void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
184 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
185 const float halfWidth = glyph->mBitmapWidth * 0.5f;
186 const float height = glyph->mBitmapHeight;
187
188 vOffset += glyph->mBitmapTop + height;
189
190 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800191 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
192 if (!ok) {
193 ALOGW("The path for drawTextOnPath is empty or null");
194 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700195
196 // Move along the tangent and offset by the normal
197 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
198 -tangent->fY * halfWidth + tangent->fX * vOffset);
199 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
200 tangent->fY * halfWidth + tangent->fX * vOffset);
201 destination[2].set(destination[1].fX + tangent->fY * height,
202 destination[1].fY - tangent->fX * height);
203 destination[3].set(destination[0].fX + tangent->fY * height,
204 destination[0].fY - tangent->fX * height);
205
206 const float u1 = glyph->mBitmapMinU;
207 const float u2 = glyph->mBitmapMaxU;
208 const float v1 = glyph->mBitmapMinV;
209 const float v2 = glyph->mBitmapMaxV;
210
211 mState->appendRotatedMeshQuad(
212 position->fX + destination[0].fX,
213 position->fY + destination[0].fY, u1, v2,
214 position->fX + destination[1].fX,
215 position->fY + destination[1].fY, u2, v2,
216 position->fX + destination[2].fX,
217 position->fY + destination[2].fY, u2, v1,
218 position->fX + destination[3].fX,
219 position->fY + destination[3].fY, u1, v1,
220 glyph->mCacheTexture);
221}
222
223CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
224 CachedGlyphInfo* cachedGlyph = NULL;
225 ssize_t index = mCachedGlyphs.indexOfKey(textUnit);
226 if (index >= 0) {
227 cachedGlyph = mCachedGlyphs.valueAt(index);
228 } else {
229 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
230 }
231
232 // Is the glyph still in texture cache?
233 if (!cachedGlyph->mIsValid) {
Romain Guye3a9b242013-01-08 11:15:30 -0800234 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit, NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700235 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
236 }
237
238 return cachedGlyph;
239}
240
Romain Guy9f5dab32012-09-04 12:55:44 -0700241void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
242 int numGlyphs, int x, int y, const float* positions) {
243 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
244 0, 0, NULL, positions);
245}
246
247void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
248 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
249 if (numGlyphs == 0 || text == NULL || len == 0) {
250 return;
251 }
252
253 text += start;
254
255 int glyphsCount = 0;
256 SkFixed prevRsbDelta = 0;
257
258 float penX = 0.0f;
259
260 SkPoint position;
261 SkVector tangent;
262
263 SkPathMeasure measure(*path, false);
264 float pathLength = SkScalarToFloat(measure.getLength());
265
266 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
267 float textWidth = SkScalarToFloat(paint->measureText(text, len));
268 float pathOffset = pathLength;
269 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
270 textWidth *= 0.5f;
271 pathOffset *= 0.5f;
272 }
273 penX += pathOffset - textWidth;
274 }
275
276 while (glyphsCount < numGlyphs && penX < pathLength) {
277 glyph_t glyph = GET_GLYPH(text);
278
279 if (IS_END_OF_STRING(glyph)) {
280 break;
281 }
282
283 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
284 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
285 prevRsbDelta = cachedGlyph->mRsbDelta;
286
287 if (cachedGlyph->mIsValid) {
288 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
289 }
290
291 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
292
293 glyphsCount++;
294 }
295}
296
297void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
298 int numGlyphs, Rect *bounds, const float* positions) {
299 if (bounds == NULL) {
300 ALOGE("No return rectangle provided to measure text");
301 return;
302 }
303 bounds->set(1e6, -1e6, -1e6, 1e6);
304 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
305}
306
307void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
308
309 if (numGlyphs == 0 || text == NULL) {
310 return;
311 }
312 int glyphsCount = 0;
313
314 while (glyphsCount < numGlyphs) {
315 glyph_t glyph = GET_GLYPH(text);
316
317 // Reached the end of the string
318 if (IS_END_OF_STRING(glyph)) {
319 break;
320 }
321
322 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
323
324 glyphsCount++;
325 }
326}
327
328void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
329 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
330 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
331 if (numGlyphs == 0 || text == NULL || len == 0) {
332 return;
333 }
334
335 static RenderGlyph gRenderGlyph[] = {
336 &android::uirenderer::Font::drawCachedGlyph,
337 &android::uirenderer::Font::drawCachedGlyphBitmap,
338 &android::uirenderer::Font::measureCachedGlyph
339 };
340 RenderGlyph render = gRenderGlyph[mode];
341
342 text += start;
343 int glyphsCount = 0;
344
Romain Guye3a9b242013-01-08 11:15:30 -0800345 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700346
Romain Guye3a9b242013-01-08 11:15:30 -0800347 while (glyphsCount < numGlyphs) {
348 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700349
Romain Guye3a9b242013-01-08 11:15:30 -0800350 // Reached the end of the string
351 if (IS_END_OF_STRING(glyph)) {
352 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700353 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700354
Romain Guye3a9b242013-01-08 11:15:30 -0800355 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700356
Romain Guye3a9b242013-01-08 11:15:30 -0800357 // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
358 if (cachedGlyph->mIsValid) {
359 int penX = x + positions[(glyphsCount << 1)];
360 int penY = y + positions[(glyphsCount << 1) + 1];
361
362 switch (align) {
363 case SkPaint::kRight_Align:
364 penX -= SkFixedToFloat(cachedGlyph->mAdvanceX);
365 penY -= SkFixedToFloat(cachedGlyph->mAdvanceY);
366 break;
367 case SkPaint::kCenter_Align:
368 penX -= SkFixedToFloat(cachedGlyph->mAdvanceX >> 1);
369 penY -= SkFixedToFloat(cachedGlyph->mAdvanceY >> 1);
370 default:
371 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700372 }
373
Romain Guye3a9b242013-01-08 11:15:30 -0800374 (*this.*render)(cachedGlyph, penX, penY,
375 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700376 }
Romain Guye3a9b242013-01-08 11:15:30 -0800377
378 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700379 }
380}
381
382void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
383 bool precaching) {
384 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
385 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
386 glyph->mBitmapLeft = skiaGlyph.fLeft;
387 glyph->mBitmapTop = skiaGlyph.fTop;
388 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
389 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
390
391 uint32_t startX = 0;
392 uint32_t startY = 0;
393
394 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800395 if (!skiaGlyph.fImage) {
396 paint->findImage(skiaGlyph, NULL);
397 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700398 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
399
400 if (!glyph->mIsValid) {
401 return;
402 }
403
404 uint32_t endX = startX + skiaGlyph.fWidth;
405 uint32_t endY = startY + skiaGlyph.fHeight;
406
407 glyph->mStartX = startX;
408 glyph->mStartY = startY;
409 glyph->mBitmapWidth = skiaGlyph.fWidth;
410 glyph->mBitmapHeight = skiaGlyph.fHeight;
411
Romain Guy80872462012-09-04 16:42:01 -0700412 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
413 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700414
415 glyph->mBitmapMinU = startX / (float) cacheWidth;
416 glyph->mBitmapMinV = startY / (float) cacheHeight;
417 glyph->mBitmapMaxU = endX / (float) cacheWidth;
418 glyph->mBitmapMaxV = endY / (float) cacheHeight;
419
Romain Guy9b1204b2012-09-04 15:22:57 -0700420 mState->setTextureDirty();
Romain Guy9f5dab32012-09-04 12:55:44 -0700421}
422
423CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
424 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
425 mCachedGlyphs.add(glyph, newGlyph);
426
Romain Guye3a9b242013-01-08 11:15:30 -0800427 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, NULL);
Romain Guy9f5dab32012-09-04 12:55:44 -0700428 newGlyph->mGlyphIndex = skiaGlyph.fID;
429 newGlyph->mIsValid = false;
430
431 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
432
433 return newGlyph;
434}
435
Romain Guye3a9b242013-01-08 11:15:30 -0800436Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
437 FontDescription description(paint, matrix);
438 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700439
Romain Guye3a9b242013-01-08 11:15:30 -0800440 if (font) {
441 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700442 }
443
Romain Guye3a9b242013-01-08 11:15:30 -0800444 Font* newFont = new Font(state, description);
445 state->mActiveFonts.put(description, newFont);
Romain Guy9f5dab32012-09-04 12:55:44 -0700446 return newFont;
447}
448
449}; // namespace uirenderer
450}; // namespace android