Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Fabrice Di Meglio | 163268b | 2011-09-07 18:12:11 -0700 | [diff] [blame] | 17 | #define LOG_TAG "TextLayoutCache" |
| 18 | |
Raph Levien | d98efca | 2012-10-25 23:17:33 -0700 | [diff] [blame] | 19 | #include <utils/JenkinsHash.h> |
| 20 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 21 | #include "TextLayoutCache.h" |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 22 | #include "TextLayout.h" |
Victoria Lease | 43b692d | 2013-12-03 15:02:28 -0800 | [diff] [blame] | 23 | #include "SkGlyphCache.h" |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 24 | #include "SkTypeface_android.h" |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 25 | #include "HarfBuzzNGFaceSkia.h" |
Fabrice Di Meglio | 902a5b3 | 2011-12-08 18:59:14 -0800 | [diff] [blame] | 26 | #include <unicode/unistr.h> |
Fabrice Di Meglio | 3632b7f | 2012-04-24 19:55:18 -0700 | [diff] [blame] | 27 | #include <unicode/uchar.h> |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 28 | #include <hb-icu.h> |
Fabrice Di Meglio | 5de5b1a | 2011-08-09 14:37:22 -0700 | [diff] [blame] | 29 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Fabrice Di Meglio | 163268b | 2011-09-07 18:12:11 -0700 | [diff] [blame] | 32 | //-------------------------------------------------------------------------------------------------- |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 33 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 34 | ANDROID_SINGLETON_STATIC_INSTANCE(TextLayoutEngine); |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 35 | |
Fabrice Di Meglio | 163268b | 2011-09-07 18:12:11 -0700 | [diff] [blame] | 36 | //-------------------------------------------------------------------------------------------------- |
| 37 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 38 | TextLayoutCache::TextLayoutCache(TextLayoutShaper* shaper) : |
| 39 | mShaper(shaper), |
Raph Levien | d98efca | 2012-10-25 23:17:33 -0700 | [diff] [blame] | 40 | mCache(LruCache<TextLayoutCacheKey, sp<TextLayoutValue> >::kUnlimitedCapacity), |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 41 | mSize(0), mMaxSize(MB(DEFAULT_TEXT_LAYOUT_CACHE_SIZE_IN_MB)), |
| 42 | mCacheHitCount(0), mNanosecondsSaved(0) { |
| 43 | init(); |
| 44 | } |
| 45 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 46 | TextLayoutCache::~TextLayoutCache() { |
| 47 | mCache.clear(); |
| 48 | } |
| 49 | |
| 50 | void TextLayoutCache::init() { |
| 51 | mCache.setOnEntryRemovedListener(this); |
| 52 | |
| 53 | mDebugLevel = readRtlDebugLevel(); |
| 54 | mDebugEnabled = mDebugLevel & kRtlDebugCaches; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 55 | ALOGD("Using debug level = %d - Debug Enabled = %d", mDebugLevel, mDebugEnabled); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 56 | |
| 57 | mCacheStartTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 58 | |
| 59 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 60 | ALOGD("Initialization is done - Start time = %lld", mCacheStartTime); |
Fabrice Di Meglio | 9f82b58 | 2011-03-08 12:02:59 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Fabrice Di Meglio | 163268b | 2011-09-07 18:12:11 -0700 | [diff] [blame] | 63 | mInitialized = true; |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Callbacks |
| 68 | */ |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 69 | void TextLayoutCache::operator()(TextLayoutCacheKey& text, sp<TextLayoutValue>& desc) { |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 70 | size_t totalSizeToDelete = text.getSize() + desc->getSize(); |
| 71 | mSize -= totalSizeToDelete; |
| 72 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 73 | ALOGD("Cache value %p deleted, size = %d", desc.get(), totalSizeToDelete); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Cache clearing |
| 79 | */ |
Raph Levien | 13ba4e4 | 2012-09-12 15:15:51 -0700 | [diff] [blame] | 80 | void TextLayoutCache::purgeCaches() { |
| 81 | AutoMutex _l(mLock); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 82 | mCache.clear(); |
Raph Levien | 13ba4e4 | 2012-09-12 15:15:51 -0700 | [diff] [blame] | 83 | mShaper->purgeCaches(); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Caching |
| 88 | */ |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 89 | sp<TextLayoutValue> TextLayoutCache::getValue(const SkPaint* paint, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 90 | const jchar* text, jint start, jint count, jint contextCount, jint dirFlags) { |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 91 | AutoMutex _l(mLock); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 92 | nsecs_t startTime = 0; |
| 93 | if (mDebugEnabled) { |
| 94 | startTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 95 | } |
| 96 | |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 97 | // Create the key |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 98 | TextLayoutCacheKey key(paint, text, start, count, contextCount, dirFlags); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 99 | |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 100 | // Get value from cache if possible |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 101 | sp<TextLayoutValue> value = mCache.get(key); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 102 | |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 103 | // Value not found for the key, we need to add a new value in the cache |
| 104 | if (value == NULL) { |
Fabrice Di Meglio | 010d5c4 | 2011-04-21 15:33:50 -0700 | [diff] [blame] | 105 | if (mDebugEnabled) { |
| 106 | startTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 107 | } |
| 108 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 109 | value = new TextLayoutValue(contextCount); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 110 | |
| 111 | // Compute advances and store them |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 112 | mShaper->computeValues(value.get(), paint, |
Raph Levien | 832815c | 2012-10-02 10:30:41 -0700 | [diff] [blame] | 113 | reinterpret_cast<const UChar*>(key.getText()), start, count, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 114 | size_t(contextCount), int(dirFlags)); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 115 | |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 116 | if (mDebugEnabled) { |
| 117 | value->setElapsedTime(systemTime(SYSTEM_TIME_MONOTONIC) - startTime); |
| 118 | } |
Fabrice Di Meglio | 010d5c4 | 2011-04-21 15:33:50 -0700 | [diff] [blame] | 119 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 120 | // Don't bother to add in the cache if the entry is too big |
Fabrice Di Meglio | 1de9e7a | 2011-04-05 13:43:18 -0700 | [diff] [blame] | 121 | size_t size = key.getSize() + value->getSize(); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 122 | if (size <= mMaxSize) { |
| 123 | // Cleanup to make some room if needed |
| 124 | if (mSize + size > mMaxSize) { |
| 125 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 126 | ALOGD("Need to clean some entries for making some room for a new entry"); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 127 | } |
| 128 | while (mSize + size > mMaxSize) { |
| 129 | // This will call the callback |
Jeff Brown | d9e688c | 2011-11-11 15:40:13 -0800 | [diff] [blame] | 130 | bool removedOne = mCache.removeOldest(); |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 131 | LOG_ALWAYS_FATAL_IF(!removedOne, "The cache is non-empty but we " |
| 132 | "failed to remove the oldest entry. " |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 133 | "mSize = %u, size = %u, mMaxSize = %u, mCache.size() = %u", |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 134 | mSize, size, mMaxSize, mCache.size()); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | // Update current cache size |
| 139 | mSize += size; |
| 140 | |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 141 | bool putOne = mCache.put(key, value); |
| 142 | LOG_ALWAYS_FATAL_IF(!putOne, "Failed to put an entry into the cache. " |
| 143 | "This indicates that the cache already has an entry with the " |
| 144 | "same key but it should not since we checked earlier!" |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 145 | " - start = %d, count = %d, contextCount = %d - Text = '%s'", |
Raph Levien | 832815c | 2012-10-02 10:30:41 -0700 | [diff] [blame] | 146 | start, count, contextCount, String8(key.getText() + start, count).string()); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 147 | |
| 148 | if (mDebugEnabled) { |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 149 | nsecs_t totalTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 150 | ALOGD("CACHE MISS: Added entry %p " |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 151 | "with start = %d, count = %d, contextCount = %d, " |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 152 | "entry size %d bytes, remaining space %d bytes" |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 153 | " - Compute time %0.6f ms - Put time %0.6f ms - Text = '%s'", |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 154 | value.get(), start, count, contextCount, size, mMaxSize - mSize, |
| 155 | value->getElapsedTime() * 0.000001f, |
| 156 | (totalTime - value->getElapsedTime()) * 0.000001f, |
Raph Levien | 832815c | 2012-10-02 10:30:41 -0700 | [diff] [blame] | 157 | String8(key.getText() + start, count).string()); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 158 | } |
| 159 | } else { |
| 160 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 161 | ALOGD("CACHE MISS: Calculated but not storing entry because it is too big " |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 162 | "with start = %d, count = %d, contextCount = %d, " |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 163 | "entry size %d bytes, remaining space %d bytes" |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 164 | " - Compute time %0.6f ms - Text = '%s'", |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 165 | start, count, contextCount, size, mMaxSize - mSize, |
| 166 | value->getElapsedTime() * 0.000001f, |
Raph Levien | 832815c | 2012-10-02 10:30:41 -0700 | [diff] [blame] | 167 | String8(key.getText() + start, count).string()); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 168 | } |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 169 | } |
| 170 | } else { |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 171 | // This is a cache hit, just log timestamp and user infos |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 172 | if (mDebugEnabled) { |
| 173 | nsecs_t elapsedTimeThruCacheGet = systemTime(SYSTEM_TIME_MONOTONIC) - startTime; |
Fabrice Di Meglio | 1de9e7a | 2011-04-05 13:43:18 -0700 | [diff] [blame] | 174 | mNanosecondsSaved += (value->getElapsedTime() - elapsedTimeThruCacheGet); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 175 | ++mCacheHitCount; |
| 176 | |
Fabrice Di Meglio | 1de9e7a | 2011-04-05 13:43:18 -0700 | [diff] [blame] | 177 | if (value->getElapsedTime() > 0) { |
| 178 | float deltaPercent = 100 * ((value->getElapsedTime() - elapsedTimeThruCacheGet) |
| 179 | / ((float)value->getElapsedTime())); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 180 | ALOGD("CACHE HIT #%d with start = %d, count = %d, contextCount = %d" |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 181 | "- Compute time %0.6f ms - " |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 182 | "Cache get time %0.6f ms - Gain in percent: %2.2f - Text = '%s'", |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 183 | mCacheHitCount, start, count, contextCount, |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 184 | value->getElapsedTime() * 0.000001f, |
| 185 | elapsedTimeThruCacheGet * 0.000001f, |
| 186 | deltaPercent, |
Raph Levien | 832815c | 2012-10-02 10:30:41 -0700 | [diff] [blame] | 187 | String8(key.getText() + start, count).string()); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 188 | } |
| 189 | if (mCacheHitCount % DEFAULT_DUMP_STATS_CACHE_HIT_INTERVAL == 0) { |
| 190 | dumpCacheStats(); |
| 191 | } |
| 192 | } |
| 193 | } |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 194 | return value; |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void TextLayoutCache::dumpCacheStats() { |
| 198 | float remainingPercent = 100 * ((mMaxSize - mSize) / ((float)mMaxSize)); |
| 199 | float timeRunningInSec = (systemTime(SYSTEM_TIME_MONOTONIC) - mCacheStartTime) / 1000000000; |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 200 | |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 201 | size_t cacheSize = mCache.size(); |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 202 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 203 | ALOGD("------------------------------------------------"); |
| 204 | ALOGD("Cache stats"); |
| 205 | ALOGD("------------------------------------------------"); |
| 206 | ALOGD("pid : %d", getpid()); |
| 207 | ALOGD("running : %.0f seconds", timeRunningInSec); |
| 208 | ALOGD("entries : %d", cacheSize); |
| 209 | ALOGD("max size : %d bytes", mMaxSize); |
Raph Levien | d98efca | 2012-10-25 23:17:33 -0700 | [diff] [blame] | 210 | ALOGD("used : %d bytes according to mSize", mSize); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 211 | ALOGD("remaining : %d bytes or %2.2f percent", mMaxSize - mSize, remainingPercent); |
| 212 | ALOGD("hits : %d", mCacheHitCount); |
| 213 | ALOGD("saved : %0.6f ms", mNanosecondsSaved * 0.000001f); |
| 214 | ALOGD("------------------------------------------------"); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 217 | /** |
| 218 | * TextLayoutCacheKey |
| 219 | */ |
Raph Levien | 832815c | 2012-10-02 10:30:41 -0700 | [diff] [blame] | 220 | TextLayoutCacheKey::TextLayoutCacheKey(): start(0), count(0), contextCount(0), |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 221 | dirFlags(0), typeface(NULL), textSize(0), textSkewX(0), textScaleX(0), flags(0), |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 222 | hinting(SkPaint::kNo_Hinting) { |
| 223 | paintOpts.setUseFontFallbacks(true); |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 226 | TextLayoutCacheKey::TextLayoutCacheKey(const SkPaint* paint, const UChar* text, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 227 | size_t start, size_t count, size_t contextCount, int dirFlags) : |
| 228 | start(start), count(count), contextCount(contextCount), |
| 229 | dirFlags(dirFlags) { |
Raph Levien | 832815c | 2012-10-02 10:30:41 -0700 | [diff] [blame] | 230 | textCopy.setTo(text, contextCount); |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 231 | typeface = paint->getTypeface(); |
| 232 | textSize = paint->getTextSize(); |
| 233 | textSkewX = paint->getTextSkewX(); |
| 234 | textScaleX = paint->getTextScaleX(); |
| 235 | flags = paint->getFlags(); |
| 236 | hinting = paint->getHinting(); |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 237 | paintOpts = paint->getPaintOptionsAndroid(); |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Fabrice Di Meglio | e74fef3 | 2011-09-18 14:30:21 -0700 | [diff] [blame] | 240 | TextLayoutCacheKey::TextLayoutCacheKey(const TextLayoutCacheKey& other) : |
Fabrice Di Meglio | e74fef3 | 2011-09-18 14:30:21 -0700 | [diff] [blame] | 241 | textCopy(other.textCopy), |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 242 | start(other.start), |
Jeff Brown | 7aac297 | 2011-09-19 16:17:58 -0700 | [diff] [blame] | 243 | count(other.count), |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 244 | contextCount(other.contextCount), |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 245 | dirFlags(other.dirFlags), |
Fabrice Di Meglio | e74fef3 | 2011-09-18 14:30:21 -0700 | [diff] [blame] | 246 | typeface(other.typeface), |
| 247 | textSize(other.textSize), |
| 248 | textSkewX(other.textSkewX), |
| 249 | textScaleX(other.textScaleX), |
| 250 | flags(other.flags), |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 251 | hinting(other.hinting), |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 252 | paintOpts(other.paintOpts) { |
Fabrice Di Meglio | e74fef3 | 2011-09-18 14:30:21 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Fabrice Di Meglio | 717060b | 2011-09-27 15:53:42 -0700 | [diff] [blame] | 255 | int TextLayoutCacheKey::compare(const TextLayoutCacheKey& lhs, const TextLayoutCacheKey& rhs) { |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 256 | int deltaInt = lhs.start - rhs.start; |
| 257 | if (deltaInt != 0) return (deltaInt); |
| 258 | |
| 259 | deltaInt = lhs.count - rhs.count; |
| 260 | if (deltaInt != 0) return (deltaInt); |
| 261 | |
| 262 | deltaInt = lhs.contextCount - rhs.contextCount; |
Fabrice Di Meglio | 717060b | 2011-09-27 15:53:42 -0700 | [diff] [blame] | 263 | if (deltaInt != 0) return (deltaInt); |
| 264 | |
| 265 | if (lhs.typeface < rhs.typeface) return -1; |
| 266 | if (lhs.typeface > rhs.typeface) return +1; |
| 267 | |
| 268 | if (lhs.textSize < rhs.textSize) return -1; |
| 269 | if (lhs.textSize > rhs.textSize) return +1; |
| 270 | |
| 271 | if (lhs.textSkewX < rhs.textSkewX) return -1; |
| 272 | if (lhs.textSkewX > rhs.textSkewX) return +1; |
| 273 | |
| 274 | if (lhs.textScaleX < rhs.textScaleX) return -1; |
| 275 | if (lhs.textScaleX > rhs.textScaleX) return +1; |
| 276 | |
| 277 | deltaInt = lhs.flags - rhs.flags; |
| 278 | if (deltaInt != 0) return (deltaInt); |
| 279 | |
| 280 | deltaInt = lhs.hinting - rhs.hinting; |
| 281 | if (deltaInt != 0) return (deltaInt); |
| 282 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 283 | deltaInt = lhs.dirFlags - rhs.dirFlags; |
| 284 | if (deltaInt) return (deltaInt); |
| 285 | |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 286 | if (lhs.paintOpts != rhs.paintOpts) |
| 287 | return memcmp(&lhs.paintOpts, &rhs.paintOpts, sizeof(SkPaintOptionsAndroid)); |
Billy Hewlett | ac1cbaf | 2012-07-18 09:51:45 -0700 | [diff] [blame] | 288 | |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 289 | return memcmp(lhs.getText(), rhs.getText(), lhs.contextCount * sizeof(UChar)); |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Jeff Brown | 06daa7b | 2011-11-11 15:14:56 -0800 | [diff] [blame] | 292 | size_t TextLayoutCacheKey::getSize() const { |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 293 | return sizeof(TextLayoutCacheKey) + sizeof(UChar) * contextCount; |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Raph Levien | d98efca | 2012-10-25 23:17:33 -0700 | [diff] [blame] | 296 | hash_t TextLayoutCacheKey::hash() const { |
| 297 | uint32_t hash = JenkinsHashMix(0, start); |
| 298 | hash = JenkinsHashMix(hash, count); |
| 299 | /* contextCount not needed because it's included in text, below */ |
| 300 | hash = JenkinsHashMix(hash, hash_type(typeface)); |
| 301 | hash = JenkinsHashMix(hash, hash_type(textSize)); |
| 302 | hash = JenkinsHashMix(hash, hash_type(textSkewX)); |
| 303 | hash = JenkinsHashMix(hash, hash_type(textScaleX)); |
| 304 | hash = JenkinsHashMix(hash, flags); |
| 305 | hash = JenkinsHashMix(hash, hinting); |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 306 | hash = JenkinsHashMix(hash, paintOpts.getFontVariant()); |
Raph Levien | d98efca | 2012-10-25 23:17:33 -0700 | [diff] [blame] | 307 | // Note: leaving out language is not problematic, as equality comparisons |
| 308 | // are still valid - the only bad thing that could happen is collisions. |
| 309 | hash = JenkinsHashMixShorts(hash, getText(), contextCount); |
| 310 | return JenkinsHashWhiten(hash); |
| 311 | } |
| 312 | |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 313 | /** |
| 314 | * TextLayoutCacheValue |
| 315 | */ |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 316 | TextLayoutValue::TextLayoutValue(size_t contextCount) : |
Fabrice Di Meglio | 4dd99e5 | 2011-09-19 10:47:10 -0700 | [diff] [blame] | 317 | mTotalAdvance(0), mElapsedTime(0) { |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 318 | mBounds.setEmpty(); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 319 | // Give a hint for advances and glyphs vectors size |
| 320 | mAdvances.setCapacity(contextCount); |
| 321 | mGlyphs.setCapacity(contextCount); |
Raph Levien | 2301d32 | 2012-07-17 16:39:49 -0700 | [diff] [blame] | 322 | mPos.setCapacity(contextCount * 2); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 325 | size_t TextLayoutValue::getSize() const { |
| 326 | return sizeof(TextLayoutValue) + sizeof(jfloat) * mAdvances.capacity() + |
Raph Levien | 2301d32 | 2012-07-17 16:39:49 -0700 | [diff] [blame] | 327 | sizeof(jchar) * mGlyphs.capacity() + sizeof(jfloat) * mPos.capacity(); |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 330 | void TextLayoutValue::setElapsedTime(uint32_t time) { |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 331 | mElapsedTime = time; |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 334 | uint32_t TextLayoutValue::getElapsedTime() { |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 335 | return mElapsedTime; |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 338 | TextLayoutShaper::TextLayoutShaper() { |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 339 | mBuffer = hb_buffer_create(); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Fabrice Di Meglio | 15cc68c | 2012-05-15 14:47:03 -0700 | [diff] [blame] | 342 | TextLayoutShaper::~TextLayoutShaper() { |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 343 | hb_buffer_destroy(mBuffer); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 344 | } |
| 345 | |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 346 | void TextLayoutShaper::computeValues(TextLayoutValue* value, const SkPaint* paint, |
| 347 | const UChar* chars, size_t start, size_t count, size_t contextCount, int dirFlags) { |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 348 | computeValues(paint, chars, start, count, contextCount, dirFlags, |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 349 | &value->mAdvances, &value->mTotalAdvance, &value->mBounds, |
| 350 | &value->mGlyphs, &value->mPos); |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 351 | #if DEBUG_ADVANCES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 352 | ALOGD("Advances - start = %d, count = %d, contextCount = %d, totalAdvance = %f", start, count, |
Fabrice Di Meglio | 5448f03 | 2011-12-02 15:56:19 -0800 | [diff] [blame] | 353 | contextCount, value->mTotalAdvance); |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 354 | #endif |
| 355 | } |
| 356 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 357 | void TextLayoutShaper::computeValues(const SkPaint* paint, const UChar* chars, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 358 | size_t start, size_t count, size_t contextCount, int dirFlags, |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 359 | Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance, SkRect* outBounds, |
Raph Levien | 2301d32 | 2012-07-17 16:39:49 -0700 | [diff] [blame] | 360 | Vector<jchar>* const outGlyphs, Vector<jfloat>* const outPos) { |
| 361 | *outTotalAdvance = 0; |
Jeff Brown | a03bded | 2011-12-05 17:36:16 -0800 | [diff] [blame] | 362 | if (!count) { |
Jeff Brown | a03bded | 2011-12-05 17:36:16 -0800 | [diff] [blame] | 363 | return; |
| 364 | } |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 365 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 366 | UBiDiLevel bidiReq = 0; |
| 367 | bool forceLTR = false; |
| 368 | bool forceRTL = false; |
| 369 | |
| 370 | switch (dirFlags & kBidi_Mask) { |
| 371 | case kBidi_LTR: bidiReq = 0; break; // no ICU constant, canonical LTR level |
| 372 | case kBidi_RTL: bidiReq = 1; break; // no ICU constant, canonical RTL level |
| 373 | case kBidi_Default_LTR: bidiReq = UBIDI_DEFAULT_LTR; break; |
| 374 | case kBidi_Default_RTL: bidiReq = UBIDI_DEFAULT_RTL; break; |
| 375 | case kBidi_Force_LTR: forceLTR = true; break; // every char is LTR |
| 376 | case kBidi_Force_RTL: forceRTL = true; break; // every char is RTL |
| 377 | } |
| 378 | |
Fabrice Di Meglio | 5beeda0 | 2011-10-24 17:05:55 -0700 | [diff] [blame] | 379 | bool useSingleRun = false; |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 380 | bool isRTL = forceRTL; |
| 381 | if (forceLTR || forceRTL) { |
| 382 | useSingleRun = true; |
| 383 | } else { |
| 384 | UBiDi* bidi = ubidi_open(); |
| 385 | if (bidi) { |
| 386 | UErrorCode status = U_ZERO_ERROR; |
Fabrice Di Meglio | 06732fd | 2011-04-18 15:44:53 -0700 | [diff] [blame] | 387 | #if DEBUG_GLYPHS |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 388 | ALOGD("******** ComputeValues -- start"); |
| 389 | ALOGD(" -- string = '%s'", String8(chars + start, count).string()); |
| 390 | ALOGD(" -- start = %d", start); |
| 391 | ALOGD(" -- count = %d", count); |
| 392 | ALOGD(" -- contextCount = %d", contextCount); |
| 393 | ALOGD(" -- bidiReq = %d", bidiReq); |
Fabrice Di Meglio | 06732fd | 2011-04-18 15:44:53 -0700 | [diff] [blame] | 394 | #endif |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 395 | ubidi_setPara(bidi, chars, contextCount, bidiReq, NULL, &status); |
| 396 | if (U_SUCCESS(status)) { |
| 397 | int paraDir = ubidi_getParaLevel(bidi) & kDirection_Mask; // 0 if ltr, 1 if rtl |
| 398 | ssize_t rc = ubidi_countRuns(bidi, &status); |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 399 | #if DEBUG_GLYPHS |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 400 | ALOGD(" -- dirFlags = %d", dirFlags); |
| 401 | ALOGD(" -- paraDir = %d", paraDir); |
| 402 | ALOGD(" -- run-count = %d", int(rc)); |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 403 | #endif |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 404 | if (U_SUCCESS(status) && rc == 1) { |
| 405 | // Normal case: one run, status is ok |
| 406 | isRTL = (paraDir == 1); |
| 407 | useSingleRun = true; |
| 408 | } else if (!U_SUCCESS(status) || rc < 1) { |
| 409 | ALOGW("Need to force to single run -- string = '%s'," |
| 410 | " status = %d, rc = %d", |
| 411 | String8(chars + start, count).string(), status, int(rc)); |
| 412 | isRTL = (paraDir == 1); |
| 413 | useSingleRun = true; |
| 414 | } else { |
| 415 | int32_t end = start + count; |
| 416 | for (size_t i = 0; i < size_t(rc); ++i) { |
| 417 | int32_t startRun = -1; |
| 418 | int32_t lengthRun = -1; |
| 419 | UBiDiDirection runDir = ubidi_getVisualRun(bidi, i, &startRun, &lengthRun); |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame] | 420 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 421 | if (startRun == -1 || lengthRun == -1) { |
| 422 | // Something went wrong when getting the visual run, need to clear |
| 423 | // already computed data before doing a single run pass |
| 424 | ALOGW("Visual run is not valid"); |
| 425 | outGlyphs->clear(); |
| 426 | outAdvances->clear(); |
| 427 | outPos->clear(); |
| 428 | *outTotalAdvance = 0; |
| 429 | isRTL = (paraDir == 1); |
| 430 | useSingleRun = true; |
| 431 | break; |
| 432 | } |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame] | 433 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 434 | if (startRun >= end) { |
| 435 | continue; |
| 436 | } |
| 437 | int32_t endRun = startRun + lengthRun; |
| 438 | if (endRun <= int32_t(start)) { |
| 439 | continue; |
| 440 | } |
| 441 | if (startRun < int32_t(start)) { |
| 442 | startRun = int32_t(start); |
| 443 | } |
| 444 | if (endRun > end) { |
| 445 | endRun = end; |
| 446 | } |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame] | 447 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 448 | lengthRun = endRun - startRun; |
| 449 | isRTL = (runDir == UBIDI_RTL); |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame] | 450 | #if DEBUG_GLYPHS |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 451 | ALOGD("Processing Bidi Run = %d -- run-start = %d, run-len = %d, isRTL = %d", |
| 452 | i, startRun, lengthRun, isRTL); |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame] | 453 | #endif |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 454 | computeRunValues(paint, chars, startRun, lengthRun, contextCount, isRTL, |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 455 | outAdvances, outTotalAdvance, outBounds, outGlyphs, outPos); |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame] | 456 | |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 457 | } |
Fabrice Di Meglio | 6d9fe5b | 2013-02-11 18:27:34 -0800 | [diff] [blame] | 458 | } |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 459 | } else { |
| 460 | ALOGW("Cannot set Para"); |
| 461 | useSingleRun = true; |
| 462 | isRTL = (bidiReq = 1) || (bidiReq = UBIDI_DEFAULT_RTL); |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 463 | } |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 464 | ubidi_close(bidi); |
Fabrice Di Meglio | 589e4e2 | 2011-04-25 16:48:51 -0700 | [diff] [blame] | 465 | } else { |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 466 | ALOGW("Cannot ubidi_open()"); |
Fabrice Di Meglio | 5beeda0 | 2011-10-24 17:05:55 -0700 | [diff] [blame] | 467 | useSingleRun = true; |
| 468 | isRTL = (bidiReq = 1) || (bidiReq = UBIDI_DEFAULT_RTL); |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 469 | } |
| 470 | } |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 471 | |
Fabrice Di Meglio | 5beeda0 | 2011-10-24 17:05:55 -0700 | [diff] [blame] | 472 | // Default single run case |
| 473 | if (useSingleRun){ |
| 474 | #if DEBUG_GLYPHS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 475 | ALOGD("Using a SINGLE BiDi Run " |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 476 | "-- run-start = %d, run-len = %d, isRTL = %d", start, count, isRTL); |
Fabrice Di Meglio | 5beeda0 | 2011-10-24 17:05:55 -0700 | [diff] [blame] | 477 | #endif |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 478 | computeRunValues(paint, chars, start, count, contextCount, isRTL, |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 479 | outAdvances, outTotalAdvance, outBounds, outGlyphs, outPos); |
Fabrice Di Meglio | 5beeda0 | 2011-10-24 17:05:55 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Fabrice Di Meglio | 589e4e2 | 2011-04-25 16:48:51 -0700 | [diff] [blame] | 482 | #if DEBUG_GLYPHS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 483 | ALOGD(" -- Total returned glyphs-count = %d", outGlyphs->size()); |
| 484 | ALOGD("******** ComputeValues -- end"); |
Fabrice Di Meglio | 589e4e2 | 2011-04-25 16:48:51 -0700 | [diff] [blame] | 485 | #endif |
Fabrice Di Meglio | 689e515 | 2011-04-13 16:07:37 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 488 | #define HB_IsHighSurrogate(ucs) \ |
| 489 | (((ucs) & 0xfc00) == 0xd800) |
| 490 | |
| 491 | #define HB_IsLowSurrogate(ucs) \ |
| 492 | (((ucs) & 0xfc00) == 0xdc00) |
| 493 | |
| 494 | #ifndef HB_SurrogateToUcs4 |
| 495 | #define HB_SurrogateToUcs4_(high, low) \ |
| 496 | (((hb_codepoint_t)(high))<<10) + (low) - 0x35fdc00; |
| 497 | #endif |
| 498 | |
| 499 | #define HB_InvalidCodePoint ~0u |
| 500 | |
| 501 | hb_codepoint_t |
| 502 | utf16_to_code_point(const uint16_t *chars, size_t len, ssize_t *iter) { |
| 503 | const uint16_t v = chars[(*iter)++]; |
| 504 | if (HB_IsHighSurrogate(v)) { |
| 505 | // surrogate pair |
| 506 | if (size_t(*iter) >= len) { |
| 507 | // the surrogate is incomplete. |
| 508 | return HB_InvalidCodePoint; |
| 509 | } |
| 510 | const uint16_t v2 = chars[(*iter)++]; |
| 511 | if (!HB_IsLowSurrogate(v2)) { |
| 512 | // invalidate surrogate pair. |
| 513 | (*iter)--; |
| 514 | return HB_InvalidCodePoint; |
| 515 | } |
| 516 | |
| 517 | return HB_SurrogateToUcs4(v, v2); |
| 518 | } |
| 519 | |
| 520 | if (HB_IsLowSurrogate(v)) { |
| 521 | // this isn't a valid code point |
| 522 | return HB_InvalidCodePoint; |
| 523 | } |
| 524 | |
| 525 | return v; |
| 526 | } |
| 527 | |
| 528 | hb_codepoint_t |
| 529 | utf16_to_code_point_prev(const uint16_t *chars, size_t len, ssize_t *iter) { |
| 530 | const uint16_t v = chars[(*iter)--]; |
| 531 | if (HB_IsLowSurrogate(v)) { |
| 532 | // surrogate pair |
| 533 | if (*iter < 0) { |
| 534 | // the surrogate is incomplete. |
| 535 | return HB_InvalidCodePoint; |
| 536 | } |
| 537 | const uint16_t v2 = chars[(*iter)--]; |
| 538 | if (!HB_IsHighSurrogate(v2)) { |
| 539 | // invalidate surrogate pair. |
| 540 | (*iter)++; |
| 541 | return HB_InvalidCodePoint; |
| 542 | } |
| 543 | |
| 544 | return HB_SurrogateToUcs4(v2, v); |
| 545 | } |
| 546 | |
| 547 | if (HB_IsHighSurrogate(v)) { |
| 548 | // this isn't a valid code point |
| 549 | return HB_InvalidCodePoint; |
| 550 | } |
| 551 | |
| 552 | return v; |
| 553 | } |
| 554 | |
| 555 | struct ScriptRun { |
| 556 | hb_script_t script; |
| 557 | size_t pos; |
| 558 | size_t length; |
| 559 | }; |
| 560 | |
| 561 | hb_script_t code_point_to_script(hb_codepoint_t codepoint) { |
| 562 | static hb_unicode_funcs_t* u; |
| 563 | if (!u) { |
| 564 | u = hb_icu_get_unicode_funcs(); |
| 565 | } |
| 566 | return hb_unicode_script(u, codepoint); |
| 567 | } |
| 568 | |
| 569 | bool |
| 570 | hb_utf16_script_run_next(ScriptRun* run, const uint16_t *chars, size_t len, ssize_t *iter) { |
| 571 | if (size_t(*iter) == len) |
| 572 | return false; |
| 573 | |
| 574 | run->pos = *iter; |
| 575 | const uint32_t init_cp = utf16_to_code_point(chars, len, iter); |
| 576 | const hb_script_t init_script = code_point_to_script(init_cp); |
| 577 | hb_script_t current_script = init_script; |
| 578 | run->script = init_script; |
| 579 | |
| 580 | for (;;) { |
| 581 | if (size_t(*iter) == len) |
| 582 | break; |
| 583 | const ssize_t prev_iter = *iter; |
| 584 | const uint32_t cp = utf16_to_code_point(chars, len, iter); |
| 585 | const hb_script_t script = code_point_to_script(cp); |
| 586 | |
| 587 | if (script != current_script) { |
| 588 | /* BEGIN android-changed |
| 589 | The condition was not correct by doing "a == b == constant" |
| 590 | END android-changed */ |
| 591 | if (current_script == HB_SCRIPT_INHERITED && init_script == HB_SCRIPT_INHERITED) { |
| 592 | // If we started off as inherited, we take whatever we can find. |
| 593 | run->script = script; |
| 594 | current_script = script; |
| 595 | continue; |
| 596 | } else if (script == HB_SCRIPT_INHERITED) { |
| 597 | continue; |
| 598 | } else { |
| 599 | *iter = prev_iter; |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | if (run->script == HB_SCRIPT_INHERITED) |
| 606 | run->script = HB_SCRIPT_COMMON; |
| 607 | |
| 608 | run->length = *iter - run->pos; |
| 609 | return true; |
| 610 | } |
| 611 | |
| 612 | bool |
| 613 | hb_utf16_script_run_prev(ScriptRun* run, const uint16_t *chars, size_t len, ssize_t *iter) { |
| 614 | if (*iter == -1) |
| 615 | return false; |
| 616 | |
| 617 | const size_t ending_index = *iter; |
| 618 | const uint32_t init_cp = utf16_to_code_point_prev(chars, len, iter); |
| 619 | const hb_script_t init_script = code_point_to_script(init_cp); |
| 620 | hb_script_t current_script = init_script; |
| 621 | run->script = init_script; |
Raph Levien | 9d47db2 | 2013-04-23 12:42:16 -0700 | [diff] [blame] | 622 | size_t break_iter = *iter; |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 623 | |
| 624 | for (;;) { |
| 625 | if (*iter < 0) |
| 626 | break; |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 627 | const uint32_t cp = utf16_to_code_point_prev(chars, len, iter); |
| 628 | const hb_script_t script = code_point_to_script(cp); |
| 629 | |
| 630 | if (script != current_script) { |
| 631 | if (current_script == HB_SCRIPT_INHERITED && init_script == HB_SCRIPT_INHERITED) { |
| 632 | // If we started off as inherited, we take whatever we can find. |
| 633 | run->script = script; |
| 634 | current_script = script; |
Raph Levien | 9d47db2 | 2013-04-23 12:42:16 -0700 | [diff] [blame] | 635 | // In cases of script1 + inherited + script2, always group the inherited |
| 636 | // with script1. |
| 637 | break_iter = *iter; |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 638 | continue; |
| 639 | } else if (script == HB_SCRIPT_INHERITED) { |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 640 | continue; |
| 641 | } else { |
Raph Levien | 9d47db2 | 2013-04-23 12:42:16 -0700 | [diff] [blame] | 642 | *iter = break_iter; |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 643 | break; |
| 644 | } |
Raph Levien | 9d47db2 | 2013-04-23 12:42:16 -0700 | [diff] [blame] | 645 | } else { |
| 646 | break_iter = *iter; |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | if (run->script == HB_SCRIPT_INHERITED) |
| 651 | run->script = HB_SCRIPT_COMMON; |
| 652 | |
| 653 | run->pos = *iter + 1; |
| 654 | run->length = ending_index - *iter; |
| 655 | return true; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | static void logGlyphs(hb_buffer_t* buffer) { |
| 660 | unsigned int numGlyphs; |
| 661 | hb_glyph_info_t* info = hb_buffer_get_glyph_infos(buffer, &numGlyphs); |
| 662 | hb_glyph_position_t* positions = hb_buffer_get_glyph_positions(buffer, NULL); |
| 663 | ALOGD(" -- glyphs count=%d", numGlyphs); |
| 664 | for (size_t i = 0; i < numGlyphs; i++) { |
| 665 | ALOGD(" -- glyph[%d] = %d, cluster = %u, advance = %0.2f, offset.x = %0.2f, offset.y = %0.2f", i, |
| 666 | info[i].codepoint, |
| 667 | info[i].cluster, |
| 668 | HBFixedToFloat(positions[i].x_advance), |
| 669 | HBFixedToFloat(positions[i].x_offset), |
| 670 | HBFixedToFloat(positions[i].y_offset)); |
Fabrice Di Meglio | abb0f29 | 2011-08-18 17:59:29 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 674 | void TextLayoutShaper::computeRunValues(const SkPaint* paint, const UChar* contextChars, |
| 675 | size_t start, size_t count, size_t contextCount, bool isRTL, |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 676 | Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance, SkRect* outBounds, |
Raph Levien | 2301d32 | 2012-07-17 16:39:49 -0700 | [diff] [blame] | 677 | Vector<jchar>* const outGlyphs, Vector<jfloat>* const outPos) { |
Jeff Brown | a03bded | 2011-12-05 17:36:16 -0800 | [diff] [blame] | 678 | if (!count) { |
| 679 | // We cannot shape an empty run. |
Jeff Brown | a03bded | 2011-12-05 17:36:16 -0800 | [diff] [blame] | 680 | return; |
| 681 | } |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 682 | |
Raph Levien | 57e9723 | 2012-04-24 16:04:34 -0700 | [diff] [blame] | 683 | // To be filled in later |
| 684 | for (size_t i = 0; i < count; i++) { |
| 685 | outAdvances->add(0); |
| 686 | } |
Fabrice Di Meglio | 902a5b3 | 2011-12-08 18:59:14 -0800 | [diff] [blame] | 687 | |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 688 | // Set the string properties |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 689 | const UChar* chars = contextChars + start; |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 690 | |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 691 | // Define shaping paint properties |
| 692 | mShapingPaint.setTextSize(paint->getTextSize()); |
Raph Levien | 2301d32 | 2012-07-17 16:39:49 -0700 | [diff] [blame] | 693 | float skewX = paint->getTextSkewX(); |
| 694 | mShapingPaint.setTextSkewX(skewX); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 695 | mShapingPaint.setTextScaleX(paint->getTextScaleX()); |
| 696 | mShapingPaint.setFlags(paint->getFlags()); |
| 697 | mShapingPaint.setHinting(paint->getHinting()); |
Derek Sollenberger | d7a8077 | 2013-05-28 10:44:26 -0400 | [diff] [blame] | 698 | mShapingPaint.setPaintOptionsAndroid(paint->getPaintOptionsAndroid()); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 699 | |
| 700 | // Split the BiDi run into Script runs. Harfbuzz will populate the pos, length and script |
| 701 | // into the shaperItem |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 702 | ssize_t indexFontRun = isRTL ? count - 1 : 0; |
Raph Levien | 2301d32 | 2012-07-17 16:39:49 -0700 | [diff] [blame] | 703 | jfloat totalAdvance = *outTotalAdvance; |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 704 | ScriptRun run; // relative to chars |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 705 | while ((isRTL) ? |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 706 | hb_utf16_script_run_prev(&run, chars, count, &indexFontRun): |
| 707 | hb_utf16_script_run_next(&run, chars, count, &indexFontRun)) { |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 708 | |
Fabrice Di Meglio | 9c418db | 2011-09-18 12:54:38 -0700 | [diff] [blame] | 709 | #if DEBUG_GLYPHS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 710 | ALOGD("-------- Start of Script Run --------"); |
| 711 | ALOGD("Shaping Script Run with"); |
| 712 | ALOGD(" -- isRTL = %d", isRTL); |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 713 | ALOGD(" -- HB script = %c%c%c%c", HB_UNTAG(run.script)); |
| 714 | ALOGD(" -- run.pos = %d", int(run.pos)); |
| 715 | ALOGD(" -- run.length = %d", int(run.length)); |
| 716 | ALOGD(" -- run = '%s'", String8(chars + run.pos, run.length).string()); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 717 | ALOGD(" -- string = '%s'", String8(chars, count).string()); |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 718 | #endif |
| 719 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 720 | hb_buffer_reset(mBuffer); |
| 721 | // Note: if we want to set unicode functions, etc., this is the place. |
| 722 | |
| 723 | hb_buffer_set_direction(mBuffer, isRTL ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); |
| 724 | hb_buffer_set_script(mBuffer, run.script); |
Raph Levien | 677726b | 2013-10-08 15:45:16 +0200 | [diff] [blame] | 725 | SkString langString = paint->getPaintOptionsAndroid().getLanguage().getTag(); |
| 726 | hb_buffer_set_language(mBuffer, hb_language_from_string(langString.c_str(), -1)); |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 727 | hb_buffer_add_utf16(mBuffer, contextChars, contextCount, start + run.pos, run.length); |
| 728 | |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 729 | // Initialize Harfbuzz Shaper and get the base glyph count for offsetting the glyphIDs |
| 730 | // and shape the Font run |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 731 | size_t glyphBaseCount = shapeFontRun(paint); |
| 732 | unsigned int numGlyphs; |
| 733 | hb_glyph_info_t* info = hb_buffer_get_glyph_infos(mBuffer, &numGlyphs); |
| 734 | hb_glyph_position_t* positions = hb_buffer_get_glyph_positions(mBuffer, NULL); |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 735 | |
Fabrice Di Meglio | 78b868f | 2011-05-17 19:48:57 -0700 | [diff] [blame] | 736 | #if DEBUG_GLYPHS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 737 | ALOGD("Got from Harfbuzz"); |
| 738 | ALOGD(" -- glyphBaseCount = %d", glyphBaseCount); |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 739 | ALOGD(" -- num_glyph = %d", numGlyphs); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 740 | ALOGD(" -- isDevKernText = %d", paint->isDevKernText()); |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 741 | ALOGD(" -- initial totalAdvance = %f", totalAdvance); |
Fabrice Di Meglio | 4dd99e5 | 2011-09-19 10:47:10 -0700 | [diff] [blame] | 742 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 743 | logGlyphs(mBuffer); |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 744 | #endif |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 745 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 746 | for (size_t i = 0; i < numGlyphs; i++) { |
| 747 | size_t cluster = info[i].cluster - start; |
| 748 | float xAdvance = HBFixedToFloat(positions[i].x_advance); |
| 749 | outAdvances->replaceAt(outAdvances->itemAt(cluster) + xAdvance, cluster); |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 750 | jchar glyphId = info[i].codepoint + glyphBaseCount; |
| 751 | outGlyphs->add(glyphId); |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 752 | float xo = HBFixedToFloat(positions[i].x_offset); |
| 753 | float yo = -HBFixedToFloat(positions[i].y_offset); |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 754 | |
| 755 | float xpos = totalAdvance + xo + yo * skewX; |
| 756 | float ypos = yo; |
| 757 | outPos->add(xpos); |
| 758 | outPos->add(ypos); |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 759 | totalAdvance += xAdvance; |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 760 | |
Victoria Lease | 43b692d | 2013-12-03 15:02:28 -0800 | [diff] [blame] | 761 | SkAutoGlyphCache autoCache(mShapingPaint, NULL, NULL); |
| 762 | const SkGlyph& metrics = autoCache.getCache()->getGlyphIDMetrics(glyphId); |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 763 | outBounds->join(xpos + metrics.fLeft, ypos + metrics.fTop, |
| 764 | xpos + metrics.fLeft + metrics.fWidth, ypos + metrics.fTop + metrics.fHeight); |
| 765 | |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 766 | } |
Fabrice Di Meglio | fcf2be1 | 2011-04-05 17:02:36 -0700 | [diff] [blame] | 767 | } |
Fabrice Di Meglio | 5448f03 | 2011-12-02 15:56:19 -0800 | [diff] [blame] | 768 | |
Fabrice Di Meglio | ef9bb3c | 2011-10-17 11:06:46 -0700 | [diff] [blame] | 769 | *outTotalAdvance = totalAdvance; |
Fabrice Di Meglio | 5448f03 | 2011-12-02 15:56:19 -0800 | [diff] [blame] | 770 | |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 771 | #if DEBUG_GLYPHS |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 772 | ALOGD(" -- final totalAdvance = %f", totalAdvance); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 773 | ALOGD("-------- End of Script Run --------"); |
Fabrice Di Meglio | 56e6e54 | 2011-11-30 15:48:18 -0800 | [diff] [blame] | 774 | #endif |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 777 | /** |
| 778 | * Return the first typeface in the logical change, starting with this typeface, |
| 779 | * that contains the specified unichar, or NULL if none is found. |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 780 | */ |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 781 | SkTypeface* TextLayoutShaper::typefaceForScript(const SkPaint* paint, SkTypeface* typeface, |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 782 | hb_script_t script) { |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 783 | SkTypeface::Style currentStyle = SkTypeface::kNormal; |
| 784 | if (typeface) { |
| 785 | currentStyle = typeface->style(); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 786 | } |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 787 | typeface = SkCreateTypefaceForScriptNG(script, currentStyle); |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 788 | #if DEBUG_GLYPHS |
Victoria Lease | ded5ed9 | 2013-02-12 13:36:22 -0800 | [diff] [blame] | 789 | ALOGD("Using Harfbuzz Script %c%c%c%c, Style %d", HB_UNTAG(script), currentStyle); |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 790 | #endif |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 791 | return typeface; |
| 792 | } |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 793 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 794 | bool TextLayoutShaper::isComplexScript(hb_script_t script) { |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 795 | switch (script) { |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 796 | case HB_SCRIPT_COMMON: |
| 797 | case HB_SCRIPT_GREEK: |
| 798 | case HB_SCRIPT_CYRILLIC: |
| 799 | case HB_SCRIPT_HANGUL: |
| 800 | case HB_SCRIPT_INHERITED: |
Junichi Monma | 6d191ed | 2013-01-17 16:13:22 +0900 | [diff] [blame] | 801 | case HB_SCRIPT_HAN: |
| 802 | case HB_SCRIPT_KATAKANA: |
| 803 | case HB_SCRIPT_HIRAGANA: |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 804 | return false; |
| 805 | default: |
| 806 | return true; |
| 807 | } |
| 808 | } |
| 809 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 810 | size_t TextLayoutShaper::shapeFontRun(const SkPaint* paint) { |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 811 | // Update Harfbuzz Shaper |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 812 | |
| 813 | SkTypeface* typeface = paint->getTypeface(); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 814 | |
| 815 | // Get the glyphs base count for offsetting the glyphIDs returned by Harfbuzz |
| 816 | // This is needed as the Typeface used for shaping can be not the default one |
| 817 | // when we are shaping any script that needs to use a fallback Font. |
| 818 | // If we are a "common" script we dont need to shift |
| 819 | size_t baseGlyphCount = 0; |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 820 | hb_codepoint_t firstUnichar = 0; |
| 821 | if (isComplexScript(hb_buffer_get_script(mBuffer))) { |
| 822 | unsigned int numGlyphs; |
| 823 | hb_glyph_info_t* info = hb_buffer_get_glyph_infos(mBuffer, &numGlyphs); |
| 824 | for (size_t i = 0; i < numGlyphs; i++) { |
| 825 | firstUnichar = info[i].codepoint; |
| 826 | if (firstUnichar != ' ') { |
| 827 | break; |
| 828 | } |
Raph Levien | b294435 | 2012-05-01 09:41:50 -0700 | [diff] [blame] | 829 | } |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 830 | baseGlyphCount = paint->getBaseGlyphCount(firstUnichar); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Raph Levien | dd0a912 | 2013-05-06 15:00:41 -0700 | [diff] [blame] | 833 | SkTypeface* scriptTypeface = NULL; |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 834 | if (baseGlyphCount != 0) { |
Raph Levien | dd0a912 | 2013-05-06 15:00:41 -0700 | [diff] [blame] | 835 | scriptTypeface = typefaceForScript(paint, typeface, |
| 836 | hb_buffer_get_script(mBuffer)); |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 837 | #if DEBUG_GLYPHS |
Raph Levien | dd0a912 | 2013-05-06 15:00:41 -0700 | [diff] [blame] | 838 | ALOGD("Using Default Typeface for script %c%c%c%c", |
| 839 | HB_UNTAG(hb_buffer_get_script(mBuffer))); |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 840 | #endif |
Raph Levien | dd0a912 | 2013-05-06 15:00:41 -0700 | [diff] [blame] | 841 | } |
| 842 | if (scriptTypeface) { |
| 843 | typeface = scriptTypeface; |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 844 | } else { |
Raph Levien | dd0a912 | 2013-05-06 15:00:41 -0700 | [diff] [blame] | 845 | baseGlyphCount = 0; |
| 846 | if (typeface) { |
| 847 | SkSafeRef(typeface); |
| 848 | } else { |
Victoria Lease | af1653a | 2013-04-17 10:20:23 -0700 | [diff] [blame] | 849 | typeface = SkTypeface::CreateFromName(NULL, SkTypeface::kNormal); |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 850 | #if DEBUG_GLYPHS |
Victoria Lease | cc0f9d8 | 2013-04-15 15:29:36 -0700 | [diff] [blame] | 851 | ALOGD("Using Default Typeface (normal style)"); |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 852 | #endif |
| 853 | } |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 856 | mShapingPaint.setTypeface(typeface); |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 857 | hb_face_t* face = referenceCachedHBFace(typeface); |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 858 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 859 | float sizeY = paint->getTextSize(); |
| 860 | float sizeX = sizeY * paint->getTextScaleX(); |
| 861 | hb_font_t* font = createFont(face, &mShapingPaint, sizeX, sizeY); |
| 862 | hb_face_destroy(face); |
Raph Levien | 2301d32 | 2012-07-17 16:39:49 -0700 | [diff] [blame] | 863 | |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 864 | #if DEBUG_GLYPHS |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 865 | ALOGD("Run typeface = %p, uniqueID = %d, face = %p", |
| 866 | typeface, typeface->uniqueID(), face); |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 867 | #endif |
Billy Hewlett | d6deccb | 2012-06-22 10:30:55 -0700 | [diff] [blame] | 868 | SkSafeUnref(typeface); |
Raph Levien | 1637dcd | 2012-05-02 10:41:14 -0700 | [diff] [blame] | 869 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 870 | hb_shape(font, mBuffer, NULL, 0); |
| 871 | hb_font_destroy(font); |
| 872 | |
Victoria Lease | 8450a6e | 2013-10-04 17:33:35 -0700 | [diff] [blame] | 873 | mShapingPaint.setTypeface(paint->getTypeface()); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 874 | return baseGlyphCount; |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 877 | hb_face_t* TextLayoutShaper::referenceCachedHBFace(SkTypeface* typeface) { |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 878 | SkFontID fontId = typeface->uniqueID(); |
| 879 | ssize_t index = mCachedHBFaces.indexOfKey(fontId); |
| 880 | if (index >= 0) { |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 881 | return hb_face_reference(mCachedHBFaces.valueAt(index)); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 882 | } |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 883 | // TODO: destroy function |
| 884 | hb_face_t* face = hb_face_create_for_tables(harfbuzzSkiaReferenceTable, typeface, NULL); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 885 | #if DEBUG_GLYPHS |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 886 | ALOGD("Created HB_NewFace %p from paint typeface = %p", face, typeface); |
Fabrice Di Meglio | 0af10b5 | 2011-11-18 17:36:41 -0800 | [diff] [blame] | 887 | #endif |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 888 | mCachedHBFaces.add(fontId, face); |
| 889 | return hb_face_reference(face); |
Fabrice Di Meglio | 79df532 | 2011-09-19 15:17:56 -0700 | [diff] [blame] | 890 | } |
Fabrice Di Meglio | 48796a8 | 2011-04-05 15:22:41 -0700 | [diff] [blame] | 891 | |
Fabrice Di Meglio | 15cc68c | 2012-05-15 14:47:03 -0700 | [diff] [blame] | 892 | void TextLayoutShaper::purgeCaches() { |
Raph Levien | 75394d6 | 2012-06-02 15:47:29 -0700 | [diff] [blame] | 893 | size_t cacheSize = mCachedHBFaces.size(); |
| 894 | for (size_t i = 0; i < cacheSize; i++) { |
Raph Levien | aaedde5 | 2012-10-30 15:55:33 -0700 | [diff] [blame] | 895 | hb_face_destroy(mCachedHBFaces.valueAt(i)); |
Raph Levien | 75394d6 | 2012-06-02 15:47:29 -0700 | [diff] [blame] | 896 | } |
Fabrice Di Meglio | 15cc68c | 2012-05-15 14:47:03 -0700 | [diff] [blame] | 897 | mCachedHBFaces.clear(); |
Fabrice Di Meglio | 15cc68c | 2012-05-15 14:47:03 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 900 | TextLayoutEngine::TextLayoutEngine() { |
| 901 | mShaper = new TextLayoutShaper(); |
| 902 | #if USE_TEXT_LAYOUT_CACHE |
| 903 | mTextLayoutCache = new TextLayoutCache(mShaper); |
| 904 | #else |
| 905 | mTextLayoutCache = NULL; |
| 906 | #endif |
| 907 | } |
| 908 | |
| 909 | TextLayoutEngine::~TextLayoutEngine() { |
| 910 | delete mTextLayoutCache; |
| 911 | delete mShaper; |
| 912 | } |
| 913 | |
| 914 | sp<TextLayoutValue> TextLayoutEngine::getValue(const SkPaint* paint, const jchar* text, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 915 | jint start, jint count, jint contextCount, jint dirFlags) { |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 916 | sp<TextLayoutValue> value; |
| 917 | #if USE_TEXT_LAYOUT_CACHE |
| 918 | value = mTextLayoutCache->getValue(paint, text, start, count, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 919 | contextCount, dirFlags); |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 920 | if (value == NULL) { |
| 921 | ALOGE("Cannot get TextLayoutCache value for text = '%s'", |
| 922 | String8(text + start, count).string()); |
| 923 | } |
| 924 | #else |
| 925 | value = new TextLayoutValue(count); |
| 926 | mShaper->computeValues(value.get(), paint, |
Fabrice Di Meglio | da12f38 | 2013-03-15 11:26:56 -0700 | [diff] [blame] | 927 | reinterpret_cast<const UChar*>(text), start, count, contextCount, dirFlags); |
Fabrice Di Meglio | a731b08 | 2012-01-23 18:18:45 -0800 | [diff] [blame] | 928 | #endif |
| 929 | return value; |
| 930 | } |
| 931 | |
Fabrice Di Meglio | 30ca5cd | 2012-05-07 17:45:44 -0700 | [diff] [blame] | 932 | void TextLayoutEngine::purgeCaches() { |
| 933 | #if USE_TEXT_LAYOUT_CACHE |
Raph Levien | 13ba4e4 | 2012-09-12 15:15:51 -0700 | [diff] [blame] | 934 | mTextLayoutCache->purgeCaches(); |
Fabrice Di Meglio | 15cc68c | 2012-05-15 14:47:03 -0700 | [diff] [blame] | 935 | #if DEBUG_GLYPHS |
| 936 | ALOGD("Purged TextLayoutEngine caches"); |
| 937 | #endif |
Fabrice Di Meglio | 30ca5cd | 2012-05-07 17:45:44 -0700 | [diff] [blame] | 938 | #endif |
| 939 | } |
| 940 | |
| 941 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 942 | } // namespace android |