Skip to content

Commit 42b04f6

Browse files
Fabrice Di MeglioAndroid (Google) Code Review
authored andcommitted
Merge "Make TextLayoutCache no more dependent on ICU"
2 parents 2210fff + 54dc642 commit 42b04f6

3 files changed

Lines changed: 75 additions & 82 deletions

File tree

core/jni/android/graphics/TextLayout.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ void TextLayout::getTextRunAdvancesICU(SkPaint* paint, const jchar* chars, jint
283283
jint count, jint contextCount, jint dirFlags,
284284
jfloat* resultAdvances, jfloat& resultTotalAdvance) {
285285
// Compute advances and return them
286-
TextLayoutCacheValue::computeAdvancesWithICU(paint, chars, start, count, contextCount, dirFlags,
286+
computeAdvancesWithICU(paint, chars, start, count, contextCount, dirFlags,
287287
resultAdvances, &resultTotalAdvance);
288288
}
289289

@@ -321,4 +321,73 @@ void TextLayout::drawTextOnPath(SkPaint* paint, const jchar* text, int count,
321321
}
322322
}
323323

324+
void TextLayout::computeAdvancesWithICU(SkPaint* paint, const UChar* chars,
325+
size_t start, size_t count, size_t contextCount, int dirFlags,
326+
jfloat* outAdvances, jfloat* outTotalAdvance) {
327+
SkAutoSTMalloc<CHAR_BUFFER_SIZE, jchar> tempBuffer(contextCount);
328+
jchar* buffer = tempBuffer.get();
329+
SkScalar* scalarArray = (SkScalar*)outAdvances;
330+
331+
// this is where we'd call harfbuzz
332+
// for now we just use ushape.c
333+
size_t widths;
334+
const jchar* text;
335+
if (dirFlags & 0x1) { // rtl, call arabic shaping in case
336+
UErrorCode status = U_ZERO_ERROR;
337+
// Use fixed length since we need to keep start and count valid
338+
u_shapeArabic(chars, contextCount, buffer, contextCount,
339+
U_SHAPE_LENGTH_FIXED_SPACES_NEAR |
340+
U_SHAPE_TEXT_DIRECTION_LOGICAL | U_SHAPE_LETTERS_SHAPE |
341+
U_SHAPE_X_LAMALEF_SUB_ALTERNATE, &status);
342+
// we shouldn't fail unless there's an out of memory condition,
343+
// in which case we're hosed anyway
344+
for (int i = start, e = i + count; i < e; ++i) {
345+
if (buffer[i] == UNICODE_NOT_A_CHAR) {
346+
buffer[i] = UNICODE_ZWSP; // zero-width-space for skia
347+
}
348+
}
349+
text = buffer + start;
350+
widths = paint->getTextWidths(text, count << 1, scalarArray);
351+
} else {
352+
text = chars + start;
353+
widths = paint->getTextWidths(text, count << 1, scalarArray);
354+
}
355+
356+
jfloat totalAdvance = 0;
357+
if (widths < count) {
358+
#if DEBUG_ADVANCES
359+
LOGD("ICU -- count=%d", widths);
360+
#endif
361+
// Skia operates on code points, not code units, so surrogate pairs return only
362+
// one value. Expand the result so we have one value per UTF-16 code unit.
363+
364+
// Note, skia's getTextWidth gets confused if it encounters a surrogate pair,
365+
// leaving the remaining widths zero. Not nice.
366+
for (size_t i = 0, p = 0; i < widths; ++i) {
367+
totalAdvance += outAdvances[p++] = SkScalarToFloat(scalarArray[i]);
368+
if (p < count &&
369+
text[p] >= UNICODE_FIRST_LOW_SURROGATE &&
370+
text[p] < UNICODE_FIRST_PRIVATE_USE &&
371+
text[p-1] >= UNICODE_FIRST_HIGH_SURROGATE &&
372+
text[p-1] < UNICODE_FIRST_LOW_SURROGATE) {
373+
outAdvances[p++] = 0;
374+
}
375+
#if DEBUG_ADVANCES
376+
LOGD("icu-adv = %f - total = %f", outAdvances[i], totalAdvance);
377+
#endif
378+
}
379+
} else {
380+
#if DEBUG_ADVANCES
381+
LOGD("ICU -- count=%d", count);
382+
#endif
383+
for (size_t i = 0; i < count; i++) {
384+
totalAdvance += outAdvances[i] = SkScalarToFloat(scalarArray[i]);
385+
#if DEBUG_ADVANCES
386+
LOGD("icu-adv = %f - total = %f", outAdvances[i], totalAdvance);
387+
#endif
388+
}
389+
}
390+
*outTotalAdvance = totalAdvance;
391+
}
392+
324393
}

core/jni/android/graphics/TextLayout.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,9 @@ class TextLayout {
106106
UErrorCode &status);
107107
static void handleText(SkPaint* paint, const jchar* text, jsize len,
108108
int bidiFlags, jfloat x, jfloat y, SkCanvas* canvas, SkPath* path);
109+
110+
static void computeAdvancesWithICU(SkPaint* paint, const UChar* chars,
111+
size_t start, size_t count, size_t contextCount, int dirFlags,
112+
jfloat* outAdvances, jfloat* outTotalAdvance);
109113
};
110114
} // namespace android

