blob: 762f2bba3340eff709539b9ab85671e7c4b2a6b1 [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 Guy9f5dab32012-09-04 12:55:44 -070067 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
68 delete mCachedGlyphs.valueAt(i);
69 }
70}
71
Romain Guye3a9b242013-01-08 11:15:30 -080072hash_t Font::FontDescription::hash() const {
73 uint32_t hash = JenkinsHashMix(0, mFontId);
74 hash = JenkinsHashMix(hash, android::hash_type(mFontSize));
75 hash = JenkinsHashMix(hash, android::hash_type(mFlags));
76 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle));
77 hash = JenkinsHashMix(hash, android::hash_type(mScaleX));
78 hash = JenkinsHashMix(hash, android::hash_type(mStyle));
79 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth));
Romain Guyb969a0d2013-02-05 14:38:40 -080080 hash = JenkinsHashMix(hash, int(mAntiAliasing));
Romain Guy2d5945e2013-06-18 12:59:25 -070081 hash = JenkinsHashMix(hash, android::hash_type(mHinting));
Romain Guyc74f45a2013-02-26 19:10:14 -080082 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX]));
83 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY]));
Romain Guye3a9b242013-01-08 11:15:30 -080084 return JenkinsHashWhiten(hash);
85}
86
87int Font::FontDescription::compare(const Font::FontDescription& lhs,
88 const Font::FontDescription& rhs) {
89 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId);
90 if (deltaInt != 0) return deltaInt;
91
92 if (lhs.mFontSize < rhs.mFontSize) return -1;
93 if (lhs.mFontSize > rhs.mFontSize) return +1;
94
95 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1;
96 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1;
97
98 deltaInt = int(lhs.mFlags) - int(rhs.mFlags);
99 if (deltaInt != 0) return deltaInt;
100
101 if (lhs.mScaleX < rhs.mScaleX) return -1;
102 if (lhs.mScaleX > rhs.mScaleX) return +1;
103
104 deltaInt = int(lhs.mStyle) - int(rhs.mStyle);
105 if (deltaInt != 0) return deltaInt;
106
107 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1;
108 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1;
109
Romain Guyb969a0d2013-02-05 14:38:40 -0800110 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing);
111 if (deltaInt != 0) return deltaInt;
112
Romain Guy2d5945e2013-06-18 12:59:25 -0700113 deltaInt = int(lhs.mHinting) - int(rhs.mHinting);
114 if (deltaInt != 0) return deltaInt;
115
Romain Guyc74f45a2013-02-26 19:10:14 -0800116 if (lhs.mLookupTransform[SkMatrix::kMScaleX] <
117 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1;
118 if (lhs.mLookupTransform[SkMatrix::kMScaleX] >
119 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1;
120
121 if (lhs.mLookupTransform[SkMatrix::kMScaleY] <
122 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1;
123 if (lhs.mLookupTransform[SkMatrix::kMScaleY] >
124 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1;
125
Romain Guye3a9b242013-01-08 11:15:30 -0800126 return 0;
127}
128
Romain Guy80872462012-09-04 16:42:01 -0700129void Font::invalidateTextureCache(CacheTexture* cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700130 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) {
131 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i);
Romain Guy521dc512012-09-04 19:10:33 -0700132 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700133 cachedGlyph->mIsValid = false;
134 }
135 }
136}
137
138void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000139 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700140 int width = (int) glyph->mBitmapWidth;
141 int height = (int) glyph->mBitmapHeight;
142
Chris Craik39c5e7c2014-08-15 15:46:37 -0700143 int nPenX = x + glyph->mBitmapLeft;
144 int nPenY = y + glyph->mBitmapTop;
145
Romain Guy9f5dab32012-09-04 12:55:44 -0700146 if (bounds->bottom > nPenY) {
147 bounds->bottom = nPenY;
148 }
149 if (bounds->left > nPenX) {
150 bounds->left = nPenX;
151 }
152 if (bounds->right < nPenX + width) {
153 bounds->right = nPenX + width;
154 }
155 if (bounds->top < nPenY + height) {
156 bounds->top = nPenY + height;
157 }
158}
159
160void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000161 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Romain Guye3a9b242013-01-08 11:15:30 -0800162 float width = (float) glyph->mBitmapWidth;
163 float height = (float) glyph->mBitmapHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700164
Chris Craik39c5e7c2014-08-15 15:46:37 -0700165 float nPenX = x + glyph->mBitmapLeft;
166 float nPenY = y + glyph->mBitmapTop + height;
167
Romain Guy9f5dab32012-09-04 12:55:44 -0700168 float u1 = glyph->mBitmapMinU;
169 float u2 = glyph->mBitmapMaxU;
170 float v1 = glyph->mBitmapMinV;
171 float v2 = glyph->mBitmapMaxV;
172
Romain Guy9f5dab32012-09-04 12:55:44 -0700173 mState->appendMeshQuad(nPenX, nPenY, u1, v2,
174 nPenX + width, nPenY, u2, v2,
175 nPenX + width, nPenY - height, u2, v1,
176 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture);
177}
178
Romain Guy624234f2013-03-05 16:43:31 -0800179void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000180 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) {
Chris Craik39c5e7c2014-08-15 15:46:37 -0700181 float width = (float) glyph->mBitmapWidth;
182 float height = (float) glyph->mBitmapHeight;
183
Romain Guya4adcf02013-02-28 12:15:35 -0800184 SkPoint p[4];
Chris Craik39c5e7c2014-08-15 15:46:37 -0700185 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + height);
186 p[1].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop + height);
187 p[2].iset(glyph->mBitmapLeft + width, glyph->mBitmapTop);
Romain Guy874f5c62013-03-01 18:07:35 -0800188 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop);
Romain Guya4adcf02013-02-28 12:15:35 -0800189
Romain Guy874f5c62013-03-01 18:07:35 -0800190 mDescription.mInverseLookupTransform.mapPoints(p, 4);
Romain Guya4adcf02013-02-28 12:15:35 -0800191
192 p[0].offset(x, y);
193 p[1].offset(x, y);
194 p[2].offset(x, y);
195 p[3].offset(x, y);
196
197 float u1 = glyph->mBitmapMinU;
198 float u2 = glyph->mBitmapMaxU;
199 float v1 = glyph->mBitmapMinV;
200 float v2 = glyph->mBitmapMaxV;
201
202 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800203 p[0].x(), p[0].y(), u1, v2,
204 p[1].x(), p[1].y(), u2, v2,
205 p[2].x(), p[2].y(), u2, v1,
206 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture);
Romain Guya4adcf02013-02-28 12:15:35 -0800207}
208
Romain Guycf51a412013-04-08 19:40:31 -0700209void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000210 uint32_t bitmapWidth, uint32_t bitmapHeight, Rect* bounds, const float* pos) {
Romain Guycf51a412013-04-08 19:40:31 -0700211 int dstX = x + glyph->mBitmapLeft;
212 int dstY = y + glyph->mBitmapTop;
Romain Guy9f5dab32012-09-04 12:55:44 -0700213
Romain Guy80872462012-09-04 16:42:01 -0700214 CacheTexture* cacheTexture = glyph->mCacheTexture;
Romain Guycf51a412013-04-08 19:40:31 -0700215 PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer();
Digish Pandyab9312a52014-05-09 15:05:16 +0530216
217 uint32_t formatSize = PixelBuffer::formatSize(pixelBuffer->getFormat());
Digish Pandyac62c1cc2014-05-12 14:37:04 +0530218 uint32_t alpha_channel_offset = PixelBuffer::formatAlphaOffset(pixelBuffer->getFormat());
Digish Pandyab9312a52014-05-09 15:05:16 +0530219 uint32_t cacheWidth = cacheTexture->getWidth();
220 uint32_t srcStride = formatSize * cacheWidth;
221 uint32_t startY = glyph->mStartY * srcStride;
222 uint32_t endY = startY + (glyph->mBitmapHeight * srcStride);
223
Romain Guycf51a412013-04-08 19:40:31 -0700224 const uint8_t* cacheBuffer = pixelBuffer->map();
225
226 for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY;
Digish Pandyab9312a52014-05-09 15:05:16 +0530227 cacheY += srcStride, bitmapY += bitmapWidth) {
228
Chris Craikde25a672015-06-30 13:12:24 -0700229 for (uint32_t i = 0; i < glyph->mBitmapWidth; ++i) {
230 uint8_t* dst = &(bitmap[bitmapY + dstX + i]);
231 const uint8_t& src = cacheBuffer[
232 cacheY + (glyph->mStartX + i)*formatSize + alpha_channel_offset];
233 // Add alpha values to a max of 255, full opacity. This is done to handle
234 // fonts/strings where glyphs overlap.
235 *dst = std::min(*dst + src, 255);
Digish Pandyab9312a52014-05-09 15:05:16 +0530236 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700237 }
238}
239
240void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset,
241 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) {
242 const float halfWidth = glyph->mBitmapWidth * 0.5f;
243 const float height = glyph->mBitmapHeight;
244
245 vOffset += glyph->mBitmapTop + height;
246
247 SkPoint destination[4];
Romain Guye67307c2013-02-11 18:01:20 -0800248 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent);
249 if (!ok) {
250 ALOGW("The path for drawTextOnPath is empty or null");
251 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700252
253 // Move along the tangent and offset by the normal
254 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset,
255 -tangent->fY * halfWidth + tangent->fX * vOffset);
256 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset,
257 tangent->fY * halfWidth + tangent->fX * vOffset);
258 destination[2].set(destination[1].fX + tangent->fY * height,
259 destination[1].fY - tangent->fX * height);
260 destination[3].set(destination[0].fX + tangent->fY * height,
261 destination[0].fY - tangent->fX * height);
262
263 const float u1 = glyph->mBitmapMinU;
264 const float u2 = glyph->mBitmapMaxU;
265 const float v1 = glyph->mBitmapMinV;
266 const float v2 = glyph->mBitmapMaxV;
267
268 mState->appendRotatedMeshQuad(
Romain Guy874f5c62013-03-01 18:07:35 -0800269 position->x() + destination[0].x(),
270 position->y() + destination[0].y(), u1, v2,
271 position->x() + destination[1].x(),
272 position->y() + destination[1].y(), u2, v2,
273 position->x() + destination[2].x(),
274 position->y() + destination[2].y(), u2, v1,
275 position->x() + destination[3].x(),
276 position->y() + destination[3].y(), u1, v1,
Romain Guy9f5dab32012-09-04 12:55:44 -0700277 glyph->mCacheTexture);
278}
279
Chris Craikd218a922014-01-02 17:13:34 -0800280CachedGlyphInfo* Font::getCachedGlyph(const SkPaint* paint, glyph_t textUnit, bool precaching) {
Romain Guybd3055f2013-03-13 16:14:47 -0700281 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit);
282 if (cachedGlyph) {
Romain Guyc74f45a2013-02-26 19:10:14 -0800283 // Is the glyph still in texture cache?
284 if (!cachedGlyph->mIsValid) {
Derek Sollenberger413995e2014-10-13 11:18:53 -0400285 SkDeviceProperties deviceProperties(kUnknown_SkPixelGeometry, 1.0f);
286 SkAutoGlyphCache autoCache(*paint, &deviceProperties, &mDescription.mLookupTransform);
Victoria Lease43b692d2013-12-03 15:02:28 -0800287 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), textUnit);
Victoria Lease2ee2d592013-12-17 13:54:29 -0800288 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), cachedGlyph, precaching);
Romain Guyc74f45a2013-02-26 19:10:14 -0800289 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700290 } else {
291 cachedGlyph = cacheGlyph(paint, textUnit, precaching);
292 }
293
Romain Guy9f5dab32012-09-04 12:55:44 -0700294 return cachedGlyph;
295}
296
Chris Craikd218a922014-01-02 17:13:34 -0800297void Font::render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700298 int numGlyphs, int x, int y, const float* positions) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800299 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, nullptr,
300 0, 0, nullptr, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700301}
302
Chris Craikd218a922014-01-02 17:13:34 -0800303void Font::render(const SkPaint* paint, const char *text, uint32_t start, uint32_t len,
304 int numGlyphs, const SkPath* path, float hOffset, float vOffset) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800305 if (numGlyphs == 0 || text == nullptr || len == 0) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700306 return;
307 }
308
309 text += start;
310
311 int glyphsCount = 0;
312 SkFixed prevRsbDelta = 0;
313
314 float penX = 0.0f;
315
316 SkPoint position;
317 SkVector tangent;
318
319 SkPathMeasure measure(*path, false);
320 float pathLength = SkScalarToFloat(measure.getLength());
321
322 if (paint->getTextAlign() != SkPaint::kLeft_Align) {
323 float textWidth = SkScalarToFloat(paint->measureText(text, len));
324 float pathOffset = pathLength;
325 if (paint->getTextAlign() == SkPaint::kCenter_Align) {
326 textWidth *= 0.5f;
327 pathOffset *= 0.5f;
328 }
329 penX += pathOffset - textWidth;
330 }
331
332 while (glyphsCount < numGlyphs && penX < pathLength) {
333 glyph_t glyph = GET_GLYPH(text);
334
335 if (IS_END_OF_STRING(glyph)) {
336 break;
337 }
338
339 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
340 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta));
341 prevRsbDelta = cachedGlyph->mRsbDelta;
342
Romain Guya4adcf02013-02-28 12:15:35 -0800343 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700344 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent);
345 }
346
347 penX += SkFixedToFloat(cachedGlyph->mAdvanceX);
348
349 glyphsCount++;
350 }
351}
352
Chris Craikd218a922014-01-02 17:13:34 -0800353void Font::measure(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700354 int numGlyphs, Rect *bounds, const float* positions) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800355 if (bounds == nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700356 ALOGE("No return rectangle provided to measure text");
357 return;
358 }
359 bounds->set(1e6, -1e6, -1e6, 1e6);
Chris Craikd41c4d82015-01-05 15:51:13 -0800360 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, nullptr, 0, 0, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700361}
362
Chris Craikd218a922014-01-02 17:13:34 -0800363void Font::precache(const SkPaint* paint, const char* text, int numGlyphs) {
Chris Craik70850ea2014-11-18 10:49:23 -0800364 ATRACE_NAME("Precache Glyphs");
Romain Guybd3055f2013-03-13 16:14:47 -0700365
Chris Craikd41c4d82015-01-05 15:51:13 -0800366 if (numGlyphs == 0 || text == nullptr) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700367 return;
368 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700369
Romain Guybd3055f2013-03-13 16:14:47 -0700370 int glyphsCount = 0;
Romain Guy9f5dab32012-09-04 12:55:44 -0700371 while (glyphsCount < numGlyphs) {
372 glyph_t glyph = GET_GLYPH(text);
373
374 // Reached the end of the string
375 if (IS_END_OF_STRING(glyph)) {
376 break;
377 }
378
Andreas Gampeedaecc12014-11-10 20:54:07 -0800379 getCachedGlyph(paint, glyph, true);
Romain Guy9f5dab32012-09-04 12:55:44 -0700380 glyphsCount++;
381 }
382}
383
Chris Craikd218a922014-01-02 17:13:34 -0800384void Font::render(const SkPaint* paint, const char* text, uint32_t start, uint32_t len,
Romain Guy9f5dab32012-09-04 12:55:44 -0700385 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap,
386 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800387 if (numGlyphs == 0 || text == nullptr || len == 0) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700388 return;
389 }
390
391 static RenderGlyph gRenderGlyph[] = {
392 &android::uirenderer::Font::drawCachedGlyph,
Romain Guy624234f2013-03-05 16:43:31 -0800393 &android::uirenderer::Font::drawCachedGlyphTransformed,
Romain Guy9f5dab32012-09-04 12:55:44 -0700394 &android::uirenderer::Font::drawCachedGlyphBitmap,
Romain Guya4adcf02013-02-28 12:15:35 -0800395 &android::uirenderer::Font::drawCachedGlyphBitmap,
396 &android::uirenderer::Font::measureCachedGlyph,
Romain Guy9f5dab32012-09-04 12:55:44 -0700397 &android::uirenderer::Font::measureCachedGlyph
398 };
Romain Guy624234f2013-03-05 16:43:31 -0800399 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform];
Romain Guy9f5dab32012-09-04 12:55:44 -0700400
401 text += start;
402 int glyphsCount = 0;
403
Romain Guye3a9b242013-01-08 11:15:30 -0800404 while (glyphsCount < numGlyphs) {
405 glyph_t glyph = GET_GLYPH(text);
Romain Guy9f5dab32012-09-04 12:55:44 -0700406
Romain Guye3a9b242013-01-08 11:15:30 -0800407 // Reached the end of the string
408 if (IS_END_OF_STRING(glyph)) {
409 break;
Romain Guy9f5dab32012-09-04 12:55:44 -0700410 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700411
Romain Guye3a9b242013-01-08 11:15:30 -0800412 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700413
Romain Guya4adcf02013-02-28 12:15:35 -0800414 // If it's still not valid, we couldn't cache it, so we shouldn't
415 // draw garbage; also skip empty glyphs (spaces)
416 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
Alexander Toresson3ed19272013-08-28 16:13:06 +0200417 int penX = x + (int) roundf(positions[(glyphsCount << 1)]);
418 int penY = y + (int) roundf(positions[(glyphsCount << 1) + 1]);
Romain Guye3a9b242013-01-08 11:15:30 -0800419
Alexander Toresson3ed19272013-08-28 16:13:06 +0200420 (*this.*render)(cachedGlyph, penX, penY,
Romain Guye3a9b242013-01-08 11:15:30 -0800421 bitmap, bitmapW, bitmapH, bounds, positions);
Romain Guy9f5dab32012-09-04 12:55:44 -0700422 }
Romain Guye3a9b242013-01-08 11:15:30 -0800423
424 glyphsCount++;
Romain Guy9f5dab32012-09-04 12:55:44 -0700425 }
426}
427
Andreas Gampe64bb4132014-11-22 00:35:09 +0000428void Font::updateGlyphCache(const SkPaint* paint, const SkGlyph& skiaGlyph,
Chris Craikd218a922014-01-02 17:13:34 -0800429 SkGlyphCache* skiaGlyphCache, CachedGlyphInfo* glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700430 glyph->mAdvanceX = skiaGlyph.fAdvanceX;
431 glyph->mAdvanceY = skiaGlyph.fAdvanceY;
432 glyph->mBitmapLeft = skiaGlyph.fLeft;
433 glyph->mBitmapTop = skiaGlyph.fTop;
434 glyph->mLsbDelta = skiaGlyph.fLsbDelta;
435 glyph->mRsbDelta = skiaGlyph.fRsbDelta;
436
437 uint32_t startX = 0;
438 uint32_t startY = 0;
439
440 // Get the bitmap for the glyph
Romain Guyb969a0d2013-02-05 14:38:40 -0800441 if (!skiaGlyph.fImage) {
Victoria Lease2ee2d592013-12-17 13:54:29 -0800442 skiaGlyphCache->findImage(skiaGlyph);
Romain Guyb969a0d2013-02-05 14:38:40 -0800443 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700444 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching);
445
446 if (!glyph->mIsValid) {
447 return;
448 }
449
450 uint32_t endX = startX + skiaGlyph.fWidth;
451 uint32_t endY = startY + skiaGlyph.fHeight;
452
453 glyph->mStartX = startX;
454 glyph->mStartY = startY;
455 glyph->mBitmapWidth = skiaGlyph.fWidth;
456 glyph->mBitmapHeight = skiaGlyph.fHeight;
457
Romain Guya4adcf02013-02-28 12:15:35 -0800458 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0;
459 if (!empty) {
460 uint32_t cacheWidth = glyph->mCacheTexture->getWidth();
461 uint32_t cacheHeight = glyph->mCacheTexture->getHeight();
Romain Guy9f5dab32012-09-04 12:55:44 -0700462
Romain Guya4adcf02013-02-28 12:15:35 -0800463 glyph->mBitmapMinU = startX / (float) cacheWidth;
464 glyph->mBitmapMinV = startY / (float) cacheHeight;
465 glyph->mBitmapMaxU = endX / (float) cacheWidth;
466 glyph->mBitmapMaxV = endY / (float) cacheHeight;
Romain Guy9f5dab32012-09-04 12:55:44 -0700467
Romain Guya4adcf02013-02-28 12:15:35 -0800468 mState->setTextureDirty();
469 }
Romain Guy9f5dab32012-09-04 12:55:44 -0700470}
471
Chris Craikd218a922014-01-02 17:13:34 -0800472CachedGlyphInfo* Font::cacheGlyph(const SkPaint* paint, glyph_t glyph, bool precaching) {
Romain Guy9f5dab32012-09-04 12:55:44 -0700473 CachedGlyphInfo* newGlyph = new CachedGlyphInfo();
474 mCachedGlyphs.add(glyph, newGlyph);
475
Derek Sollenberger413995e2014-10-13 11:18:53 -0400476 SkDeviceProperties deviceProperties(kUnknown_SkPixelGeometry, 1.0f);
477 SkAutoGlyphCache autoCache(*paint, &deviceProperties, &mDescription.mLookupTransform);
Victoria Lease43b692d2013-12-03 15:02:28 -0800478 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), glyph);
Romain Guy9f5dab32012-09-04 12:55:44 -0700479 newGlyph->mIsValid = false;
Romain Guya4adcf02013-02-28 12:15:35 -0800480 newGlyph->mGlyphIndex = skiaGlyph.fID;
Romain Guy9f5dab32012-09-04 12:55:44 -0700481
Victoria Lease2ee2d592013-12-17 13:54:29 -0800482 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), newGlyph, precaching);
Romain Guy9f5dab32012-09-04 12:55:44 -0700483
484 return newGlyph;
485}
486
Chris Craik59744b72014-07-01 17:56:52 -0700487Font* Font::create(FontRenderer* state, const SkPaint* paint, const SkMatrix& matrix) {
Romain Guye3a9b242013-01-08 11:15:30 -0800488 FontDescription description(paint, matrix);
489 Font* font = state->mActiveFonts.get(description);
Romain Guy9f5dab32012-09-04 12:55:44 -0700490
Romain Guya4adcf02013-02-28 12:15:35 -0800491 if (!font) {
492 font = new Font(state, description);
493 state->mActiveFonts.put(description, font);
Romain Guy9f5dab32012-09-04 12:55:44 -0700494 }
Romain Guy624234f2013-03-05 16:43:31 -0800495 font->mIdentityTransform = matrix.isIdentity();
Romain Guy9f5dab32012-09-04 12:55:44 -0700496
Romain Guya4adcf02013-02-28 12:15:35 -0800497 return font;
Romain Guy9f5dab32012-09-04 12:55:44 -0700498}
499
500}; // namespace uirenderer
501}; // namespace android