diff options
author | Deepanshu Gupta <deepanshu@google.com> | 2014-08-02 13:16:24 -0700 |
---|---|---|
committer | Deepanshu Gupta <deepanshu@google.com> | 2014-08-05 21:12:16 +0000 |
commit | 45aab0eb8f479b9967e7c3c43d0342412fb49b57 (patch) | |
tree | 4da893b6644a431efdc30df573865db0594cd47f /tools/layoutlib/bridge | |
parent | 01e840ff9441e005153d799e71d65b38bcb21902 (diff) | |
download | frameworks_base-45aab0eb8f479b9967e7c3c43d0342412fb49b57.zip frameworks_base-45aab0eb8f479b9967e7c3c43d0342412fb49b57.tar.gz frameworks_base-45aab0eb8f479b9967e7c3c43d0342412fb49b57.tar.bz2 |
LayoutLib: Use ICU's line break algo.
Change-Id: I7e5b0ab7423a72f5a4b0e1163d0a537f0b03ba07
Diffstat (limited to 'tools/layoutlib/bridge')
-rw-r--r-- | tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java b/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java new file mode 100644 index 0000000..5a467b2 --- /dev/null +++ b/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java @@ -0,0 +1,55 @@ +package android.text; + +import com.android.tools.layoutlib.annotations.LayoutlibDelegate; + +import java.text.CharacterIterator; +import java.util.Arrays; +import java.util.Locale; + +import com.ibm.icu.lang.UCharacter; +import com.ibm.icu.text.BreakIterator; +import com.ibm.icu.util.ULocale; +import javax.swing.text.Segment; + +/** + * Delegate that provides implementation for native methods in {@link android.text.StaticLayout} + * + * Through the layoutlib_create tool, selected methods of Handler have been replaced + * by calls to methods of the same name in this delegate class. + * + */ +public class StaticLayout_Delegate { + + /** + * Fills the recycle array with positions that are suitable to break the text at. The array + * must be terminated by '-1'. + */ + @LayoutlibDelegate + /*package*/ static int[] nLineBreakOpportunities(String locale, char[] text, int length, + int[] recycle) { + BreakIterator iterator = BreakIterator.getLineInstance(new ULocale(locale)); + Segment segment = new Segment(text, 0, length); + iterator.setText(segment); + if (recycle == null) { + // Because 42 is the answer to everything. + recycle = new int[42]; + } + int breakOpp = iterator.first(); + recycle[0] = breakOpp; + //noinspection ConstantConditions + assert BreakIterator.DONE == -1; + for (int i = 1; breakOpp != BreakIterator.DONE; ++i) { + if (i >= recycle.length) { + recycle = doubleSize(recycle); + } + assert (i < recycle.length); + breakOpp = iterator.next(); + recycle[i] = breakOpp; + } + return recycle; + } + + private static int[] doubleSize(int[] array) { + return Arrays.copyOf(array, array.length * 2); + } +} |