blob: 2beedada409f3312c2c9c1c7d204054efc41bf9a [file] [log] [blame]
Doug Feltf7cb1f72010-07-01 16:20:43 -07001/*
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 Megliob02d0ca2011-12-08 14:05:44 -080017#define LOG_TAG "TextLayout"
18
Doug Feltf7cb1f72010-07-01 16:20:43 -070019#include "TextLayout.h"
Fabrice Di Megliod313c662011-02-24 19:56:18 -080020#include "TextLayoutCache.h"
Doug Feltf7cb1f72010-07-01 16:20:43 -070021
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 Feltf7cb1f72010-07-01 16:20:43 -070029namespace android {
Fabrice Di Megliod313c662011-02-24 19:56:18 -080030
Doug Feltf7cb1f72010-07-01 16:20:43 -070031// 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.
35bool 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 Megliodd347df2011-02-17 13:22:08 -080044 if (text[i] >= UNICODE_FIRST_RTL_CHAR) {
Doug Feltf7cb1f72010-07-01 16:20:43 -070045 return true;
46 }
47 }
48 return false;
49}
50
Romain Guye8e62a42010-07-23 18:55:21 -070051// 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.
54void TextLayout::handleText(SkPaint *paint, const jchar* text, jsize len,
Fabrice Di Meglioc511bee82012-01-05 13:30:54 -080055 jint bidiFlags, jfloat x, jfloat y, SkPath *path) {
Fabrice Di Meglioa731b082012-01-23 18:18:45 -080056 sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
57 text, 0, len, len, bidiFlags);
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080058 if (value == NULL) {
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080059 return ;
Romain Guye8e62a42010-07-23 18:55:21 -070060 }
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080061 SkScalar x_ = SkFloatToScalar(x);
62 SkScalar y_ = SkFloatToScalar(y);
Fabrice Di Meglioc511bee82012-01-05 13:30:54 -080063 // Beware: this needs Glyph encoding (already done on the Paint constructor)
64 paint->getTextPath(value->getGlyphs(), value->getGlyphsCount() * 2, x_, y_, path);
Romain Guy61c8c9c2010-08-09 20:48:09 -070065}
66
Fabrice Di Megliod313c662011-02-24 19:56:18 -080067void TextLayout::getTextRunAdvances(SkPaint* paint, const jchar* chars, jint start,
Doug Feltf7cb1f72010-07-01 16:20:43 -070068 jint count, jint contextCount, jint dirFlags,
Fabrice Di Meglio79df5322011-09-19 15:17:56 -070069 jfloat* resultAdvances, jfloat* resultTotalAdvance) {
Fabrice Di Meglioa731b082012-01-23 18:18:45 -080070 sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
71 chars, start, count, contextCount, dirFlags);
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080072 if (value == NULL) {
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -080073 return ;
74 }
75 if (resultAdvances) {
76 memcpy(resultAdvances, value->getAdvances(), value->getAdvancesCount() * sizeof(jfloat));
77 }
78 if (resultTotalAdvance) {
79 *resultTotalAdvance = value->getTotalAdvance();
Fabrice Di Meglioaf033ca2011-06-06 11:51:46 -070080 }
Doug Feltf7cb1f72010-07-01 16:20:43 -070081}
82
Fabrice Di Meglioeee49c62011-03-24 17:21:23 -070083void TextLayout::getTextRunAdvancesICU(SkPaint* paint, const jchar* chars, jint start,
84 jint count, jint contextCount, jint dirFlags,
85 jfloat* resultAdvances, jfloat& resultTotalAdvance) {
86 // Compute advances and return them
Fabrice Di Meglio54dc6422011-09-18 15:38:08 -070087 computeAdvancesWithICU(paint, chars, start, count, contextCount, dirFlags,
Fabrice Di Meglioeee49c62011-03-24 17:21:23 -070088 resultAdvances, &resultTotalAdvance);
89}
Doug Feltf7cb1f72010-07-01 16:20:43 -070090
Doug Feltf7cb1f72010-07-01 16:20:43 -070091void TextLayout::getTextPath(SkPaint *paint, const jchar *text, jsize len,
92 jint bidiFlags, jfloat x, jfloat y, SkPath *path) {
Fabrice Di Meglioc511bee82012-01-05 13:30:54 -080093 handleText(paint, text, len, bidiFlags, x, y, path);
Doug Feltf7cb1f72010-07-01 16:20:43 -070094}
95
96
97void TextLayout::drawTextOnPath(SkPaint* paint, const jchar* text, int count,
98 int bidiFlags, jfloat hOffset, jfloat vOffset,
99 SkPath* path, SkCanvas* canvas) {
100
101 SkScalar h_ = SkFloatToScalar(hOffset);
102 SkScalar v_ = SkFloatToScalar(vOffset);
103
Fabrice Di Meglioa731b082012-01-23 18:18:45 -0800104 sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
105 text, 0, count, count, bidiFlags);
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -0800106 if (value == NULL) {
Fabrice Di Meglioa731b082012-01-23 18:18:45 -0800107 return;
Doug Feltf7cb1f72010-07-01 16:20:43 -0700108 }
Fabrice Di Meglioa731b082012-01-23 18:18:45 -0800109
Fabrice Di Meglioc511bee82012-01-05 13:30:54 -0800110 // Beware: this needs Glyph encoding (already done on the Paint constructor)
Fabrice Di Megliob02d0ca2011-12-08 14:05:44 -0800111 canvas->drawTextOnPathHV(value->getGlyphs(), value->getGlyphsCount() * 2, *path, h_, v_, *paint);
Doug Feltf7cb1f72010-07-01 16:20:43 -0700112}
113
Fabrice Di Meglio54dc6422011-09-18 15:38:08 -0700114void TextLayout::computeAdvancesWithICU(SkPaint* paint, const UChar* chars,
115 size_t start, size_t count, size_t contextCount, int dirFlags,
116 jfloat* outAdvances, jfloat* outTotalAdvance) {
117 SkAutoSTMalloc<CHAR_BUFFER_SIZE, jchar> tempBuffer(contextCount);
118 jchar* buffer = tempBuffer.get();
119 SkScalar* scalarArray = (SkScalar*)outAdvances;
120
121 // this is where we'd call harfbuzz
122 // for now we just use ushape.c
123 size_t widths;
124 const jchar* text;
125 if (dirFlags & 0x1) { // rtl, call arabic shaping in case
126 UErrorCode status = U_ZERO_ERROR;
127 // Use fixed length since we need to keep start and count valid
128 u_shapeArabic(chars, contextCount, buffer, contextCount,
129 U_SHAPE_LENGTH_FIXED_SPACES_NEAR |
130 U_SHAPE_TEXT_DIRECTION_LOGICAL | U_SHAPE_LETTERS_SHAPE |
131 U_SHAPE_X_LAMALEF_SUB_ALTERNATE, &status);
132 // we shouldn't fail unless there's an out of memory condition,
133 // in which case we're hosed anyway
134 for (int i = start, e = i + count; i < e; ++i) {
135 if (buffer[i] == UNICODE_NOT_A_CHAR) {
136 buffer[i] = UNICODE_ZWSP; // zero-width-space for skia
137 }
138 }
139 text = buffer + start;
140 widths = paint->getTextWidths(text, count << 1, scalarArray);
141 } else {
142 text = chars + start;
143 widths = paint->getTextWidths(text, count << 1, scalarArray);
144 }
145
146 jfloat totalAdvance = 0;
147 if (widths < count) {
148#if DEBUG_ADVANCES
Steve Block5baa3a62011-12-20 16:23:08 +0000149 ALOGD("ICU -- count=%d", widths);
Fabrice Di Meglio54dc6422011-09-18 15:38:08 -0700150#endif
151 // Skia operates on code points, not code units, so surrogate pairs return only
152 // one value. Expand the result so we have one value per UTF-16 code unit.
153
154 // Note, skia's getTextWidth gets confused if it encounters a surrogate pair,
155 // leaving the remaining widths zero. Not nice.
156 for (size_t i = 0, p = 0; i < widths; ++i) {
157 totalAdvance += outAdvances[p++] = SkScalarToFloat(scalarArray[i]);
158 if (p < count &&
159 text[p] >= UNICODE_FIRST_LOW_SURROGATE &&
160 text[p] < UNICODE_FIRST_PRIVATE_USE &&
161 text[p-1] >= UNICODE_FIRST_HIGH_SURROGATE &&
162 text[p-1] < UNICODE_FIRST_LOW_SURROGATE) {
163 outAdvances[p++] = 0;
164 }
165#if DEBUG_ADVANCES
Steve Block5baa3a62011-12-20 16:23:08 +0000166 ALOGD("icu-adv = %f - total = %f", outAdvances[i], totalAdvance);
Fabrice Di Meglio54dc6422011-09-18 15:38:08 -0700167#endif
168 }
169 } else {
170#if DEBUG_ADVANCES
Steve Block5baa3a62011-12-20 16:23:08 +0000171 ALOGD("ICU -- count=%d", count);
Fabrice Di Meglio54dc6422011-09-18 15:38:08 -0700172#endif
173 for (size_t i = 0; i < count; i++) {
174 totalAdvance += outAdvances[i] = SkScalarToFloat(scalarArray[i]);
175#if DEBUG_ADVANCES
Steve Block5baa3a62011-12-20 16:23:08 +0000176 ALOGD("icu-adv = %f - total = %f", outAdvances[i], totalAdvance);
Fabrice Di Meglio54dc6422011-09-18 15:38:08 -0700177#endif
178 }
179 }
180 *outTotalAdvance = totalAdvance;
181}
182
Doug Feltf7cb1f72010-07-01 16:20:43 -0700183}