Merge "Fix that TextLine overwrites wordSpcing on paint"

This commit is contained in:
Haoyu Zhang
2019-01-09 03:55:15 +00:00
committed by Android (Google) Code Review
3 changed files with 29 additions and 6 deletions

View File

@@ -376,9 +376,13 @@ public class StaticLayout extends Layout {
* Set paragraph justification mode. The default value is
* {@link Layout#JUSTIFICATION_MODE_NONE}. If the last line is too short for justification,
* the last line will be displayed with the alignment set by {@link #setAlignment}.
* When Justification mode is JUSTIFICATION_MODE_INTER_WORD, wordSpacing on the given
* {@link Paint} will be ignored. This behavior also affects Spans which change the
* wordSpacing.
*
* @param justificationMode justification mode for the paragraph.
* @return this builder, useful for chaining.
* @see Paint#setWordSpacing(float)
*/
@NonNull
public Builder setJustificationMode(@JustificationMode int justificationMode) {

View File

@@ -75,8 +75,9 @@ public class TextLine {
private int mEllipsisEnd;
// Additional width of whitespace for justification. This value is per whitespace, thus
// the line width will increase by mAddedWidth x (number of stretchable whitespaces).
private float mAddedWidth;
// the line width will increase by mAddedWidthForJustify x (number of stretchable whitespaces).
private float mAddedWidthForJustify;
private boolean mIsJustifying;
private final TextPaint mWorkPaint = new TextPaint();
private final TextPaint mActivePaint = new TextPaint();
@@ -229,7 +230,8 @@ public class TextLine {
}
}
mTabs = tabStops;
mAddedWidth = 0;
mAddedWidthForJustify = 0;
mIsJustifying = false;
mEllipsisStart = ellipsisStart != ellipsisEnd ? ellipsisStart : 0;
mEllipsisEnd = ellipsisStart != ellipsisEnd ? ellipsisEnd : 0;
@@ -255,7 +257,8 @@ public class TextLine {
return;
}
final float width = Math.abs(measure(end, false, null));
mAddedWidth = (justifyWidth - width) / spaces;
mAddedWidthForJustify = (justifyWidth - width) / spaces;
mIsJustifying = true;
}
/**
@@ -713,7 +716,9 @@ public class TextLine {
TextPaint wp = mWorkPaint;
wp.set(mPaint);
wp.setWordSpacing(mAddedWidth);
if (mIsJustifying) {
wp.setWordSpacing(mAddedWidthForJustify);
}
int spanStart = runStart;
int spanLimit;
@@ -849,7 +854,9 @@ public class TextLine {
FontMetricsInt fmi, boolean needWidth, int offset,
@Nullable ArrayList<DecorationInfo> decorations) {
wp.setWordSpacing(mAddedWidth);
if (mIsJustifying) {
wp.setWordSpacing(mAddedWidthForJustify);
}
// Get metrics first (even for empty strings or "0" width runs)
if (fmi != null) {
expandMetricsFromPaint(fmi, wp);

View File

@@ -253,6 +253,18 @@ public class TextLineTest {
new float[]{0.0f, -10.0f, -10.0f, -100.0f, -110.0f, -110.0f});
}
@Test
public void testMeasure_wordSpacing() {
final TextPaint paint = new TextPaint();
paint.setTypeface(TYPEFACE);
paint.setTextSize(10.0f); // make 1em = 10px
paint.setWordSpacing(10.0f);
TextLine tl = getTextLine("I I", paint);
assertMeasurements(tl, 3, false,
new float[]{0.0f, 10.0f, 120.0f, 130.0f});
}
@Test
public void testHandleRun_ellipsizedReplacementSpan_isSkipped() {
final Spannable text = new SpannableStringBuilder("This is a... text");