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