blob: e8046443b946aec5f852f214db39245598ab33eb [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) {
Romain Guy0f6675332013-03-01 14:31:04 -0800287 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit,
288 &mDescription.mLookupTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -0800289 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
290 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700291 } else {
292 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
293 }
294
Romain Guy9f5dab32012-09-04 12:55:44 -0700295 return cachedGlyph;
296}
297
Romain Guy9f5dab32012-09-04 12:55:44 -0700298void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
299 int numGlyphs, int x, int y, const float* positions) {
300 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
301 0, 0, NULL, positions);
302}
303
304void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
305 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
306 if (numGlyphs == 0 || text == NULL || len == 0) {
307 return;
308 }
309
310 text += start;
311
312 int glyphsCount = 0;
313 SkFixed prevRsbDelta = 0;
314
315 float penX = 0.0f;
316
317 SkPoint position;
318 SkVector tangent;
319
320 SkPathMeasure measure(*path, false);
321 float pathLength = SkScalarToFloat(measure.getLength());
322
323 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
324 float textWidth = SkScalarToFloat(paint->measureText(text, len));
325 float pathOffset = pathLength;
326 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
327 textWidth *= 0.5f;
328 pathOffset *= 0.5f;
329 }
330 penX += pathOffset - textWidth;
331 }
332
333 while (glyphsCount < numGlyphs && penX < pathLength) {
334 glyph_t glyph = GET_GLYPH(text);
335
336 if (IS_END_OF_STRING(glyph)) {
337 break;
338 }
339
340 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
341 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
342 prevRsbDelta = cachedGlyph->mRsbDelta;
343
Romain Guya4adcf02013-02-28 12:15:35 -0800344 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700345 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
346 }
347
348 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
349
350 glyphsCount++;
351 }
352}
353
354void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
355 int numGlyphs, Rect *bounds, const float* positions) {
356 if (bounds == NULL) {
357 ALOGE("No return rectangle provided to measure text");
358 return;
359 }
360 bounds->set(1e6, -1e6, -1e6, 1e6);
361 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
362}
363
364void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700365 if (numGlyphs == 0 || text == NULL) {
366 return;
367 }
368 int glyphsCount = 0;
369
370 while (glyphsCount < numGlyphs) {
371 glyph_t glyph = GET_GLYPH(text);
372
373 // Reached the end of the string
374 if (IS_END_OF_STRING(glyph)) {
375 break;
376 }
377
378 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
379
380 glyphsCount++;
381 }
382}
383
384void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
385 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
386 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
387 if (numGlyphs == 0 || text == NULL || len == 0) {
388 return;
389 }
390
391 static RenderGlyph gRenderGlyph[] = {
392 &android::uirenderer::Font::drawCachedGlyph,
Romain Guya4adcf02013-02-28 12:15:35 -0800393 &android::uirenderer::Font::drawCachedGlyphPerspective,
Romain Guy9f5dab32012-09-04 12:55:44 -0700394 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800395 &android::uirenderer::Font::drawCachedGlyphBitmap,
396 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700397 &android::uirenderer::Font::measureCachedGlyph
398 };
Romain Guya4adcf02013-02-28 12:15:35 -0800399 RenderGlyph render = gRenderGlyph[(mode << 1) + mTransform.isPerspective()];
Romain Guy9f5dab32012-09-04 12:55:44 -0700400
401 text += start;
402 int glyphsCount = 0;
403
Romain Guya4adcf02013-02-28 12:15:35 -0800404 const bool applyTransform = !mTransform.isIdentity() && !mTransform.isPerspective();
Romain Guye3a9b242013-01-08 11:15:30 -0800405 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700406
Romain Guye3a9b242013-01-08 11:15:30 -0800407 while (glyphsCount < numGlyphs) {
408 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700409
Romain Guye3a9b242013-01-08 11:15:30 -0800410 // Reached the end of the string
411 if (IS_END_OF_STRING(glyph)) {
412 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700413 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700414
Romain Guye3a9b242013-01-08 11:15:30 -0800415 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700416
Romain Guya4adcf02013-02-28 12:15:35 -0800417 // If it's still not valid, we couldn't cache it, so we shouldn't
418 // draw garbage; also skip empty glyphs (spaces)
419 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800420 float penX = x + positions[(glyphsCount << 1)];
421 float penY = y + positions[(glyphsCount << 1) + 1];
Romain Guye3a9b242013-01-08 11:15:30 -0800422
Romain Guya4adcf02013-02-28 12:15:35 -0800423 if (applyTransform) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800424 mTransform.mapPoint(penX, penY);
Romain Guy9f5dab32012-09-04 12:55:44 -0700425 }
426
Romain Guyc74f45a2013-02-26 19:10:14 -0800427 (*this.*render)(cachedGlyph, roundf(penX), roundf(penY),
Romain Guye3a9b242013-01-08 11:15:30 -0800428 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700429 }
Romain Guye3a9b242013-01-08 11:15:30 -0800430
431 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700432 }
433}
434
435void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
436 bool precaching) {
437 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
438 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
439 glyph->mBitmapLeft = skiaGlyph.fLeft;
440 glyph->mBitmapTop = skiaGlyph.fTop;
441 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
442 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
443
444 uint32_t startX = 0;
445 uint32_t startY = 0;
446
447 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800448 if (!skiaGlyph.fImage) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800449 paint->findImage(skiaGlyph, &mDescription.mLookupTransform);
Romain Guyb969a0d2013-02-05 14:38:40 -0800450 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700451 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
452
453 if (!glyph->mIsValid) {
454 return;
455 }
456
457 uint32_t endX = startX + skiaGlyph.fWidth;
458 uint32_t endY = startY + skiaGlyph.fHeight;
459
460 glyph->mStartX = startX;
461 glyph->mStartY = startY;
462 glyph->mBitmapWidth = skiaGlyph.fWidth;
463 glyph->mBitmapHeight = skiaGlyph.fHeight;
464
Romain Guya4adcf02013-02-28 12:15:35 -0800465 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
466 if (!empty) {
467 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
468 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700469
Romain Guya4adcf02013-02-28 12:15:35 -0800470 glyph->mBitmapMinU = startX / (float) cacheWidth;
471 glyph->mBitmapMinV = startY / (float) cacheHeight;
472 glyph->mBitmapMaxU = endX / (float) cacheWidth;
473 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700474
Romain Guya4adcf02013-02-28 12:15:35 -0800475 mState->setTextureDirty();
476 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700477}
478
479CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
480 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
481 mCachedGlyphs.add(glyph, newGlyph);
482
Romain Guyc74f45a2013-02-26 19:10:14 -0800483 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, &mDescription.mLookupTransform);
Romain Guy9f5dab32012-09-04 12:55:44 -0700484 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800485 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700486
487 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
488
489 return newGlyph;
490}
491
Romain Guye3a9b242013-01-08 11:15:30 -0800492Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
493 FontDescription description(paint, matrix);
494 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700495
Romain Guya4adcf02013-02-28 12:15:35 -0800496 if (!font) {
497 font = new Font(state, description);
498 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700499 }
Romain Guya4adcf02013-02-28 12:15:35 -0800500 font->mTransform.load(matrix);
Romain Guy9f5dab32012-09-04 12:55:44 -0700501
Romain Guya4adcf02013-02-28 12:15:35 -0800502 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700503}
504
505}; // namespace uirenderer
506}; // namespace android