blob: 884ccd9afacaeef81fefd1c49896ccc75595569b [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
Romain Guy14c40b42013-01-08 18:03:07 -080022#include <SkGlyph.h>
Victoria Lease43b692d2013-12-03 15:02:28 -080023#include <SkGlyphCache.h>
John Reck1bcacfd2017-11-03 10:12:19 -070024#include <SkSurfaceProps.h>
Romain Guy9f5dab32012-09-04 12:55:44 -070025#include <SkUtils.h>
26
Romain Guycf51a412013-04-08 19:40:31 -070027#include "../Debug.h"
28#include "../FontRenderer.h"
29#include "../PixelBuffer.h"
30#include "../Properties.h"
John Reck1bcacfd2017-11-03 10:12:19 -070031#include "Font.h"
32#include "FontUtil.h"
Romain Guy9f5dab32012-09-04 12:55:44 -070033
34namespace android {
35namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
38// Font
39///////////////////////////////////////////////////////////////////////////////
40
John Reck1bcacfd2017-11-03 10:12:19 -070041Font::Font(FontRenderer* state, const Font::FontDescription& desc)
42 : 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 Guy9f5dab32012-09-04 12:55:44 -070064 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
65 delete mCachedGlyphs.valueAt(i);
66 }
67}
68
Romain Guye3a9b242013-01-08 11:15:30 -080069hash_t Font::FontDescription::hash() const {
70 uint32_t hash = JenkinsHashMix(0, mFontId);
71 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
72 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
73 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
74 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
75 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
76 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080077 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guy2d5945e2013-06-18 12:59:25 -070078 hash = JenkinsHashMix(hash, android::hash_type(mHinting));
Romain Guyc74f45a2013-02-26 19:10:14 -080079 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX]));
80 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY]));
Romain Guye3a9b242013-01-08 11:15:30 -080081 return JenkinsHashWhiten(hash);
82}
83
84int Font::FontDescription::compare(const Font::FontDescription& lhs,
John Reck1bcacfd2017-11-03 10:12:19 -070085 const Font::FontDescription& rhs) {
Romain Guye3a9b242013-01-08 11:15:30 -080086 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
87 if (deltaInt != 0) return deltaInt;
88
89 if (lhs.mFontSize < rhs.mFontSize) return -1;
90 if (lhs.mFontSize > rhs.mFontSize) return +1;
91
92 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
93 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
94
95 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
96 if (deltaInt != 0) return deltaInt;
97
98 if (lhs.mScaleX < rhs.mScaleX) return -1;
99 if (lhs.mScaleX > rhs.mScaleX) return +1;
100
101 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
102 if (deltaInt != 0) return deltaInt;
103
104 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
105 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
106
Romain Guyb969a0d2013-02-05 14:38:40 -0800107 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
108 if (deltaInt != 0) return deltaInt;
109
Romain Guy2d5945e2013-06-18 12:59:25 -0700110 deltaInt = int(lhs.mHinting) - int(rhs.mHinting);
111 if (deltaInt != 0) return deltaInt;
112
John Reck1bcacfd2017-11-03 10:12:19 -0700113 if (lhs.mLookupTransform[SkMatrix::kMScaleX] < rhs.mLookupTransform[SkMatrix::kMScaleX])
114 return -1;
115 if (lhs.mLookupTransform[SkMatrix::kMScaleX] > rhs.mLookupTransform[SkMatrix::kMScaleX])
116 return +1;
Romain Guyc74f45a2013-02-26 19:10:14 -0800117
John Reck1bcacfd2017-11-03 10:12:19 -0700118 if (lhs.mLookupTransform[SkMatrix::kMScaleY] < rhs.mLookupTransform[SkMatrix::kMScaleY])
119 return -1;
120 if (lhs.mLookupTransform[SkMatrix::kMScaleY] > rhs.mLookupTransform[SkMatrix::kMScaleY])
121 return +1;
Romain Guyc74f45a2013-02-26 19:10:14 -0800122
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
John Reck1bcacfd2017-11-03 10:12:19 -0700135void Font::measureCachedGlyph(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap,
136 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
137 int width = (int)glyph->mBitmapWidth;
138 int height = (int)glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700139
Chris Craik39c5e7c2014-08-15 15:46:37 -0700140 int nPenX = x + glyph->mBitmapLeft;
141 int nPenY = y + glyph->mBitmapTop;
142
Romain Guy9f5dab32012-09-04 12:55:44 -0700143 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
John Reck1bcacfd2017-11-03 10:12:19 -0700157void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap, uint32_t bitmapW,
158 uint32_t bitmapH, Rect* bounds, const float* pos) {
159 float width = (float)glyph->mBitmapWidth;
160 float height = (float)glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700161
Chris Craik39c5e7c2014-08-15 15:46:37 -0700162 float nPenX = x + glyph->mBitmapLeft;
163 float nPenY = y + glyph->mBitmapTop + height;
164
Romain Guy9f5dab32012-09-04 12:55:44 -0700165 float u1 = glyph->mBitmapMinU;
166 float u2 = glyph->mBitmapMaxU;
167 float v1 = glyph->mBitmapMinV;
168 float v2 = glyph->mBitmapMaxV;
169
John Reck1bcacfd2017-11-03 10:12:19 -0700170 mState->appendMeshQuad(nPenX, nPenY, u1, v2, nPenX + width, nPenY, u2, v2, nPenX + width,
171 nPenY - height, u2, v1, nPenX, nPenY - height, u1, v1,
172 glyph->mCacheTexture);
Romain Guy9f5dab32012-09-04 12:55:44 -0700173}
174
John Reck1bcacfd2017-11-03 10:12:19 -0700175void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap,
176 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds,
177 const float* pos) {
178 float width = (float)glyph->mBitmapWidth;
179 float height = (float)glyph->mBitmapHeight;
Chris Craik39c5e7c2014-08-15 15:46:37 -0700180
Romain Guya4adcf02013-02-28 12:15:35 -0800181 SkPoint p[4];
Chris Craik39c5e7c2014-08-15 15:46:37 -0700182 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + height);
183 p[1].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop + height);
184 p[2].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop);
Romain Guy874f5c62013-03-01 18:07:35 -0800185 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop);
Romain Guya4adcf02013-02-28 12:15:35 -0800186
Romain Guy874f5c62013-03-01 18:07:35 -0800187 mDescription.mInverseLookupTransform.mapPoints(p, 4);
Romain Guya4adcf02013-02-28 12:15:35 -0800188
189 p[0].offset(x, y);
190 p[1].offset(x, y);
191 p[2].offset(x, y);
192 p[3].offset(x, y);
193
194 float u1 = glyph->mBitmapMinU;
195 float u2 = glyph->mBitmapMaxU;
196 float v1 = glyph->mBitmapMinV;
197 float v2 = glyph->mBitmapMaxV;
198
John Reck1bcacfd2017-11-03 10:12:19 -0700199 mState->appendRotatedMeshQuad(p[0].x(), p[0].y(), u1, v2, p[1].x(), p[1].y(), u2, v2, p[2].x(),
200 p[2].y(), u2, v1, p[3].x(), p[3].y(), u1, v1,
201 glyph->mCacheTexture);
Romain Guya4adcf02013-02-28 12:15:35 -0800202}
203
Romain Guycf51a412013-04-08 19:40:31 -0700204void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap,
John Reck1bcacfd2017-11-03 10:12:19 -0700205 uint32_t bitmapWidth, uint32_t bitmapHeight, Rect* bounds,
206 const float* pos) {
Romain Guycf51a412013-04-08 19:40:31 -0700207 int dstX = x + glyph->mBitmapLeft;
208 int dstY = y + glyph->mBitmapTop;
Romain Guy9f5dab32012-09-04 12:55:44 -0700209
Romain Guy80872462012-09-04 16:42:01 -0700210 CacheTexture* cacheTexture = glyph->mCacheTexture;
Romain Guycf51a412013-04-08 19:40:31 -0700211 PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer();
Digish Pandyab9312a52014-05-09 15:05:16 +0530212
213 uint32_t formatSize = PixelBuffer::formatSize(pixelBuffer->getFormat());
Digish Pandyac62c1cc2014-05-12 14:37:04 +0530214 uint32_t alpha_channel_offset = PixelBuffer::formatAlphaOffset(pixelBuffer->getFormat());
Digish Pandyab9312a52014-05-09 15:05:16 +0530215 uint32_t cacheWidth = cacheTexture->getWidth();
216 uint32_t srcStride = formatSize * cacheWidth;
217 uint32_t startY = glyph->mStartY * srcStride;
218 uint32_t endY = startY + (glyph->mBitmapHeight * srcStride);
219
Romain Guycf51a412013-04-08 19:40:31 -0700220 const uint8_t* cacheBuffer = pixelBuffer->map();
221
222 for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY;
John Reck1bcacfd2017-11-03 10:12:19 -0700223 cacheY += srcStride, bitmapY += bitmapWidth) {
Chris Craikde25a672015-06-30 13:12:24 -0700224 for (uint32_t i = 0; i < glyph->mBitmapWidth; ++i) {
225 uint8_t* dst = &(bitmap[bitmapY + dstX + i]);
John Reck1bcacfd2017-11-03 10:12:19 -0700226 const uint8_t& src =
227 cacheBuffer[cacheY + (glyph->mStartX + i) * formatSize + alpha_channel_offset];
Chris Craikde25a672015-06-30 13:12:24 -0700228 // Add alpha values to a max of 255, full opacity. This is done to handle
229 // fonts/strings where glyphs overlap.
230 *dst = std::min(*dst + src, 255);
Digish Pandyab9312a52014-05-09 15:05:16 +0530231 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700232 }
233}
234
235void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
John Reck1bcacfd2017-11-03 10:12:19 -0700236 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700237 const float halfWidth = glyph->mBitmapWidth * 0.5f;
238 const float height = glyph->mBitmapHeight;
239
240 vOffset += glyph->mBitmapTop + height;
241
242 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800243 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
244 if (!ok) {
245 ALOGW("The path for drawTextOnPath is empty or null");
246 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700247
248 // Move along the tangent and offset by the normal
249 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
John Reck1bcacfd2017-11-03 10:12:19 -0700250 -tangent->fY * halfWidth + tangent->fX * vOffset);
Romain Guy9f5dab32012-09-04 12:55:44 -0700251 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
John Reck1bcacfd2017-11-03 10:12:19 -0700252 tangent->fY * halfWidth + tangent->fX * vOffset);
Romain Guy9f5dab32012-09-04 12:55:44 -0700253 destination[2].set(destination[1].fX + tangent->fY * height,
John Reck1bcacfd2017-11-03 10:12:19 -0700254 destination[1].fY - tangent->fX * height);
Romain Guy9f5dab32012-09-04 12:55:44 -0700255 destination[3].set(destination[0].fX + tangent->fY * height,
John Reck1bcacfd2017-11-03 10:12:19 -0700256 destination[0].fY - tangent->fX * height);
Romain Guy9f5dab32012-09-04 12:55:44 -0700257
258 const float u1 = glyph->mBitmapMinU;
259 const float u2 = glyph->mBitmapMaxU;
260 const float v1 = glyph->mBitmapMinV;
261 const float v2 = glyph->mBitmapMaxV;
262
263 mState->appendRotatedMeshQuad(
John Reck1bcacfd2017-11-03 10:12:19 -0700264 position->x() + destination[0].x(), position->y() + destination[0].y(), u1, v2,
265 position->x() + destination[1].x(), position->y() + destination[1].y(), u2, v2,
266 position->x() + destination[2].x(), position->y() + destination[2].y(), u2, v1,
267 position->x() + destination[3].x(), position->y() + destination[3].y(), u1, v1,
Romain Guy9f5dab32012-09-04 12:55:44 -0700268 glyph->mCacheTexture);
269}
270
Chris Craikd218a922014-01-02 17:13:34 -0800271CachedGlyphInfo* Font::getCachedGlyph(const 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) {
Leon Scroggins III0e9059b2015-06-19 17:42:41 -0400276 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
John Reck1bcacfd2017-11-03 10:12:19 -0700277 SkAutoGlyphCacheNoGamma autoCache(*paint, &surfaceProps,
278 &mDescription.mLookupTransform);
Derek Sollenberger36b73a82018-03-07 08:01:19 -0500279 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.get(), textUnit);
280 updateGlyphCache(paint, skiaGlyph, autoCache.get(), cachedGlyph, precaching);
Romain Guyc74f45a2013-02-26 19:10:14 -0800281 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700282 } else {
283 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
284 }
285
Romain Guy9f5dab32012-09-04 12:55:44 -0700286 return cachedGlyph;
287}
288
John Reck1bcacfd2017-11-03 10:12:19 -0700289void Font::render(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, int x, int y,
290 const float* positions) {
291 render(paint, glyphs, numGlyphs, x, y, FRAMEBUFFER, nullptr, 0, 0, nullptr, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700292}
293
John Reck1bcacfd2017-11-03 10:12:19 -0700294void Font::render(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, const SkPath* path,
295 float hOffset, float vOffset) {
Chris Craike8c3c812016-02-05 20:10:50 -0800296 if (numGlyphs == 0 || glyphs == nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700297 return;
298 }
299
Romain Guy9f5dab32012-09-04 12:55:44 -0700300 int glyphsCount = 0;
Ben Wagnerf4f18a22016-03-28 11:09:39 -0400301 int prevRsbDelta = 0;
Romain Guy9f5dab32012-09-04 12:55:44 -0700302
303 float penX = 0.0f;
304
305 SkPoint position;
306 SkVector tangent;
307
308 SkPathMeasure measure(*path, false);
309 float pathLength = SkScalarToFloat(measure.getLength());
310
311 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
Chris Craike8c3c812016-02-05 20:10:50 -0800312 float textWidth = SkScalarToFloat(paint->measureText(glyphs, numGlyphs * 2));
Romain Guy9f5dab32012-09-04 12:55:44 -0700313 float pathOffset = pathLength;
314 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
315 textWidth *= 0.5f;
316 pathOffset *= 0.5f;
317 }
318 penX += pathOffset - textWidth;
319 }
320
321 while (glyphsCount < numGlyphs && penX < pathLength) {
Chris Craike8c3c812016-02-05 20:10:50 -0800322 glyph_t glyph = *(glyphs++);
Romain Guy9f5dab32012-09-04 12:55:44 -0700323
324 if (IS_END_OF_STRING(glyph)) {
325 break;
326 }
327
328 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Ben Wagnerf4f18a22016-03-28 11:09:39 -0400329 penX += AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta);
Romain Guy9f5dab32012-09-04 12:55:44 -0700330 prevRsbDelta = cachedGlyph->mRsbDelta;
331
Romain Guya4adcf02013-02-28 12:15:35 -0800332 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700333 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
334 }
335
Ben Wagnerf4f18a22016-03-28 11:09:39 -0400336 penX += cachedGlyph->mAdvanceX;
Romain Guy9f5dab32012-09-04 12:55:44 -0700337
338 glyphsCount++;
339 }
340}
341
John Reck1bcacfd2017-11-03 10:12:19 -0700342void Font::measure(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, Rect* bounds,
343 const float* positions) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800344 if (bounds == nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700345 ALOGE("No return rectangle provided to measure text");
346 return;
347 }
348 bounds->set(1e6, -1e6, -1e6, 1e6);
Chris Craike8c3c812016-02-05 20:10:50 -0800349 render(paint, glyphs, numGlyphs, 0, 0, MEASURE, nullptr, 0, 0, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700350}
351
Chris Craike8c3c812016-02-05 20:10:50 -0800352void Font::precache(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs) {
Chris Craike8c3c812016-02-05 20:10:50 -0800353 if (numGlyphs == 0 || glyphs == nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700354 return;
355 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700356
Romain Guybd3055f2013-03-13 16:14:47 -0700357 int glyphsCount = 0;
Romain Guy9f5dab32012-09-04 12:55:44 -0700358 while (glyphsCount < numGlyphs) {
Chris Craike8c3c812016-02-05 20:10:50 -0800359 glyph_t glyph = *(glyphs++);
Romain Guy9f5dab32012-09-04 12:55:44 -0700360
361 // Reached the end of the string
362 if (IS_END_OF_STRING(glyph)) {
363 break;
364 }
365
Andreas Gampeedaecc12014-11-10 20:54:07 -0800366 getCachedGlyph(paint, glyph, true);
Romain Guy9f5dab32012-09-04 12:55:44 -0700367 glyphsCount++;
368 }
369}
370
John Reck1bcacfd2017-11-03 10:12:19 -0700371void Font::render(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs, int x, int y,
372 RenderMode mode, uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH,
373 Rect* bounds, const float* positions) {
Chris Craike8c3c812016-02-05 20:10:50 -0800374 if (numGlyphs == 0 || glyphs == nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700375 return;
376 }
377
John Reck1bcacfd2017-11-03 10:12:19 -0700378 static RenderGlyph gRenderGlyph[] = {&android::uirenderer::Font::drawCachedGlyph,
379 &android::uirenderer::Font::drawCachedGlyphTransformed,
380 &android::uirenderer::Font::drawCachedGlyphBitmap,
381 &android::uirenderer::Font::drawCachedGlyphBitmap,
382 &android::uirenderer::Font::measureCachedGlyph,
383 &android::uirenderer::Font::measureCachedGlyph};
Romain Guy624234f2013-03-05 16:43:31 -0800384 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform];
Romain Guy9f5dab32012-09-04 12:55:44 -0700385
Romain Guy9f5dab32012-09-04 12:55:44 -0700386 int glyphsCount = 0;
387
Romain Guye3a9b242013-01-08 11:15:30 -0800388 while (glyphsCount < numGlyphs) {
Chris Craike8c3c812016-02-05 20:10:50 -0800389 glyph_t glyph = *(glyphs++);
Romain Guy9f5dab32012-09-04 12:55:44 -0700390
Romain Guye3a9b242013-01-08 11:15:30 -0800391 // Reached the end of the string
392 if (IS_END_OF_STRING(glyph)) {
393 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700394 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700395
Romain Guye3a9b242013-01-08 11:15:30 -0800396 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700397
Romain Guya4adcf02013-02-28 12:15:35 -0800398 // If it's still not valid, we couldn't cache it, so we shouldn't
399 // draw garbage; also skip empty glyphs (spaces)
400 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
John Reck1bcacfd2017-11-03 10:12:19 -0700401 int penX = x + (int)roundf(positions[(glyphsCount << 1)]);
402 int penY = y + (int)roundf(positions[(glyphsCount << 1) + 1]);
sergeyvaf102be2016-09-09 18:02:07 -0700403#ifdef BUGREPORT_FONT_CACHE_USAGE
404 mState->historyTracker().glyphRendered(cachedGlyph, penX, penY);
405#endif
John Reck1bcacfd2017-11-03 10:12:19 -0700406 (*this.*render)(cachedGlyph, penX, penY, bitmap, bitmapW, bitmapH, bounds, positions);
sergeyvaf102be2016-09-09 18:02:07 -0700407 } else {
408#ifdef BUGREPORT_FONT_CACHE_USAGE
409 mState->historyTracker().glyphRendered(cachedGlyph, -1, -1);
410#endif
Romain Guy9f5dab32012-09-04 12:55:44 -0700411 }
Romain Guye3a9b242013-01-08 11:15:30 -0800412
413 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700414 }
415}
416
Andreas Gampe64bb4132014-11-22 00:35:09 +0000417void Font::updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph,
John Reck1bcacfd2017-11-03 10:12:19 -0700418 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700419 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
420 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
421 glyph->mBitmapLeft = skiaGlyph.fLeft;
422 glyph->mBitmapTop = skiaGlyph.fTop;
423 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
424 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
425
426 uint32_t startX = 0;
427 uint32_t startY = 0;
428
429 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800430 if (!skiaGlyph.fImage) {
Victoria Lease2ee2d592013-12-17 13:54:29 -0800431 skiaGlyphCache->findImage(skiaGlyph);
Romain Guyb969a0d2013-02-05 14:38:40 -0800432 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700433 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
434
435 if (!glyph->mIsValid) {
436 return;
437 }
438
439 uint32_t endX = startX + skiaGlyph.fWidth;
440 uint32_t endY = startY + skiaGlyph.fHeight;
441
442 glyph->mStartX = startX;
443 glyph->mStartY = startY;
444 glyph->mBitmapWidth = skiaGlyph.fWidth;
445 glyph->mBitmapHeight = skiaGlyph.fHeight;
446
Romain Guya4adcf02013-02-28 12:15:35 -0800447 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
448 if (!empty) {
449 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
450 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700451
John Reck1bcacfd2017-11-03 10:12:19 -0700452 glyph->mBitmapMinU = startX / (float)cacheWidth;
453 glyph->mBitmapMinV = startY / (float)cacheHeight;
454 glyph->mBitmapMaxU = endX / (float)cacheWidth;
455 glyph->mBitmapMaxV = endY / (float)cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700456
Romain Guya4adcf02013-02-28 12:15:35 -0800457 mState->setTextureDirty();
458 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700459}
460
Chris Craikd218a922014-01-02 17:13:34 -0800461CachedGlyphInfo* Font::cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700462 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
463 mCachedGlyphs.add(glyph, newGlyph);
464
Leon Scroggins III0e9059b2015-06-19 17:42:41 -0400465 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
466 SkAutoGlyphCacheNoGamma autoCache(*paint, &surfaceProps, &mDescription.mLookupTransform);
Derek Sollenberger36b73a82018-03-07 08:01:19 -0500467 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.get(), glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700468 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800469 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700470
Derek Sollenberger36b73a82018-03-07 08:01:19 -0500471 updateGlyphCache(paint, skiaGlyph, autoCache.get(), newGlyph, precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700472
473 return newGlyph;
474}
475
Chris Craik59744b72014-07-01 17:56:52 -0700476Font* Font::create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix) {
Romain Guye3a9b242013-01-08 11:15:30 -0800477 FontDescription description(paint, matrix);
478 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700479
Romain Guya4adcf02013-02-28 12:15:35 -0800480 if (!font) {
481 font = new Font(state, description);
482 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700483 }
Romain Guy624234f2013-03-05 16:43:31 -0800484 font->mIdentityTransform = matrix.isIdentity();
Romain Guy9f5dab32012-09-04 12:55:44 -0700485
Romain Guya4adcf02013-02-28 12:15:35 -0800486 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700487}
488
John Reck1bcacfd2017-11-03 10:12:19 -0700489}; // namespace uirenderer
490}; // namespace android