From fe12aa77743c4a4a98409418a6cbb0be11b8fee5 Mon Sep 17 00:00:00 2001 From: Diego Perez Date: Mon, 9 Mar 2015 10:43:18 +0000 Subject: [PATCH] Add support for the new native Builder class on StaticLayout. This patch updates StaticLayout_Delegate to support the new Builder class introduced in ag/646289. Change-Id: I9f8c528b8751ce69482e33ac6f1bc42406a0b606 --- .../src/android/text/StaticLayout_Delegate.java | 38 ++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java b/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java index 929d625de55c..e24b3d5ec7ef 100644 --- a/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java +++ b/tools/layoutlib/bridge/src/android/text/StaticLayout_Delegate.java @@ -1,6 +1,7 @@ package android.text; import com.android.annotations.NonNull; +import com.android.layoutlib.bridge.impl.DelegateManager; import com.android.tools.layoutlib.annotations.LayoutlibDelegate; import android.text.StaticLayout.LineBreaks; @@ -27,14 +28,19 @@ public class StaticLayout_Delegate { private static final char CHAR_NEWLINE = 0x0A; private static final char CHAR_ZWSP = 0x200B; // Zero width space. + // ---- Builder delegate manager ---- + private static final DelegateManager sBuilderManager = + new DelegateManager(Builder.class); + @LayoutlibDelegate - /*package*/ static int nComputeLineBreaks(String locale, char[] inputText, float[] widths, + /*package*/ static int nComputeLineBreaks(long nativeBuilder, char[] inputText, float[] widths, int length, float firstWidth, int firstWidthLineCount, float restWidth, int[] variableTabStops, int defaultTabStop, boolean optimize, LineBreaks recycle, int[] recycleBreaks, float[] recycleWidths, boolean[] recycleFlags, int recycleLength) { + Builder builder = sBuilderManager.getDelegate(nativeBuilder); // compute all possible breakpoints. - BreakIterator it = BreakIterator.getLineInstance(new ULocale(locale)); + BreakIterator it = BreakIterator.getLineInstance(new ULocale(builder.mLocale)); it.setText(new Segment(inputText, 0, length)); // average word length in english is 5. So, initialize the possible breaks with a guess. List breaks = new ArrayList((int) Math.ceil(length / 5d)); @@ -97,4 +103,32 @@ public class StaticLayout_Delegate { PrimitiveType.PENALTY.getNewPrimitive(length, 0, -PrimitiveType.PENALTY_INFINITY)); return primitives; } + + @LayoutlibDelegate + /*package*/ static long nNewBuilder() { + return sBuilderManager.addNewDelegate(new Builder()); + } + + @LayoutlibDelegate + /*package*/ static void nFinishBuilder(long nativeBuilder) { + } + + @LayoutlibDelegate + /*package*/ static void nFreeBuilder(long nativeBuilder) { + sBuilderManager.removeJavaReferenceFor(nativeBuilder); + } + + @LayoutlibDelegate + /*package*/ static void nBuilderSetLocale(long nativeBuilder, String locale) { + Builder builder = sBuilderManager.getDelegate(nativeBuilder); + builder.mLocale = locale; + } + + /** + * Java representation of the native Builder class. It currently only stores the locale + * set by nBuilderSetLocale. + */ + static class Builder { + String mLocale; + } } -- 2.11.0