blob: d48b6128cc2bf9636706881178a9288e413d4e4f [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 Guya4adcf02013-02-28 12:15:35 -080055 mLookupTransform.reset();
Romain Guyc74f45a2013-02-26 19:10:14 -080056 mLookupTransform[SkMatrix::kMScaleX] = matrix.data[mat4::kScaleX];
57 mLookupTransform[SkMatrix::kMScaleY] = matrix.data[mat4::kScaleY];
58 mLookupTransform[SkMatrix::kMSkewX] = matrix.data[mat4::kSkewX];
59 mLookupTransform[SkMatrix::kMSkewY] = matrix.data[mat4::kSkewY];
Romain Guye3a9b242013-01-08 11:15:30 -080060}
Romain Guy9f5dab32012-09-04 12:55:44 -070061
62Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070063 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070064
65 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
66 delete mCachedGlyphs.valueAt(i);
67 }
68}
69
Romain Guye3a9b242013-01-08 11:15:30 -080070hash_t Font::FontDescription::hash() const {
71 uint32_t hash = JenkinsHashMix(0, mFontId);
72 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
73 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
74 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
75 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
76 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
77 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080078 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guyc74f45a2013-02-26 19:10:14 -080079 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX]));
80 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY]));
81 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMSkewX]));
82 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMSkewY]));
Romain Guye3a9b242013-01-08 11:15:30 -080083 return JenkinsHashWhiten(hash);
84}
85
86int Font::FontDescription::compare(const Font::FontDescription& lhs,
87 const Font::FontDescription& rhs) {
88 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
89 if (deltaInt != 0) return deltaInt;
90
91 if (lhs.mFontSize < rhs.mFontSize) return -1;
92 if (lhs.mFontSize > rhs.mFontSize) return +1;
93
94 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
95 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
96
97 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
98 if (deltaInt != 0) return deltaInt;
99
100 if (lhs.mScaleX < rhs.mScaleX) return -1;
101 if (lhs.mScaleX > rhs.mScaleX) return +1;
102
103 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
104 if (deltaInt != 0) return deltaInt;
105
106 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
107 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
108
Romain Guyb969a0d2013-02-05 14:38:40 -0800109 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
110 if (deltaInt != 0) return deltaInt;
111
Romain Guyc74f45a2013-02-26 19:10:14 -0800112 if (lhs.mLookupTransform[SkMatrix::kMScaleX] <
113 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1;
114 if (lhs.mLookupTransform[SkMatrix::kMScaleX] >
115 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1;
116
117 if (lhs.mLookupTransform[SkMatrix::kMScaleY] <
118 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1;
119 if (lhs.mLookupTransform[SkMatrix::kMScaleY] >
120 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1;
121
122 if (lhs.mLookupTransform[SkMatrix::kMSkewX] <
123 rhs.mLookupTransform[SkMatrix::kMSkewX]) return -1;
124 if (lhs.mLookupTransform[SkMatrix::kMSkewX] >
125 rhs.mLookupTransform[SkMatrix::kMSkewX]) return +1;
126
127 if (lhs.mLookupTransform[SkMatrix::kMSkewY] <
128 rhs.mLookupTransform[SkMatrix::kMSkewY]) return -1;
129 if (lhs.mLookupTransform[SkMatrix::kMSkewY] >
130 rhs.mLookupTransform[SkMatrix::kMSkewY]) return +1;
131
Romain Guye3a9b242013-01-08 11:15:30 -0800132 return 0;
133}
134
Romain Guy80872462012-09-04 16:42:01 -0700135void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700136 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
137 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700138 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700139 cachedGlyph->mIsValid = false;
140 }
141 }
142}
143
144void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
145 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
146 int nPenX = x + glyph->mBitmapLeft;
147 int nPenY = y + glyph->mBitmapTop;
148
149 int width = (int) glyph->mBitmapWidth;
150 int height = (int) glyph->mBitmapHeight;
151
152 if (bounds->bottom > nPenY) {
153 bounds->bottom = nPenY;
154 }
155 if (bounds->left > nPenX) {
156 bounds->left = nPenX;
157 }
158 if (bounds->right < nPenX + width) {
159 bounds->right = nPenX + width;
160 }
161 if (bounds->top < nPenY + height) {
162 bounds->top = nPenY + height;
163 }
164}
165
166void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
167 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800168 float nPenX = x + glyph->mBitmapLeft;
Romain Guya4adcf02013-02-28 12:15:35 -0800169 float nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight;
Romain Guye3a9b242013-01-08 11:15:30 -0800170
171 float width = (float) glyph->mBitmapWidth;
172 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700173
174 float u1 = glyph->mBitmapMinU;
175 float u2 = glyph->mBitmapMaxU;
176 float v1 = glyph->mBitmapMinV;
177 float v2 = glyph->mBitmapMaxV;
178
Romain Guy9f5dab32012-09-04 12:55:44 -0700179 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
180 nPenX + width, nPenY, u2, v2,
181 nPenX + width, nPenY - height, u2, v1,
182 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
183}
184
Romain Guya4adcf02013-02-28 12:15:35 -0800185void Font::drawCachedGlyphPerspective(CachedGlyphInfo* glyph, int x, int y,
186 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
187 SkMatrix i;
188 if (!mDescription.mLookupTransform.invert(&i)) {
189 return;
190 }
191
192 SkPoint p[4];
193 p[0].set(glyph->mBitmapLeft, glyph->mBitmapTop + glyph->mBitmapHeight);
194 p[1].set(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop + glyph->mBitmapHeight);
195 p[2].set(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop);
196 p[3].set(glyph->mBitmapLeft, glyph->mBitmapTop);
197
198 i.mapPoints(p, 4);
199
200 p[0].offset(x, y);
201 p[1].offset(x, y);
202 p[2].offset(x, y);
203 p[3].offset(x, y);
204
205 float u1 = glyph->mBitmapMinU;
206 float u2 = glyph->mBitmapMaxU;
207 float v1 = glyph->mBitmapMinV;
208 float v2 = glyph->mBitmapMaxV;
209
210 mState->appendRotatedMeshQuad(
211 p[0].fX, p[0].fY, u1, v2,
212 p[1].fX, p[1].fY, u2, v2,
213 p[2].fX, p[2].fY, u2, v1,
214 p[3].fX, p[3].fY, u1, v1, glyph->mCacheTexture);
215}
216
Romain Guy9f5dab32012-09-04 12:55:44 -0700217void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
218 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
219 int nPenX = x + glyph->mBitmapLeft;
220 int nPenY = y + glyph->mBitmapTop;
221
222 uint32_t endX = glyph->mStartX + glyph->mBitmapWidth;
223 uint32_t endY = glyph->mStartY + glyph->mBitmapHeight;
224
Romain Guy80872462012-09-04 16:42:01 -0700225 CacheTexture* cacheTexture = glyph->mCacheTexture;
226 uint32_t cacheWidth = cacheTexture->getWidth();
227 const uint8_t* cacheBuffer = cacheTexture->getTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -0700228
229 uint32_t cacheX = 0, cacheY = 0;
230 int32_t bX = 0, bY = 0;
231 for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) {
232 for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700233 uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX];
234 bitmap[bY * bitmapW + bX] = tempCol;
235 }
236 }
237}
238
239void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
240 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
241 const float halfWidth = glyph->mBitmapWidth * 0.5f;
242 const float height = glyph->mBitmapHeight;
243
244 vOffset += glyph->mBitmapTop + height;
245
246 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800247 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
248 if (!ok) {
249 ALOGW("The path for drawTextOnPath is empty or null");
250 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700251
252 // Move along the tangent and offset by the normal
253 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
254 -tangent->fY * halfWidth + tangent->fX * vOffset);
255 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
256 tangent->fY * halfWidth + tangent->fX * vOffset);
257 destination[2].set(destination[1].fX + tangent->fY * height,
258 destination[1].fY - tangent->fX * height);
259 destination[3].set(destination[0].fX + tangent->fY * height,
260 destination[0].fY - tangent->fX * height);
261
262 const float u1 = glyph->mBitmapMinU;
263 const float u2 = glyph->mBitmapMaxU;
264 const float v1 = glyph->mBitmapMinV;
265 const float v2 = glyph->mBitmapMaxV;
266
267 mState->appendRotatedMeshQuad(
268 position->fX + destination[0].fX,
269 position->fY + destination[0].fY, u1, v2,
270 position->fX + destination[1].fX,
271 position->fY + destination[1].fY, u2, v2,
272 position->fX + destination[2].fX,
273 position->fY + destination[2].fY, u2, v1,
274 position->fX + destination[3].fX,
275 position->fY + destination[3].fY, u1, v1,
276 glyph->mCacheTexture);
277}
278
279CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
280 CachedGlyphInfo* cachedGlyph = NULL;
281 ssize_t index = mCachedGlyphs.indexOfKey(textUnit);
282 if (index >= 0) {
283 cachedGlyph = mCachedGlyphs.valueAt(index);
Romain Guyc74f45a2013-02-26 19:10:14 -0800284
285 // Is the glyph still in texture cache?
286 if (!cachedGlyph->mIsValid) {
287 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit, &mDescription.mLookupTransform);
288 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
289 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700290 } else {
291 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
292 }
293
Romain Guy9f5dab32012-09-04 12:55:44 -0700294 return cachedGlyph;
295}
296
Romain Guy9f5dab32012-09-04 12:55:44 -0700297void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
298 int numGlyphs, int x, int y, const float* positions) {
299 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
300 0, 0, NULL, positions);
301}
302
303void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
304 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
305 if (numGlyphs == 0 || text == NULL || len == 0) {
306 return;
307 }
308
309 text += start;
310
311 int glyphsCount = 0;
312 SkFixed prevRsbDelta = 0;
313
314 float penX = 0.0f;
315
316 SkPoint position;
317 SkVector tangent;
318
319 SkPathMeasure measure(*path, false);
320 float pathLength = SkScalarToFloat(measure.getLength());
321
322 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
323 float textWidth = SkScalarToFloat(paint->measureText(text, len));
324 float pathOffset = pathLength;
325 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
326 textWidth *= 0.5f;
327 pathOffset *= 0.5f;
328 }
329 penX += pathOffset - textWidth;
330 }
331
332 while (glyphsCount < numGlyphs && penX < pathLength) {
333 glyph_t glyph = GET_GLYPH(text);
334
335 if (IS_END_OF_STRING(glyph)) {
336 break;
337 }
338
339 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
340 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
341 prevRsbDelta = cachedGlyph->mRsbDelta;
342
Romain Guya4adcf02013-02-28 12:15:35 -0800343 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700344 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
345 }
346
347 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
348
349 glyphsCount++;
350 }
351}
352
353void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
354 int numGlyphs, Rect *bounds, const float* positions) {
355 if (bounds == NULL) {
356 ALOGE("No return rectangle provided to measure text");
357 return;
358 }
359 bounds->set(1e6, -1e6, -1e6, 1e6);
360 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
361}
362
363void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700364 if (numGlyphs == 0 || text == NULL) {
365 return;
366 }
367 int glyphsCount = 0;
368
369 while (glyphsCount < numGlyphs) {
370 glyph_t glyph = GET_GLYPH(text);
371
372 // Reached the end of the string
373 if (IS_END_OF_STRING(glyph)) {
374 break;
375 }
376
377 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
378
379 glyphsCount++;
380 }
381}
382
383void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
384 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
385 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
386 if (numGlyphs == 0 || text == NULL || len == 0) {
387 return;
388 }
389
390 static RenderGlyph gRenderGlyph[] = {
391 &android::uirenderer::Font::drawCachedGlyph,
Romain Guya4adcf02013-02-28 12:15:35 -0800392 &android::uirenderer::Font::drawCachedGlyphPerspective,
Romain Guy9f5dab32012-09-04 12:55:44 -0700393 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800394 &android::uirenderer::Font::drawCachedGlyphBitmap,
395 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700396 &android::uirenderer::Font::measureCachedGlyph
397 };
Romain Guya4adcf02013-02-28 12:15:35 -0800398 RenderGlyph render = gRenderGlyph[(mode << 1) + mTransform.isPerspective()];
Romain Guy9f5dab32012-09-04 12:55:44 -0700399
400 text += start;
401 int glyphsCount = 0;
402
Romain Guya4adcf02013-02-28 12:15:35 -0800403 const bool applyTransform = !mTransform.isIdentity() && !mTransform.isPerspective();
Romain Guye3a9b242013-01-08 11:15:30 -0800404 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700405
Romain Guye3a9b242013-01-08 11:15:30 -0800406 while (glyphsCount < numGlyphs) {
407 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700408
Romain Guye3a9b242013-01-08 11:15:30 -0800409 // Reached the end of the string
410 if (IS_END_OF_STRING(glyph)) {
411 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700412 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700413
Romain Guye3a9b242013-01-08 11:15:30 -0800414 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700415
Romain Guya4adcf02013-02-28 12:15:35 -0800416 // If it's still not valid, we couldn't cache it, so we shouldn't
417 // draw garbage; also skip empty glyphs (spaces)
418 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800419 float penX = x + positions[(glyphsCount << 1)];
420 float penY = y + positions[(glyphsCount << 1) + 1];
Romain Guye3a9b242013-01-08 11:15:30 -0800421
Romain Guya4adcf02013-02-28 12:15:35 -0800422 if (applyTransform) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800423 mTransform.mapPoint(penX, penY);
Romain Guy9f5dab32012-09-04 12:55:44 -0700424 }
425
Romain Guyc74f45a2013-02-26 19:10:14 -0800426 (*this.*render)(cachedGlyph, roundf(penX), roundf(penY),
Romain Guye3a9b242013-01-08 11:15:30 -0800427 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700428 }
Romain Guye3a9b242013-01-08 11:15:30 -0800429
430 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700431 }
432}
433
434void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
435 bool precaching) {
436 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
437 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
438 glyph->mBitmapLeft = skiaGlyph.fLeft;
439 glyph->mBitmapTop = skiaGlyph.fTop;
440 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
441 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
442
443 uint32_t startX = 0;
444 uint32_t startY = 0;
445
446 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800447 if (!skiaGlyph.fImage) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800448 paint->findImage(skiaGlyph, &mDescription.mLookupTransform);
Romain Guyb969a0d2013-02-05 14:38:40 -0800449 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700450 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
451
452 if (!glyph->mIsValid) {
453 return;
454 }
455
456 uint32_t endX = startX + skiaGlyph.fWidth;
457 uint32_t endY = startY + skiaGlyph.fHeight;
458
459 glyph->mStartX = startX;
460 glyph->mStartY = startY;
461 glyph->mBitmapWidth = skiaGlyph.fWidth;
462 glyph->mBitmapHeight = skiaGlyph.fHeight;
463
Romain Guya4adcf02013-02-28 12:15:35 -0800464 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
465 if (!empty) {
466 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
467 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700468
Romain Guya4adcf02013-02-28 12:15:35 -0800469 glyph->mBitmapMinU = startX / (float) cacheWidth;
470 glyph->mBitmapMinV = startY / (float) cacheHeight;
471 glyph->mBitmapMaxU = endX / (float) cacheWidth;
472 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700473
Romain Guya4adcf02013-02-28 12:15:35 -0800474 mState->setTextureDirty();
475 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700476}
477
478CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
479 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
480 mCachedGlyphs.add(glyph, newGlyph);
481
Romain Guyc74f45a2013-02-26 19:10:14 -0800482 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, &mDescription.mLookupTransform);
Romain Guy9f5dab32012-09-04 12:55:44 -0700483 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800484 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700485
486 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
487
488 return newGlyph;
489}
490
Romain Guye3a9b242013-01-08 11:15:30 -0800491Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
492 FontDescription description(paint, matrix);
493 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700494
Romain Guya4adcf02013-02-28 12:15:35 -0800495 if (!font) {
496 font = new Font(state, description);
497 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700498 }
Romain Guya4adcf02013-02-28 12:15:35 -0800499 font->mTransform.load(matrix);
Romain Guy9f5dab32012-09-04 12:55:44 -0700500
Romain Guya4adcf02013-02-28 12:15:35 -0800501 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700502}
503
504}; // namespace uirenderer
505}; // namespace android