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