import android.text.style.ReplacementSpan;
import android.text.style.TabStopSpan;
+import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;
private static final ParagraphStyle[] NO_PARA_SPANS =
ArrayUtils.emptyArray(ParagraphStyle.class);
+ /** @hide */
+ @IntDef({JUSTIFICATION_MODE_NONE, JUSTIFICATION_MODE_INTER_WORD})
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface JustificationMode {}
+
/**
- * Return how wide a layout must be in order to display the
- * specified text with one line per paragraph.
+ * Value for justification mode indicating no justification.
+ */
+ public static final int JUSTIFICATION_MODE_NONE = 0;
+
+ /**
+ * Value for justification mode indicating the text is justified by stretching word spacing.
+ */
+ public static final int JUSTIFICATION_MODE_INTER_WORD = 1;
+
+ /**
+ * Return how wide a layout must be in order to display the specified text with one line per
+ * paragraph.
+ *
+ * <p>As of O, Uses
+ * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
+ * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
*/
public static float getDesiredWidth(CharSequence source,
TextPaint paint) {
}
/**
+ * Return how wide a layout must be in order to display the specified text slice with one
+ * line per paragraph.
+ *
+ * <p>As of O, Uses
+ * {@link TextDirectionHeuristics#FIRSTSTRONG_LTR} as the default text direction heuristics. In
+ * the earlier versions uses {@link TextDirectionHeuristics#LTR} as the default.</p>
+ */
+ public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
+ return getDesiredWidth(source, start, end, paint, TextDirectionHeuristics.FIRSTSTRONG_LTR);
+ }
+
+ /**
* Return how wide a layout must be in order to display the
* specified text slice with one line per paragraph.
+ *
+ * @hide
*/
- public static float getDesiredWidth(CharSequence source,
- int start, int end,
- TextPaint paint) {
+ public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint,
+ TextDirectionHeuristic textDir) {
float need = 0;
int next;
next = end;
// note, omits trailing paragraph char
- float w = measurePara(paint, source, i, next);
+ float w = measurePara(paint, source, i, next, textDir);
if (w > need)
need = w;
mTextDir = textDir;
}
+ /** @hide */
+ protected void setJustificationMode(@JustificationMode int justificationMode) {
+ mJustificationMode = justificationMode;
+ }
+
/**
* Replace constructor properties of this Layout with new ones. Be careful.
*/
drawText(canvas, firstLine, lastLine);
}
+ private boolean isJustificationRequired(int lineNum) {
+ if (mJustificationMode == JUSTIFICATION_MODE_NONE) return false;
+ final int lineEnd = getLineEnd(lineNum);
+ return lineEnd < mText.length() && mText.charAt(lineEnd - 1) != '\n';
+ }
+
+ private float getJustifyWidth(int lineNum) {
+ Alignment paraAlign = mAlignment;
+ TabStops tabStops = null;
+ boolean tabStopsIsInitialized = false;
+
+ int left = 0;
+ int right = mWidth;
+
+ final int dir = getParagraphDirection(lineNum);
+
+ ParagraphStyle[] spans = NO_PARA_SPANS;
+ if (mSpannedText) {
+ Spanned sp = (Spanned) mText;
+ final int start = getLineStart(lineNum);
+
+ final boolean isFirstParaLine = (start == 0 || mText.charAt(start - 1) == '\n');
+
+ if (isFirstParaLine) {
+ final int spanEnd = sp.nextSpanTransition(start, mText.length(),
+ ParagraphStyle.class);
+ spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);
+
+ for (int n = spans.length - 1; n >= 0; n--) {
+ if (spans[n] instanceof AlignmentSpan) {
+ paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
+ break;
+ }
+ }
+ }
+
+ final int length = spans.length;
+ boolean useFirstLineMargin = isFirstParaLine;
+ for (int n = 0; n < length; n++) {
+ if (spans[n] instanceof LeadingMarginSpan2) {
+ int count = ((LeadingMarginSpan2) spans[n]).getLeadingMarginLineCount();
+ int startLine = getLineForOffset(sp.getSpanStart(spans[n]));
+ if (lineNum < startLine + count) {
+ useFirstLineMargin = true;
+ break;
+ }
+ }
+ }
+ for (int n = 0; n < length; n++) {
+ if (spans[n] instanceof LeadingMarginSpan) {
+ LeadingMarginSpan margin = (LeadingMarginSpan) spans[n];
+ if (dir == DIR_RIGHT_TO_LEFT) {
+ right -= margin.getLeadingMargin(useFirstLineMargin);
+ } else {
+ left += margin.getLeadingMargin(useFirstLineMargin);
+ }
+ }
+ }
+ }
+
+ if (getLineContainsTab(lineNum)) {
+ tabStops = new TabStops(TAB_INCREMENT, spans);
+ }
+
+ final Alignment align;
+ if (paraAlign == Alignment.ALIGN_LEFT) {
+ align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
+ } else if (paraAlign == Alignment.ALIGN_RIGHT) {
+ align = (dir == DIR_LEFT_TO_RIGHT) ? Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
+ } else {
+ align = paraAlign;
+ }
+
+ final int indentWidth;
+ if (align == Alignment.ALIGN_NORMAL) {
+ if (dir == DIR_LEFT_TO_RIGHT) {
+ indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
+ } else {
+ indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
+ }
+ } else if (align == Alignment.ALIGN_OPPOSITE) {
+ if (dir == DIR_LEFT_TO_RIGHT) {
+ indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
+ } else {
+ indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
+ }
+ } else { // Alignment.ALIGN_CENTER
+ indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
+ }
+
+ return right - left - indentWidth;
+ }
+
/**
* @hide
*/
int previousLineEnd = getLineStart(firstLine);
ParagraphStyle[] spans = NO_PARA_SPANS;
int spanEnd = 0;
- TextPaint paint = mPaint;
+ final TextPaint paint = mPaint;
CharSequence buf = mText;
Alignment paraAlign = mAlignment;
for (int lineNum = firstLine; lineNum <= lastLine; lineNum++) {
int start = previousLineEnd;
previousLineEnd = getLineStart(lineNum + 1);
+ final boolean justify = isJustificationRequired(lineNum);
int end = getLineVisibleEnd(lineNum, start, previousLineEnd);
int ltop = previousLineBottom;
}
int x;
+ final int indentWidth;
if (align == Alignment.ALIGN_NORMAL) {
if (dir == DIR_LEFT_TO_RIGHT) {
- x = left + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
+ indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
+ x = left + indentWidth;
} else {
- x = right + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
+ indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
+ x = right - indentWidth;
}
} else {
int max = (int)getLineExtent(lineNum, tabStops, false);
if (align == Alignment.ALIGN_OPPOSITE) {
if (dir == DIR_LEFT_TO_RIGHT) {
- x = right - max + getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
+ indentWidth = -getIndentAdjust(lineNum, Alignment.ALIGN_RIGHT);
+ x = right - max - indentWidth;
} else {
- x = left - max + getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
+ indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_LEFT);
+ x = left - max + indentWidth;
}
} else { // Alignment.ALIGN_CENTER
+ indentWidth = getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
max = max & ~1;
- x = ((right + left - max) >> 1) +
- getIndentAdjust(lineNum, Alignment.ALIGN_CENTER);
+ x = ((right + left - max) >> 1) + indentWidth;
}
}
paint.setHyphenEdit(getHyphen(lineNum));
Directions directions = getLineDirections(lineNum);
- if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab) {
+ if (directions == DIRS_ALL_LEFT_TO_RIGHT && !mSpannedText && !hasTab && !justify) {
// XXX: assumes there's nothing additional to be done
canvas.drawText(buf, start, end, x, lbaseline, paint);
} else {
tl.set(paint, buf, start, end, dir, directions, hasTab, tabStops);
+ if (justify) {
+ tl.justify(right - left - indentWidth);
+ }
tl.draw(canvas, x, ltop, lbaseline, lbottom);
}
paint.setHyphenEdit(0);
}
/**
+ * Return the total height of this layout.
+ *
+ * @param cap if true and max lines is set, returns the height of the layout at the max lines.
+ *
+ * @hide
+ */
+ public int getHeight(boolean cap) {
+ return getHeight();
+ }
+
+ /**
* Return the base alignment of this layout.
*/
public final Alignment getAlignment() {
if (limit > lineEnd) {
limit = lineEnd;
}
+ if (limit == start) {
+ continue;
+ }
level[limit - lineStart - 1] =
(byte) ((runs[i + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK);
}
}
/**
- * Computes in linear time the results of calling
- * #getHorizontal for all offsets on a line.
+ * Computes in linear time the results of calling #getHorizontal for all offsets on a line.
+ *
* @param line The line giving the offsets we compute information for
* @param clamped Whether to clamp the results to the width of the layout
* @param primary Whether the results should be the primary or the secondary horizontal
TextLine.recycle(tl);
if (clamped) {
- for (int offset = 0; offset <= wid.length; ++offset) {
+ for (int offset = 0; offset < wid.length; ++offset) {
if (wid[offset] > mWidth) {
wid[offset] = mWidth;
}
int dir = getParagraphDirection(line);
TextLine tl = TextLine.obtain();
+ mPaint.setHyphenEdit(getHyphen(line));
tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
+ if (isJustificationRequired(line)) {
+ tl.justify(getJustifyWidth(line));
+ }
float width = tl.metrics(null);
+ mPaint.setHyphenEdit(0);
TextLine.recycle(tl);
return width;
}
int dir = getParagraphDirection(line);
TextLine tl = TextLine.obtain();
+ mPaint.setHyphenEdit(getHyphen(line));
tl.set(mPaint, mText, start, end, dir, directions, hasTabs, tabStops);
+ if (isJustificationRequired(line)) {
+ tl.justify(getJustifyWidth(line));
+ }
float width = tl.metrics(null);
+ mPaint.setHyphenEdit(0);
TextLine.recycle(tl);
return width;
}
low = guess;
}
- if (low < 0)
+ if (low < 0) {
return 0;
- else
+ } else {
return low;
+ }
}
/**
guess = (high + low) / 2;
int adguess = getOffsetAtStartOf(guess);
- if (horizontal.get(adguess) * swap >= horiz * swap)
+ if (horizontal.get(adguess) * swap >= horiz * swap) {
high = guess;
- else
+ } else {
low = guess;
+ }
}
if (low < here + 1)
return end - 1;
}
- // Note: keep this in sync with Minikin LineBreaker::isLineEndSpace()
- if (!(ch == ' ' || ch == '\t' || ch == 0x1680 ||
- (0x2000 <= ch && ch <= 0x200A && ch != 0x2007) ||
- ch == 0x205F || ch == 0x3000)) {
+ if (!TextLine.isLineEndSpace(ch)) {
break;
}
}
/* package */
- static float measurePara(TextPaint paint, CharSequence text, int start, int end) {
-
+ static float measurePara(TextPaint paint, CharSequence text, int start, int end,
+ TextDirectionHeuristic textDir) {
MeasuredText mt = MeasuredText.obtain();
TextLine tl = TextLine.obtain();
try {
- mt.setPara(text, start, end, TextDirectionHeuristics.LTR, null);
+ mt.setPara(text, start, end, textDir, null);
Directions directions;
int dir;
if (mt.mEasy) {
}
}
tl.set(paint, text, start, end, dir, directions, hasTabs, tabStops);
- return margin + tl.metrics(null);
+ return margin + Math.abs(tl.metrics(null));
} finally {
TextLine.recycle(tl);
MeasuredText.recycle(mt);
// To simply test for an RTL direction, test the bit using
// DIR_RTL_FLAG, if set then the direction is rtl.
- /* package */ int[] mDirections;
- /* package */ Directions(int[] dirs) {
+ /**
+ * @hide
+ */
+ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
+ public int[] mDirections;
+
+ /**
+ * @hide
+ */
+ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
+ public Directions(int[] dirs) {
mDirections = dirs;
}
}
private boolean mSpannedText;
private TextDirectionHeuristic mTextDir;
private SpanSet<LineBackgroundSpan> mLineBackgroundSpans;
+ private int mJustificationMode;
public static final int DIR_LEFT_TO_RIGHT = 1;
public static final int DIR_RIGHT_TO_LEFT = -1;
private static final int TAB_INCREMENT = 20;
- /* package */ static final Directions DIRS_ALL_LEFT_TO_RIGHT =
+ /** @hide */
+ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
+ public static final Directions DIRS_ALL_LEFT_TO_RIGHT =
new Directions(new int[] { 0, RUN_LENGTH_MASK });
- /* package */ static final Directions DIRS_ALL_RIGHT_TO_LEFT =
+
+ /** @hide */
+ @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
+ public static final Directions DIRS_ALL_RIGHT_TO_LEFT =
new Directions(new int[] { 0, RUN_LENGTH_MASK | RUN_RTL_FLAG });
}