blob: fb0753bbc76ca4f9b1c8064381223513c2182345 [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
17#include <cutils/compiler.h>
18
Romain Guye3a9b242013-01-08 11:15:30 -080019#include <utils/JenkinsHash.h>
Romain Guybd3055f2013-03-13 16:14:47 -070020#include <utils/Trace.h>
Romain Guye3a9b242013-01-08 11:15:30 -080021
Leon Scroggins III0e9059b2015-06-19 17:42:41 -040022#include <SkSurfaceProps.h>
Romain Guy14c40b42013-01-08 18:03:07 -080023#include <SkGlyph.h>
Victoria Lease43b692d2013-12-03 15:02:28 -080024#include <SkGlyphCache.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070025#include <SkUtils.h>
26
Romain Guy9f5dab32012-09-04 12:55:44 -070027#include "FontUtil.h"
28#include "Font.h"
Romain Guycf51a412013-04-08 19:40:31 -070029#include "../Debug.h"
30#include "../FontRenderer.h"
31#include "../PixelBuffer.h"
32#include "../Properties.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070033
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) :
Derek Sollenberger413995e2014-10-13 11:18:53 -040042 mState(state), mDescription(desc) { }
Romain Guy9f5dab32012-09-04 12:55:44 -070043
Chris Craik59744b72014-07-01 17:56:52 -070044Font::FontDescription::FontDescription(const SkPaint* paint, const SkMatrix& rasterMatrix)
45 : mLookupTransform(rasterMatrix) {
Romain Guye3a9b242013-01-08 11:15:30 -080046 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 Guy2d5945e2013-06-18 12:59:25 -070057 mHinting = paint->getHinting();
Romain Guy874f5c62013-03-01 18:07:35 -080058 if (!mLookupTransform.invert(&mInverseLookupTransform)) {
59 ALOGW("Could not query the inverse lookup transform for this font");
60 }
Romain Guye3a9b242013-01-08 11:15:30 -080061}
Romain Guy9f5dab32012-09-04 12:55:44 -070062
63Font::~Font() {
Romain Guy9b1204b2012-09-04 15:22:57 -070064 mState->removeFont(this);
Romain Guy9f5dab32012-09-04 12:55:44 -070065
66 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
67 delete mCachedGlyphs.valueAt(i);
68 }
69}
70
Romain Guye3a9b242013-01-08 11:15:30 -080071hash_t Font::FontDescription::hash() const {
72 uint32_t hash = JenkinsHashMix(0, mFontId);
73 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
74 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
75 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
76 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
77 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
78 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080079 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guy2d5945e2013-06-18 12:59:25 -070080 hash = JenkinsHashMix(hash, android::hash_type(mHinting));
Romain Guyc74f45a2013-02-26 19:10:14 -080081 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX]));
82 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY]));
Romain Guye3a9b242013-01-08 11:15:30 -080083 return JenkinsHashWhiten(hash);
84}
85
86int Font::FontDescription::compare(const Font::FontDescription& lhs,
87 const Font::FontDescription& rhs) {
88 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
89 if (deltaInt != 0) return deltaInt;
90
91 if (lhs.mFontSize < rhs.mFontSize) return -1;
92 if (lhs.mFontSize > rhs.mFontSize) return +1;
93
94 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
95 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
96
97 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
98 if (deltaInt != 0) return deltaInt;
99
100 if (lhs.mScaleX < rhs.mScaleX) return -1;
101 if (lhs.mScaleX > rhs.mScaleX) return +1;
102
103 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
104 if (deltaInt != 0) return deltaInt;
105
106 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
107 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
108
Romain Guyb969a0d2013-02-05 14:38:40 -0800109 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
110 if (deltaInt != 0) return deltaInt;
111
Romain Guy2d5945e2013-06-18 12:59:25 -0700112 deltaInt = int(lhs.mHinting) - int(rhs.mHinting);
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
Romain Guye3a9b242013-01-08 11:15:30 -0800125 return 0;
126}
127
Romain Guy80872462012-09-04 16:42:01 -0700128void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700129 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
130 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700131 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700132 cachedGlyph->mIsValid = false;
133 }
134 }
135}
136
137void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000138 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700139 int width = (int) glyph->mBitmapWidth;
140 int height = (int) glyph->mBitmapHeight;
141
Chris Craik39c5e7c2014-08-15 15:46:37 -0700142 int nPenX = x + glyph->mBitmapLeft;
143 int nPenY = y + glyph->mBitmapTop;
144
Romain Guy9f5dab32012-09-04 12:55:44 -0700145 if (bounds->bottom > nPenY) {
146 bounds->bottom = nPenY;
147 }
148 if (bounds->left > nPenX) {
149 bounds->left = nPenX;
150 }
151 if (bounds->right < nPenX + width) {
152 bounds->right = nPenX + width;
153 }
154 if (bounds->top < nPenY + height) {
155 bounds->top = nPenY + height;
156 }
157}
158
159void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000160 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800161 float width = (float) glyph->mBitmapWidth;
162 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700163
Chris Craik39c5e7c2014-08-15 15:46:37 -0700164 float nPenX = x + glyph->mBitmapLeft;
165 float nPenY = y + glyph->mBitmapTop + height;
166
Romain Guy9f5dab32012-09-04 12:55:44 -0700167 float u1 = glyph->mBitmapMinU;
168 float u2 = glyph->mBitmapMaxU;
169 float v1 = glyph->mBitmapMinV;
170 float v2 = glyph->mBitmapMaxV;
171
Romain Guy9f5dab32012-09-04 12:55:44 -0700172 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
173 nPenX + width, nPenY, u2, v2,
174 nPenX + width, nPenY - height, u2, v1,
175 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
176}
177
Romain Guy624234f2013-03-05 16:43:31 -0800178void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000179 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Chris Craik39c5e7c2014-08-15 15:46:37 -0700180 float width = (float) glyph->mBitmapWidth;
181 float height = (float) glyph->mBitmapHeight;
182
Romain Guya4adcf02013-02-28 12:15:35 -0800183 SkPoint p[4];
Chris Craik39c5e7c2014-08-15 15:46:37 -0700184 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + height);
185 p[1].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop + height);
186 p[2].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop);
Romain Guy874f5c62013-03-01 18:07:35 -0800187 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop);
Romain Guya4adcf02013-02-28 12:15:35 -0800188
Romain Guy874f5c62013-03-01 18:07:35 -0800189 mDescription.mInverseLookupTransform.mapPoints(p, 4);
Romain Guya4adcf02013-02-28 12:15:35 -0800190
191 p[0].offset(x, y);
192 p[1].offset(x, y);
193 p[2].offset(x, y);
194 p[3].offset(x, y);
195
196 float u1 = glyph->mBitmapMinU;
197 float u2 = glyph->mBitmapMaxU;
198 float v1 = glyph->mBitmapMinV;
199 float v2 = glyph->mBitmapMaxV;
200
201 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800202 p[0].x(), p[0].y(), u1, v2,
203 p[1].x(), p[1].y(), u2, v2,
204 p[2].x(), p[2].y(), u2, v1,
205 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture);
Romain Guya4adcf02013-02-28 12:15:35 -0800206}
207
Romain Guycf51a412013-04-08 19:40:31 -0700208void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000209 uint32_t bitmapWidth, uint32_t bitmapHeight, Rect* bounds, const float* pos) {
Romain Guycf51a412013-04-08 19:40:31 -0700210 int dstX = x + glyph->mBitmapLeft;
211 int dstY = y + glyph->mBitmapTop;
Romain Guy9f5dab32012-09-04 12:55:44 -0700212
Romain Guy80872462012-09-04 16:42:01 -0700213 CacheTexture* cacheTexture = glyph->mCacheTexture;
Romain Guycf51a412013-04-08 19:40:31 -0700214 PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer();
Digish Pandyab9312a52014-05-09 15:05:16 +0530215
216 uint32_t formatSize = PixelBuffer::formatSize(pixelBuffer->getFormat());
Digish Pandyac62c1cc2014-05-12 14:37:04 +0530217 uint32_t alpha_channel_offset = PixelBuffer::formatAlphaOffset(pixelBuffer->getFormat());
Digish Pandyab9312a52014-05-09 15:05:16 +0530218 uint32_t cacheWidth = cacheTexture->getWidth();
219 uint32_t srcStride = formatSize * cacheWidth;
220 uint32_t startY = glyph->mStartY * srcStride;
221 uint32_t endY = startY + (glyph->mBitmapHeight * srcStride);
222
Romain Guycf51a412013-04-08 19:40:31 -0700223 const uint8_t* cacheBuffer = pixelBuffer->map();
224
225 for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY;
Digish Pandyab9312a52014-05-09 15:05:16 +0530226 cacheY += srcStride, bitmapY += bitmapWidth) {
227
Chris Craikde25a672015-06-30 13:12:24 -0700228 for (uint32_t i = 0; i < glyph->mBitmapWidth; ++i) {
229 uint8_t* dst = &(bitmap[bitmapY + dstX + i]);
230 const uint8_t& src = cacheBuffer[
231 cacheY + (glyph->mStartX + i)*formatSize + alpha_channel_offset];
232 // Add alpha values to a max of 255, full opacity. This is done to handle
233 // fonts/strings where glyphs overlap.
234 *dst = std::min(*dst + src, 255);
Digish Pandyab9312a52014-05-09 15:05:16 +0530235 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700236 }
237}
238
239void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
240 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
241 const float halfWidth = glyph->mBitmapWidth * 0.5f;
242 const float height = glyph->mBitmapHeight;
243
244 vOffset += glyph->mBitmapTop + height;
245
246 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800247 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
248 if (!ok) {
249 ALOGW("The path for drawTextOnPath is empty or null");
250 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700251
252 // Move along the tangent and offset by the normal
253 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
254 -tangent->fY * halfWidth + tangent->fX * vOffset);
255 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
256 tangent->fY * halfWidth + tangent->fX * vOffset);
257 destination[2].set(destination[1].fX + tangent->fY * height,
258 destination[1].fY - tangent->fX * height);
259 destination[3].set(destination[0].fX + tangent->fY * height,
260 destination[0].fY - tangent->fX * height);
261
262 const float u1 = glyph->mBitmapMinU;
263 const float u2 = glyph->mBitmapMaxU;
264 const float v1 = glyph->mBitmapMinV;
265 const float v2 = glyph->mBitmapMaxV;
266
267 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800268 position->x() + destination[0].x(),
269 position->y() + destination[0].y(), u1, v2,
270 position->x() + destination[1].x(),
271 position->y() + destination[1].y(), u2, v2,
272 position->x() + destination[2].x(),
273 position->y() + destination[2].y(), u2, v1,
274 position->x() + destination[3].x(),
275 position->y() + destination[3].y(), u1, v1,
Romain Guy9f5dab32012-09-04 12:55:44 -0700276 glyph->mCacheTexture);
277}
278
Chris Craikd218a922014-01-02 17:13:34 -0800279CachedGlyphInfo* Font::getCachedGlyph(const SkPaint* paint, glyph_t textUnit, bool precaching) {
Romain Guybd3055f2013-03-13 16:14:47 -0700280 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit);
281 if (cachedGlyph) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800282 // Is the glyph still in texture cache?
283 if (!cachedGlyph->mIsValid) {
Leon Scroggins III0e9059b2015-06-19 17:42:41 -0400284 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
285 SkAutoGlyphCacheNoGamma autoCache(*paint, &surfaceProps, &mDescription.mLookupTransform);
Victoria Lease43b692d2013-12-03 15:02:28 -0800286 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), textUnit);
Victoria Lease2ee2d592013-12-17 13:54:29 -0800287 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), cachedGlyph, precaching);
Romain Guyc74f45a2013-02-26 19:10:14 -0800288 }
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
Chris Craikd218a922014-01-02 17:13:34 -0800296void Font::render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700297 int numGlyphs, int x, int y, const float* positions) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800298 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, nullptr,
299 0, 0, nullptr, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700300}
301
Chris Craikd218a922014-01-02 17:13:34 -0800302void Font::render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
303 int numGlyphs, const SkPath* path, float hOffset, float vOffset) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800304 if (numGlyphs == 0 || text == nullptr || len == 0) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700305 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
Chris Craikd218a922014-01-02 17:13:34 -0800352void Font::measure(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700353 int numGlyphs, Rect *bounds, const float* positions) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800354 if (bounds == nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700355 ALOGE("No return rectangle provided to measure text");
356 return;
357 }
358 bounds->set(1e6, -1e6, -1e6, 1e6);
Chris Craikd41c4d82015-01-05 15:51:13 -0800359 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, nullptr, 0, 0, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700360}
361
Chris Craikd218a922014-01-02 17:13:34 -0800362void Font::precache(const SkPaint* paint, const char* text, int numGlyphs) {
Chris Craik70850ea2014-11-18 10:49:23 -0800363 ATRACE_NAME("Precache Glyphs");
Romain Guybd3055f2013-03-13 16:14:47 -0700364
Chris Craikd41c4d82015-01-05 15:51:13 -0800365 if (numGlyphs == 0 || text == nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700366 return;
367 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700368
Romain Guybd3055f2013-03-13 16:14:47 -0700369 int glyphsCount = 0;
Romain Guy9f5dab32012-09-04 12:55:44 -0700370 while (glyphsCount < numGlyphs) {
371 glyph_t glyph = GET_GLYPH(text);
372
373 // Reached the end of the string
374 if (IS_END_OF_STRING(glyph)) {
375 break;
376 }
377
Andreas Gampeedaecc12014-11-10 20:54:07 -0800378 getCachedGlyph(paint, glyph, true);
Romain Guy9f5dab32012-09-04 12:55:44 -0700379 glyphsCount++;
380 }
381}
382
Chris Craikd218a922014-01-02 17:13:34 -0800383void Font::render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700384 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
385 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800386 if (numGlyphs == 0 || text == nullptr || len == 0) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700387 return;
388 }
389
390 static RenderGlyph gRenderGlyph[] = {
391 &android::uirenderer::Font::drawCachedGlyph,
Romain Guy624234f2013-03-05 16:43:31 -0800392 &android::uirenderer::Font::drawCachedGlyphTransformed,
Romain Guy9f5dab32012-09-04 12:55:44 -0700393 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800394 &android::uirenderer::Font::drawCachedGlyphBitmap,
395 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700396 &android::uirenderer::Font::measureCachedGlyph
397 };
Romain Guy624234f2013-03-05 16:43:31 -0800398 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform];
Romain Guy9f5dab32012-09-04 12:55:44 -0700399
400 text += start;
401 int glyphsCount = 0;
402
Romain Guye3a9b242013-01-08 11:15:30 -0800403 while (glyphsCount < numGlyphs) {
404 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700405
Romain Guye3a9b242013-01-08 11:15:30 -0800406 // Reached the end of the string
407 if (IS_END_OF_STRING(glyph)) {
408 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700409 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700410
Romain Guye3a9b242013-01-08 11:15:30 -0800411 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700412
Romain Guya4adcf02013-02-28 12:15:35 -0800413 // If it's still not valid, we couldn't cache it, so we shouldn't
414 // draw garbage; also skip empty glyphs (spaces)
415 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Alexander Toresson3ed19272013-08-28 16:13:06 +0200416 int penX = x + (int) roundf(positions[(glyphsCount << 1)]);
417 int penY = y + (int) roundf(positions[(glyphsCount << 1) + 1]);
Romain Guye3a9b242013-01-08 11:15:30 -0800418
Alexander Toresson3ed19272013-08-28 16:13:06 +0200419 (*this.*render)(cachedGlyph, penX, penY,
Romain Guye3a9b242013-01-08 11:15:30 -0800420 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700421 }
Romain Guye3a9b242013-01-08 11:15:30 -0800422
423 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700424 }
425}
426
Andreas Gampe64bb4132014-11-22 00:35:09 +0000427void Font::updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph,
Chris Craikd218a922014-01-02 17:13:34 -0800428 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700429 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
430 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
431 glyph->mBitmapLeft = skiaGlyph.fLeft;
432 glyph->mBitmapTop = skiaGlyph.fTop;
433 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
434 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
435
436 uint32_t startX = 0;
437 uint32_t startY = 0;
438
439 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800440 if (!skiaGlyph.fImage) {
Victoria Lease2ee2d592013-12-17 13:54:29 -0800441 skiaGlyphCache->findImage(skiaGlyph);
Romain Guyb969a0d2013-02-05 14:38:40 -0800442 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700443 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
444
445 if (!glyph->mIsValid) {
446 return;
447 }
448
449 uint32_t endX = startX + skiaGlyph.fWidth;
450 uint32_t endY = startY + skiaGlyph.fHeight;
451
452 glyph->mStartX = startX;
453 glyph->mStartY = startY;
454 glyph->mBitmapWidth = skiaGlyph.fWidth;
455 glyph->mBitmapHeight = skiaGlyph.fHeight;
456
Romain Guya4adcf02013-02-28 12:15:35 -0800457 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
458 if (!empty) {
459 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
460 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700461
Romain Guya4adcf02013-02-28 12:15:35 -0800462 glyph->mBitmapMinU = startX / (float) cacheWidth;
463 glyph->mBitmapMinV = startY / (float) cacheHeight;
464 glyph->mBitmapMaxU = endX / (float) cacheWidth;
465 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700466
Romain Guya4adcf02013-02-28 12:15:35 -0800467 mState->setTextureDirty();
468 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700469}
470
Chris Craikd218a922014-01-02 17:13:34 -0800471CachedGlyphInfo* Font::cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700472 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
473 mCachedGlyphs.add(glyph, newGlyph);
474
Leon Scroggins III0e9059b2015-06-19 17:42:41 -0400475 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
476 SkAutoGlyphCacheNoGamma autoCache(*paint, &surfaceProps, &mDescription.mLookupTransform);
Victoria Lease43b692d2013-12-03 15:02:28 -0800477 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700478 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800479 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700480
Victoria Lease2ee2d592013-12-17 13:54:29 -0800481 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), newGlyph, precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700482
483 return newGlyph;
484}
485
Chris Craik59744b72014-07-01 17:56:52 -0700486Font* Font::create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix) {
Romain Guye3a9b242013-01-08 11:15:30 -0800487 FontDescription description(paint, matrix);
488 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700489
Romain Guya4adcf02013-02-28 12:15:35 -0800490 if (!font) {
491 font = new Font(state, description);
492 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700493 }
Romain Guy624234f2013-03-05 16:43:31 -0800494 font->mIdentityTransform = matrix.isIdentity();
Romain Guy9f5dab32012-09-04 12:55:44 -0700495
Romain Guya4adcf02013-02-28 12:15:35 -0800496 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700497}
498
499}; // namespace uirenderer
500}; // namespace android