Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 19 | #include <cutils/compiler.h> |
| 20 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 21 | #include <utils/JenkinsHash.h> |
| 22 | |
Romain Guy | 14c40b4 | 2013-01-08 18:03:07 -0800 | [diff] [blame] | 23 | #include <SkGlyph.h> |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 24 | #include <SkUtils.h> |
| 25 | |
| 26 | #include "Debug.h" |
| 27 | #include "FontUtil.h" |
| 28 | #include "Font.h" |
| 29 | #include "FontRenderer.h" |
| 30 | #include "Properties.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | // Font |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 39 | Font::Font(FontRenderer* state, const Font::FontDescription& desc) : |
| 40 | mState(state), mDescription(desc) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 43 | Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) { |
| 44 | mFontId = SkTypeface::UniqueID(paint->getTypeface()); |
| 45 | mFontSize = paint->getTextSize(); |
| 46 | mFlags = 0; |
| 47 | if (paint->isFakeBoldText()) { |
| 48 | mFlags |= Font::kFakeBold; |
| 49 | } |
| 50 | mItalicStyle = paint->getTextSkewX(); |
| 51 | mScaleX = paint->getTextScaleX(); |
| 52 | mStyle = paint->getStyle(); |
| 53 | mStrokeWidth = paint->getStrokeWidth(); |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 54 | mAntiAliasing = paint->isAntiAlias(); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 55 | mLookupTransform[SkMatrix::kMScaleX] = matrix.data[mat4::kScaleX]; |
| 56 | mLookupTransform[SkMatrix::kMScaleY] = matrix.data[mat4::kScaleY]; |
| 57 | mLookupTransform[SkMatrix::kMSkewX] = matrix.data[mat4::kSkewX]; |
| 58 | mLookupTransform[SkMatrix::kMSkewY] = matrix.data[mat4::kSkewY]; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 59 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 60 | |
| 61 | Font::~Font() { |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 62 | mState->removeFont(this); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 63 | |
| 64 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { |
| 65 | delete mCachedGlyphs.valueAt(i); |
| 66 | } |
| 67 | } |
| 68 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 69 | hash_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 Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 77 | hash = JenkinsHashMix(hash, int(mAntiAliasing)); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 78 | hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX])); |
| 79 | hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY])); |
| 80 | hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMSkewX])); |
| 81 | hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMSkewY])); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 82 | return JenkinsHashWhiten(hash); |
| 83 | } |
| 84 | |
| 85 | int Font::FontDescription::compare(const Font::FontDescription& lhs, |
| 86 | const Font::FontDescription& rhs) { |
| 87 | int deltaInt = int(lhs.mFontId) - int(rhs.mFontId); |
| 88 | if (deltaInt != 0) return deltaInt; |
| 89 | |
| 90 | if (lhs.mFontSize < rhs.mFontSize) return -1; |
| 91 | if (lhs.mFontSize > rhs.mFontSize) return +1; |
| 92 | |
| 93 | if (lhs.mItalicStyle < rhs.mItalicStyle) return -1; |
| 94 | if (lhs.mItalicStyle > rhs.mItalicStyle) return +1; |
| 95 | |
| 96 | deltaInt = int(lhs.mFlags) - int(rhs.mFlags); |
| 97 | if (deltaInt != 0) return deltaInt; |
| 98 | |
| 99 | if (lhs.mScaleX < rhs.mScaleX) return -1; |
| 100 | if (lhs.mScaleX > rhs.mScaleX) return +1; |
| 101 | |
| 102 | deltaInt = int(lhs.mStyle) - int(rhs.mStyle); |
| 103 | if (deltaInt != 0) return deltaInt; |
| 104 | |
| 105 | if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1; |
| 106 | if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1; |
| 107 | |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 108 | deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing); |
| 109 | if (deltaInt != 0) return deltaInt; |
| 110 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 111 | if (lhs.mLookupTransform[SkMatrix::kMScaleX] < |
| 112 | rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1; |
| 113 | if (lhs.mLookupTransform[SkMatrix::kMScaleX] > |
| 114 | rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1; |
| 115 | |
| 116 | if (lhs.mLookupTransform[SkMatrix::kMScaleY] < |
| 117 | rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1; |
| 118 | if (lhs.mLookupTransform[SkMatrix::kMScaleY] > |
| 119 | rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1; |
| 120 | |
| 121 | if (lhs.mLookupTransform[SkMatrix::kMSkewX] < |
| 122 | rhs.mLookupTransform[SkMatrix::kMSkewX]) return -1; |
| 123 | if (lhs.mLookupTransform[SkMatrix::kMSkewX] > |
| 124 | rhs.mLookupTransform[SkMatrix::kMSkewX]) return +1; |
| 125 | |
| 126 | if (lhs.mLookupTransform[SkMatrix::kMSkewY] < |
| 127 | rhs.mLookupTransform[SkMatrix::kMSkewY]) return -1; |
| 128 | if (lhs.mLookupTransform[SkMatrix::kMSkewY] > |
| 129 | rhs.mLookupTransform[SkMatrix::kMSkewY]) return +1; |
| 130 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 134 | void Font::invalidateTextureCache(CacheTexture* cacheTexture) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 135 | for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { |
| 136 | CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i); |
Romain Guy | 521dc51 | 2012-09-04 19:10:33 -0700 | [diff] [blame] | 137 | if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 138 | cachedGlyph->mIsValid = false; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, |
| 144 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { |
| 145 | int nPenX = x + glyph->mBitmapLeft; |
| 146 | int nPenY = y + glyph->mBitmapTop; |
| 147 | |
| 148 | int width = (int) glyph->mBitmapWidth; |
| 149 | int height = (int) glyph->mBitmapHeight; |
| 150 | |
| 151 | if (bounds->bottom > nPenY) { |
| 152 | bounds->bottom = nPenY; |
| 153 | } |
| 154 | if (bounds->left > nPenX) { |
| 155 | bounds->left = nPenX; |
| 156 | } |
| 157 | if (bounds->right < nPenX + width) { |
| 158 | bounds->right = nPenX + width; |
| 159 | } |
| 160 | if (bounds->top < nPenY + height) { |
| 161 | bounds->top = nPenY + height; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, |
| 166 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 167 | float nPenX = x + glyph->mBitmapLeft; |
| 168 | float nPenY = y + (glyph->mBitmapTop + glyph->mBitmapHeight); |
| 169 | |
| 170 | float width = (float) glyph->mBitmapWidth; |
| 171 | float height = (float) glyph->mBitmapHeight; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 172 | |
| 173 | float u1 = glyph->mBitmapMinU; |
| 174 | float u2 = glyph->mBitmapMaxU; |
| 175 | float v1 = glyph->mBitmapMinV; |
| 176 | float v2 = glyph->mBitmapMaxV; |
| 177 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 178 | mState->appendMeshQuad(nPenX, nPenY, u1, v2, |
| 179 | nPenX + width, nPenY, u2, v2, |
| 180 | nPenX + width, nPenY - height, u2, v1, |
| 181 | nPenX, nPenY - height, u1, v1, glyph->mCacheTexture); |
| 182 | } |
| 183 | |
| 184 | void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, |
| 185 | uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { |
| 186 | int nPenX = x + glyph->mBitmapLeft; |
| 187 | int nPenY = y + glyph->mBitmapTop; |
| 188 | |
| 189 | uint32_t endX = glyph->mStartX + glyph->mBitmapWidth; |
| 190 | uint32_t endY = glyph->mStartY + glyph->mBitmapHeight; |
| 191 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 192 | CacheTexture* cacheTexture = glyph->mCacheTexture; |
| 193 | uint32_t cacheWidth = cacheTexture->getWidth(); |
| 194 | const uint8_t* cacheBuffer = cacheTexture->getTexture(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 195 | |
| 196 | uint32_t cacheX = 0, cacheY = 0; |
| 197 | int32_t bX = 0, bY = 0; |
| 198 | for (cacheX = glyph->mStartX, bX = nPenX; cacheX < endX; cacheX++, bX++) { |
| 199 | for (cacheY = glyph->mStartY, bY = nPenY; cacheY < endY; cacheY++, bY++) { |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 200 | uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX]; |
| 201 | bitmap[bY * bitmapW + bX] = tempCol; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset, |
| 207 | SkPathMeasure& measure, SkPoint* position, SkVector* tangent) { |
| 208 | const float halfWidth = glyph->mBitmapWidth * 0.5f; |
| 209 | const float height = glyph->mBitmapHeight; |
| 210 | |
| 211 | vOffset += glyph->mBitmapTop + height; |
| 212 | |
| 213 | SkPoint destination[4]; |
Romain Guy | e67307c | 2013-02-11 18:01:20 -0800 | [diff] [blame] | 214 | bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent); |
| 215 | if (!ok) { |
| 216 | ALOGW("The path for drawTextOnPath is empty or null"); |
| 217 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 218 | |
| 219 | // Move along the tangent and offset by the normal |
| 220 | destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset, |
| 221 | -tangent->fY * halfWidth + tangent->fX * vOffset); |
| 222 | destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset, |
| 223 | tangent->fY * halfWidth + tangent->fX * vOffset); |
| 224 | destination[2].set(destination[1].fX + tangent->fY * height, |
| 225 | destination[1].fY - tangent->fX * height); |
| 226 | destination[3].set(destination[0].fX + tangent->fY * height, |
| 227 | destination[0].fY - tangent->fX * height); |
| 228 | |
| 229 | const float u1 = glyph->mBitmapMinU; |
| 230 | const float u2 = glyph->mBitmapMaxU; |
| 231 | const float v1 = glyph->mBitmapMinV; |
| 232 | const float v2 = glyph->mBitmapMaxV; |
| 233 | |
| 234 | mState->appendRotatedMeshQuad( |
| 235 | position->fX + destination[0].fX, |
| 236 | position->fY + destination[0].fY, u1, v2, |
| 237 | position->fX + destination[1].fX, |
| 238 | position->fY + destination[1].fY, u2, v2, |
| 239 | position->fX + destination[2].fX, |
| 240 | position->fY + destination[2].fY, u2, v1, |
| 241 | position->fX + destination[3].fX, |
| 242 | position->fY + destination[3].fY, u1, v1, |
| 243 | glyph->mCacheTexture); |
| 244 | } |
| 245 | |
| 246 | CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) { |
| 247 | CachedGlyphInfo* cachedGlyph = NULL; |
| 248 | ssize_t index = mCachedGlyphs.indexOfKey(textUnit); |
| 249 | if (index >= 0) { |
| 250 | cachedGlyph = mCachedGlyphs.valueAt(index); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 251 | |
| 252 | // Is the glyph still in texture cache? |
| 253 | if (!cachedGlyph->mIsValid) { |
| 254 | const SkGlyph& skiaGlyph = GET_METRICS(paint, textUnit, &mDescription.mLookupTransform); |
| 255 | updateGlyphCache(paint, skiaGlyph, cachedGlyph, precaching); |
| 256 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 257 | } else { |
| 258 | cachedGlyph = cacheGlyph(paint, textUnit, precaching); |
| 259 | } |
| 260 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 261 | return cachedGlyph; |
| 262 | } |
| 263 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 264 | void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
| 265 | int numGlyphs, int x, int y, const float* positions) { |
| 266 | render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL, |
| 267 | 0, 0, NULL, positions); |
| 268 | } |
| 269 | |
| 270 | void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, |
| 271 | int numGlyphs, SkPath* path, float hOffset, float vOffset) { |
| 272 | if (numGlyphs == 0 || text == NULL || len == 0) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | text += start; |
| 277 | |
| 278 | int glyphsCount = 0; |
| 279 | SkFixed prevRsbDelta = 0; |
| 280 | |
| 281 | float penX = 0.0f; |
| 282 | |
| 283 | SkPoint position; |
| 284 | SkVector tangent; |
| 285 | |
| 286 | SkPathMeasure measure(*path, false); |
| 287 | float pathLength = SkScalarToFloat(measure.getLength()); |
| 288 | |
| 289 | if (paint->getTextAlign() != SkPaint::kLeft_Align) { |
| 290 | float textWidth = SkScalarToFloat(paint->measureText(text, len)); |
| 291 | float pathOffset = pathLength; |
| 292 | if (paint->getTextAlign() == SkPaint::kCenter_Align) { |
| 293 | textWidth *= 0.5f; |
| 294 | pathOffset *= 0.5f; |
| 295 | } |
| 296 | penX += pathOffset - textWidth; |
| 297 | } |
| 298 | |
| 299 | while (glyphsCount < numGlyphs && penX < pathLength) { |
| 300 | glyph_t glyph = GET_GLYPH(text); |
| 301 | |
| 302 | if (IS_END_OF_STRING(glyph)) { |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph); |
| 307 | penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta)); |
| 308 | prevRsbDelta = cachedGlyph->mRsbDelta; |
| 309 | |
| 310 | if (cachedGlyph->mIsValid) { |
| 311 | drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent); |
| 312 | } |
| 313 | |
| 314 | penX += SkFixedToFloat(cachedGlyph->mAdvanceX); |
| 315 | |
| 316 | glyphsCount++; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
| 321 | int numGlyphs, Rect *bounds, const float* positions) { |
| 322 | if (bounds == NULL) { |
| 323 | ALOGE("No return rectangle provided to measure text"); |
| 324 | return; |
| 325 | } |
| 326 | bounds->set(1e6, -1e6, -1e6, 1e6); |
| 327 | render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions); |
| 328 | } |
| 329 | |
| 330 | void Font::precache(SkPaint* paint, const char* text, int numGlyphs) { |
| 331 | |
| 332 | if (numGlyphs == 0 || text == NULL) { |
| 333 | return; |
| 334 | } |
| 335 | int glyphsCount = 0; |
| 336 | |
| 337 | while (glyphsCount < numGlyphs) { |
| 338 | glyph_t glyph = GET_GLYPH(text); |
| 339 | |
| 340 | // Reached the end of the string |
| 341 | if (IS_END_OF_STRING(glyph)) { |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true); |
| 346 | |
| 347 | glyphsCount++; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len, |
| 352 | int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, |
| 353 | uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) { |
| 354 | if (numGlyphs == 0 || text == NULL || len == 0) { |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | static RenderGlyph gRenderGlyph[] = { |
| 359 | &android::uirenderer::Font::drawCachedGlyph, |
| 360 | &android::uirenderer::Font::drawCachedGlyphBitmap, |
| 361 | &android::uirenderer::Font::measureCachedGlyph |
| 362 | }; |
| 363 | RenderGlyph render = gRenderGlyph[mode]; |
| 364 | |
| 365 | text += start; |
| 366 | int glyphsCount = 0; |
| 367 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 368 | const SkPaint::Align align = paint->getTextAlign(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 369 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 370 | while (glyphsCount < numGlyphs) { |
| 371 | glyph_t glyph = GET_GLYPH(text); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 372 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 373 | // Reached the end of the string |
| 374 | if (IS_END_OF_STRING(glyph)) { |
| 375 | break; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 376 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 377 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 378 | CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 379 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 380 | // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage |
| 381 | if (cachedGlyph->mIsValid) { |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 382 | float penX = x + positions[(glyphsCount << 1)]; |
| 383 | float penY = y + positions[(glyphsCount << 1) + 1]; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 384 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 385 | if (!mTransform.isIdentity()) { |
| 386 | mTransform.mapPoint(penX, penY); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 389 | (*this.*render)(cachedGlyph, roundf(penX), roundf(penY), |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 390 | bitmap, bitmapW, bitmapH, bounds, positions); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 391 | } |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 392 | |
| 393 | glyphsCount++; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, CachedGlyphInfo* glyph, |
| 398 | bool precaching) { |
| 399 | glyph->mAdvanceX = skiaGlyph.fAdvanceX; |
| 400 | glyph->mAdvanceY = skiaGlyph.fAdvanceY; |
| 401 | glyph->mBitmapLeft = skiaGlyph.fLeft; |
| 402 | glyph->mBitmapTop = skiaGlyph.fTop; |
| 403 | glyph->mLsbDelta = skiaGlyph.fLsbDelta; |
| 404 | glyph->mRsbDelta = skiaGlyph.fRsbDelta; |
| 405 | |
| 406 | uint32_t startX = 0; |
| 407 | uint32_t startY = 0; |
| 408 | |
| 409 | // Get the bitmap for the glyph |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 410 | if (!skiaGlyph.fImage) { |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 411 | paint->findImage(skiaGlyph, &mDescription.mLookupTransform); |
Romain Guy | b969a0d | 2013-02-05 14:38:40 -0800 | [diff] [blame] | 412 | } |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 413 | mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching); |
| 414 | |
| 415 | if (!glyph->mIsValid) { |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | uint32_t endX = startX + skiaGlyph.fWidth; |
| 420 | uint32_t endY = startY + skiaGlyph.fHeight; |
| 421 | |
| 422 | glyph->mStartX = startX; |
| 423 | glyph->mStartY = startY; |
| 424 | glyph->mBitmapWidth = skiaGlyph.fWidth; |
| 425 | glyph->mBitmapHeight = skiaGlyph.fHeight; |
| 426 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 427 | uint32_t cacheWidth = glyph->mCacheTexture->getWidth(); |
| 428 | uint32_t cacheHeight = glyph->mCacheTexture->getHeight(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 429 | |
| 430 | glyph->mBitmapMinU = startX / (float) cacheWidth; |
| 431 | glyph->mBitmapMinV = startY / (float) cacheHeight; |
| 432 | glyph->mBitmapMaxU = endX / (float) cacheWidth; |
| 433 | glyph->mBitmapMaxV = endY / (float) cacheHeight; |
| 434 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 435 | mState->setTextureDirty(); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) { |
| 439 | CachedGlyphInfo* newGlyph = new CachedGlyphInfo(); |
| 440 | mCachedGlyphs.add(glyph, newGlyph); |
| 441 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 442 | const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, &mDescription.mLookupTransform); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 443 | newGlyph->mGlyphIndex = skiaGlyph.fID; |
| 444 | newGlyph->mIsValid = false; |
| 445 | |
| 446 | updateGlyphCache(paint, skiaGlyph, newGlyph, precaching); |
| 447 | |
| 448 | return newGlyph; |
| 449 | } |
| 450 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 451 | Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) { |
| 452 | FontDescription description(paint, matrix); |
| 453 | Font* font = state->mActiveFonts.get(description); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 454 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 455 | if (font) { |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame^] | 456 | font->mTransform.load(matrix); |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 457 | return font; |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 460 | Font* newFont = new Font(state, description); |
| 461 | state->mActiveFonts.put(description, newFont); |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 462 | return newFont; |
| 463 | } |
| 464 | |
| 465 | }; // namespace uirenderer |
| 466 | }; // namespace android |