1package android.text;
2
3import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
4
5import java.text.CharacterIterator;
6import java.util.Arrays;
7import java.util.Locale;
8
9import com.ibm.icu.lang.UCharacter;
10import com.ibm.icu.text.BreakIterator;
11import com.ibm.icu.util.ULocale;
12import javax.swing.text.Segment;
13
14/**
15 * Delegate that provides implementation for native methods in {@link android.text.StaticLayout}
16 * <p/>
17 * Through the layoutlib_create tool, selected methods of StaticLayout have been replaced
18 * by calls to methods of the same name in this delegate class.
19 *
20 */
21public class StaticLayout_Delegate {
22
23    /**
24     * Fills the recycle array with positions that are suitable to break the text at. The array
25     * must be terminated by '-1'.
26     */
27    @LayoutlibDelegate
28    /*package*/ static int[] nLineBreakOpportunities(String locale, char[] text, int length,
29            int[] recycle) {
30        BreakIterator iterator = BreakIterator.getLineInstance(new ULocale(locale));
31        Segment segment = new Segment(text, 0, length);
32        iterator.setText(segment);
33        if (recycle == null) {
34            // Because 42 is the answer to everything.
35            recycle = new int[42];
36        }
37        int breakOpp = iterator.first();
38        recycle[0] = breakOpp;
39        //noinspection ConstantConditions
40        assert BreakIterator.DONE == -1;
41        for (int i = 1; breakOpp != BreakIterator.DONE; ++i) {
42            if (i >= recycle.length) {
43                recycle = doubleSize(recycle);
44            }
45            assert (i < recycle.length);
46            breakOpp = iterator.next();
47            recycle[i] = breakOpp;
48        }
49        return recycle;
50    }
51
52    private static int[] doubleSize(int[] array) {
53        return Arrays.copyOf(array, array.length * 2);
54    }
55}
56