Font.cpp revision c74f45a334f0e3725c23cdd270cbcb0efac4ea75
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 19#include <cutils/compiler.h> 20 21#include <utils/JenkinsHash.h> 22 23#include <SkGlyph.h> 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 32namespace android { 33namespace uirenderer { 34 35/////////////////////////////////////////////////////////////////////////////// 36// Font 37/////////////////////////////////////////////////////////////////////////////// 38 39Font::Font(FontRenderer* state, const Font::FontDescription& desc) : 40 mState(state), mDescription(desc) { 41} 42 43Font::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(); 54 mAntiAliasing = paint->isAntiAlias(); 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]; 59} 60 61Font::~Font() { 62 mState->removeFont(this); 63 64 for (uint32_t i = 0; i < mCachedGlyphs.size(); i++) { 65 delete mCachedGlyphs.valueAt(i); 66 } 67} 68 69hash_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)); 77 hash = JenkinsHashMix(hash, int(mAntiAliasing)); 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])); 82 return JenkinsHashWhiten(hash); 83} 84 85int 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 108 deltaInt = int(lhs.mAntiAliasing) - int(rhs.mAntiAliasing); 109 if (deltaInt != 0) return deltaInt; 110 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 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::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 192 CacheTexture* cacheTexture = glyph->mCacheTexture; 193 uint32_t cacheWidth = cacheTexture->getWidth(); 194 const uint8_t* cacheBuffer = cacheTexture->getTexture(); 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++) { 200 uint8_t tempCol = cacheBuffer[cacheY * cacheWidth + cacheX]; 201 bitmap[bY * bitmapW + bX] = tempCol; 202 } 203 } 204} 205 206void 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]; 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 } 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 246CachedGlyphInfo* 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); 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 } 257 } else { 258 cachedGlyph = cacheGlyph(paint, textUnit, precaching); 259 } 260 261 return cachedGlyph; 262} 263 264void 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 270void 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 320void 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 330void 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 351void 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 368 const SkPaint::Align align = paint->getTextAlign(); 369 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); 379 380 // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage 381 if (cachedGlyph->mIsValid) { 382 float penX = x + positions[(glyphsCount << 1)]; 383 float penY = y + positions[(glyphsCount << 1) + 1]; 384 385 if (!mTransform.isIdentity()) { 386 mTransform.mapPoint(penX, penY); 387 } 388 389 (*this.*render)(cachedGlyph, roundf(penX), roundf(penY), 390 bitmap, bitmapW, bitmapH, bounds, positions); 391 } 392 393 glyphsCount++; 394 } 395} 396 397void 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 410 if (!skiaGlyph.fImage) { 411 paint->findImage(skiaGlyph, &mDescription.mLookupTransform); 412 } 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 427 uint32_t cacheWidth = glyph->mCacheTexture->getWidth(); 428 uint32_t cacheHeight = glyph->mCacheTexture->getHeight(); 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 435 mState->setTextureDirty(); 436} 437 438CachedGlyphInfo* Font::cacheGlyph(SkPaint* paint, glyph_t glyph, bool precaching) { 439 CachedGlyphInfo* newGlyph = new CachedGlyphInfo(); 440 mCachedGlyphs.add(glyph, newGlyph); 441 442 const SkGlyph& skiaGlyph = GET_METRICS(paint, glyph, &mDescription.mLookupTransform); 443 newGlyph->mGlyphIndex = skiaGlyph.fID; 444 newGlyph->mIsValid = false; 445 446 updateGlyphCache(paint, skiaGlyph, newGlyph, precaching); 447 448 return newGlyph; 449} 450 451Font* Font::create(FontRenderer* state, const SkPaint* paint, const mat4& matrix) { 452 FontDescription description(paint, matrix); 453 Font* font = state->mActiveFonts.get(description); 454 455 if (font) { 456 font->mTransform.load(matrix); 457 return font; 458 } 459 460 Font* newFont = new Font(state, description); 461 state->mActiveFonts.put(description, newFont); 462 return newFont; 463} 464 465}; // namespace uirenderer 466}; // namespace android 467