From fb1b47931407723a83045f649235d53982116f98 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Fri, 8 Mar 2019 14:05:08 -0800 Subject: Hide packing structure from public API The hyphen edit is packed into 8bit integer in native library, but this should not be exposed to public API as API council suggested. This CL includes: - Removing packing structure from HyphenEdit and move constants from Hyphenator to Paint. - Replace LineBreaker's getLineHyphenEdit with two separate start/end function. - Replace Paint's getHyphenEdit with two separate start/end function. - StaticLayout still relies on this packing mechanism but no longer need to be the same packing structure as in Minikin. Bug: 124794844 Test: atest CtsGraphicsTestCases Test: atest CtsTextTestCases Change-Id: I8206061f554cf6e119b2b565162f54317eac630a --- graphics/java/android/graphics/Paint.java | 166 ++++++++++++++++----- .../java/android/graphics/text/LineBreaker.java | 30 +++- 2 files changed, 151 insertions(+), 45 deletions(-) (limited to 'graphics/java/android') diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index 9eeb43b579bc..452f7c93f8aa 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -364,19 +364,79 @@ public class Paint { */ private static final int CURSOR_OPT_MAX_VALUE = CURSOR_AT; + /** @hide */ + @IntDef(prefix = { "START_HYPHEN_EDIT_" }, value = { + START_HYPHEN_EDIT_NO_EDIT, + START_HYPHEN_EDIT_INSERT_HYPHEN, + START_HYPHEN_EDIT_INSERT_ZWJ + }) + @Retention(RetentionPolicy.SOURCE) + public @interface StartHyphenEdit {} + /** - * Mask for hyphen edits that happen at the end of a line. Keep in sync with the definition in - * Minikin's Hyphenator.h. - * @hide + * An integer representing the starting of the line has no modification for hyphenation. */ - public static final int HYPHENEDIT_MASK_END_OF_LINE = 0x07; + public static final int START_HYPHEN_EDIT_NO_EDIT = 0x00; /** - * Mask for hyphen edits that happen at the start of a line. Keep in sync with the definition in - * Minikin's Hyphenator.h. - * @hide + * An integer representing the starting of the line has normal hyphen character (U+002D). + */ + public static final int START_HYPHEN_EDIT_INSERT_HYPHEN = 0x01; + + /** + * An integer representing the starting of the line has Zero-Width-Joiner (U+200D). + */ + public static final int START_HYPHEN_EDIT_INSERT_ZWJ = 0x02; + + /** @hide */ + @IntDef(prefix = { "END_HYPHEN_EDIT_" }, value = { + END_HYPHEN_EDIT_NO_EDIT, + END_HYPHEN_EDIT_REPLACE_WITH_HYPHEN, + END_HYPHEN_EDIT_INSERT_HYPHEN, + END_HYPHEN_EDIT_INSERT_ARMENIAN_HYPHEN, + END_HYPHEN_EDIT_INSERT_MAQAF, + END_HYPHEN_EDIT_INSERT_UCAS_HYPHEN, + END_HYPHEN_EDIT_INSERT_ZWJ_AND_HYPHEN + }) + @Retention(RetentionPolicy.SOURCE) + public @interface EndHyphenEdit {} + + /** + * An integer representing the end of the line has no modification for hyphenation. + */ + public static final int END_HYPHEN_EDIT_NO_EDIT = 0x00; + + /** + * An integer representing the character at the end of the line is replaced with hyphen + * character (U+002D). + */ + public static final int END_HYPHEN_EDIT_REPLACE_WITH_HYPHEN = 0x01; + + /** + * An integer representing the end of the line has normal hyphen character (U+002D). + */ + public static final int END_HYPHEN_EDIT_INSERT_HYPHEN = 0x02; + + /** + * An integer representing the end of the line has Armentian hyphen (U+058A). + */ + public static final int END_HYPHEN_EDIT_INSERT_ARMENIAN_HYPHEN = 0x03; + + /** + * An integer representing the end of the line has maqaf (Hebrew hyphen, U+05BE). */ - public static final int HYPHENEDIT_MASK_START_OF_LINE = 0x03 << 3; + public static final int END_HYPHEN_EDIT_INSERT_MAQAF = 0x04; + + /** + * An integer representing the end of the line has Canadian Syllabics hyphen (U+1400). + */ + public static final int END_HYPHEN_EDIT_INSERT_UCAS_HYPHEN = 0x05; + + /** + * An integer representing the end of the line has Zero-Width-Joiner (U+200D) followed by normal + * hyphen character (U+002D). + */ + public static final int END_HYPHEN_EDIT_INSERT_ZWJ_AND_HYPHEN = 0x06; /** * The Style specifies if the primitive being drawn is filled, stroked, or @@ -1873,54 +1933,80 @@ public class Paint { } /** - * Get the current value of packed hyphen edit. + * Get the current value of start hyphen edit. + * + * The default value is 0 which is equivalent to {@link #START_HYPHEN_EDIT_NO_EDIT}. * - * You can extract start hyphen edit and end hyphen edit by using - * {@link android.text.Hyphenator#unpackStartHyphenEdit(int)} and - * {@link android.text.Hyphenator#unpackEndHyphenEdit(int)}. + * @return the current starting hyphen edit value + * @see #setStartHyphenEdit(int) + */ + public @StartHyphenEdit int getStartHyphenEdit() { + return nGetStartHyphenEdit(mNativePaint); + } + + /** + * Get the current value of end hyphen edit. * - * The default value is 0 which is equivalent to packed value of - * {@link android.text.Hyphenator#START_HYPHEN_EDIT_NO_EDIT} and - * {@link android.text.Hyphenator#END_HYPHEN_EDIT_NO_EDIT}. + * The default value is 0 which is equivalent to {@link #END_HYPHEN_EDIT_NO_EDIT}. * - * @return the current hyphen edit value - * @see #setHyphenEdit(int) + * @return the current starting hyphen edit value + * @see #setStartHyphenEdit(int) */ - public int getHyphenEdit() { - return nGetHyphenEdit(mNativePaint); + public @EndHyphenEdit int getEndHyphenEdit() { + return nGetEndHyphenEdit(mNativePaint); } /** - * Set a packed hyphen edit on the paint. + * Set a start hyphen edit on the paint. * - * By setting hyphen edit, the measurement and drawing is performed with modifying hyphenation - * at the start of line and end of line. For example, by passing - * {@link android.text.Hyphenator#END_HYPHEN_EDIT_INSERT_HYPHEN} like as follows, HYPHEN(U+2010) + * By setting start hyphen edit, the measurement and drawing is performed with modifying + * hyphenation at the start of line. For example, by passing + * {@link #START_HYPHEN_EDIT_INSERT_HYPHEN} like as follows, HYPHEN(U+2010) + * character is appended at the start of line. + * + *
+     * 
+     *   Paint paint = new Paint();
+     *   paint.setStartHyphenEdit(Paint.START_HYPHEN_EDIT_INSERT_HYPHEN);
+     *   paint.measureText("abc", 0, 3);  // Returns the width of "‐abc"
+     *   Canvas.drawText("abc", 0, 3, 0f, 0f, paint);  // Draws "‐abc"
+     * 
+     * 
+ * + * The default value is 0 which is equivalent to + * {@link #START_HYPHEN_EDIT_NO_EDIT}. + * + * @param startHyphen a start hyphen edit value. + * @see #getStartHyphenEdit() + */ + public void setStartHyphenEdit(@StartHyphenEdit int startHyphen) { + nSetStartHyphenEdit(mNativePaint, startHyphen); + } + + /** + * Set a end hyphen edit on the paint. + * + * By setting end hyphen edit, the measurement and drawing is performed with modifying + * hyphenation at the end of line. For example, by passing + * {@link #END_HYPHEN_EDIT_INSERT_HYPHEN} like as follows, HYPHEN(U+2010) * character is appended at the end of line. * *
      * 
      *   Paint paint = new Paint();
-     *   paint.setHyphenEdit(Hyphenator.packHyphenEdit(
-     *       Hyphenator.START_HYPHEN_EDIT_NO_EDIT,
-     *       Hyphenator.END_HYPHEN_EDIT_INSERT_HYPHEN));
+     *   paint.setEndHyphenEdit(Paint.END_HYPHEN_EDIT_INSERT_HYPHEN);
      *   paint.measureText("abc", 0, 3);  // Returns the width of "abc‐"
      *   Canvas.drawText("abc", 0, 3, 0f, 0f, paint);  // Draws "abc‐"
      * 
      * 
* - * You can pack start hyphen edit and end hyphen edit by - * {@link android.text.Hyphenator#packHyphenEdit(int,int)} + * The default value is 0 which is equivalent to {@link #END_HYPHEN_EDIT_NO_EDIT}. * - * The default value is 0 which is equivalent to packed value of - * {@link android.text.Hyphenator#START_HYPHEN_EDIT_NO_EDIT} and - * {@link android.text.Hyphenator#END_HYPHEN_EDIT_NO_EDIT}. - * - * @param hyphen a packed hyphen edit value. - * @see #getHyphenEdit() + * @param endHyphen a end hyphen edit value. + * @see #getEndHyphenEdit() */ - public void setHyphenEdit(int hyphen) { - nSetHyphenEdit(mNativePaint, hyphen); + public void setEndHyphenEdit(@EndHyphenEdit int endHyphen) { + nSetEndHyphenEdit(mNativePaint, endHyphen); } /** @@ -3063,9 +3149,13 @@ public class Paint { @CriticalNative private static native void nSetWordSpacing(long paintPtr, float wordSpacing); @CriticalNative - private static native int nGetHyphenEdit(long paintPtr); + private static native int nGetStartHyphenEdit(long paintPtr); + @CriticalNative + private static native int nGetEndHyphenEdit(long paintPtr); + @CriticalNative + private static native void nSetStartHyphenEdit(long paintPtr, int hyphen); @CriticalNative - private static native void nSetHyphenEdit(long paintPtr, int hyphen); + private static native void nSetEndHyphenEdit(long paintPtr, int hyphen); @CriticalNative private static native void nSetStrokeMiter(long paintPtr, float miter); @CriticalNative diff --git a/graphics/java/android/graphics/text/LineBreaker.java b/graphics/java/android/graphics/text/LineBreaker.java index 046bbcffb76a..9cabf1ce185d 100644 --- a/graphics/java/android/graphics/text/LineBreaker.java +++ b/graphics/java/android/graphics/text/LineBreaker.java @@ -342,8 +342,12 @@ public class LineBreaker { */ public static class Result { // Following two contstant must be synced with minikin's line breaker. + // TODO(nona): Remove these constatns by introducing native methods. private static final int TAB_MASK = 0x20000000; private static final int HYPHEN_MASK = 0xFF; + private static final int START_HYPHEN_MASK = 0x18; // 0b11000 + private static final int END_HYPHEN_MASK = 0x7; // 0b00111 + private static final int START_HYPHEN_BITS_SHIFT = 3; private static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry( Result.class.getClassLoader(), nGetReleaseResultFunc(), 32); @@ -414,17 +418,29 @@ public class LineBreaker { } /** - * Returns a packed hyphen edit for the line. + * Returns a start hyphen edit for the line. * * @param lineIndex an index of the line. - * @return a packed hyphen edit for the line. + * @return a start hyphen edit for the line. * - * @see android.text.Hyphenator#unpackStartHyphenEdit(int) - * @see android.text.Hyphenator#unpackEndHyphenEdit(int) - * @see android.text.Hyphenator#packHyphenEdit(int,int) + * @see android.graphics.Paint#setStartHyphenEdit + * @see android.graphics.Paint#getStartHyphenEdit */ - public int getLineHyphenEdit(int lineIndex) { - return (nGetLineFlag(mPtr, lineIndex) & HYPHEN_MASK); + public int getStartLineHyphenEdit(int lineIndex) { + return (nGetLineFlag(mPtr, lineIndex) & START_HYPHEN_MASK) >> START_HYPHEN_BITS_SHIFT; + } + + /** + * Returns an end hyphen edit for the line. + * + * @param lineIndex an index of the line. + * @return an end hyphen edit for the line. + * + * @see android.graphics.Paint#setEndHyphenEdit + * @see android.graphics.Paint#getEndHyphenEdit + */ + public int getEndLineHyphenEdit(int lineIndex) { + return nGetLineFlag(mPtr, lineIndex) & END_HYPHEN_MASK; } } -- cgit v1.2.3-59-g8ed1b