diff options
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/text/DynamicLayout.java | 15 | ||||
| -rw-r--r-- | core/java/android/text/Hyphenator.java | 133 | ||||
| -rw-r--r-- | core/java/android/text/Layout.java | 24 | ||||
| -rw-r--r-- | core/java/android/text/StaticLayout.java | 42 | ||||
| -rw-r--r-- | core/java/android/text/TextLine.java | 39 |
5 files changed, 87 insertions, 166 deletions
diff --git a/core/java/android/text/DynamicLayout.java b/core/java/android/text/DynamicLayout.java index c928da103557..32982f9df15d 100644 --- a/core/java/android/text/DynamicLayout.java +++ b/core/java/android/text/DynamicLayout.java @@ -674,7 +674,8 @@ public class DynamicLayout extends Layout { objects[0] = reflowed.getLineDirections(i); final int end = (i == n - 1) ? where + after : reflowed.getLineStart(i + 1); - ints[HYPHEN] = reflowed.getHyphen(i) & HYPHEN_MASK; + ints[HYPHEN] = StaticLayout.packHyphenEdit( + reflowed.getStartHyphenEdit(i), reflowed.getEndHyphenEdit(i)); ints[MAY_PROTRUDE_FROM_TOP_OR_BOTTOM] |= contentMayProtrudeFromLineTopOrBottom(text, start, end) ? MAY_PROTRUDE_FROM_TOP_OR_BOTTOM_MASK : 0; @@ -1056,8 +1057,16 @@ public class DynamicLayout extends Layout { * @hide */ @Override - public int getHyphen(int line) { - return mInts.getValue(line, HYPHEN) & HYPHEN_MASK; + public @Paint.StartHyphenEdit int getStartHyphenEdit(int line) { + return StaticLayout.unpackStartHyphenEdit(mInts.getValue(line, HYPHEN) & HYPHEN_MASK); + } + + /** + * @hide + */ + @Override + public @Paint.EndHyphenEdit int getEndHyphenEdit(int line) { + return StaticLayout.unpackEndHyphenEdit(mInts.getValue(line, HYPHEN) & HYPHEN_MASK); } private boolean getContentMayProtrudeFromTopOrBottom(int line) { diff --git a/core/java/android/text/Hyphenator.java b/core/java/android/text/Hyphenator.java index e4200ac0bc6c..6f0628ad38e6 100644 --- a/core/java/android/text/Hyphenator.java +++ b/core/java/android/text/Hyphenator.java @@ -16,142 +16,15 @@ package android.text; -import android.annotation.IntDef; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - /** - * Provides constants and pack/unpack methods for the HyphenEdit. - * - * Hyphenator provides constant values for start of line and end of line modification. - * For example, by passing {@link #END_HYPHEN_EDIT_INSERT_HYPHEN} like as follows, HYPHEN(U+2010) - * character is appended at the end of line. + * Does the native Hyphenator initialization. * - * <pre> - * <code> - * Paint paint = new Paint(); - * paint.setHyphenEdit(Hyphenator.packHyphenEdit( - * Hyphenator.START_HYPHEN_EDIT_NO_EDIT, - * Hyphenator.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‐" - * </code> - * </pre> - * - * @see android.graphics.Paint#setHyphenEdit(int) + * @hide */ public class Hyphenator { private Hyphenator() {} - /** @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 {} - - /** - * An integer representing the starting of the line has no modification for hyphenation. - */ - public static final int START_HYPHEN_EDIT_NO_EDIT = 0x00; - - /** - * 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 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; - - // Following three constants are used for packing start hyphen edit and end hyphen edit into - // single integer. Following encodings must be the same as the minikin's one. - // See frameworks/minikin/include/Hyphenator.h for more details. - private static final int END_HYPHEN_EDIT_MASK = 0x07; // 0b00111 - private static final int START_HYPHEN_EDIT_MASK = 0x18; // 0b11000 - private static final int START_HYPHEN_EDIT_SHIFT = 0x03; - - /** - * Extract start hyphen edit from packed value. - */ - public static @StartHyphenEdit int unpackStartHyphenEdit(int hyphenEdit) { - return (hyphenEdit & START_HYPHEN_EDIT_MASK) >> START_HYPHEN_EDIT_SHIFT; - } - - /** - * Extract end hyphen edit from packed value. - */ - public static @EndHyphenEdit int unpackEndHyphenEdit(int hyphenEdit) { - return hyphenEdit & END_HYPHEN_EDIT_MASK; - } - - /** - * Pack the start hyphen edit and end hyphen edit into single integer. - */ - public static int packHyphenEdit(@StartHyphenEdit int startHyphenEdit, - @EndHyphenEdit int endHyphenEdit) { - return ((startHyphenEdit << START_HYPHEN_EDIT_SHIFT) & START_HYPHEN_EDIT_MASK) - | (endHyphenEdit & END_HYPHEN_EDIT_MASK); - } - - - /** - * @hide - */ + // This method is called from Zygote. public static void init() { nInit(); } diff --git a/core/java/android/text/Layout.java b/core/java/android/text/Layout.java index 2d5f3bf8c862..fb6dc228e786 100644 --- a/core/java/android/text/Layout.java +++ b/core/java/android/text/Layout.java @@ -437,7 +437,8 @@ public abstract class Layout { previousLineEnd = getLineStart(lineNum + 1); final boolean justify = isJustificationRequired(lineNum); int end = getLineVisibleEnd(lineNum, start, previousLineEnd); - paint.setHyphenEdit(getHyphen(lineNum)); + paint.setStartHyphenEdit(getStartHyphenEdit(lineNum)); + paint.setEndHyphenEdit(getEndHyphenEdit(lineNum)); int ltop = previousLineBottom; int lbottom = getLineTop(lineNum + 1); @@ -910,12 +911,21 @@ public abstract class Layout { public abstract int getBottomPadding(); /** - * Returns the hyphen edit for a line. + * Returns the start hyphen edit for a line. * * @hide */ - public int getHyphen(int line) { - return 0; + public @Paint.StartHyphenEdit int getStartHyphenEdit(int line) { + return Paint.START_HYPHEN_EDIT_NO_EDIT; + } + + /** + * Returns the end hyphen edit for a line. + * + * @hide + */ + public @Paint.EndHyphenEdit int getEndHyphenEdit(int line) { + return Paint.END_HYPHEN_EDIT_NO_EDIT; } /** @@ -1418,7 +1428,8 @@ public abstract class Layout { final TextLine tl = TextLine.obtain(); final TextPaint paint = mWorkPaint; paint.set(mPaint); - paint.setHyphenEdit(getHyphen(line)); + paint.setStartHyphenEdit(getStartHyphenEdit(line)); + paint.setEndHyphenEdit(getEndHyphenEdit(line)); tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops, getEllipsisStart(line), getEllipsisStart(line) + getEllipsisCount(line)); if (isJustificationRequired(line)) { @@ -1447,7 +1458,8 @@ public abstract class Layout { final TextLine tl = TextLine.obtain(); final TextPaint paint = mWorkPaint; paint.set(mPaint); - paint.setHyphenEdit(getHyphen(line)); + paint.setStartHyphenEdit(getStartHyphenEdit(line)); + paint.setEndHyphenEdit(getEndHyphenEdit(line)); tl.set(paint, mText, start, end, dir, directions, hasTabs, tabStops, getEllipsisStart(line), getEllipsisStart(line) + getEllipsisCount(line)); if (isJustificationRequired(line)) { diff --git a/core/java/android/text/StaticLayout.java b/core/java/android/text/StaticLayout.java index defe2cefc1f7..9fefc83056ac 100644 --- a/core/java/android/text/StaticLayout.java +++ b/core/java/android/text/StaticLayout.java @@ -779,7 +779,8 @@ public class StaticLayout extends Layout { ascents[i] = res.getLineAscent(i); descents[i] = res.getLineDescent(i); hasTabs[i] = res.hasLineTab(i); - hyphenEdits[i] = res.getLineHyphenEdit(i); + hyphenEdits[i] = + packHyphenEdit(res.getStartLineHyphenEdit(i), res.getEndLineHyphenEdit(i)); } final int remainingLineCount = mMaximumVisibleLineCount - mLineCount; @@ -1258,20 +1259,42 @@ public class StaticLayout extends Layout { return mBottomPadding; } + // To store into single int field, pack the pair of start and end hyphen edit. + static int packHyphenEdit( + @Paint.StartHyphenEdit int start, @Paint.EndHyphenEdit int end) { + return start << START_HYPHEN_BITS_SHIFT | end; + } + + static int unpackStartHyphenEdit(int packedHyphenEdit) { + return (packedHyphenEdit & START_HYPHEN_MASK) >> START_HYPHEN_BITS_SHIFT; + } + + static int unpackEndHyphenEdit(int packedHyphenEdit) { + return packedHyphenEdit & END_HYPHEN_MASK; + } + /** - * Returns the packed hyphen edit value for this line. + * Returns the start hyphen edit value for this line. * - * You can extract start hyphen edit and end hyphen edit by using - * {@link Hyphenator#unpackStartHyphenEdit(int)} and - * {@link Hyphenator#unpackEndHyphenEdit(int)}. + * @param lineNumber a line number + * @return A start hyphen edit value. + * @hide + */ + @Override + public @Paint.StartHyphenEdit int getStartHyphenEdit(int lineNumber) { + return unpackStartHyphenEdit(mLines[mColumns * lineNumber + HYPHEN] & HYPHEN_MASK); + } + + /** + * Returns the packed hyphen edit value for this line. * * @param lineNumber a line number - * @return A packed hyphen edit value. + * @return An end hyphen edit value. * @hide */ @Override - public int getHyphen(int lineNumber) { - return mLines[mColumns * lineNumber + HYPHEN] & HYPHEN_MASK; + public @Paint.EndHyphenEdit int getEndHyphenEdit(int lineNumber) { + return unpackEndHyphenEdit(mLines[mColumns * lineNumber + HYPHEN] & HYPHEN_MASK); } /** @@ -1395,6 +1418,9 @@ public class StaticLayout extends Layout { private static final int DIR_SHIFT = 30; private static final int TAB_MASK = 0x20000000; private static final int HYPHEN_MASK = 0xFF; + private static final int START_HYPHEN_BITS_SHIFT = 3; + private static final int START_HYPHEN_MASK = 0x18; // 0b11000 + private static final int END_HYPHEN_MASK = 0x7; // 0b00111 private static final int TAB_INCREMENT = 20; // same as Layout, but that's private diff --git a/core/java/android/text/TextLine.java b/core/java/android/text/TextLine.java index 915a18e59226..86651060a394 100644 --- a/core/java/android/text/TextLine.java +++ b/core/java/android/text/TextLine.java @@ -1012,19 +1012,14 @@ public class TextLine { return runIsRtl ? -ret : ret; } - private int adjustHyphenEdit(int start, int limit, int packedHyphenEdit) { - int result = packedHyphenEdit; - // Only draw hyphens on first or last run in line. Disable them otherwise. - if (start > 0) { // not the first run - result = Hyphenator.packHyphenEdit(Hyphenator.START_HYPHEN_EDIT_NO_EDIT, - Hyphenator.unpackEndHyphenEdit(packedHyphenEdit)); - } - if (limit < mLen) { // not the last run - result = Hyphenator.packHyphenEdit(Hyphenator.unpackStartHyphenEdit(packedHyphenEdit), - Hyphenator.END_HYPHEN_EDIT_NO_EDIT); - result &= ~Paint.HYPHENEDIT_MASK_END_OF_LINE; - } - return result; + private int adjustStartHyphenEdit(int start, @Paint.StartHyphenEdit int startHyphenEdit) { + // Only draw hyphens on first in line. Disable them otherwise. + return start > 0 ? Paint.START_HYPHEN_EDIT_NO_EDIT : startHyphenEdit; + } + + private int adjustEndHyphenEdit(int limit, @Paint.EndHyphenEdit int endHyphenEdit) { + // Only draw hyphens on last run in line. Disable them otherwise. + return limit < mLen ? Paint.END_HYPHEN_EDIT_NO_EDIT : endHyphenEdit; } private static final class DecorationInfo { @@ -1115,7 +1110,8 @@ public class TextLine { if (!needsSpanMeasurement) { final TextPaint wp = mWorkPaint; wp.set(mPaint); - wp.setHyphenEdit(adjustHyphenEdit(start, limit, wp.getHyphenEdit())); + wp.setStartHyphenEdit(adjustStartHyphenEdit(start, wp.getStartHyphenEdit())); + wp.setEndHyphenEdit(adjustEndHyphenEdit(limit, wp.getEndHyphenEdit())); return handleText(wp, start, limit, start, limit, runIsRtl, c, x, top, y, bottom, fmi, needWidth, measureLimit, null); } @@ -1193,8 +1189,10 @@ public class TextLine { // The style of the present chunk of text is substantially different from the // style of the previous chunk. We need to handle the active piece of text // and restart with the present chunk. - activePaint.setHyphenEdit(adjustHyphenEdit( - activeStart, activeEnd, mPaint.getHyphenEdit())); + activePaint.setStartHyphenEdit( + adjustStartHyphenEdit(activeStart, mPaint.getStartHyphenEdit())); + activePaint.setEndHyphenEdit( + adjustEndHyphenEdit(activeEnd, mPaint.getEndHyphenEdit())); x += handleText(activePaint, activeStart, activeEnd, i, inext, runIsRtl, c, x, top, y, bottom, fmi, needWidth || activeEnd < measureLimit, Math.min(activeEnd, mlimit), mDecorations); @@ -1218,8 +1216,10 @@ public class TextLine { } } // Handle the final piece of text. - activePaint.setHyphenEdit(adjustHyphenEdit( - activeStart, activeEnd, mPaint.getHyphenEdit())); + activePaint.setStartHyphenEdit( + adjustStartHyphenEdit(activeStart, mPaint.getStartHyphenEdit())); + activePaint.setEndHyphenEdit( + adjustEndHyphenEdit(activeEnd, mPaint.getEndHyphenEdit())); x += handleText(activePaint, activeStart, activeEnd, i, inext, runIsRtl, c, x, top, y, bottom, fmi, needWidth || activeEnd < measureLimit, Math.min(activeEnd, mlimit), mDecorations); @@ -1323,7 +1323,8 @@ public class TextLine { && lp.getTextSkewX() == rp.getTextSkewX() && lp.getLetterSpacing() == rp.getLetterSpacing() && lp.getWordSpacing() == rp.getWordSpacing() - && lp.getHyphenEdit() == rp.getHyphenEdit() + && lp.getStartHyphenEdit() == rp.getStartHyphenEdit() + && lp.getEndHyphenEdit() == rp.getEndHyphenEdit() && lp.bgColor == rp.bgColor && lp.baselineShift == rp.baselineShift && lp.linkColor == rp.linkColor |
