Font.cpp revision 7e13d1d4a46a01ebaa412d622ef76d90bd39d12a
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 17#define LOG_TAG "OpenGLRenderer" 18#define ATRACE_TAG ATRACE_TAG_VIEW 19 20#include <cutils/compiler.h> 21 22#include <utils/JenkinsHash.h> 23#include <utils/Trace.h> 24 25#include <SkGlyph.h> 26#include <SkGlyphCache.h> 27#include <SkUtils.h> 28 29#include "FontUtil.h" 30#include "Font.h" 31#include "../Debug.h" 32#include "../FontRenderer.h" 33#include "../PixelBuffer.h" 34#include "../Properties.h" 35 36namespace android { 37namespace uirenderer { 38 39/////////////////////////////////////////////////////////////////////////////// 40// Font 41/////////////////////////////////////////////////////////////////////////////// 42 43Font::Font(FontRenderer* state, const Font::FontDescription& desc) : 44 mState(state), mDescription(desc) { 45} 46 47Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) { 48 mFontId = SkTypeface::UniqueID(paint->getTypeface()); 49 mFontSize = paint->getTextSize(); 50 mFlags = 0; 51 if (paint->isFakeBoldText()) { 52 mFlags |= Font::kFakeBold; 53 } 54 mItalicStyle = paint->getTextSkewX(); 55 mScaleX = paint->getTextScaleX(); 56 mStyle = paint->getStyle(); 57 mStrokeWidth = paint->getStrokeWidth(); 58 mAntiAliasing = paint->isAntiAlias(); 59 mHinting = paint->getHinting(); 60 mLookupTransform.reset(); 61 mLookupTransform[SkMatrix::kMScaleX] = roundf(fmaxf(1.0f, matrix[mat4::kScaleX])); 62 mLookupTransform[SkMatrix::kMScaleY] = roundf(fmaxf(1.0f, matrix[mat4::kScaleY])); 63 if (!mLookupTransform.invert(&mInverseLookupTransform)) { 64 ALOGW("Could not query the inverse lookup transform for this font"); 65 } 66} 67 68Font::~Font() { 69 mState->removeFont(this); 70 71 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { 72 delete mCachedGlyphs.valueAt(i); 73 } 74} 75 76hash_t Font::FontDescription::hash() const { 77 uint32_t hash = JenkinsHashMix(0, mFontId); 78 hash = JenkinsHashMix(hash, android::hash_type(mFontSize)); 79 hash = JenkinsHashMix(hash, android::hash_type(mFlags)); 80 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle)); 81 hash = JenkinsHashMix(hash, android::hash_type(mScaleX)); 82 hash = JenkinsHashMix(hash, android::hash_type(mStyle)); 83 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth)); 84 hash = JenkinsHashMix(hash, int(mAntiAliasing)); 85 hash = JenkinsHashMix(hash, android::hash_type(mHinting)); 86 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX])); 87 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY])); 88 return JenkinsHashWhiten(hash); 89} 90 91int Font::FontDescription::compare(const Font::FontDescription& lhs, 92 const Font::FontDescription& rhs) { 93 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId); 94 if (deltaInt != 0) return deltaInt; 95 96 if (lhs.mFontSize < rhs.mFontSize) return -1; 97 if (lhs.mFontSize > rhs.mFontSize) return +1; 98 99 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1; 100 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1; 101 102 deltaInt = int(lhs.mFlags) - int(rhs.mFlags); 103 if (deltaInt != 0) return deltaInt; 104 105 if (lhs.mScaleX < rhs.mScaleX) return -1; 106 if (lhs.mScaleX > rhs.mScaleX) return +1; 107 108 deltaInt = int(lhs.mStyle) - int(rhs.mStyle); 109 if (deltaInt != 0) return deltaInt; 110 111 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1; 112 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1; 113 114 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing); 115 if (deltaInt != 0) return deltaInt; 116 117 deltaInt = int(lhs.mHinting) - int(rhs.mHinting); 118 if (deltaInt != 0) return deltaInt; 119 120 if (lhs.mLookupTransform[SkMatrix::kMScaleX] < 121 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1; 122 if (lhs.mLookupTransform[SkMatrix::kMScaleX] > 123 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1; 124 125 if (lhs.mLookupTransform[SkMatrix::kMScaleY] < 126 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1; 127 if (lhs.mLookupTransform[SkMatrix::kMScaleY] > 128 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1; 129 130 return 0; 131} 132 133void Font::invalidateTextureCache(CacheTexture* cacheTexture) { 134 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { 135 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i); 136 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) { 137 cachedGlyph->mIsValid = false; 138 } 139 } 140} 141 142void Font::measureCachedGlyph(CachedGlyphInfo *glyph, int x, int y, 143 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { 144 int nPenX = x + glyph->mBitmapLeft; 145 int nPenY = y + glyph->mBitmapTop; 146 147 int width = (int) glyph->mBitmapWidth; 148 int height = (int) glyph->mBitmapHeight; 149 150 if (bounds->bottom > nPenY) { 151 bounds->bottom = nPenY; 152 } 153 if (bounds->left > nPenX) { 154 bounds->left = nPenX; 155 } 156 if (bounds->right < nPenX + width) { 157 bounds->right = nPenX + width; 158 } 159 if (bounds->top < nPenY + height) { 160 bounds->top = nPenY + height; 161 } 162} 163 164void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, 165 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { 166 float nPenX = x + glyph->mBitmapLeft; 167 float nPenY = y + glyph->mBitmapTop + glyph->mBitmapHeight; 168 169 float width = (float) glyph->mBitmapWidth; 170 float height = (float) glyph->mBitmapHeight; 171 172 float u1 = glyph->mBitmapMinU; 173 float u2 = glyph->mBitmapMaxU; 174 float v1 = glyph->mBitmapMinV; 175 float v2 = glyph->mBitmapMaxV; 176 177 mState->appendMeshQuad(nPenX, nPenY, u1, v2, 178 nPenX + width, nPenY, u2, v2, 179 nPenX + width, nPenY - height, u2, v1, 180 nPenX, nPenY - height, u1, v1, glyph->mCacheTexture); 181} 182 183void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y, 184 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { 185 SkPoint p[4]; 186 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + glyph->mBitmapHeight); 187 p[1].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop + glyph->mBitmapHeight); 188 p[2].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop); 189 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop); 190 191 mDescription.mInverseLookupTransform.mapPoints(p, 4); 192 193 p[0].offset(x, y); 194 p[1].offset(x, y); 195 p[2].offset(x, y); 196 p[3].offset(x, y); 197 198 float u1 = glyph->mBitmapMinU; 199 float u2 = glyph->mBitmapMaxU; 200 float v1 = glyph->mBitmapMinV; 201 float v2 = glyph->mBitmapMaxV; 202 203 mState->appendRotatedMeshQuad( 204 p[0].x(), p[0].y(), u1, v2, 205 p[1].x(), p[1].y(), u2, v2, 206 p[2].x(), p[2].y(), u2, v1, 207 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture); 208} 209 210void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap, 211 uint32_t bitmapWidth, uint32_t bitmapHeight, Rect* bounds, const float* pos) { 212 int dstX = x + glyph->mBitmapLeft; 213 int dstY = y + glyph->mBitmapTop; 214 215 CacheTexture* cacheTexture = glyph->mCacheTexture; 216 PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer(); 217 218 uint32_t formatSize = PixelBuffer::formatSize(pixelBuffer->getFormat()); 219 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 224 const uint8_t* cacheBuffer = pixelBuffer->map(); 225 226 for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY; 227 cacheY += srcStride, bitmapY += bitmapWidth) { 228 229 if (formatSize == 1) { 230 memcpy(&bitmap[bitmapY + dstX], &cacheBuffer[cacheY + glyph->mStartX], glyph->mBitmapWidth); 231 } else { 232 for (uint32_t i = 0; i < glyph->mBitmapWidth; ++i) { 233 bitmap[bitmapY + dstX + i] = cacheBuffer[cacheY + (glyph->mStartX + i)*formatSize]; 234 } 235 } 236 } 237 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]; 248 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 } 252 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( 269 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, 277 glyph->mCacheTexture); 278} 279 280CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) { 281 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit); 282 if (cachedGlyph) { 283 // Is the glyph still in texture cache? 284 if (!cachedGlyph->mIsValid) { 285 SkAutoGlyphCache autoCache(*paint, NULL, &mDescription.mLookupTransform); 286 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), textUnit); 287 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), cachedGlyph, precaching); 288 } 289 } else { 290 cachedGlyph = cacheGlyph(paint, textUnit, precaching); 291 } 292 293 return cachedGlyph; 294} 295 296void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, 297 int numGlyphs, int x, int y, const float* positions) { 298 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL, 299 0, 0, NULL, positions); 300} 301 302void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, 303 int numGlyphs, SkPath* path, float hOffset, float vOffset) { 304 if (numGlyphs == 0 || text == NULL || len == 0) { 305 return; 306 } 307 308 text += start; 309 310 int glyphsCount = 0; 311 SkFixed prevRsbDelta = 0; 312 313 float penX = 0.0f; 314 315 SkPoint position; 316 SkVector tangent; 317 318 SkPathMeasure measure(*path, false); 319 float pathLength = SkScalarToFloat(measure.getLength()); 320 321 if (paint->getTextAlign() != SkPaint::kLeft_Align) { 322 float textWidth = SkScalarToFloat(paint->measureText(text, len)); 323 float pathOffset = pathLength; 324 if (paint->getTextAlign() == SkPaint::kCenter_Align) { 325 textWidth *= 0.5f; 326 pathOffset *= 0.5f; 327 } 328 penX += pathOffset - textWidth; 329 } 330 331 while (glyphsCount < numGlyphs && penX < pathLength) { 332 glyph_t glyph = GET_GLYPH(text); 333 334 if (IS_END_OF_STRING(glyph)) { 335 break; 336 } 337 338 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph); 339 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta)); 340 prevRsbDelta = cachedGlyph->mRsbDelta; 341 342 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) { 343 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent); 344 } 345 346 penX += SkFixedToFloat(cachedGlyph->mAdvanceX); 347 348 glyphsCount++; 349 } 350} 351 352void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len, 353 int numGlyphs, Rect *bounds, const float* positions) { 354 if (bounds == NULL) { 355 ALOGE("No return rectangle provided to measure text"); 356 return; 357 } 358 bounds->set(1e6, -1e6, -1e6, 1e6); 359 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions); 360} 361 362void Font::precache(SkPaint* paint, const char* text, int numGlyphs) { 363 ATRACE_NAME("precacheText"); 364 365 if (numGlyphs == 0 || text == NULL) { 366 return; 367 } 368 369 int glyphsCount = 0; 370 while (glyphsCount < numGlyphs) { 371 glyph_t glyph = GET_GLYPH(text); 372 373 // Reached the end of the string 374 if (IS_END_OF_STRING(glyph)) { 375 break; 376 } 377 378 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true); 379 glyphsCount++; 380 } 381} 382 383void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len, 384 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, 385 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) { 386 if (numGlyphs == 0 || text == NULL || len == 0) { 387 return; 388 } 389 390 static RenderGlyph gRenderGlyph[] = { 391 &android::uirenderer::Font::drawCachedGlyph, 392 &android::uirenderer::Font::drawCachedGlyphTransformed, 393 &android::uirenderer::Font::drawCachedGlyphBitmap, 394 &android::uirenderer::Font::drawCachedGlyphBitmap, 395 &android::uirenderer::Font::measureCachedGlyph, 396 &android::uirenderer::Font::measureCachedGlyph 397 }; 398 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform]; 399 400 text += start; 401 int glyphsCount = 0; 402 403 const SkPaint::Align align = paint->getTextAlign(); 404 405 while (glyphsCount < numGlyphs) { 406 glyph_t glyph = GET_GLYPH(text); 407 408 // Reached the end of the string 409 if (IS_END_OF_STRING(glyph)) { 410 break; 411 } 412 413 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph); 414 415 // If it's still not valid, we couldn't cache it, so we shouldn't 416 // draw garbage; also skip empty glyphs (spaces) 417 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) { 418 int penX = x + (int) roundf(positions[(glyphsCount << 1)]); 419 int penY = y + (int) roundf(positions[(glyphsCount << 1) + 1]); 420 421 (*this.*render)(cachedGlyph, penX, penY, 422 bitmap, bitmapW, bitmapH, bounds, positions); 423 } 424 425 glyphsCount++; 426 } 427} 428 429void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, SkGlyphCache* skiaGlyphCache, 430 CachedGlyphInfo* glyph, bool precaching) { 431 glyph->mAdvanceX = skiaGlyph.fAdvanceX; 432 glyph->mAdvanceY = skiaGlyph.fAdvanceY; 433 glyph->mBitmapLeft = skiaGlyph.fLeft; 434 glyph->mBitmapTop = skiaGlyph.fTop; 435 glyph->mLsbDelta = skiaGlyph.fLsbDelta; 436 glyph->mRsbDelta = skiaGlyph.fRsbDelta; 437 438 uint32_t startX = 0; 439 uint32_t startY = 0; 440 441 // Get the bitmap for the glyph 442 if (!skiaGlyph.fImage) { 443 skiaGlyphCache->findImage(skiaGlyph); 444 } 445 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching); 446 447 if (!glyph->mIsValid) { 448 return; 449 } 450 451 uint32_t endX = startX + skiaGlyph.fWidth; 452 uint32_t endY = startY + skiaGlyph.fHeight; 453 454 glyph->mStartX = startX; 455 glyph->mStartY = startY; 456 glyph->mBitmapWidth = skiaGlyph.fWidth; 457 glyph->mBitmapHeight = skiaGlyph.fHeight; 458 459 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0; 460 if (!empty) { 461 uint32_t cacheWidth = glyph->mCacheTexture->getWidth(); 462 uint32_t cacheHeight = glyph->mCacheTexture->getHeight(); 463 464 glyph->mBitmapMinU = startX / (float) cacheWidth; 465 glyph->mBitmapMinV = startY / (float) cacheHeight; 466 glyph->mBitmapMaxU = endX / (float) cacheWidth; 467 glyph->mBitmapMaxV = endY / (float) cacheHeight; 468 469 mState->setTextureDirty(); 470 } 471} 472 473CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) { 474 CachedGlyphInfo* newGlyph = new CachedGlyphInfo(); 475 mCachedGlyphs.add(glyph, newGlyph); 476 477 SkAutoGlyphCache autoCache(*paint, NULL, &mDescription.mLookupTransform); 478 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), glyph); 479 newGlyph->mIsValid = false; 480 newGlyph->mGlyphIndex = skiaGlyph.fID; 481 482 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), newGlyph, precaching); 483 484 return newGlyph; 485} 486 487Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) { 488 FontDescription description(paint, matrix); 489 Font* font = state->mActiveFonts.get(description); 490 491 if (!font) { 492 font = new Font(state, description); 493 state->mActiveFonts.put(description, font); 494 } 495 font->mIdentityTransform = matrix.isIdentity(); 496 497 return font; 498} 499 500}; // namespace uirenderer 501}; // namespace android 502