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