Font.cpp revision 2c423b22b42269c0652dff4155707cec823fbfc1
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 mDeviceProperties = SkDeviceProperties::Make(SkDeviceProperties::Geometry::MakeDefault(), 1.0f); 46} 47 48Font::FontDescription::FontDescription(const SkPaint* paint, const mat4& matrix) { 49 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(); 59 mAntiAliasing = paint->isAntiAlias(); 60 mHinting = paint->getHinting(); 61 mLookupTransform.reset(); 62 mLookupTransform[SkMatrix::kMScaleX] = roundf(fmaxf(1.0f, matrix[mat4::kScaleX])); 63 mLookupTransform[SkMatrix::kMScaleY] = roundf(fmaxf(1.0f, matrix[mat4::kScaleY])); 64 if (!mLookupTransform.invert(&mInverseLookupTransform)) { 65 ALOGW("Could not query the inverse lookup transform for this font"); 66 } 67} 68 69Font::~Font() { 70 mState->removeFont(this); 71 72 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { 73 delete mCachedGlyphs.valueAt(i); 74 } 75} 76 77hash_t Font::FontDescription::hash() const { 78 uint32_t hash = JenkinsHashMix(0, mFontId); 79 hash = JenkinsHashMix(hash, android::hash_type(mFontSize)); 80 hash = JenkinsHashMix(hash, android::hash_type(mFlags)); 81 hash = JenkinsHashMix(hash, android::hash_type(mItalicStyle)); 82 hash = JenkinsHashMix(hash, android::hash_type(mScaleX)); 83 hash = JenkinsHashMix(hash, android::hash_type(mStyle)); 84 hash = JenkinsHashMix(hash, android::hash_type(mStrokeWidth)); 85 hash = JenkinsHashMix(hash, int(mAntiAliasing)); 86 hash = JenkinsHashMix(hash, android::hash_type(mHinting)); 87 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleX])); 88 hash = JenkinsHashMix(hash, android::hash_type(mLookupTransform[SkMatrix::kMScaleY])); 89 return JenkinsHashWhiten(hash); 90} 91 92int Font::FontDescription::compare(const Font::FontDescription& lhs, 93 const Font::FontDescription& rhs) { 94 int deltaInt = int(lhs.mFontId) - int(rhs.mFontId); 95 if (deltaInt != 0) return deltaInt; 96 97 if (lhs.mFontSize < rhs.mFontSize) return -1; 98 if (lhs.mFontSize > rhs.mFontSize) return +1; 99 100 if (lhs.mItalicStyle < rhs.mItalicStyle) return -1; 101 if (lhs.mItalicStyle > rhs.mItalicStyle) return +1; 102 103 deltaInt = int(lhs.mFlags) - int(rhs.mFlags); 104 if (deltaInt != 0) return deltaInt; 105 106 if (lhs.mScaleX < rhs.mScaleX) return -1; 107 if (lhs.mScaleX > rhs.mScaleX) return +1; 108 109 deltaInt = int(lhs.mStyle) - int(rhs.mStyle); 110 if (deltaInt != 0) return deltaInt; 111 112 if (lhs.mStrokeWidth < rhs.mStrokeWidth) return -1; 113 if (lhs.mStrokeWidth > rhs.mStrokeWidth) return +1; 114 115 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing); 116 if (deltaInt != 0) return deltaInt; 117 118 deltaInt = int(lhs.mHinting) - int(rhs.mHinting); 119 if (deltaInt != 0) return deltaInt; 120 121 if (lhs.mLookupTransform[SkMatrix::kMScaleX] < 122 rhs.mLookupTransform[SkMatrix::kMScaleX]) return -1; 123 if (lhs.mLookupTransform[SkMatrix::kMScaleX] > 124 rhs.mLookupTransform[SkMatrix::kMScaleX]) return +1; 125 126 if (lhs.mLookupTransform[SkMatrix::kMScaleY] < 127 rhs.mLookupTransform[SkMatrix::kMScaleY]) return -1; 128 if (lhs.mLookupTransform[SkMatrix::kMScaleY] > 129 rhs.mLookupTransform[SkMatrix::kMScaleY]) return +1; 130 131 return 0; 132} 133 134void Font::invalidateTextureCache(CacheTexture* cacheTexture) { 135 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { 136 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueAt(i); 137 if (!cacheTexture || cachedGlyph->mCacheTexture == cacheTexture) { 138 cachedGlyph->mIsValid = false; 139 } 140 } 141} 142 143void 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 165void Font::drawCachedGlyph(CachedGlyphInfo* glyph, int x, int y, 166 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { 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; 172 173 float u1 = glyph->mBitmapMinU; 174 float u2 = glyph->mBitmapMaxU; 175 float v1 = glyph->mBitmapMinV; 176 float v2 = glyph->mBitmapMaxV; 177 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 184void Font::drawCachedGlyphTransformed(CachedGlyphInfo* glyph, int x, int y, 185 uint8_t* bitmap, uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* pos) { 186 SkPoint p[4]; 187 p[0].iset(glyph->mBitmapLeft, glyph->mBitmapTop + glyph->mBitmapHeight); 188 p[1].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop + glyph->mBitmapHeight); 189 p[2].iset(glyph->mBitmapLeft + glyph->mBitmapWidth, glyph->mBitmapTop); 190 p[3].iset(glyph->mBitmapLeft, glyph->mBitmapTop); 191 192 mDescription.mInverseLookupTransform.mapPoints(p, 4); 193 194 p[0].offset(x, y); 195 p[1].offset(x, y); 196 p[2].offset(x, y); 197 p[3].offset(x, y); 198 199 float u1 = glyph->mBitmapMinU; 200 float u2 = glyph->mBitmapMaxU; 201 float v1 = glyph->mBitmapMinV; 202 float v2 = glyph->mBitmapMaxV; 203 204 mState->appendRotatedMeshQuad( 205 p[0].x(), p[0].y(), u1, v2, 206 p[1].x(), p[1].y(), u2, v2, 207 p[2].x(), p[2].y(), u2, v1, 208 p[3].x(), p[3].y(), u1, v1, glyph->mCacheTexture); 209} 210 211void Font::drawCachedGlyphBitmap(CachedGlyphInfo* glyph, int x, int y, uint8_t* bitmap, 212 uint32_t bitmapWidth, uint32_t bitmapHeight, Rect* bounds, const float* pos) { 213 int dstX = x + glyph->mBitmapLeft; 214 int dstY = y + glyph->mBitmapTop; 215 216 CacheTexture* cacheTexture = glyph->mCacheTexture; 217 218 uint32_t cacheWidth = cacheTexture->getWidth(); 219 uint32_t startY = glyph->mStartY * cacheWidth; 220 uint32_t endY = startY + (glyph->mBitmapHeight * cacheWidth); 221 222 PixelBuffer* pixelBuffer = cacheTexture->getPixelBuffer(); 223 const uint8_t* cacheBuffer = pixelBuffer->map(); 224 225 for (uint32_t cacheY = startY, bitmapY = dstY * bitmapWidth; cacheY < endY; 226 cacheY += cacheWidth, bitmapY += bitmapWidth) { 227 memcpy(&bitmap[bitmapY + dstX], &cacheBuffer[cacheY + glyph->mStartX], glyph->mBitmapWidth); 228 } 229} 230 231void Font::drawCachedGlyph(CachedGlyphInfo* glyph, float x, float hOffset, float vOffset, 232 SkPathMeasure& measure, SkPoint* position, SkVector* tangent) { 233 const float halfWidth = glyph->mBitmapWidth * 0.5f; 234 const float height = glyph->mBitmapHeight; 235 236 vOffset += glyph->mBitmapTop + height; 237 238 SkPoint destination[4]; 239 bool ok = measure.getPosTan(x + hOffset + glyph->mBitmapLeft + halfWidth, position, tangent); 240 if (!ok) { 241 ALOGW("The path for drawTextOnPath is empty or null"); 242 } 243 244 // Move along the tangent and offset by the normal 245 destination[0].set(-tangent->fX * halfWidth - tangent->fY * vOffset, 246 -tangent->fY * halfWidth + tangent->fX * vOffset); 247 destination[1].set(tangent->fX * halfWidth - tangent->fY * vOffset, 248 tangent->fY * halfWidth + tangent->fX * vOffset); 249 destination[2].set(destination[1].fX + tangent->fY * height, 250 destination[1].fY - tangent->fX * height); 251 destination[3].set(destination[0].fX + tangent->fY * height, 252 destination[0].fY - tangent->fX * height); 253 254 const float u1 = glyph->mBitmapMinU; 255 const float u2 = glyph->mBitmapMaxU; 256 const float v1 = glyph->mBitmapMinV; 257 const float v2 = glyph->mBitmapMaxV; 258 259 mState->appendRotatedMeshQuad( 260 position->x() + destination[0].x(), 261 position->y() + destination[0].y(), u1, v2, 262 position->x() + destination[1].x(), 263 position->y() + destination[1].y(), u2, v2, 264 position->x() + destination[2].x(), 265 position->y() + destination[2].y(), u2, v1, 266 position->x() + destination[3].x(), 267 position->y() + destination[3].y(), u1, v1, 268 glyph->mCacheTexture); 269} 270 271CachedGlyphInfo* Font::getCachedGlyph(SkPaint* paint, glyph_t textUnit, bool precaching) { 272 CachedGlyphInfo* cachedGlyph = mCachedGlyphs.valueFor(textUnit); 273 if (cachedGlyph) { 274 // Is the glyph still in texture cache? 275 if (!cachedGlyph->mIsValid) { 276 SkAutoGlyphCache autoCache(*paint, &mDeviceProperties, &mDescription.mLookupTransform); 277 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), textUnit); 278 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), cachedGlyph, precaching); 279 } 280 } else { 281 cachedGlyph = cacheGlyph(paint, textUnit, precaching); 282 } 283 284 return cachedGlyph; 285} 286 287void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, 288 int numGlyphs, int x, int y, const float* positions) { 289 render(paint, text, start, len, numGlyphs, x, y, FRAMEBUFFER, NULL, 290 0, 0, NULL, positions); 291} 292 293void Font::render(SkPaint* paint, const char *text, uint32_t start, uint32_t len, 294 int numGlyphs, SkPath* path, float hOffset, float vOffset) { 295 if (numGlyphs == 0 || text == NULL || len == 0) { 296 return; 297 } 298 299 text += start; 300 301 int glyphsCount = 0; 302 SkFixed prevRsbDelta = 0; 303 304 float penX = 0.0f; 305 306 SkPoint position; 307 SkVector tangent; 308 309 SkPathMeasure measure(*path, false); 310 float pathLength = SkScalarToFloat(measure.getLength()); 311 312 if (paint->getTextAlign() != SkPaint::kLeft_Align) { 313 float textWidth = SkScalarToFloat(paint->measureText(text, len)); 314 float pathOffset = pathLength; 315 if (paint->getTextAlign() == SkPaint::kCenter_Align) { 316 textWidth *= 0.5f; 317 pathOffset *= 0.5f; 318 } 319 penX += pathOffset - textWidth; 320 } 321 322 while (glyphsCount < numGlyphs && penX < pathLength) { 323 glyph_t glyph = GET_GLYPH(text); 324 325 if (IS_END_OF_STRING(glyph)) { 326 break; 327 } 328 329 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph); 330 penX += SkFixedToFloat(AUTO_KERN(prevRsbDelta, cachedGlyph->mLsbDelta)); 331 prevRsbDelta = cachedGlyph->mRsbDelta; 332 333 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) { 334 drawCachedGlyph(cachedGlyph, penX, hOffset, vOffset, measure, &position, &tangent); 335 } 336 337 penX += SkFixedToFloat(cachedGlyph->mAdvanceX); 338 339 glyphsCount++; 340 } 341} 342 343void Font::measure(SkPaint* paint, const char* text, uint32_t start, uint32_t len, 344 int numGlyphs, Rect *bounds, const float* positions) { 345 if (bounds == NULL) { 346 ALOGE("No return rectangle provided to measure text"); 347 return; 348 } 349 bounds->set(1e6, -1e6, -1e6, 1e6); 350 render(paint, text, start, len, numGlyphs, 0, 0, MEASURE, NULL, 0, 0, bounds, positions); 351} 352 353void Font::precache(SkPaint* paint, const char* text, int numGlyphs) { 354 ATRACE_NAME("precacheText"); 355 356 if (numGlyphs == 0 || text == NULL) { 357 return; 358 } 359 360 int glyphsCount = 0; 361 while (glyphsCount < numGlyphs) { 362 glyph_t glyph = GET_GLYPH(text); 363 364 // Reached the end of the string 365 if (IS_END_OF_STRING(glyph)) { 366 break; 367 } 368 369 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph, true); 370 glyphsCount++; 371 } 372} 373 374void Font::render(SkPaint* paint, const char* text, uint32_t start, uint32_t len, 375 int numGlyphs, int x, int y, RenderMode mode, uint8_t *bitmap, 376 uint32_t bitmapW, uint32_t bitmapH, Rect* bounds, const float* positions) { 377 if (numGlyphs == 0 || text == NULL || len == 0) { 378 return; 379 } 380 381 static RenderGlyph gRenderGlyph[] = { 382 &android::uirenderer::Font::drawCachedGlyph, 383 &android::uirenderer::Font::drawCachedGlyphTransformed, 384 &android::uirenderer::Font::drawCachedGlyphBitmap, 385 &android::uirenderer::Font::drawCachedGlyphBitmap, 386 &android::uirenderer::Font::measureCachedGlyph, 387 &android::uirenderer::Font::measureCachedGlyph 388 }; 389 RenderGlyph render = gRenderGlyph[(mode << 1) + !mIdentityTransform]; 390 391 text += start; 392 int glyphsCount = 0; 393 394 const SkPaint::Align align = paint->getTextAlign(); 395 396 while (glyphsCount < numGlyphs) { 397 glyph_t glyph = GET_GLYPH(text); 398 399 // Reached the end of the string 400 if (IS_END_OF_STRING(glyph)) { 401 break; 402 } 403 404 CachedGlyphInfo* cachedGlyph = getCachedGlyph(paint, glyph); 405 406 // If it's still not valid, we couldn't cache it, so we shouldn't 407 // draw garbage; also skip empty glyphs (spaces) 408 if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) { 409 int penX = x + (int) roundf(positions[(glyphsCount << 1)]); 410 int penY = y + (int) roundf(positions[(glyphsCount << 1) + 1]); 411 412 (*this.*render)(cachedGlyph, penX, penY, 413 bitmap, bitmapW, bitmapH, bounds, positions); 414 } 415 416 glyphsCount++; 417 } 418} 419 420void Font::updateGlyphCache(SkPaint* paint, const SkGlyph& skiaGlyph, SkGlyphCache* skiaGlyphCache, 421 CachedGlyphInfo* glyph, bool precaching) { 422 glyph->mAdvanceX = skiaGlyph.fAdvanceX; 423 glyph->mAdvanceY = skiaGlyph.fAdvanceY; 424 glyph->mBitmapLeft = skiaGlyph.fLeft; 425 glyph->mBitmapTop = skiaGlyph.fTop; 426 glyph->mLsbDelta = skiaGlyph.fLsbDelta; 427 glyph->mRsbDelta = skiaGlyph.fRsbDelta; 428 429 uint32_t startX = 0; 430 uint32_t startY = 0; 431 432 // Get the bitmap for the glyph 433 if (!skiaGlyph.fImage) { 434 skiaGlyphCache->findImage(skiaGlyph); 435 } 436 mState->cacheBitmap(skiaGlyph, glyph, &startX, &startY, precaching); 437 438 if (!glyph->mIsValid) { 439 return; 440 } 441 442 uint32_t endX = startX + skiaGlyph.fWidth; 443 uint32_t endY = startY + skiaGlyph.fHeight; 444 445 glyph->mStartX = startX; 446 glyph->mStartY = startY; 447 glyph->mBitmapWidth = skiaGlyph.fWidth; 448 glyph->mBitmapHeight = skiaGlyph.fHeight; 449 450 bool empty = skiaGlyph.fWidth == 0 || skiaGlyph.fHeight == 0; 451 if (!empty) { 452 uint32_t cacheWidth = glyph->mCacheTexture->getWidth(); 453 uint32_t cacheHeight = glyph->mCacheTexture->getHeight(); 454 455 glyph->mBitmapMinU = startX / (float) cacheWidth; 456 glyph->mBitmapMinV = startY / (float) cacheHeight; 457 glyph->mBitmapMaxU = endX / (float) cacheWidth; 458 glyph->mBitmapMaxV = endY / (float) cacheHeight; 459 460 mState->setTextureDirty(); 461 } 462} 463 464CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) { 465 CachedGlyphInfo* newGlyph = new CachedGlyphInfo(); 466 mCachedGlyphs.add(glyph, newGlyph); 467 468 SkAutoGlyphCache autoCache(*paint, &mDeviceProperties, &mDescription.mLookupTransform); 469 const SkGlyph& skiaGlyph = GET_METRICS(autoCache.getCache(), glyph); 470 newGlyph->mIsValid = false; 471 newGlyph->mGlyphIndex = skiaGlyph.fID; 472 473 updateGlyphCache(paint, skiaGlyph, autoCache.getCache(), newGlyph, precaching); 474 475 return newGlyph; 476} 477 478Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) { 479 FontDescription description(paint, matrix); 480 Font* font = state->mActiveFonts.get(description); 481 482 if (!font) { 483 font = new Font(state, description); 484 state->mActiveFonts.put(description, font); 485 } 486 font->mIdentityTransform = matrix.isIdentity(); 487 488 return font; 489} 490 491}; // namespace uirenderer 492}; // namespace android 493