core/jni/android/graphics/TextLayoutCache.cpp

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ void TextLayoutCache::init() {
5252
mCacheStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
5353

5454
if (mDebugEnabled) {
55-
LOGD("Start time: %lld", mCacheStartTime);
56-
#if RTL_USE_HARFBUZZ
57-
LOGD("Using HARFBUZZ");
58-
#else
59-
LOGD("Using ICU");
60-
#endif
61-
LOGD("Initialization is done");
55+
LOGD("Initialization is done - Start time: %lld", mCacheStartTime);
6256
}
6357

6458
mInitialized = true;
@@ -302,13 +296,8 @@ void TextLayoutCacheValue::computeValues(SkPaint* paint, const UChar* chars, siz
302296
mAdvancesCount = count;
303297
mAdvances = new float[count];
304298

305-
#if RTL_USE_HARFBUZZ
306299
computeValuesWithHarfbuzz(paint, chars, start, count, contextCount, dirFlags,
307300
mAdvances, &mTotalAdvance, &mGlyphs, &mGlyphsCount);
308-
#else
309-
computeAdvancesWithICU(paint, chars, start, count, contextCount, dirFlags,
310-
mAdvances, &mTotalAdvance);
311-
#endif
312301
#if DEBUG_ADVANCES
313302
LOGD("Advances - count=%d - countextCount=%d - totalAdvance=%f - "
314303
"adv[0]=%f adv[1]=%f adv[2]=%f adv[3]=%f", count, contextCount, mTotalAdvance,
@@ -632,75 +621,6 @@ void TextLayoutCacheValue::computeRunValuesWithHarfbuzz(SkPaint* paint, const UC
632621
HB_FreeFace(shaperItem.face);
633622
}
634623

635-
void TextLayoutCacheValue::computeAdvancesWithICU(SkPaint* paint, const UChar* chars,
636-
size_t start, size_t count, size_t contextCount, int dirFlags,
637-
jfloat* outAdvances, jfloat* outTotalAdvance) {
638-
SkAutoSTMalloc<CHAR_BUFFER_SIZE, jchar> tempBuffer(contextCount);
639-
jchar* buffer = tempBuffer.get();
640-
SkScalar* scalarArray = (SkScalar*)outAdvances;
641-
642-
// this is where we'd call harfbuzz
643-
// for now we just use ushape.c
644-
size_t widths;
645-
const jchar* text;
646-
if (dirFlags & 0x1) { // rtl, call arabic shaping in case
647-
UErrorCode status = U_ZERO_ERROR;
648-
// Use fixed length since we need to keep start and count valid
649-
u_shapeArabic(chars, contextCount, buffer, contextCount,
650-
U_SHAPE_LENGTH_FIXED_SPACES_NEAR |
651-
U_SHAPE_TEXT_DIRECTION_LOGICAL | U_SHAPE_LETTERS_SHAPE |
652-
U_SHAPE_X_LAMALEF_SUB_ALTERNATE, &status);
653-
// we shouldn't fail unless there's an out of memory condition,
654-
// in which case we're hosed anyway
655-
for (int i = start, e = i + count; i < e; ++i) {
656-
if (buffer[i] == UNICODE_NOT_A_CHAR) {
657-
buffer[i] = UNICODE_ZWSP; // zero-width-space for skia
658-
}
659-
}
660-
text = buffer + start;
661-
widths = paint->getTextWidths(text, count << 1, scalarArray);
662-
} else {
663-
text = chars + start;
664-
widths = paint->getTextWidths(text, count << 1, scalarArray);
665-
}
666-
667-
jfloat totalAdvance = 0;
668-
if (widths < count) {
669-
#if DEBUG_ADVANCES
670-
LOGD("ICU -- count=%d", widths);
671-
#endif
672-
// Skia operates on code points, not code units, so surrogate pairs return only
673-
// one value. Expand the result so we have one value per UTF-16 code unit.
674-
675-
// Note, skia's getTextWidth gets confused if it encounters a surrogate pair,
676-
// leaving the remaining widths zero. Not nice.
677-
for (size_t i = 0, p = 0; i < widths; ++i) {
678-
totalAdvance += outAdvances[p++] = SkScalarToFloat(scalarArray[i]);
679-
if (p < count &&
680-
text[p] >= UNICODE_FIRST_LOW_SURROGATE &&
681-
text[p] < UNICODE_FIRST_PRIVATE_USE &&
682-
text[p-1] >= UNICODE_FIRST_HIGH_SURROGATE &&
683-
text[p-1] < UNICODE_FIRST_LOW_SURROGATE) {
684-
outAdvances[p++] = 0;
685-
}
686-
#if DEBUG_ADVANCES
687-
LOGD("icu-adv = %f - total = %f", outAdvances[i], totalAdvance);
688-
#endif
689-
}
690-
} else {
691-
#if DEBUG_ADVANCES
692-
LOGD("ICU -- count=%d", count);
693-
#endif
694-
for (size_t i = 0; i < count; i++) {
695-
totalAdvance += outAdvances[i] = SkScalarToFloat(scalarArray[i]);
696-
#if DEBUG_ADVANCES
697-
LOGD("icu-adv = %f - total = %f", outAdvances[i], totalAdvance);
698-
#endif
699-
}
700-
}
701-
*outTotalAdvance = totalAdvance;
702-
}
703-
704624
void TextLayoutCacheValue::deleteGlyphArrays(HB_ShaperItem* shaperItem) {
705625
delete[] shaperItem->glyphs;
706626
delete[] shaperItem->attributes;

0 commit comments

Comments
 (0)