blob: bf522b7cc734a6fda41abbefad5f185b353eed78 [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 Guy874f5c62013-03-01 18:07:35 -080060 if (!mLookupTransform.invert(&mInverseLookupTransform)) {
61 ALOGW("Could not query the inverse lookup transform for this font");
62 }
Romain Guye3a9b242013-01-08 11:15:30 -080063}
Romain Guy9f5dab32012-09-04 12:55:44 -070064
65Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070066 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070067
68 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
69 delete mCachedGlyphs.valueAt(i);
70 }
71}
72
Romain Guye3a9b242013-01-08 11:15:30 -080073hash_t Font::FontDescription::hash() const {
74 uint32_t hash = JenkinsHashMix(0, mFontId);
75 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
76 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
77 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
78 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
79 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
80 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080081 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guyc74f45a2013-02-26 19:10:14 -080082 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX]));
83 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY]));
84 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMSkewX]));
85 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMSkewY]));
Romain Guye3a9b242013-01-08 11:15:30 -080086 return JenkinsHashWhiten(hash);
87}
88
89int Font::FontDescription::compare(const Font::FontDescription& lhs,
90 const Font::FontDescription& rhs) {
91 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
92 if (deltaInt != 0) return deltaInt;
93
94 if (lhs.mFontSize < rhs.mFontSize) return -1;
95 if (lhs.mFontSize > rhs.mFontSize) return +1;
96
97 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
98 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
99
100 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
101 if (deltaInt != 0) return deltaInt;
102
103 if (lhs.mScaleX < rhs.mScaleX) return -1;
104 if (lhs.mScaleX > rhs.mScaleX) return +1;
105
106 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
107 if (deltaInt != 0) return deltaInt;
108
109 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
110 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
111
Romain Guyb969a0d2013-02-05 14:38:40 -0800112 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
113 if (deltaInt != 0) return deltaInt;
114
Romain Guyc74f45a2013-02-26 19:10:14 -0800115 if (lhs.mLookupTransform[SkMatrix::kMScaleX] <
116 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1;
117 if (lhs.mLookupTransform[SkMatrix::kMScaleX] >
118 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1;
119
120 if (lhs.mLookupTransform[SkMatrix::kMScaleY] <
121 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1;
122 if (lhs.mLookupTransform[SkMatrix::kMScaleY] >
123 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1;
124
125 if (lhs.mLookupTransform[SkMatrix::kMSkewX] <
126 rhs.mLookupTransform[SkMatrix::kMSkewX]) return -1;
127 if (lhs.mLookupTransform[SkMatrix::kMSkewX] >
128 rhs.mLookupTransform[SkMatrix::kMSkewX]) return +1;
129
130 if (lhs.mLookupTransform[SkMatrix::kMSkewY] <
131 rhs.mLookupTransform[SkMatrix::kMSkewY]) return -1;
132 if (lhs.mLookupTransform[SkMatrix::kMSkewY] >
133 rhs.mLookupTransform[SkMatrix::kMSkewY]) return +1;
134
Romain Guye3a9b242013-01-08 11:15:30 -0800135 return 0;
136}
137
Romain Guy80872462012-09-04 16:42:01 -0700138void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700139 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
140 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700141 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700142 cachedGlyph->mIsValid = false;
143 }
144 }
145}
146
147void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
148 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
149 int nPenX = x + glyph->mBitmapLeft;
150 int nPenY = y + glyph->mBitmapTop;
151
152 int width = (int) glyph->mBitmapWidth;
153 int height = (int) glyph->mBitmapHeight;
154
155 if (bounds->bottom > nPenY) {
156 bounds->bottom = nPenY;
157 }
158 if (bounds->left > nPenX) {
159 bounds->left = nPenX;
160 }
161 if (bounds->right < nPenX + width) {
162 bounds->right = nPenX + width;
163 }
164 if (bounds->top < nPenY + height) {
165 bounds->top = nPenY + height;
166 }
167}
168
169void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
170 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800171 float nPenX = x + glyph->mBitmapLeft;
Romain Guya4adcf02013-02-28 12:15:35 -0800172 float nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight;
Romain Guye3a9b242013-01-08 11:15:30 -0800173
174 float width = (float) glyph->mBitmapWidth;
175 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700176
177 float u1 = glyph->mBitmapMinU;
178 float u2 = glyph->mBitmapMaxU;
179 float v1 = glyph->mBitmapMinV;
180 float v2 = glyph->mBitmapMaxV;
181
Romain Guy9f5dab32012-09-04 12:55:44 -0700182 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
183 nPenX + width, nPenY, u2, v2,
184 nPenX + width, nPenY - height, u2, v1,
185 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
186}
187
Romain Guya4adcf02013-02-28 12:15:35 -0800188void Font::drawCachedGlyphPerspective(CachedGlyphInfo* glyph, int x, int y,
189 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guya4adcf02013-02-28 12:15:35 -0800190 SkPoint p[4];
Romain Guy874f5c62013-03-01 18:07:35 -0800191 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + glyph->mBitmapHeight);
192 p[1].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop + glyph->mBitmapHeight);
193 p[2].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop);
194 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop);
Romain Guya4adcf02013-02-28 12:15:35 -0800195
Romain Guy874f5c62013-03-01 18:07:35 -0800196 mDescription.mInverseLookupTransform.mapPoints(p, 4);
Romain Guya4adcf02013-02-28 12:15:35 -0800197
198 p[0].offset(x, y);
199 p[1].offset(x, y);
200 p[2].offset(x, y);
201 p[3].offset(x, y);
202
203 float u1 = glyph->mBitmapMinU;
204 float u2 = glyph->mBitmapMaxU;
205 float v1 = glyph->mBitmapMinV;
206 float v2 = glyph->mBitmapMaxV;
207
208 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800209 p[0].x(), p[0].y(), u1, v2,
210 p[1].x(), p[1].y(), u2, v2,
211 p[2].x(), p[2].y(), u2, v1,
212 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture);
Romain Guya4adcf02013-02-28 12:15:35 -0800213}
214
Romain Guy9f5dab32012-09-04 12:55:44 -0700215void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
216 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
217 int nPenX = x + glyph->mBitmapLeft;
218 int nPenY = y + glyph->mBitmapTop;
219
220 uint32_t endX = glyph->mStartX + glyph->mBitmapWidth;
221 uint32_t endY = glyph->mStartY + glyph->mBitmapHeight;
222
Romain Guy80872462012-09-04 16:42:01 -0700223 CacheTexture* cacheTexture = glyph->mCacheTexture;
224 uint32_t cacheWidth = cacheTexture->getWidth();
225 const uint8_t* cacheBuffer = cacheTexture->getTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -0700226
227 uint32_t cacheX = 0, cacheY = 0;
228 int32_t bX = 0, bY = 0;
229 for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) {
230 for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700231 uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX];
232 bitmap[bY * bitmapW + bX] = tempCol;
233 }
234 }
235}
236
237void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
238 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
239 const float halfWidth = glyph->mBitmapWidth * 0.5f;
240 const float height = glyph->mBitmapHeight;
241
242 vOffset += glyph->mBitmapTop + height;
243
244 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800245 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
246 if (!ok) {
247 ALOGW("The path for drawTextOnPath is empty or null");
248 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700249
250 // Move along the tangent and offset by the normal
251 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
252 -tangent->fY * halfWidth + tangent->fX * vOffset);
253 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
254 tangent->fY * halfWidth + tangent->fX * vOffset);
255 destination[2].set(destination[1].fX + tangent->fY * height,
256 destination[1].fY - tangent->fX * height);
257 destination[3].set(destination[0].fX + tangent->fY * height,
258 destination[0].fY - tangent->fX * height);
259
260 const float u1 = glyph->mBitmapMinU;
261 const float u2 = glyph->mBitmapMaxU;
262 const float v1 = glyph->mBitmapMinV;
263 const float v2 = glyph->mBitmapMaxV;
264
265 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800266 position->x() + destination[0].x(),
267 position->y() + destination[0].y(), u1, v2,
268 position->x() + destination[1].x(),
269 position->y() + destination[1].y(), u2, v2,
270 position->x() + destination[2].x(),
271 position->y() + destination[2].y(), u2, v1,
272 position->x() + destination[3].x(),
273 position->y() + destination[3].y(), u1, v1,
Romain Guy9f5dab32012-09-04 12:55:44 -0700274 glyph->mCacheTexture);
275}
276
277CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
278 CachedGlyphInfo* cachedGlyph = NULL;
279 ssize_t index = mCachedGlyphs.indexOfKey(textUnit);
280 if (index >= 0) {
281 cachedGlyph = mCachedGlyphs.valueAt(index);
Romain Guyc74f45a2013-02-26 19:10:14 -0800282
283 // Is the glyph still in texture cache?
284 if (!cachedGlyph->mIsValid) {
Romain Guy0f6675332013-03-01 14:31:04 -0800285 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit,
286 &mDescription.mLookupTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -0800287 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
288 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700289 } else {
290 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
291 }
292
Romain Guy9f5dab32012-09-04 12:55:44 -0700293 return cachedGlyph;
294}
295
Romain Guy9f5dab32012-09-04 12:55:44 -0700296void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
297 int numGlyphs, int x, int y, const float* positions) {
298 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
299 0, 0, NULL, positions);
300}
301
302void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
303 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
304 if (numGlyphs == 0 || text == NULL || len == 0) {
305 return;
306 }
307
308 text += start;
309
310 int glyphsCount = 0;
311 SkFixed prevRsbDelta = 0;
312
313 float penX = 0.0f;
314
315 SkPoint position;
316 SkVector tangent;
317
318 SkPathMeasure measure(*path, false);
319 float pathLength = SkScalarToFloat(measure.getLength());
320
321 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
322 float textWidth = SkScalarToFloat(paint->measureText(text, len));
323 float pathOffset = pathLength;
324 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
325 textWidth *= 0.5f;
326 pathOffset *= 0.5f;
327 }
328 penX += pathOffset - textWidth;
329 }
330
331 while (glyphsCount < numGlyphs && penX < pathLength) {
332 glyph_t glyph = GET_GLYPH(text);
333
334 if (IS_END_OF_STRING(glyph)) {
335 break;
336 }
337
338 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
339 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
340 prevRsbDelta = cachedGlyph->mRsbDelta;
341
Romain Guya4adcf02013-02-28 12:15:35 -0800342 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700343 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
344 }
345
346 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
347
348 glyphsCount++;
349 }
350}
351
352void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
353 int numGlyphs, Rect *bounds, const float* positions) {
354 if (bounds == NULL) {
355 ALOGE("No return rectangle provided to measure text");
356 return;
357 }
358 bounds->set(1e6, -1e6, -1e6, 1e6);
359 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
360}
361
362void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700363 if (numGlyphs == 0 || text == NULL) {
364 return;
365 }
366 int glyphsCount = 0;
367
368 while (glyphsCount < numGlyphs) {
369 glyph_t glyph = GET_GLYPH(text);
370
371 // Reached the end of the string
372 if (IS_END_OF_STRING(glyph)) {
373 break;
374 }
375
376 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
377
378 glyphsCount++;
379 }
380}
381
382void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
383 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
384 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
385 if (numGlyphs == 0 || text == NULL || len == 0) {
386 return;
387 }
388
389 static RenderGlyph gRenderGlyph[] = {
390 &android::uirenderer::Font::drawCachedGlyph,
Romain Guya4adcf02013-02-28 12:15:35 -0800391 &android::uirenderer::Font::drawCachedGlyphPerspective,
Romain Guy9f5dab32012-09-04 12:55:44 -0700392 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800393 &android::uirenderer::Font::drawCachedGlyphBitmap,
394 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700395 &android::uirenderer::Font::measureCachedGlyph
396 };
Romain Guya4adcf02013-02-28 12:15:35 -0800397 RenderGlyph render = gRenderGlyph[(mode << 1) + mTransform.isPerspective()];
Romain Guy9f5dab32012-09-04 12:55:44 -0700398
399 text += start;
400 int glyphsCount = 0;
401
Romain Guya4adcf02013-02-28 12:15:35 -0800402 const bool applyTransform = !mTransform.isIdentity() && !mTransform.isPerspective();
Romain Guye3a9b242013-01-08 11:15:30 -0800403 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700404
Romain Guye3a9b242013-01-08 11:15:30 -0800405 while (glyphsCount < numGlyphs) {
406 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700407
Romain Guye3a9b242013-01-08 11:15:30 -0800408 // Reached the end of the string
409 if (IS_END_OF_STRING(glyph)) {
410 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700411 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700412
Romain Guye3a9b242013-01-08 11:15:30 -0800413 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700414
Romain Guya4adcf02013-02-28 12:15:35 -0800415 // If it's still not valid, we couldn't cache it, so we shouldn't
416 // draw garbage; also skip empty glyphs (spaces)
417 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800418 float penX = x + positions[(glyphsCount << 1)];
419 float penY = y + positions[(glyphsCount << 1) + 1];
Romain Guye3a9b242013-01-08 11:15:30 -0800420
Romain Guya4adcf02013-02-28 12:15:35 -0800421 if (applyTransform) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800422 mTransform.mapPoint(penX, penY);
Romain Guy9f5dab32012-09-04 12:55:44 -0700423 }
424
Romain Guyc74f45a2013-02-26 19:10:14 -0800425 (*this.*render)(cachedGlyph, roundf(penX), roundf(penY),
Romain Guye3a9b242013-01-08 11:15:30 -0800426 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700427 }
Romain Guye3a9b242013-01-08 11:15:30 -0800428
429 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700430 }
431}
432
433void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
434 bool precaching) {
435 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
436 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
437 glyph->mBitmapLeft = skiaGlyph.fLeft;
438 glyph->mBitmapTop = skiaGlyph.fTop;
439 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
440 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
441
442 uint32_t startX = 0;
443 uint32_t startY = 0;
444
445 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800446 if (!skiaGlyph.fImage) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800447 paint->findImage(skiaGlyph, &mDescription.mLookupTransform);
Romain Guyb969a0d2013-02-05 14:38:40 -0800448 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700449 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
450
451 if (!glyph->mIsValid) {
452 return;
453 }
454
455 uint32_t endX = startX + skiaGlyph.fWidth;
456 uint32_t endY = startY + skiaGlyph.fHeight;
457
458 glyph->mStartX = startX;
459 glyph->mStartY = startY;
460 glyph->mBitmapWidth = skiaGlyph.fWidth;
461 glyph->mBitmapHeight = skiaGlyph.fHeight;
462
Romain Guya4adcf02013-02-28 12:15:35 -0800463 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
464 if (!empty) {
465 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
466 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700467
Romain Guya4adcf02013-02-28 12:15:35 -0800468 glyph->mBitmapMinU = startX / (float) cacheWidth;
469 glyph->mBitmapMinV = startY / (float) cacheHeight;
470 glyph->mBitmapMaxU = endX / (float) cacheWidth;
471 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700472
Romain Guya4adcf02013-02-28 12:15:35 -0800473 mState->setTextureDirty();
474 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700475}
476
477CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
478 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
479 mCachedGlyphs.add(glyph, newGlyph);
480
Romain Guyc74f45a2013-02-26 19:10:14 -0800481 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, &mDescription.mLookupTransform);
Romain Guy9f5dab32012-09-04 12:55:44 -0700482 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800483 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700484
485 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
486
487 return newGlyph;
488}
489
Romain Guye3a9b242013-01-08 11:15:30 -0800490Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
491 FontDescription description(paint, matrix);
492 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700493
Romain Guya4adcf02013-02-28 12:15:35 -0800494 if (!font) {
495 font = new Font(state, description);
496 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700497 }
Romain Guya4adcf02013-02-28 12:15:35 -0800498 font->mTransform.load(matrix);
Romain Guy9f5dab32012-09-04 12:55:44 -0700499
Romain Guya4adcf02013-02-28 12:15:35 -0800500 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700501}
502
503}; // namespace uirenderer
504}; // namespace android