blob: 02c1aa191e5cd2f9dee2723dfa46489dcebcb5cd [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
28#include "Debug.h"
29#include "FontUtil.h"
30#include "Font.h"
31#include "FontRenderer.h"
32#include "Properties.h"
33
34namespace android {
35namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
38// Font
39///////////////////////////////////////////////////////////////////////////////
40
Romain Guye3a9b242013-01-08 11:15:30 -080041Font::Font(FontRenderer* state, const Font::FontDescription& desc) :
42 mState(state), mDescription(desc) {
Romain Guy9f5dab32012-09-04 12:55:44 -070043}
44
Romain Guye3a9b242013-01-08 11:15:30 -080045Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) {
46 mFontId = SkTypeface::UniqueID(paint->getTypeface());
47 mFontSize = paint->getTextSize();
48 mFlags = 0;
49 if (paint->isFakeBoldText()) {
50 mFlags |= Font::kFakeBold;
51 }
52 mItalicStyle = paint->getTextSkewX();
53 mScaleX = paint->getTextScaleX();
54 mStyle = paint->getStyle();
55 mStrokeWidth = paint->getStrokeWidth();
Romain Guyb969a0d2013-02-05 14:38:40 -080056 mAntiAliasing = paint->isAntiAlias();
Romain Guya4adcf02013-02-28 12:15:35 -080057 mLookupTransform.reset();
Romain Guy8afce812013-03-06 19:09:59 -080058 mLookupTransform[SkMatrix::kMScaleX] = roundf(fmaxf(1.0f, matrix[mat4::kScaleX]));
59 mLookupTransform[SkMatrix::kMScaleY] = roundf(fmaxf(1.0f, matrix[mat4::kScaleY]));
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]));
Romain Guye3a9b242013-01-08 11:15:30 -080084 return JenkinsHashWhiten(hash);
85}
86
87int Font::FontDescription::compare(const Font::FontDescription& lhs,
88 const Font::FontDescription& rhs) {
89 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
90 if (deltaInt != 0) return deltaInt;
91
92 if (lhs.mFontSize < rhs.mFontSize) return -1;
93 if (lhs.mFontSize > rhs.mFontSize) return +1;
94
95 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
96 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
97
98 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
99 if (deltaInt != 0) return deltaInt;
100
101 if (lhs.mScaleX < rhs.mScaleX) return -1;
102 if (lhs.mScaleX > rhs.mScaleX) return +1;
103
104 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
105 if (deltaInt != 0) return deltaInt;
106
107 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
108 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
109
Romain Guyb969a0d2013-02-05 14:38:40 -0800110 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
111 if (deltaInt != 0) return deltaInt;
112
Romain Guyc74f45a2013-02-26 19:10:14 -0800113 if (lhs.mLookupTransform[SkMatrix::kMScaleX] <
114 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1;
115 if (lhs.mLookupTransform[SkMatrix::kMScaleX] >
116 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1;
117
118 if (lhs.mLookupTransform[SkMatrix::kMScaleY] <
119 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1;
120 if (lhs.mLookupTransform[SkMatrix::kMScaleY] >
121 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1;
122
Romain Guye3a9b242013-01-08 11:15:30 -0800123 return 0;
124}
125
Romain Guy80872462012-09-04 16:42:01 -0700126void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700127 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
128 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700129 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700130 cachedGlyph->mIsValid = false;
131 }
132 }
133}
134
135void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
136 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
137 int nPenX = x + glyph->mBitmapLeft;
138 int nPenY = y + glyph->mBitmapTop;
139
140 int width = (int) glyph->mBitmapWidth;
141 int height = (int) glyph->mBitmapHeight;
142
143 if (bounds->bottom > nPenY) {
144 bounds->bottom = nPenY;
145 }
146 if (bounds->left > nPenX) {
147 bounds->left = nPenX;
148 }
149 if (bounds->right < nPenX + width) {
150 bounds->right = nPenX + width;
151 }
152 if (bounds->top < nPenY + height) {
153 bounds->top = nPenY + height;
154 }
155}
156
157void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
158 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800159 float nPenX = x + glyph->mBitmapLeft;
Romain Guya4adcf02013-02-28 12:15:35 -0800160 float nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight;
Romain Guye3a9b242013-01-08 11:15:30 -0800161
162 float width = (float) glyph->mBitmapWidth;
163 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700164
165 float u1 = glyph->mBitmapMinU;
166 float u2 = glyph->mBitmapMaxU;
167 float v1 = glyph->mBitmapMinV;
168 float v2 = glyph->mBitmapMaxV;
169
Romain Guy9f5dab32012-09-04 12:55:44 -0700170 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
171 nPenX + width, nPenY, u2, v2,
172 nPenX + width, nPenY - height, u2, v1,
173 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
174}
175
Romain Guy624234f2013-03-05 16:43:31 -0800176void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Romain Guya4adcf02013-02-28 12:15:35 -0800177 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guya4adcf02013-02-28 12:15:35 -0800178 SkPoint p[4];
Romain Guy874f5c62013-03-01 18:07:35 -0800179 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + glyph->mBitmapHeight);
180 p[1].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop + glyph->mBitmapHeight);
181 p[2].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop);
182 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop);
Romain Guya4adcf02013-02-28 12:15:35 -0800183
Romain Guy874f5c62013-03-01 18:07:35 -0800184 mDescription.mInverseLookupTransform.mapPoints(p, 4);
Romain Guya4adcf02013-02-28 12:15:35 -0800185
186 p[0].offset(x, y);
187 p[1].offset(x, y);
188 p[2].offset(x, y);
189 p[3].offset(x, y);
190
191 float u1 = glyph->mBitmapMinU;
192 float u2 = glyph->mBitmapMaxU;
193 float v1 = glyph->mBitmapMinV;
194 float v2 = glyph->mBitmapMaxV;
195
196 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800197 p[0].x(), p[0].y(), u1, v2,
198 p[1].x(), p[1].y(), u2, v2,
199 p[2].x(), p[2].y(), u2, v1,
200 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture);
Romain Guya4adcf02013-02-28 12:15:35 -0800201}
202
Romain Guy9f5dab32012-09-04 12:55:44 -0700203void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y,
204 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
205 int nPenX = x + glyph->mBitmapLeft;
206 int nPenY = y + glyph->mBitmapTop;
207
208 uint32_t endX = glyph->mStartX + glyph->mBitmapWidth;
209 uint32_t endY = glyph->mStartY + glyph->mBitmapHeight;
210
Romain Guy80872462012-09-04 16:42:01 -0700211 CacheTexture* cacheTexture = glyph->mCacheTexture;
212 uint32_t cacheWidth = cacheTexture->getWidth();
213 const uint8_t* cacheBuffer = cacheTexture->getTexture();
Romain Guy9f5dab32012-09-04 12:55:44 -0700214
215 uint32_t cacheX = 0, cacheY = 0;
216 int32_t bX = 0, bY = 0;
217 for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) {
218 for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700219 uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX];
220 bitmap[bY * bitmapW + bX] = tempCol;
221 }
222 }
223}
224
225void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
226 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
227 const float halfWidth = glyph->mBitmapWidth * 0.5f;
228 const float height = glyph->mBitmapHeight;
229
230 vOffset += glyph->mBitmapTop + height;
231
232 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800233 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
234 if (!ok) {
235 ALOGW("The path for drawTextOnPath is empty or null");
236 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700237
238 // Move along the tangent and offset by the normal
239 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
240 -tangent->fY * halfWidth + tangent->fX * vOffset);
241 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
242 tangent->fY * halfWidth + tangent->fX * vOffset);
243 destination[2].set(destination[1].fX + tangent->fY * height,
244 destination[1].fY - tangent->fX * height);
245 destination[3].set(destination[0].fX + tangent->fY * height,
246 destination[0].fY - tangent->fX * height);
247
248 const float u1 = glyph->mBitmapMinU;
249 const float u2 = glyph->mBitmapMaxU;
250 const float v1 = glyph->mBitmapMinV;
251 const float v2 = glyph->mBitmapMaxV;
252
253 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800254 position->x() + destination[0].x(),
255 position->y() + destination[0].y(), u1, v2,
256 position->x() + destination[1].x(),
257 position->y() + destination[1].y(), u2, v2,
258 position->x() + destination[2].x(),
259 position->y() + destination[2].y(), u2, v1,
260 position->x() + destination[3].x(),
261 position->y() + destination[3].y(), u1, v1,
Romain Guy9f5dab32012-09-04 12:55:44 -0700262 glyph->mCacheTexture);
263}
264
265CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
Romain Guybd3055f2013-03-13 16:14:47 -0700266 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit);
267 if (cachedGlyph) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800268 // Is the glyph still in texture cache?
269 if (!cachedGlyph->mIsValid) {
Romain Guy0f667532013-03-01 14:31:04 -0800270 const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit,
271 &mDescription.mLookupTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -0800272 updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching);
273 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700274 } else {
275 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
276 }
277
Romain Guy9f5dab32012-09-04 12:55:44 -0700278 return cachedGlyph;
279}
280
Romain Guy9f5dab32012-09-04 12:55:44 -0700281void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
282 int numGlyphs, int x, int y, const float* positions) {
283 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
284 0, 0, NULL, positions);
285}
286
287void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
288 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
289 if (numGlyphs == 0 || text == NULL || len == 0) {
290 return;
291 }
292
293 text += start;
294
295 int glyphsCount = 0;
296 SkFixed prevRsbDelta = 0;
297
298 float penX = 0.0f;
299
300 SkPoint position;
301 SkVector tangent;
302
303 SkPathMeasure measure(*path, false);
304 float pathLength = SkScalarToFloat(measure.getLength());
305
306 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
307 float textWidth = SkScalarToFloat(paint->measureText(text, len));
308 float pathOffset = pathLength;
309 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
310 textWidth *= 0.5f;
311 pathOffset *= 0.5f;
312 }
313 penX += pathOffset - textWidth;
314 }
315
316 while (glyphsCount < numGlyphs && penX < pathLength) {
317 glyph_t glyph = GET_GLYPH(text);
318
319 if (IS_END_OF_STRING(glyph)) {
320 break;
321 }
322
323 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
324 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
325 prevRsbDelta = cachedGlyph->mRsbDelta;
326
Romain Guya4adcf02013-02-28 12:15:35 -0800327 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700328 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
329 }
330
331 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
332
333 glyphsCount++;
334 }
335}
336
337void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
338 int numGlyphs, Rect *bounds, const float* positions) {
339 if (bounds == NULL) {
340 ALOGE("No return rectangle provided to measure text");
341 return;
342 }
343 bounds->set(1e6, -1e6, -1e6, 1e6);
344 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
345}
346
347void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
Romain Guybd3055f2013-03-13 16:14:47 -0700348 ATRACE_NAME("precacheText");
349
Romain Guy9f5dab32012-09-04 12:55:44 -0700350 if (numGlyphs == 0 || text == NULL) {
351 return;
352 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700353
Romain Guybd3055f2013-03-13 16:14:47 -0700354 int glyphsCount = 0;
Romain Guy9f5dab32012-09-04 12:55:44 -0700355 while (glyphsCount < numGlyphs) {
356 glyph_t glyph = GET_GLYPH(text);
357
358 // Reached the end of the string
359 if (IS_END_OF_STRING(glyph)) {
360 break;
361 }
362
363 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
Romain Guy9f5dab32012-09-04 12:55:44 -0700364 glyphsCount++;
365 }
366}
367
368void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
369 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
370 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
371 if (numGlyphs == 0 || text == NULL || len == 0) {
372 return;
373 }
374
375 static RenderGlyph gRenderGlyph[] = {
376 &android::uirenderer::Font::drawCachedGlyph,
Romain Guy624234f2013-03-05 16:43:31 -0800377 &android::uirenderer::Font::drawCachedGlyphTransformed,
Romain Guy9f5dab32012-09-04 12:55:44 -0700378 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800379 &android::uirenderer::Font::drawCachedGlyphBitmap,
380 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700381 &android::uirenderer::Font::measureCachedGlyph
382 };
Romain Guy624234f2013-03-05 16:43:31 -0800383 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform];
Romain Guy9f5dab32012-09-04 12:55:44 -0700384
385 text += start;
386 int glyphsCount = 0;
387
Romain Guye3a9b242013-01-08 11:15:30 -0800388 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700389
Romain Guye3a9b242013-01-08 11:15:30 -0800390 while (glyphsCount < numGlyphs) {
391 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700392
Romain Guye3a9b242013-01-08 11:15:30 -0800393 // Reached the end of the string
394 if (IS_END_OF_STRING(glyph)) {
395 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700396 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700397
Romain Guye3a9b242013-01-08 11:15:30 -0800398 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700399
Romain Guya4adcf02013-02-28 12:15:35 -0800400 // If it's still not valid, we couldn't cache it, so we shouldn't
401 // draw garbage; also skip empty glyphs (spaces)
402 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800403 float penX = x + positions[(glyphsCount << 1)];
404 float penY = y + positions[(glyphsCount << 1) + 1];
Romain Guye3a9b242013-01-08 11:15:30 -0800405
Romain Guyc74f45a2013-02-26 19:10:14 -0800406 (*this.*render)(cachedGlyph, roundf(penX), roundf(penY),
Romain Guye3a9b242013-01-08 11:15:30 -0800407 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700408 }
Romain Guye3a9b242013-01-08 11:15:30 -0800409
410 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700411 }
412}
413
414void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph,
415 bool precaching) {
416 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
417 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
418 glyph->mBitmapLeft = skiaGlyph.fLeft;
419 glyph->mBitmapTop = skiaGlyph.fTop;
420 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
421 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
422
423 uint32_t startX = 0;
424 uint32_t startY = 0;
425
426 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800427 if (!skiaGlyph.fImage) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800428 paint->findImage(skiaGlyph, &mDescription.mLookupTransform);
Romain Guyb969a0d2013-02-05 14:38:40 -0800429 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700430 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
431
432 if (!glyph->mIsValid) {
433 return;
434 }
435
436 uint32_t endX = startX + skiaGlyph.fWidth;
437 uint32_t endY = startY + skiaGlyph.fHeight;
438
439 glyph->mStartX = startX;
440 glyph->mStartY = startY;
441 glyph->mBitmapWidth = skiaGlyph.fWidth;
442 glyph->mBitmapHeight = skiaGlyph.fHeight;
443
Romain Guya4adcf02013-02-28 12:15:35 -0800444 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
445 if (!empty) {
446 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
447 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700448
Romain Guya4adcf02013-02-28 12:15:35 -0800449 glyph->mBitmapMinU = startX / (float) cacheWidth;
450 glyph->mBitmapMinV = startY / (float) cacheHeight;
451 glyph->mBitmapMaxU = endX / (float) cacheWidth;
452 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700453
Romain Guya4adcf02013-02-28 12:15:35 -0800454 mState->setTextureDirty();
455 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700456}
457
458CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
459 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
460 mCachedGlyphs.add(glyph, newGlyph);
461
Romain Guyc74f45a2013-02-26 19:10:14 -0800462 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, &mDescription.mLookupTransform);
Romain Guy9f5dab32012-09-04 12:55:44 -0700463 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800464 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700465
466 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching);
467
468 return newGlyph;
469}
470
Romain Guye3a9b242013-01-08 11:15:30 -0800471Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
472 FontDescription description(paint, matrix);
473 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700474
Romain Guya4adcf02013-02-28 12:15:35 -0800475 if (!font) {
476 font = new Font(state, description);
477 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700478 }
Romain Guy624234f2013-03-05 16:43:31 -0800479 font->mIdentityTransform = matrix.isIdentity();
Romain Guy9f5dab32012-09-04 12:55:44 -0700480
Romain Guya4adcf02013-02-28 12:15:35 -0800481 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700482}
483
484}; // namespace uirenderer
485}; // namespace android