blob: 0a6e6ef2b936ddf1eba0886c79124a846748eb96 [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>
Leon Scroggins IIIf8d87772013-12-03 16:26:51 -050026#include <SkGlyphCache.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070027#include <SkUtils.h>
28
Romain Guy9f5dab32012-09-04 12:55:44 -070029#include "FontUtil.h"
30#include "Font.h"
Romain Guycf51a412013-04-08 19:40:31 -070031#include "../Debug.h"
32#include "../FontRenderer.h"
33#include "../PixelBuffer.h"
34#include "../Properties.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070035
36namespace android {
37namespace uirenderer {
38
39///////////////////////////////////////////////////////////////////////////////
40// Font
41///////////////////////////////////////////////////////////////////////////////
42
Romain Guye3a9b242013-01-08 11:15:30 -080043Font::Font(FontRenderer* state, const Font::FontDescription& desc) :
44 mState(state), mDescription(desc) {
Victoria Leasedf64ac62014-04-22 15:00:31 -070045 mDeviceProperties = SkDeviceProperties::Make(SkDeviceProperties::Geometry::MakeDefault(), 1.0f);
Romain Guy9f5dab32012-09-04 12:55:44 -070046}
47
Romain Guye3a9b242013-01-08 11:15:30 -080048Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) {
49 mFontId = SkTypeface::UniqueID(paint->getTypeface());
50 mFontSize = paint->getTextSize();
51 mFlags = 0;
52 if (paint->isFakeBoldText()) {
53 mFlags |= Font::kFakeBold;
54 }
55 mItalicStyle = paint->getTextSkewX();
56 mScaleX = paint->getTextScaleX();
57 mStyle = paint->getStyle();
58 mStrokeWidth = paint->getStrokeWidth();
Romain Guyb969a0d2013-02-05 14:38:40 -080059 mAntiAliasing = paint->isAntiAlias();
Romain Guy2d5945e2013-06-18 12:59:25 -070060 mHinting = paint->getHinting();
Romain Guya4adcf02013-02-28 12:15:35 -080061 mLookupTransform.reset();
Romain Guy8afce812013-03-06 19:09:59 -080062 mLookupTransform[SkMatrix::kMScaleX] = roundf(fmaxf(1.0f, matrix[mat4::kScaleX]));
63 mLookupTransform[SkMatrix::kMScaleY] = roundf(fmaxf(1.0f, matrix[mat4::kScaleY]));
Romain Guy874f5c62013-03-01 18:07:35 -080064 if (!mLookupTransform.invert(&mInverseLookupTransform)) {
65 ALOGW("Could not query the inverse lookup transform for this font");
66 }
Romain Guye3a9b242013-01-08 11:15:30 -080067}
Romain Guy9f5dab32012-09-04 12:55:44 -070068
69Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070070 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070071
72 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
73 delete mCachedGlyphs.valueAt(i);
74 }
75}
76
Romain Guye3a9b242013-01-08 11:15:30 -080077hash_t Font::FontDescription::hash() const {
78 uint32_t hash = JenkinsHashMix(0, mFontId);
79 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
80 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
81 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
82 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
83 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
84 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080085 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guy2d5945e2013-06-18 12:59:25 -070086 hash = JenkinsHashMix(hash, android::hash_type(mHinting));
Romain Guyc74f45a2013-02-26 19:10:14 -080087 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX]));
88 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY]));
Romain Guye3a9b242013-01-08 11:15:30 -080089 return JenkinsHashWhiten(hash);
90}
91
92int Font::FontDescription::compare(const Font::FontDescription& lhs,
93 const Font::FontDescription& rhs) {
94 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
95 if (deltaInt != 0) return deltaInt;
96
97 if (lhs.mFontSize < rhs.mFontSize) return -1;
98 if (lhs.mFontSize > rhs.mFontSize) return +1;
99
100 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
101 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
102
103 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
104 if (deltaInt != 0) return deltaInt;
105
106 if (lhs.mScaleX < rhs.mScaleX) return -1;
107 if (lhs.mScaleX > rhs.mScaleX) return +1;
108
109 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
110 if (deltaInt != 0) return deltaInt;
111
112 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
113 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
114
Romain Guyb969a0d2013-02-05 14:38:40 -0800115 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
116 if (deltaInt != 0) return deltaInt;
117
Romain Guy2d5945e2013-06-18 12:59:25 -0700118 deltaInt = int(lhs.mHinting) - int(rhs.mHinting);
119 if (deltaInt != 0) return deltaInt;
120
Romain Guyc74f45a2013-02-26 19:10:14 -0800121 if (lhs.mLookupTransform[SkMatrix::kMScaleX] <
122 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1;
123 if (lhs.mLookupTransform[SkMatrix::kMScaleX] >
124 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1;
125
126 if (lhs.mLookupTransform[SkMatrix::kMScaleY] <
127 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1;
128 if (lhs.mLookupTransform[SkMatrix::kMScaleY] >
129 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1;
130
Romain Guye3a9b242013-01-08 11:15:30 -0800131 return 0;
132}
133
Romain Guy80872462012-09-04 16:42:01 -0700134void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700135 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
136 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700137 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700138 cachedGlyph->mIsValid = false;
139 }
140 }
141}
142
143void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
144 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
145 int nPenX = x + glyph->mBitmapLeft;
146 int nPenY = y + glyph->mBitmapTop;
147
148 int width = (int) glyph->mBitmapWidth;
149 int height = (int) glyph->mBitmapHeight;
150
151 if (bounds->bottom > nPenY) {
152 bounds->bottom = nPenY;
153 }
154 if (bounds->left > nPenX) {
155 bounds->left = nPenX;
156 }
157 if (bounds->right < nPenX + width) {
158 bounds->right = nPenX + width;
159 }
160 if (bounds->top < nPenY + height) {
161 bounds->top = nPenY + height;
162 }
163}
164
165void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
166 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800167 float nPenX = x + glyph->mBitmapLeft;
Romain Guya4adcf02013-02-28 12:15:35 -0800168 float nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight;
Romain Guye3a9b242013-01-08 11:15:30 -0800169
170 float width = (float) glyph->mBitmapWidth;
171 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700172
173 float u1 = glyph->mBitmapMinU;
174 float u2 = glyph->mBitmapMaxU;
175 float v1 = glyph->mBitmapMinV;
176 float v2 = glyph->mBitmapMaxV;
177
Romain Guy9f5dab32012-09-04 12:55:44 -0700178 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
179 nPenX + width, nPenY, u2, v2,
180 nPenX + width, nPenY - height, u2, v1,
181 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
182}
183
Romain Guy624234f2013-03-05 16:43:31 -0800184void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Romain Guya4adcf02013-02-28 12:15:35 -0800185 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guya4adcf02013-02-28 12:15:35 -0800186 SkPoint p[4];
Romain Guy874f5c62013-03-01 18:07:35 -0800187 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + glyph->mBitmapHeight);
188 p[1].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop + glyph->mBitmapHeight);
189 p[2].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop);
190 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop);
Romain Guya4adcf02013-02-28 12:15:35 -0800191
Romain Guy874f5c62013-03-01 18:07:35 -0800192 mDescription.mInverseLookupTransform.mapPoints(p, 4);
Romain Guya4adcf02013-02-28 12:15:35 -0800193
194 p[0].offset(x, y);
195 p[1].offset(x, y);
196 p[2].offset(x, y);
197 p[3].offset(x, y);
198
199 float u1 = glyph->mBitmapMinU;
200 float u2 = glyph->mBitmapMaxU;
201 float v1 = glyph->mBitmapMinV;
202 float v2 = glyph->mBitmapMaxV;
203
204 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800205 p[0].x(), p[0].y(), u1, v2,
206 p[1].x(), p[1].y(), u2, v2,
207 p[2].x(), p[2].y(), u2, v1,
208 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture);
Romain Guya4adcf02013-02-28 12:15:35 -0800209}
210
Romain Guycf51a412013-04-08 19:40:31 -0700211void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap,
212 uint32_t bitmapWidth, uint32_t bitmapHeight, Rect* bounds, const float* pos) {
213 int dstX = x + glyph->mBitmapLeft;
214 int dstY = y + glyph->mBitmapTop;
Romain Guy9f5dab32012-09-04 12:55:44 -0700215
Romain Guy80872462012-09-04 16:42:01 -0700216 CacheTexture* cacheTexture = glyph->mCacheTexture;
Romain Guy9f5dab32012-09-04 12:55:44 -0700217
Romain Guycf51a412013-04-08 19:40:31 -0700218 uint32_t cacheWidth = cacheTexture->getWidth();
219 uint32_t startY = glyph->mStartY * cacheWidth;
220 uint32_t endY = startY + (glyph->mBitmapHeight * cacheWidth);
221
222 PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer();
223 const uint8_t* cacheBuffer = pixelBuffer->map();
224
225 for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY;
226 cacheY += cacheWidth, bitmapY += bitmapWidth) {
227 memcpy(&bitmap[bitmapY + dstX], &cacheBuffer[cacheY + glyph->mStartX], glyph->mBitmapWidth);
Romain Guy9f5dab32012-09-04 12:55:44 -0700228 }
229}
230
231void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
232 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
233 const float halfWidth = glyph->mBitmapWidth * 0.5f;
234 const float height = glyph->mBitmapHeight;
235
236 vOffset += glyph->mBitmapTop + height;
237
238 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800239 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
240 if (!ok) {
241 ALOGW("The path for drawTextOnPath is empty or null");
242 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700243
244 // Move along the tangent and offset by the normal
245 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
246 -tangent->fY * halfWidth + tangent->fX * vOffset);
247 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
248 tangent->fY * halfWidth + tangent->fX * vOffset);
249 destination[2].set(destination[1].fX + tangent->fY * height,
250 destination[1].fY - tangent->fX * height);
251 destination[3].set(destination[0].fX + tangent->fY * height,
252 destination[0].fY - tangent->fX * height);
253
254 const float u1 = glyph->mBitmapMinU;
255 const float u2 = glyph->mBitmapMaxU;
256 const float v1 = glyph->mBitmapMinV;
257 const float v2 = glyph->mBitmapMaxV;
258
259 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800260 position->x() + destination[0].x(),
261 position->y() + destination[0].y(), u1, v2,
262 position->x() + destination[1].x(),
263 position->y() + destination[1].y(), u2, v2,
264 position->x() + destination[2].x(),
265 position->y() + destination[2].y(), u2, v1,
266 position->x() + destination[3].x(),
267 position->y() + destination[3].y(), u1, v1,
Romain Guy9f5dab32012-09-04 12:55:44 -0700268 glyph->mCacheTexture);
269}
270
271CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) {
Romain Guybd3055f2013-03-13 16:14:47 -0700272 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit);
273 if (cachedGlyph) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800274 // Is the glyph still in texture cache?
275 if (!cachedGlyph->mIsValid) {
Victoria Leasedf64ac62014-04-22 15:00:31 -0700276 SkAutoGlyphCache autoCache(*paint, &mDeviceProperties, &mDescription.mLookupTransform);
Leon Scroggins IIIf8d87772013-12-03 16:26:51 -0500277 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), textUnit);
278 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), cachedGlyph, precaching);
Romain Guyc74f45a2013-02-26 19:10:14 -0800279 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700280 } else {
281 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
282 }
283
Romain Guy9f5dab32012-09-04 12:55:44 -0700284 return cachedGlyph;
285}
286
Romain Guy9f5dab32012-09-04 12:55:44 -0700287void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
288 int numGlyphs, int x, int y, const float* positions) {
289 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
290 0, 0, NULL, positions);
291}
292
293void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len,
294 int numGlyphs, SkPath* path, float hOffset, float vOffset) {
295 if (numGlyphs == 0 || text == NULL || len == 0) {
296 return;
297 }
298
299 text += start;
300
301 int glyphsCount = 0;
302 SkFixed prevRsbDelta = 0;
303
304 float penX = 0.0f;
305
306 SkPoint position;
307 SkVector tangent;
308
309 SkPathMeasure measure(*path, false);
310 float pathLength = SkScalarToFloat(measure.getLength());
311
312 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
313 float textWidth = SkScalarToFloat(paint->measureText(text, len));
314 float pathOffset = pathLength;
315 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
316 textWidth *= 0.5f;
317 pathOffset *= 0.5f;
318 }
319 penX += pathOffset - textWidth;
320 }
321
322 while (glyphsCount < numGlyphs && penX < pathLength) {
323 glyph_t glyph = GET_GLYPH(text);
324
325 if (IS_END_OF_STRING(glyph)) {
326 break;
327 }
328
329 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
330 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
331 prevRsbDelta = cachedGlyph->mRsbDelta;
332
Romain Guya4adcf02013-02-28 12:15:35 -0800333 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700334 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
335 }
336
337 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
338
339 glyphsCount++;
340 }
341}
342
343void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
344 int numGlyphs, Rect *bounds, const float* positions) {
345 if (bounds == NULL) {
346 ALOGE("No return rectangle provided to measure text");
347 return;
348 }
349 bounds->set(1e6, -1e6, -1e6, 1e6);
350 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
351}
352
353void Font::precache(SkPaint* paint, const char* text, int numGlyphs) {
Romain Guybd3055f2013-03-13 16:14:47 -0700354 ATRACE_NAME("precacheText");
355
Romain Guy9f5dab32012-09-04 12:55:44 -0700356 if (numGlyphs == 0 || text == NULL) {
357 return;
358 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700359
Romain Guybd3055f2013-03-13 16:14:47 -0700360 int glyphsCount = 0;
Romain Guy9f5dab32012-09-04 12:55:44 -0700361 while (glyphsCount < numGlyphs) {
362 glyph_t glyph = GET_GLYPH(text);
363
364 // Reached the end of the string
365 if (IS_END_OF_STRING(glyph)) {
366 break;
367 }
368
369 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true);
Romain Guy9f5dab32012-09-04 12:55:44 -0700370 glyphsCount++;
371 }
372}
373
374void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len,
375 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
376 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
377 if (numGlyphs == 0 || text == NULL || len == 0) {
378 return;
379 }
380
381 static RenderGlyph gRenderGlyph[] = {
382 &android::uirenderer::Font::drawCachedGlyph,
Romain Guy624234f2013-03-05 16:43:31 -0800383 &android::uirenderer::Font::drawCachedGlyphTransformed,
Romain Guy9f5dab32012-09-04 12:55:44 -0700384 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800385 &android::uirenderer::Font::drawCachedGlyphBitmap,
386 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700387 &android::uirenderer::Font::measureCachedGlyph
388 };
Romain Guy624234f2013-03-05 16:43:31 -0800389 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform];
Romain Guy9f5dab32012-09-04 12:55:44 -0700390
391 text += start;
392 int glyphsCount = 0;
393
Romain Guye3a9b242013-01-08 11:15:30 -0800394 const SkPaint::Align align = paint->getTextAlign();
Romain Guy9f5dab32012-09-04 12:55:44 -0700395
Romain Guye3a9b242013-01-08 11:15:30 -0800396 while (glyphsCount < numGlyphs) {
397 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700398
Romain Guye3a9b242013-01-08 11:15:30 -0800399 // Reached the end of the string
400 if (IS_END_OF_STRING(glyph)) {
401 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700402 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700403
Romain Guye3a9b242013-01-08 11:15:30 -0800404 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700405
Romain Guya4adcf02013-02-28 12:15:35 -0800406 // If it's still not valid, we couldn't cache it, so we shouldn't
407 // draw garbage; also skip empty glyphs (spaces)
408 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Alexander Toresson3ed19272013-08-28 16:13:06 +0200409 int penX = x + (int) roundf(positions[(glyphsCount << 1)]);
410 int penY = y + (int) roundf(positions[(glyphsCount << 1) + 1]);
Romain Guye3a9b242013-01-08 11:15:30 -0800411
Alexander Toresson3ed19272013-08-28 16:13:06 +0200412 (*this.*render)(cachedGlyph, penX, penY,
Romain Guye3a9b242013-01-08 11:15:30 -0800413 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700414 }
Romain Guye3a9b242013-01-08 11:15:30 -0800415
416 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700417 }
418}
419
Leon Scroggins IIIf8d87772013-12-03 16:26:51 -0500420void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, SkGlyphCache* skiaGlyphCache,
421 CachedGlyphInfo* glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700422 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
423 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
424 glyph->mBitmapLeft = skiaGlyph.fLeft;
425 glyph->mBitmapTop = skiaGlyph.fTop;
426 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
427 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
428
429 uint32_t startX = 0;
430 uint32_t startY = 0;
431
432 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800433 if (!skiaGlyph.fImage) {
Leon Scroggins IIIf8d87772013-12-03 16:26:51 -0500434 skiaGlyphCache->findImage(skiaGlyph);
Romain Guyb969a0d2013-02-05 14:38:40 -0800435 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700436 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
437
438 if (!glyph->mIsValid) {
439 return;
440 }
441
442 uint32_t endX = startX + skiaGlyph.fWidth;
443 uint32_t endY = startY + skiaGlyph.fHeight;
444
445 glyph->mStartX = startX;
446 glyph->mStartY = startY;
447 glyph->mBitmapWidth = skiaGlyph.fWidth;
448 glyph->mBitmapHeight = skiaGlyph.fHeight;
449
Romain Guya4adcf02013-02-28 12:15:35 -0800450 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
451 if (!empty) {
452 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
453 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700454
Romain Guya4adcf02013-02-28 12:15:35 -0800455 glyph->mBitmapMinU = startX / (float) cacheWidth;
456 glyph->mBitmapMinV = startY / (float) cacheHeight;
457 glyph->mBitmapMaxU = endX / (float) cacheWidth;
458 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700459
Romain Guya4adcf02013-02-28 12:15:35 -0800460 mState->setTextureDirty();
461 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700462}
463
464CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) {
465 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
466 mCachedGlyphs.add(glyph, newGlyph);
467
Victoria Leasedf64ac62014-04-22 15:00:31 -0700468 SkAutoGlyphCache autoCache(*paint, &mDeviceProperties, &mDescription.mLookupTransform);
Leon Scroggins IIIf8d87772013-12-03 16:26:51 -0500469 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700470 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800471 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700472
Leon Scroggins IIIf8d87772013-12-03 16:26:51 -0500473 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), newGlyph, precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700474
475 return newGlyph;
476}
477
Romain Guye3a9b242013-01-08 11:15:30 -0800478Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) {
479 FontDescription description(paint, matrix);
480 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700481
Romain Guya4adcf02013-02-28 12:15:35 -0800482 if (!font) {
483 font = new Font(state, description);
484 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700485 }
Romain Guy624234f2013-03-05 16:43:31 -0800486 font->mIdentityTransform = matrix.isIdentity();
Romain Guy9f5dab32012-09-04 12:55:44 -0700487
Romain Guya4adcf02013-02-28 12:15:35 -0800488 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700489}
490
491}; // namespace uirenderer
492}; // namespace android