blob: 18983d8b6d32af08bad683b26ff75d7e6c8ec532 [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"
Romain Guybd3055f2013-03-13 16:14:47 -070018#define ATRACE_TAG ATRACE_TAG_VIEW
Romain Guya3dc55f2012-09-28 13:55:44 -070019
Romain Guy9f5dab32012-09-04 12:55:44 -070020#include <cutils/compiler.h>
21
Romain Guye3a9b242013-01-08 11:15:30 -080022#include <utils/JenkinsHash.h>
Romain Guybd3055f2013-03-13 16:14:47 -070023#include <utils/Trace.h>
Romain Guye3a9b242013-01-08 11:15:30 -080024
Romain Guy14c40b42013-01-08 18:03:07 -080025#include <SkGlyph.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070026#include <SkUtils.h>
27
Romain Guy9f5dab32012-09-04 12:55:44 -070028#include "FontUtil.h"
29#include "Font.h"
Romain Guycf51a412013-04-08 19:40:31 -070030#include "../Debug.h"
31#include "../FontRenderer.h"
32#include "../PixelBuffer.h"
33#include "../Properties.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070034
35namespace android {
36namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
39// Font
40///////////////////////////////////////////////////////////////////////////////
41
Romain Guye3a9b242013-01-08 11:15:30 -080042Font::Font(FontRenderer* state, const Font::FontDescription& desc) :
43 mState(state), mDescription(desc) {
Romain Guy9f5dab32012-09-04 12:55:44 -070044}
45
Romain Guye3a9b242013-01-08 11:15:30 -080046Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) {
47 mFontId = SkTypeface::UniqueID(paint->getTypeface());
48 mFontSize = paint->getTextSize();
49 mFlags = 0;
50 if (paint->isFakeBoldText()) {
51 mFlags |= Font::kFakeBold;
52 }
53 mItalicStyle = paint->getTextSkewX();
54 mScaleX = paint->getTextScaleX();
55 mStyle = paint->getStyle();
56 mStrokeWidth = paint->getStrokeWidth();
Romain Guyb969a0d2013-02-05 14:38:40 -080057 mAntiAliasing = paint->isAntiAlias();
Romain Guy2d5945e2013-06-18 12:59:25 -070058 mHinting = paint->getHinting();
Romain Guya4adcf02013-02-28 12:15:35 -080059 mLookupTransform.reset();
Romain Guy8afce812013-03-06 19:09:59 -080060 mLookupTransform[SkMatrix::kMScaleX] = roundf(fmaxf(1.0f, matrix[mat4::kScaleX]));
61 mLookupTransform[SkMatrix::kMScaleY] = roundf(fmaxf(1.0f, matrix[mat4::kScaleY]));
Romain Guy874f5c62013-03-01 18:07:35 -080062 if (!mLookupTransform.invert(&mInverseLookupTransform)) {
63 ALOGW("Could not query the inverse lookup transform for this font");
64 }
Romain Guye3a9b242013-01-08 11:15:30 -080065}
Romain Guy9f5dab32012-09-04 12:55:44 -070066
67Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070068 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070069
70 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
71 delete mCachedGlyphs.valueAt(i);
72 }
73}
74
Romain Guye3a9b242013-01-08 11:15:30 -080075hash_t Font::FontDescription::hash() const {
76 uint32_t hash = JenkinsHashMix(0, mFontId);
77 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
78 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
79 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
80 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
81 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
82 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080083 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guy2d5945e2013-06-18 12:59:25 -070084 hash = JenkinsHashMix(hash, android::hash_type(mHinting));
Romain Guyc74f45a2013-02-26 19:10:14 -080085 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX]));
86 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY]));
Romain Guye3a9b242013-01-08 11:15:30 -080087 return JenkinsHashWhiten(hash);
88}
89
90int Font::FontDescription::compare(const Font::FontDescription& lhs,
91 const Font::FontDescription& rhs) {
92 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
93 if (deltaInt != 0) return deltaInt;
94
95 if (lhs.mFontSize < rhs.mFontSize) return -1;
96 if (lhs.mFontSize > rhs.mFontSize) return +1;
97
98 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
99 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
100
101 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
102 if (deltaInt != 0) return deltaInt;
103
104 if (lhs.mScaleX < rhs.mScaleX) return -1;
105 if (lhs.mScaleX > rhs.mScaleX) return +1;
106
107 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
108 if (deltaInt != 0) return deltaInt;
109
110 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
111 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
112
Romain Guyb969a0d2013-02-05 14:38:40 -0800113 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
114 if (deltaInt != 0) return deltaInt;
115
Romain Guy2d5945e2013-06-18 12:59:25 -0700116 deltaInt = int(lhs.mHinting) - int(rhs.mHinting);
117 if (deltaInt != 0) return deltaInt;
118
Romain Guyc74f45a2013-02-26 19:10:14 -0800119 if (lhs.mLookupTransform[SkMatrix::kMScaleX] <
120 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1;
121 if (lhs.mLookupTransform[SkMatrix::kMScaleX] >
122 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1;
123
124 if (lhs.mLookupTransform[SkMatrix::kMScaleY] <
125 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1;
126 if (lhs.mLookupTransform[SkMatrix::kMScaleY] >
127 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1;
128
Romain Guye3a9b242013-01-08 11:15:30 -0800129 return 0;
130}
131
Romain Guy80872462012-09-04 16:42:01 -0700132void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700133 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
134 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700135 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700136 cachedGlyph->mIsValid = false;
137 }
138 }
139}
140
141void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
142 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
143 int nPenX = x + glyph->mBitmapLeft;
144 int nPenY = y + glyph->mBitmapTop;
145
146 int width = (int) glyph->mBitmapWidth;
147 int height = (int) glyph->mBitmapHeight;
148
149 if (bounds->bottom > nPenY) {
150 bounds->bottom = nPenY;
151 }
152 if (bounds->left > nPenX) {
153 bounds->left = nPenX;
154 }
155 if (bounds->right < nPenX + width) {
156 bounds->right = nPenX + width;
157 }
158 if (bounds->top < nPenY + height) {
159 bounds->top = nPenY + height;
160 }
161}
162
163void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
164 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800165 float nPenX = x + glyph->mBitmapLeft;
Romain Guya4adcf02013-02-28 12:15:35 -0800166 float nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight;
Romain Guye3a9b242013-01-08 11:15:30 -0800167
168 float width = (float) glyph->mBitmapWidth;
169 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700170
171 float u1 = glyph->mBitmapMinU;
172 float u2 = glyph->mBitmapMaxU;
173 float v1 = glyph->mBitmapMinV;
174 float v2 = glyph->mBitmapMaxV;
175
Romain Guy9f5dab32012-09-04 12:55:44 -0700176 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
177 nPenX + width, nPenY, u2, v2,
178 nPenX + width, nPenY - height, u2, v1,
179 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
180}
181
Romain Guy624234f2013-03-05 16:43:31 -0800182void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Romain Guya4adcf02013-02-28 12:15:35 -0800183 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guya4adcf02013-02-28 12:15:35 -0800184 SkPoint p[4];
Romain Guy874f5c62013-03-01 18:07:35 -0800185 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + glyph->mBitmapHeight);
186 p[1].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop + glyph->mBitmapHeight);
187 p[2].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop);
188 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop);
Romain Guya4adcf02013-02-28 12:15:35 -0800189
Romain Guy874f5c62013-03-01 18:07:35 -0800190 mDescription.mInverseLookupTransform.mapPoints(p, 4);
Romain Guya4adcf02013-02-28 12:15:35 -0800191
192 p[0].offset(x, y);
193 p[1].offset(x, y);
194 p[2].offset(x, y);
195 p[3].offset(x, y);
196
197 float u1 = glyph->mBitmapMinU;
198 float u2 = glyph->mBitmapMaxU;
199 float v1 = glyph->mBitmapMinV;
200 float v2 = glyph->mBitmapMaxV;
201
202 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800203 p[0].x(), p[0].y(), u1, v2,
204 p[1].x(), p[1].y(), u2, v2,
205 p[2].x(), p[2].y(), u2, v1,
206 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture);
Romain Guya4adcf02013-02-28 12:15:35 -0800207}
208
Romain Guycf51a412013-04-08 19:40:31 -0700209void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap,
210 uint32_t bitmapWidth, uint32_t bitmapHeight, Rect* bounds, const float* pos) {
211 int dstX = x + glyph->mBitmapLeft;
212 int dstY = y + glyph->mBitmapTop;
Romain Guy9f5dab32012-09-04 12:55:44 -0700213
Romain Guy80872462012-09-04 16:42:01 -0700214 CacheTexture* cacheTexture = glyph->mCacheTexture;
Romain Guy9f5dab32012-09-04 12:55:44 -0700215
Romain Guycf51a412013-04-08 19:40:31 -0700216 uint32_t cacheWidth = cacheTexture->getWidth();
217 uint32_t startY = glyph->mStartY * cacheWidth;
218 uint32_t endY = startY + (glyph->mBitmapHeight * cacheWidth);
219
220 PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer();
221 const uint8_t* cacheBuffer = pixelBuffer->map();
222
223 for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY;
224 cacheY += cacheWidth, bitmapY += bitmapWidth) {
225 memcpy(&bitmap[bitmapY + dstX], &cacheBuffer[cacheY + glyph->mStartX], glyph->mBitmapWidth);
Romain Guy9f5dab32012-09-04 12:55:44 -0700226 }
227}
228
229void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
230 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
231 const float halfWidth = glyph->mBitmapWidth * 0.5f;
232 const float height = glyph->mBitmapHeight;
233
234 vOffset += glyph->mBitmapTop + height;
235
236 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800237 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
238 if (!ok) {
239 ALOGW("The path for drawTextOnPath is empty or null");
240 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700241
242 // Move along the tangent and offset by the normal
243 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
244 -tangent->fY * halfWidth + tangent->fX * vOffset);
245 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
246 tangent->fY * halfWidth + tangent->fX * vOffset);
247 destination[2].set(destination[1].fX + tangent->fY * height,
248 destination[1].fY - tangent->fX * height);
249 destination[3].set(destination[0].fX + tangent->fY * height,
250 destination[0].fY - tangent->fX * height);
251
252 const float u1 = glyph->mBitmapMinU;
253 const float u2 = glyph->mBitmapMaxU;
254 const float v1 = glyph->mBitmapMinV;
255 const float v2 = glyph->mBitmapMaxV;
256
257 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800258 position->x() + destination[0].x(),
259 position->y() + destination[0].y(), u1, v2,
260 position->x() + destination[1].x(),
261 position->y() + destination[1].y(), u2, v2,
262 position->x() + destination[2].x(),
263 position->y() + destination[2].y(), u2, v1,
264 position->x() + destination[3].x(),
265 position->y() + destination[3].y(), u1, v1,
Romain Guy9f5dab32012-09-04 12:55:44 -0700266 glyph->mCacheTexture);
267}
268
269CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
Romain Guybd3055f2013-03-13 16:14:47 -0700270 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit);
271 if (cachedGlyph) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800272 // Is the glyph still in texture cache?
273 if (!cachedGlyph->mIsValid) {
Romain Guy0f667532013-03-01 14:31:04 -0800274 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit,
275 &mDescription.mLookupTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -0800276 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
277 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700278 } else {
279 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
280 }
281
Romain Guy9f5dab32012-09-04 12:55:44 -0700282 return cachedGlyph;
283}
284
Romain Guy9f5dab32012-09-04 12:55:44 -0700285void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
286 int numGlyphs, int x, int y, const float* positions) {
287 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
288 0, 0, NULL, positions);
289}
290
291void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
292 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
293 if (numGlyphs == 0 || text == NULL || len == 0) {
294 return;
295 }
296
297 text += start;
298
299 int glyphsCount = 0;
300 SkFixed prevRsbDelta = 0;
301
302 float penX = 0.0f;
303
304 SkPoint position;
305 SkVector tangent;
306
307 SkPathMeasure measure(*path, false);
308 float pathLength = SkScalarToFloat(measure.getLength());
309
310 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
311 float textWidth = SkScalarToFloat(paint->measureText(text, len));
312 float pathOffset = pathLength;
313 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
314 textWidth *= 0.5f;
315 pathOffset *= 0.5f;
316 }
317 penX += pathOffset - textWidth;
318 }
319
320 while (glyphsCount < numGlyphs && penX < pathLength) {
321 glyph_t glyph = GET_GLYPH(text);
322
323 if (IS_END_OF_STRING(glyph)) {
324 break;
325 }
326
327 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
328 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
329 prevRsbDelta = cachedGlyph->mRsbDelta;
330
Romain Guya4adcf02013-02-28 12:15:35 -0800331 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700332 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
333 }
334
335 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
336
337 glyphsCount++;
338 }
339}
340
341void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
342 int numGlyphs, Rect *bounds, const float* positions) {
343 if (bounds == NULL) {
344 ALOGE("No return rectangle provided to measure text");
345 return;
346 }
347 bounds->set(1e6, -1e6, -1e6, 1e6);
348 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
349}
350
351void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
Romain Guybd3055f2013-03-13 16:14:47 -0700352 ATRACE_NAME("precacheText");
353
Romain Guy9f5dab32012-09-04 12:55:44 -0700354 if (numGlyphs == 0 || text == NULL) {
355 return;
356 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700357
Romain Guybd3055f2013-03-13 16:14:47 -0700358 int glyphsCount = 0;
Romain Guy9f5dab32012-09-04 12:55:44 -0700359 while (glyphsCount < numGlyphs) {
360 glyph_t glyph = GET_GLYPH(text);
361
362 // Reached the end of the string
363 if (IS_END_OF_STRING(glyph)) {
364 break;
365 }
366
367 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
Romain Guy9f5dab32012-09-04 12:55:44 -0700368 glyphsCount++;
369 }
370}
371
372void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
373 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
374 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
375 if (numGlyphs == 0 || text == NULL || len == 0) {
376 return;
377 }
378
379 static RenderGlyph gRenderGlyph[] = {
380 &android::uirenderer::Font::drawCachedGlyph,
Romain Guy624234f2013-03-05 16:43:31 -0800381 &android::uirenderer::Font::drawCachedGlyphTransformed,
Romain Guy9f5dab32012-09-04 12:55:44 -0700382 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800383 &android::uirenderer::Font::drawCachedGlyphBitmap,
384 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700385 &android::uirenderer::Font::measureCachedGlyph
386 };
Romain Guy624234f2013-03-05 16:43:31 -0800387 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform];
Romain Guy9f5dab32012-09-04 12:55:44 -0700388
389 text += start;
390 int glyphsCount = 0;
391
Romain Guye3a9b242013-01-08 11:15:30 -0800392 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700393
Romain Guye3a9b242013-01-08 11:15:30 -0800394 while (glyphsCount < numGlyphs) {
395 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700396
Romain Guye3a9b242013-01-08 11:15:30 -0800397 // Reached the end of the string
398 if (IS_END_OF_STRING(glyph)) {
399 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700400 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700401
Romain Guye3a9b242013-01-08 11:15:30 -0800402 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700403
Romain Guya4adcf02013-02-28 12:15:35 -0800404 // If it's still not valid, we couldn't cache it, so we shouldn't
405 // draw garbage; also skip empty glyphs (spaces)
406 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800407 float penX = x + positions[(glyphsCount << 1)];
408 float penY = y + positions[(glyphsCount << 1) + 1];
Romain Guye3a9b242013-01-08 11:15:30 -0800409
Romain Guyc74f45a2013-02-26 19:10:14 -0800410 (*this.*render)(cachedGlyph, roundf(penX), roundf(penY),
Romain Guye3a9b242013-01-08 11:15:30 -0800411 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700412 }
Romain Guye3a9b242013-01-08 11:15:30 -0800413
414 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700415 }
416}
417
418void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
419 bool precaching) {
420 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
421 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
422 glyph->mBitmapLeft = skiaGlyph.fLeft;
423 glyph->mBitmapTop = skiaGlyph.fTop;
424 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
425 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
426
427 uint32_t startX = 0;
428 uint32_t startY = 0;
429
430 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800431 if (!skiaGlyph.fImage) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800432 paint->findImage(skiaGlyph, &mDescription.mLookupTransform);
Romain Guyb969a0d2013-02-05 14:38:40 -0800433 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700434 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
435
436 if (!glyph->mIsValid) {
437 return;
438 }
439
440 uint32_t endX = startX + skiaGlyph.fWidth;
441 uint32_t endY = startY + skiaGlyph.fHeight;
442
443 glyph->mStartX = startX;
444 glyph->mStartY = startY;
445 glyph->mBitmapWidth = skiaGlyph.fWidth;
446 glyph->mBitmapHeight = skiaGlyph.fHeight;
447
Romain Guya4adcf02013-02-28 12:15:35 -0800448 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
449 if (!empty) {
450 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
451 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700452
Romain Guya4adcf02013-02-28 12:15:35 -0800453 glyph->mBitmapMinU = startX / (float) cacheWidth;
454 glyph->mBitmapMinV = startY / (float) cacheHeight;
455 glyph->mBitmapMaxU = endX / (float) cacheWidth;
456 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700457
Romain Guya4adcf02013-02-28 12:15:35 -0800458 mState->setTextureDirty();
459 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700460}
461
462CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
463 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
464 mCachedGlyphs.add(glyph, newGlyph);
465
Romain Guyc74f45a2013-02-26 19:10:14 -0800466 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, &mDescription.mLookupTransform);
Romain Guy9f5dab32012-09-04 12:55:44 -0700467 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800468 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700469
470 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
471
472 return newGlyph;
473}
474
Romain Guye3a9b242013-01-08 11:15:30 -0800475Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
476 FontDescription description(paint, matrix);
477 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700478
Romain Guya4adcf02013-02-28 12:15:35 -0800479 if (!font) {
480 font = new Font(state, description);
481 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700482 }
Romain Guy624234f2013-03-05 16:43:31 -0800483 font->mIdentityTransform = matrix.isIdentity();
Romain Guy9f5dab32012-09-04 12:55:44 -0700484
Romain Guya4adcf02013-02-28 12:15:35 -0800485 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700486}
487
488}; // namespace uirenderer
489}; // namespace android