Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 17 | #define LOG_TAG "TextLayout" |
| 18 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 19 | #include "TextLayout.h" |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 20 | #include "TextLayoutCache.h" |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 21 | |
| 22 | #include <android_runtime/AndroidRuntime.h> |
| 23 | |
| 24 | #include "SkTemplates.h" |
| 25 | #include "unicode/ubidi.h" |
| 26 | #include "unicode/ushape.h" |
| 27 | #include <utils/Log.h> |
| 28 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 29 | namespace android { |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 30 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 31 | // Returns true if we might need layout. If bidiFlags force LTR, assume no layout, if |
| 32 | // bidiFlags indicate there probably is RTL, assume we do, otherwise scan the text |
| 33 | // looking for a character >= the first RTL character in unicode and assume we do if |
| 34 | // we find one. |
| 35 | bool TextLayout::needsLayout(const jchar* text, jint len, jint bidiFlags) { |
| 36 | if (bidiFlags == kBidi_Force_LTR) { |
| 37 | return false; |
| 38 | } |
| 39 | if ((bidiFlags == kBidi_RTL) || (bidiFlags == kBidi_Default_RTL) || |
| 40 | bidiFlags == kBidi_Force_RTL) { |
| 41 | return true; |
| 42 | } |
| 43 | for (int i = 0; i < len; ++i) { |
Fabrice Di Meglio | dd347df | 2011-02-17 13:22:08 -0800 | [diff] [blame] | 44 | if (text[i] >= UNICODE_FIRST_RTL_CHAR) { |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 45 | return true; |
| 46 | } |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 51 | // Draws or gets the path of a paragraph of text on a single line, running bidi and shaping. |
| 52 | // This will draw if canvas is not null, otherwise path must be non-null and it will create |
| 53 | // a path representing the text that would have been drawn. |
| 54 | void TextLayout::handleText(SkPaint *paint, const jchar* text, jsize len, |
| 55 | jint bidiFlags, jfloat x, jfloat y,SkCanvas *canvas, SkPath *path) { |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 56 | sp<TextLayoutCacheValue> value; |
| 57 | #if USE_TEXT_LAYOUT_CACHE |
| 58 | // Return advances from the cache. Compute them if needed |
| 59 | value = TextLayoutCache::getInstance().getValue(paint, text, 0, len, |
| 60 | len, bidiFlags); |
| 61 | #else |
| 62 | value = new TextLayoutCacheValue(len); |
| 63 | TextLayoutEngine::getInstance().computeValues(value.get(), paint, |
| 64 | reinterpret_cast<const UChar*>(text), 0, len, len, bidiFlags); |
| 65 | #endif |
| 66 | if (value == NULL) { |
| 67 | LOGE("Cannot get TextLayoutCache value for text = '%s'", |
| 68 | String8(text, len).string()); |
| 69 | return ; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 70 | } |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 71 | SkScalar x_ = SkFloatToScalar(x); |
| 72 | SkScalar y_ = SkFloatToScalar(y); |
| 73 | if (canvas) { |
| 74 | canvas->drawText(value->getGlyphs(), value->getGlyphsCount() * 2, x_, y_, *paint); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 75 | } else { |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 76 | paint->getTextPath(value->getGlyphs(), value->getGlyphsCount() * 2, x_, y_, path); |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 77 | } |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 80 | void TextLayout::getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start, |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 81 | jint count, jint contextCount, jint dirFlags, |
Fabrice Di Meglio | 79df532 | 2011-09-19 15:17:56 -0700 | [diff] [blame] | 82 | jfloat* resultAdvances, jfloat* resultTotalAdvance) { |
Fabrice Di Meglio | af033ca | 2011-06-06 11:51:46 -0700 | [diff] [blame] | 83 | sp<TextLayoutCacheValue> value; |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 84 | #if USE_TEXT_LAYOUT_CACHE |
| 85 | // Return advances from the cache. Compute them if needed |
Fabrice Di Meglio | 5c863f7 | 2011-10-05 18:11:59 -0700 | [diff] [blame] | 86 | value = TextLayoutCache::getInstance().getValue(paint, chars, start, count, |
| 87 | contextCount, dirFlags); |
Fabrice Di Meglio | d313c66 | 2011-02-24 19:56:18 -0800 | [diff] [blame] | 88 | #else |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 89 | value = new TextLayoutCacheValue(contextCount); |
| 90 | TextLayoutEngine::getInstance().computeValues(value.get(), paint, |
| 91 | reinterpret_cast<const UChar*>(chars), start, count, contextCount, dirFlags); |
Fabrice Di Meglio | dd347df | 2011-02-17 13:22:08 -0800 | [diff] [blame] | 92 | #endif |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 93 | if (value == NULL) { |
| 94 | LOGE("Cannot get TextLayoutCache value for text = '%s'", |
| 95 | String8(chars + start, count).string()); |
| 96 | return ; |
| 97 | } |
| 98 | if (resultAdvances) { |
| 99 | memcpy(resultAdvances, value->getAdvances(), value->getAdvancesCount() * sizeof(jfloat)); |
| 100 | } |
| 101 | if (resultTotalAdvance) { |
| 102 | *resultTotalAdvance = value->getTotalAdvance(); |
Fabrice Di Meglio | af033ca | 2011-06-06 11:51:46 -0700 | [diff] [blame] | 103 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 106 | void TextLayout::getTextRunAdvancesICU(SkPaint* paint, const jchar* chars, jint start, |
| 107 | jint count, jint contextCount, jint dirFlags, |
| 108 | jfloat* resultAdvances, jfloat& resultTotalAdvance) { |
| 109 | // Compute advances and return them |
Fabrice Di Meglio | 54dc642 | 2011-09-18 15:38:08 -0700 | [diff] [blame] | 110 | computeAdvancesWithICU(paint, chars, start, count, contextCount, dirFlags, |
Fabrice Di Meglio | eee49c6 | 2011-03-24 17:21:23 -0700 | [diff] [blame] | 111 | resultAdvances, &resultTotalAdvance); |
| 112 | } |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 113 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 114 | void TextLayout::getTextPath(SkPaint *paint, const jchar *text, jsize len, |
| 115 | jint bidiFlags, jfloat x, jfloat y, SkPath *path) { |
| 116 | handleText(paint, text, len, bidiFlags, x, y, NULL, path); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void TextLayout::drawTextOnPath(SkPaint* paint, const jchar* text, int count, |
| 121 | int bidiFlags, jfloat hOffset, jfloat vOffset, |
| 122 | SkPath* path, SkCanvas* canvas) { |
| 123 | |
| 124 | SkScalar h_ = SkFloatToScalar(hOffset); |
| 125 | SkScalar v_ = SkFloatToScalar(vOffset); |
| 126 | |
| 127 | if (!needsLayout(text, count, bidiFlags)) { |
| 128 | canvas->drawTextOnPathHV(text, count << 1, *path, h_, v_, *paint); |
| 129 | return; |
| 130 | } |
| 131 | |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 132 | sp<TextLayoutCacheValue> value; |
| 133 | #if USE_TEXT_LAYOUT_CACHE |
| 134 | value = TextLayoutCache::getInstance().getValue(paint, text, 0, count, |
| 135 | count, bidiFlags); |
| 136 | #else |
| 137 | value = new TextLayoutCacheValue(count); |
| 138 | TextLayoutEngine::getInstance().computeValues(value.get(), paint, |
| 139 | reinterpret_cast<const UChar*>(text), 0, count, count, bidiFlags); |
| 140 | #endif |
| 141 | if (value == NULL) { |
| 142 | LOGE("Cannot get TextLayoutCache value for text = '%s'", |
| 143 | String8(text, count).string()); |
| 144 | return ; |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 145 | } |
Fabrice Di Meglio | b02d0ca | 2011-12-08 14:05:44 -0800 | [diff] [blame] | 146 | |
| 147 | // Save old text encoding |
| 148 | SkPaint::TextEncoding oldEncoding = paint->getTextEncoding(); |
| 149 | // Define Glyph encoding |
| 150 | paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 151 | |
| 152 | canvas->drawTextOnPathHV(value->getGlyphs(), value->getGlyphsCount() * 2, *path, h_, v_, *paint); |
| 153 | |
| 154 | // Get back old encoding |
| 155 | paint->setTextEncoding(oldEncoding); |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Fabrice Di Meglio | 54dc642 | 2011-09-18 15:38:08 -0700 | [diff] [blame] | 158 | void TextLayout::computeAdvancesWithICU(SkPaint* paint, const UChar* chars, |
| 159 | size_t start, size_t count, size_t contextCount, int dirFlags, |
| 160 | jfloat* outAdvances, jfloat* outTotalAdvance) { |
| 161 | SkAutoSTMalloc<CHAR_BUFFER_SIZE, jchar> tempBuffer(contextCount); |
| 162 | jchar* buffer = tempBuffer.get(); |
| 163 | SkScalar* scalarArray = (SkScalar*)outAdvances; |
| 164 | |
| 165 | // this is where we'd call harfbuzz |
| 166 | // for now we just use ushape.c |
| 167 | size_t widths; |
| 168 | const jchar* text; |
| 169 | if (dirFlags & 0x1) { // rtl, call arabic shaping in case |
| 170 | UErrorCode status = U_ZERO_ERROR; |
| 171 | // Use fixed length since we need to keep start and count valid |
| 172 | u_shapeArabic(chars, contextCount, buffer, contextCount, |
| 173 | U_SHAPE_LENGTH_FIXED_SPACES_NEAR | |
| 174 | U_SHAPE_TEXT_DIRECTION_LOGICAL | U_SHAPE_LETTERS_SHAPE | |
| 175 | U_SHAPE_X_LAMALEF_SUB_ALTERNATE, &status); |
| 176 | // we shouldn't fail unless there's an out of memory condition, |
| 177 | // in which case we're hosed anyway |
| 178 | for (int i = start, e = i + count; i < e; ++i) { |
| 179 | if (buffer[i] == UNICODE_NOT_A_CHAR) { |
| 180 | buffer[i] = UNICODE_ZWSP; // zero-width-space for skia |
| 181 | } |
| 182 | } |
| 183 | text = buffer + start; |
| 184 | widths = paint->getTextWidths(text, count << 1, scalarArray); |
| 185 | } else { |
| 186 | text = chars + start; |
| 187 | widths = paint->getTextWidths(text, count << 1, scalarArray); |
| 188 | } |
| 189 | |
| 190 | jfloat totalAdvance = 0; |
| 191 | if (widths < count) { |
| 192 | #if DEBUG_ADVANCES |
| 193 | LOGD("ICU -- count=%d", widths); |
| 194 | #endif |
| 195 | // Skia operates on code points, not code units, so surrogate pairs return only |
| 196 | // one value. Expand the result so we have one value per UTF-16 code unit. |
| 197 | |
| 198 | // Note, skia's getTextWidth gets confused if it encounters a surrogate pair, |
| 199 | // leaving the remaining widths zero. Not nice. |
| 200 | for (size_t i = 0, p = 0; i < widths; ++i) { |
| 201 | totalAdvance += outAdvances[p++] = SkScalarToFloat(scalarArray[i]); |
| 202 | if (p < count && |
| 203 | text[p] >= UNICODE_FIRST_LOW_SURROGATE && |
| 204 | text[p] < UNICODE_FIRST_PRIVATE_USE && |
| 205 | text[p-1] >= UNICODE_FIRST_HIGH_SURROGATE && |
| 206 | text[p-1] < UNICODE_FIRST_LOW_SURROGATE) { |
| 207 | outAdvances[p++] = 0; |
| 208 | } |
| 209 | #if DEBUG_ADVANCES |
| 210 | LOGD("icu-adv = %f - total = %f", outAdvances[i], totalAdvance); |
| 211 | #endif |
| 212 | } |
| 213 | } else { |
| 214 | #if DEBUG_ADVANCES |
| 215 | LOGD("ICU -- count=%d", count); |
| 216 | #endif |
| 217 | for (size_t i = 0; i < count; i++) { |
| 218 | totalAdvance += outAdvances[i] = SkScalarToFloat(scalarArray[i]); |
| 219 | #if DEBUG_ADVANCES |
| 220 | LOGD("icu-adv = %f - total = %f", outAdvances[i], totalAdvance); |
| 221 | #endif |
| 222 | } |
| 223 | } |
| 224 | *outTotalAdvance = totalAdvance; |
| 225 | } |
| 226 | |
Doug Felt | f7cb1f7 | 2010-07-01 16:20:43 -0700 | [diff] [blame] | 227 | } |