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