blob: 206bd59884143d3d708419e0ef94584d0dec0a4f [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
Derek Sollenberger413995e2014-10-13 11:18:53 -040025#include <SkDeviceProperties.h>
Romain Guy14c40b42013-01-08 18:03:07 -080026#include <SkGlyph.h>
Victoria Lease43b692d2013-12-03 15:02:28 -080027#include <SkGlyphCache.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070028#include <SkUtils.h>
29
Romain Guy9f5dab32012-09-04 12:55:44 -070030#include "FontUtil.h"
31#include "Font.h"
Romain Guycf51a412013-04-08 19:40:31 -070032#include "../Debug.h"
33#include "../FontRenderer.h"
34#include "../PixelBuffer.h"
35#include "../Properties.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070036
37namespace android {
38namespace uirenderer {
39
40///////////////////////////////////////////////////////////////////////////////
41// Font
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guye3a9b242013-01-08 11:15:30 -080044Font::Font(FontRenderer* state, const Font::FontDescription& desc) :
Derek Sollenberger413995e2014-10-13 11:18:53 -040045 mState(state), mDescription(desc) { }
Romain Guy9f5dab32012-09-04 12:55:44 -070046
Chris Craik59744b72014-07-01 17:56:52 -070047Font::FontDescription::FontDescription(const SkPaint* paint, const SkMatrix& rasterMatrix)
48 : mLookupTransform(rasterMatrix) {
Romain Guye3a9b242013-01-08 11:15:30 -080049 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 Guy874f5c62013-03-01 18:07:35 -080061 if (!mLookupTransform.invert(&mInverseLookupTransform)) {
62 ALOGW("Could not query the inverse lookup transform for this font");
63 }
Romain Guye3a9b242013-01-08 11:15:30 -080064}
Romain Guy9f5dab32012-09-04 12:55:44 -070065
66Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070067 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070068
69 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
70 delete mCachedGlyphs.valueAt(i);
71 }
72}
73
Romain Guye3a9b242013-01-08 11:15:30 -080074hash_t Font::FontDescription::hash() const {
75 uint32_t hash = JenkinsHashMix(0, mFontId);
76 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
77 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
78 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
79 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
80 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
81 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080082 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guy2d5945e2013-06-18 12:59:25 -070083 hash = JenkinsHashMix(hash, android::hash_type(mHinting));
Romain Guyc74f45a2013-02-26 19:10:14 -080084 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX]));
85 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY]));
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 Guy2d5945e2013-06-18 12:59:25 -0700115 deltaInt = int(lhs.mHinting) - int(rhs.mHinting);
116 if (deltaInt != 0) return deltaInt;
117
Romain Guyc74f45a2013-02-26 19:10:14 -0800118 if (lhs.mLookupTransform[SkMatrix::kMScaleX] <
119 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1;
120 if (lhs.mLookupTransform[SkMatrix::kMScaleX] >
121 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1;
122
123 if (lhs.mLookupTransform[SkMatrix::kMScaleY] <
124 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1;
125 if (lhs.mLookupTransform[SkMatrix::kMScaleY] >
126 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1;
127
Romain Guye3a9b242013-01-08 11:15:30 -0800128 return 0;
129}
130
Romain Guy80872462012-09-04 16:42:01 -0700131void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700132 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
133 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700134 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700135 cachedGlyph->mIsValid = false;
136 }
137 }
138}
139
140void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
Andreas Gampe42ddc182014-11-21 09:49:08 -0800141 uint8_t* /* bitmap */, uint32_t /* bitmapW */, uint32_t /* bitmapH */, Rect* bounds,
142 const float* /* pos */) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700143 int width = (int) glyph->mBitmapWidth;
144 int height = (int) glyph->mBitmapHeight;
145
Chris Craik39c5e7c2014-08-15 15:46:37 -0700146 int nPenX = x + glyph->mBitmapLeft;
147 int nPenY = y + glyph->mBitmapTop;
148
Romain Guy9f5dab32012-09-04 12:55:44 -0700149 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,
Andreas Gampe42ddc182014-11-21 09:49:08 -0800164 uint8_t* /* bitmap */, uint32_t /* bitmapW */, uint32_t /* bitmapH */, Rect* /* bounds */,
165 const float* /* pos */) {
Romain Guye3a9b242013-01-08 11:15:30 -0800166 float width = (float) glyph->mBitmapWidth;
167 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700168
Chris Craik39c5e7c2014-08-15 15:46:37 -0700169 float nPenX = x + glyph->mBitmapLeft;
170 float nPenY = y + glyph->mBitmapTop + height;
171
Romain Guy9f5dab32012-09-04 12:55:44 -0700172 float u1 = glyph->mBitmapMinU;
173 float u2 = glyph->mBitmapMaxU;
174 float v1 = glyph->mBitmapMinV;
175 float v2 = glyph->mBitmapMaxV;
176
Romain Guy9f5dab32012-09-04 12:55:44 -0700177 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
178 nPenX + width, nPenY, u2, v2,
179 nPenX + width, nPenY - height, u2, v1,
180 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
181}
182
Romain Guy624234f2013-03-05 16:43:31 -0800183void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Andreas Gampe42ddc182014-11-21 09:49:08 -0800184 uint8_t* /* bitmap */, uint32_t /* bitmapW */, uint32_t /* bitmapH */, Rect* /* bounds */,
185 const float* /* pos */) {
Chris Craik39c5e7c2014-08-15 15:46:37 -0700186 float width = (float) glyph->mBitmapWidth;
187 float height = (float) glyph->mBitmapHeight;
188
Romain Guya4adcf02013-02-28 12:15:35 -0800189 SkPoint p[4];
Chris Craik39c5e7c2014-08-15 15:46:37 -0700190 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + height);
191 p[1].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop + height);
192 p[2].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop);
Romain Guy874f5c62013-03-01 18:07:35 -0800193 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop);
Romain Guya4adcf02013-02-28 12:15:35 -0800194
Romain Guy874f5c62013-03-01 18:07:35 -0800195 mDescription.mInverseLookupTransform.mapPoints(p, 4);
Romain Guya4adcf02013-02-28 12:15:35 -0800196
197 p[0].offset(x, y);
198 p[1].offset(x, y);
199 p[2].offset(x, y);
200 p[3].offset(x, y);
201
202 float u1 = glyph->mBitmapMinU;
203 float u2 = glyph->mBitmapMaxU;
204 float v1 = glyph->mBitmapMinV;
205 float v2 = glyph->mBitmapMaxV;
206
207 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800208 p[0].x(), p[0].y(), u1, v2,
209 p[1].x(), p[1].y(), u2, v2,
210 p[2].x(), p[2].y(), u2, v1,
211 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture);
Romain Guya4adcf02013-02-28 12:15:35 -0800212}
213
Romain Guycf51a412013-04-08 19:40:31 -0700214void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap,
Andreas Gampe42ddc182014-11-21 09:49:08 -0800215 uint32_t bitmapWidth, uint32_t /* bitmapHeight */, Rect* /* bounds */,
216 const float* /* pos */) {
Romain Guycf51a412013-04-08 19:40:31 -0700217 int dstX = x + glyph->mBitmapLeft;
218 int dstY = y + glyph->mBitmapTop;
Romain Guy9f5dab32012-09-04 12:55:44 -0700219
Romain Guy80872462012-09-04 16:42:01 -0700220 CacheTexture* cacheTexture = glyph->mCacheTexture;
Romain Guycf51a412013-04-08 19:40:31 -0700221 PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer();
Digish Pandyab9312a52014-05-09 15:05:16 +0530222
223 uint32_t formatSize = PixelBuffer::formatSize(pixelBuffer->getFormat());
Digish Pandyac62c1cc2014-05-12 14:37:04 +0530224 uint32_t alpha_channel_offset = PixelBuffer::formatAlphaOffset(pixelBuffer->getFormat());
Digish Pandyab9312a52014-05-09 15:05:16 +0530225 uint32_t cacheWidth = cacheTexture->getWidth();
226 uint32_t srcStride = formatSize * cacheWidth;
227 uint32_t startY = glyph->mStartY * srcStride;
228 uint32_t endY = startY + (glyph->mBitmapHeight * srcStride);
229
Romain Guycf51a412013-04-08 19:40:31 -0700230 const uint8_t* cacheBuffer = pixelBuffer->map();
231
232 for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY;
Digish Pandyab9312a52014-05-09 15:05:16 +0530233 cacheY += srcStride, bitmapY += bitmapWidth) {
234
235 if (formatSize == 1) {
236 memcpy(&bitmap[bitmapY + dstX], &cacheBuffer[cacheY + glyph->mStartX], glyph->mBitmapWidth);
237 } else {
238 for (uint32_t i = 0; i < glyph->mBitmapWidth; ++i) {
Digish Pandyac62c1cc2014-05-12 14:37:04 +0530239 bitmap[bitmapY + dstX + i] = cacheBuffer[cacheY + (glyph->mStartX + i)*formatSize + alpha_channel_offset];
Digish Pandyab9312a52014-05-09 15:05:16 +0530240 }
241 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700242 }
Digish Pandyab9312a52014-05-09 15:05:16 +0530243
Romain Guy9f5dab32012-09-04 12:55:44 -0700244}
245
246void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
247 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
248 const float halfWidth = glyph->mBitmapWidth * 0.5f;
249 const float height = glyph->mBitmapHeight;
250
251 vOffset += glyph->mBitmapTop + height;
252
253 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800254 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
255 if (!ok) {
256 ALOGW("The path for drawTextOnPath is empty or null");
257 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700258
259 // Move along the tangent and offset by the normal
260 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
261 -tangent->fY * halfWidth + tangent->fX * vOffset);
262 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
263 tangent->fY * halfWidth + tangent->fX * vOffset);
264 destination[2].set(destination[1].fX + tangent->fY * height,
265 destination[1].fY - tangent->fX * height);
266 destination[3].set(destination[0].fX + tangent->fY * height,
267 destination[0].fY - tangent->fX * height);
268
269 const float u1 = glyph->mBitmapMinU;
270 const float u2 = glyph->mBitmapMaxU;
271 const float v1 = glyph->mBitmapMinV;
272 const float v2 = glyph->mBitmapMaxV;
273
274 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800275 position->x() + destination[0].x(),
276 position->y() + destination[0].y(), u1, v2,
277 position->x() + destination[1].x(),
278 position->y() + destination[1].y(), u2, v2,
279 position->x() + destination[2].x(),
280 position->y() + destination[2].y(), u2, v1,
281 position->x() + destination[3].x(),
282 position->y() + destination[3].y(), u1, v1,
Romain Guy9f5dab32012-09-04 12:55:44 -0700283 glyph->mCacheTexture);
284}
285
Chris Craikd218a922014-01-02 17:13:34 -0800286CachedGlyphInfo* Font::getCachedGlyph(const SkPaint* paint, glyph_t textUnit, bool precaching) {
Romain Guybd3055f2013-03-13 16:14:47 -0700287 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit);
288 if (cachedGlyph) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800289 // Is the glyph still in texture cache?
290 if (!cachedGlyph->mIsValid) {
Derek Sollenberger413995e2014-10-13 11:18:53 -0400291 SkDeviceProperties deviceProperties(kUnknown_SkPixelGeometry, 1.0f);
292 SkAutoGlyphCache autoCache(*paint, &deviceProperties, &mDescription.mLookupTransform);
Victoria Lease43b692d2013-12-03 15:02:28 -0800293 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), textUnit);
Victoria Lease2ee2d592013-12-17 13:54:29 -0800294 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), cachedGlyph, precaching);
Romain Guyc74f45a2013-02-26 19:10:14 -0800295 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700296 } else {
297 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
298 }
299
Romain Guy9f5dab32012-09-04 12:55:44 -0700300 return cachedGlyph;
301}
302
Chris Craikd218a922014-01-02 17:13:34 -0800303void Font::render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700304 int numGlyphs, int x, int y, const float* positions) {
305 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL,
306 0, 0, NULL, positions);
307}
308
Chris Craikd218a922014-01-02 17:13:34 -0800309void Font::render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
310 int numGlyphs, const SkPath* path, float hOffset, float vOffset) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700311 if (numGlyphs == 0 || text == NULL || len == 0) {
312 return;
313 }
314
315 text += start;
316
317 int glyphsCount = 0;
318 SkFixed prevRsbDelta = 0;
319
320 float penX = 0.0f;
321
322 SkPoint position;
323 SkVector tangent;
324
325 SkPathMeasure measure(*path, false);
326 float pathLength = SkScalarToFloat(measure.getLength());
327
328 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
329 float textWidth = SkScalarToFloat(paint->measureText(text, len));
330 float pathOffset = pathLength;
331 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
332 textWidth *= 0.5f;
333 pathOffset *= 0.5f;
334 }
335 penX += pathOffset - textWidth;
336 }
337
338 while (glyphsCount < numGlyphs && penX < pathLength) {
339 glyph_t glyph = GET_GLYPH(text);
340
341 if (IS_END_OF_STRING(glyph)) {
342 break;
343 }
344
345 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
346 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
347 prevRsbDelta = cachedGlyph->mRsbDelta;
348
Romain Guya4adcf02013-02-28 12:15:35 -0800349 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700350 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
351 }
352
353 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
354
355 glyphsCount++;
356 }
357}
358
Chris Craikd218a922014-01-02 17:13:34 -0800359void Font::measure(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700360 int numGlyphs, Rect *bounds, const float* positions) {
361 if (bounds == NULL) {
362 ALOGE("No return rectangle provided to measure text");
363 return;
364 }
365 bounds->set(1e6, -1e6, -1e6, 1e6);
366 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions);
367}
368
Chris Craikd218a922014-01-02 17:13:34 -0800369void Font::precache(const SkPaint* paint, const char* text, int numGlyphs) {
Chris Craik70850ea2014-11-18 10:49:23 -0800370 ATRACE_NAME("Precache Glyphs");
Romain Guybd3055f2013-03-13 16:14:47 -0700371
Romain Guy9f5dab32012-09-04 12:55:44 -0700372 if (numGlyphs == 0 || text == NULL) {
373 return;
374 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700375
Romain Guybd3055f2013-03-13 16:14:47 -0700376 int glyphsCount = 0;
Romain Guy9f5dab32012-09-04 12:55:44 -0700377 while (glyphsCount < numGlyphs) {
378 glyph_t glyph = GET_GLYPH(text);
379
380 // Reached the end of the string
381 if (IS_END_OF_STRING(glyph)) {
382 break;
383 }
384
Andreas Gampeedaecc12014-11-10 20:54:07 -0800385 getCachedGlyph(paint, glyph, true);
Romain Guy9f5dab32012-09-04 12:55:44 -0700386 glyphsCount++;
387 }
388}
389
Chris Craikd218a922014-01-02 17:13:34 -0800390void Font::render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700391 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
392 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
393 if (numGlyphs == 0 || text == NULL || len == 0) {
394 return;
395 }
396
397 static RenderGlyph gRenderGlyph[] = {
398 &android::uirenderer::Font::drawCachedGlyph,
Romain Guy624234f2013-03-05 16:43:31 -0800399 &android::uirenderer::Font::drawCachedGlyphTransformed,
Romain Guy9f5dab32012-09-04 12:55:44 -0700400 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800401 &android::uirenderer::Font::drawCachedGlyphBitmap,
402 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700403 &android::uirenderer::Font::measureCachedGlyph
404 };
Romain Guy624234f2013-03-05 16:43:31 -0800405 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform];
Romain Guy9f5dab32012-09-04 12:55:44 -0700406
407 text += start;
408 int glyphsCount = 0;
409
Romain Guye3a9b242013-01-08 11:15:30 -0800410 while (glyphsCount < numGlyphs) {
411 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700412
Romain Guye3a9b242013-01-08 11:15:30 -0800413 // Reached the end of the string
414 if (IS_END_OF_STRING(glyph)) {
415 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700416 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700417
Romain Guye3a9b242013-01-08 11:15:30 -0800418 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700419
Romain Guya4adcf02013-02-28 12:15:35 -0800420 // If it's still not valid, we couldn't cache it, so we shouldn't
421 // draw garbage; also skip empty glyphs (spaces)
422 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Alexander Toresson3ed19272013-08-28 16:13:06 +0200423 int penX = x + (int) roundf(positions[(glyphsCount << 1)]);
424 int penY = y + (int) roundf(positions[(glyphsCount << 1) + 1]);
Romain Guye3a9b242013-01-08 11:15:30 -0800425
Alexander Toresson3ed19272013-08-28 16:13:06 +0200426 (*this.*render)(cachedGlyph, penX, penY,
Romain Guye3a9b242013-01-08 11:15:30 -0800427 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700428 }
Romain Guye3a9b242013-01-08 11:15:30 -0800429
430 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700431 }
432}
433
Andreas Gampe42ddc182014-11-21 09:49:08 -0800434void Font::updateGlyphCache(const SkPaint* /* paint */, const SkGlyph& skiaGlyph,
Chris Craikd218a922014-01-02 17:13:34 -0800435 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700436 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
437 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
438 glyph->mBitmapLeft = skiaGlyph.fLeft;
439 glyph->mBitmapTop = skiaGlyph.fTop;
440 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
441 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
442
443 uint32_t startX = 0;
444 uint32_t startY = 0;
445
446 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800447 if (!skiaGlyph.fImage) {
Victoria Lease2ee2d592013-12-17 13:54:29 -0800448 skiaGlyphCache->findImage(skiaGlyph);
Romain Guyb969a0d2013-02-05 14:38:40 -0800449 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700450 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
451
452 if (!glyph->mIsValid) {
453 return;
454 }
455
456 uint32_t endX = startX + skiaGlyph.fWidth;
457 uint32_t endY = startY + skiaGlyph.fHeight;
458
459 glyph->mStartX = startX;
460 glyph->mStartY = startY;
461 glyph->mBitmapWidth = skiaGlyph.fWidth;
462 glyph->mBitmapHeight = skiaGlyph.fHeight;
463
Romain Guya4adcf02013-02-28 12:15:35 -0800464 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
465 if (!empty) {
466 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
467 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700468
Romain Guya4adcf02013-02-28 12:15:35 -0800469 glyph->mBitmapMinU = startX / (float) cacheWidth;
470 glyph->mBitmapMinV = startY / (float) cacheHeight;
471 glyph->mBitmapMaxU = endX / (float) cacheWidth;
472 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700473
Romain Guya4adcf02013-02-28 12:15:35 -0800474 mState->setTextureDirty();
475 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700476}
477
Chris Craikd218a922014-01-02 17:13:34 -0800478CachedGlyphInfo* Font::cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700479 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
480 mCachedGlyphs.add(glyph, newGlyph);
481
Derek Sollenberger413995e2014-10-13 11:18:53 -0400482 SkDeviceProperties deviceProperties(kUnknown_SkPixelGeometry, 1.0f);
483 SkAutoGlyphCache autoCache(*paint, &deviceProperties, &mDescription.mLookupTransform);
Victoria Lease43b692d2013-12-03 15:02:28 -0800484 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700485 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800486 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700487
Victoria Lease2ee2d592013-12-17 13:54:29 -0800488 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), newGlyph, precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700489
490 return newGlyph;
491}
492
Chris Craik59744b72014-07-01 17:56:52 -0700493Font* Font::create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix) {
Romain Guye3a9b242013-01-08 11:15:30 -0800494 FontDescription description(paint, matrix);
495 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700496
Romain Guya4adcf02013-02-28 12:15:35 -0800497 if (!font) {
498 font = new Font(state, description);
499 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700500 }
Romain Guy624234f2013-03-05 16:43:31 -0800501 font->mIdentityTransform = matrix.isIdentity();
Romain Guy9f5dab32012-09-04 12:55:44 -0700502
Romain Guya4adcf02013-02-28 12:15:35 -0800503 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700504}
505
506}; // namespace uirenderer
507}; // namespace android