Skip to content

Commit d300e75

Browse files
author
Gilles Debunne
committed
Wrong word cut at end of lines with spaces
Bug 5185017: when the line length is exceeded at a space character, we use the previous ok width, and the last word is wrapped to next line although it fits. This back-track also generates problem with the span parsing, where the spanStart indexes are no longer monotonuously increasing. Plus some refactoring in this code (unused parameters, calls to out()) Change-Id: Ia8cd310a732752af3bd370bf0a16db23d40e83f2
1 parent fc0115f commit d300e75

File tree

2 files changed

+65
-85
lines changed

2 files changed

+65
-85
lines changed

core/java/android/text/DynamicLayout.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ private void reflow(CharSequence s, int where, int before, int after) {
281281
}
282282

283283
reflowed.generate(text, where, where + after,
284-
getPaint(), getWidth(), getAlignment(), getTextDirectionHeuristic(),
285-
getSpacingMultiplier(), getSpacingAdd(),
286-
false, true, mEllipsizedWidth, mEllipsizeAt);
284+
getPaint(), getWidth(), getTextDirectionHeuristic(), getSpacingMultiplier(),
285+
getSpacingAdd(), false,
286+
true, mEllipsizedWidth, mEllipsizeAt);
287287
int n = reflowed.getLineCount();
288288

289289
// If the new layout has a blank line at the end, but it is not

core/java/android/text/StaticLayout.java

Lines changed: 62 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ public StaticLayout(CharSequence source, int bufstart, int bufend,
137137

138138
mMeasured = MeasuredText.obtain();
139139

140-
generate(source, bufstart, bufend, paint, outerwidth, align, textDir,
141-
spacingmult, spacingadd, includepad, includepad,
142-
ellipsizedWidth, ellipsize);
140+
generate(source, bufstart, bufend, paint, outerwidth, textDir, spacingmult,
141+
spacingadd, includepad, includepad, ellipsizedWidth,
142+
ellipsize);
143143

144144
mMeasured = MeasuredText.recycle(mMeasured);
145145
mFontMetricsInt = null;
@@ -157,10 +157,10 @@ public StaticLayout(CharSequence source, int bufstart, int bufend,
157157

158158
/* package */ void generate(CharSequence source, int bufStart, int bufEnd,
159159
TextPaint paint, int outerWidth,
160-
Alignment align, TextDirectionHeuristic textDir,
161-
float spacingmult, float spacingadd,
162-
boolean includepad, boolean trackpad,
163-
float ellipsizedWidth, TextUtils.TruncateAt ellipsize) {
160+
TextDirectionHeuristic textDir, float spacingmult,
161+
float spacingadd, boolean includepad,
162+
boolean trackpad, float ellipsizedWidth,
163+
TextUtils.TruncateAt ellipsize) {
164164
mLineCount = 0;
165165

166166
int v = 0;
@@ -328,9 +328,7 @@ public StaticLayout(CharSequence source, int bufstart, int bufend,
328328
whichPaint = mWorkPaint;
329329
}
330330

331-
float wid = bm.getWidth() *
332-
-whichPaint.ascent() /
333-
bm.getHeight();
331+
float wid = bm.getWidth() * -whichPaint.ascent() / bm.getHeight();
334332

335333
w += wid;
336334
hasTabOrEmoji = true;
@@ -398,67 +396,49 @@ public StaticLayout(CharSequence source, int bufstart, int bufend,
398396
okBottom = fitBottom;
399397
}
400398
} else {
401-
final boolean moreChars = (j + 1 < spanEnd);
402-
if (ok != here) {
403-
// Log.e("text", "output ok " + here + " to " +ok);
399+
final boolean moreChars = (j + 1 < spanEnd);
400+
int endPos;
401+
int above, below, top, bottom;
402+
float currentTextWidth;
404403

405-
while (ok < spanEnd && chs[ok - paraStart] == CHAR_SPACE) {
406-
ok++;
407-
}
404+
if (ok != here) {
405+
// If it is a space that makes the length exceed width, cut here
406+
if (c == CHAR_SPACE) ok = j + 1;
408407

409-
v = out(source,
410-
here, ok,
411-
okAscent, okDescent, okTop, okBottom,
412-
v,
413-
spacingmult, spacingadd, chooseHt,
414-
chooseHtv, fm, hasTabOrEmoji,
415-
needMultiply, paraStart, chdirs, dir, easy,
416-
ok == bufEnd, includepad, trackpad,
417-
chs, widths, paraStart,
418-
ellipsize, ellipsizedWidth, okWidth,
419-
paint, moreChars);
420-
421-
here = ok;
422-
} else if (fit != here) {
423-
// Log.e("text", "output fit " + here + " to " +fit);
424-
v = out(source,
425-
here, fit,
426-
fitAscent, fitDescent,
427-
fitTop, fitBottom,
428-
v,
429-
spacingmult, spacingadd, chooseHt,
430-
chooseHtv, fm, hasTabOrEmoji,
431-
needMultiply, paraStart, chdirs, dir, easy,
432-
fit == bufEnd, includepad, trackpad,
433-
chs, widths, paraStart,
434-
ellipsize, ellipsizedWidth, fitWidth,
435-
paint, moreChars);
436-
437-
here = fit;
438-
} else {
439-
// Log.e("text", "output one " + here + " to " +(here + 1));
440-
// XXX not sure why the existing fm wasn't ok.
441-
// measureText(paint, mWorkPaint,
442-
// source, here, here + 1, fm, tab,
443-
// null);
444-
445-
v = out(source,
446-
here, here+1,
447-
fm.ascent, fm.descent,
448-
fm.top, fm.bottom,
449-
v,
450-
spacingmult, spacingadd, chooseHt,
451-
chooseHtv, fm, hasTabOrEmoji,
452-
needMultiply, paraStart, chdirs, dir, easy,
453-
here + 1 == bufEnd, includepad,
454-
trackpad,
455-
chs, widths, paraStart,
456-
ellipsize, ellipsizedWidth,
457-
widths[here - paraStart], paint, moreChars);
458-
459-
here = here + 1;
408+
while (ok < spanEnd && chs[ok - paraStart] == CHAR_SPACE) {
409+
ok++;
460410
}
461411

412+
endPos = ok;
413+
above = okAscent;
414+
below = okDescent;
415+
top = okTop;
416+
bottom = okBottom;
417+
currentTextWidth = okWidth;
418+
} else if (fit != here) {
419+
endPos = fit;
420+
above = fitAscent;
421+
below = fitDescent;
422+
top = fitTop;
423+
bottom = fitBottom;
424+
currentTextWidth = fitWidth;
425+
} else {
426+
endPos = here + 1;
427+
above = fm.ascent;
428+
below = fm.descent;
429+
top = fm.top;
430+
bottom = fm.bottom;
431+
currentTextWidth = widths[here - paraStart];
432+
}
433+
434+
v = out(source, here, endPos,
435+
above, below, top, bottom,
436+
v, spacingmult, spacingadd, chooseHt,chooseHtv, fm, hasTabOrEmoji,
437+
needMultiply, chdirs, dir, easy, bufEnd, includepad, trackpad,
438+
chs, widths, paraStart, ellipsize, ellipsizedWidth,
439+
currentTextWidth, paint, moreChars);
440+
here = endPos;
441+
462442
if (here < spanStart) {
463443
// didn't output all the text for this span
464444
// we've measured the raw widths, though, so
@@ -501,10 +481,10 @@ public StaticLayout(CharSequence source, int bufstart, int bufend,
501481
v,
502482
spacingmult, spacingadd, chooseHt,
503483
chooseHtv, fm, hasTabOrEmoji,
504-
needMultiply, paraStart, chdirs, dir, easy,
505-
paraEnd == bufEnd, includepad, trackpad,
506-
chs, widths, paraStart,
507-
ellipsize, ellipsizedWidth, w, paint, paraEnd != bufEnd);
484+
needMultiply, chdirs, dir, easy, bufEnd,
485+
includepad, trackpad, chs,
486+
widths, paraStart, ellipsize,
487+
ellipsizedWidth, w, paint, paraEnd != bufEnd);
508488
}
509489

510490
paraStart = paraEnd;
@@ -525,10 +505,10 @@ public StaticLayout(CharSequence source, int bufstart, int bufend,
525505
v,
526506
spacingmult, spacingadd, null,
527507
null, fm, false,
528-
needMultiply, bufEnd, null, DEFAULT_DIR, true,
529-
true, includepad, trackpad,
530-
null, null, bufStart,
531-
ellipsize, ellipsizedWidth, 0, paint, false);
508+
needMultiply, null, DEFAULT_DIR, true, bufEnd,
509+
includepad, trackpad, null,
510+
null, bufStart, ellipsize,
511+
ellipsizedWidth, 0, paint, false);
532512
}
533513
}
534514

@@ -628,12 +608,12 @@ private int out(CharSequence text, int start, int end,
628608
float spacingmult, float spacingadd,
629609
LineHeightSpan[] chooseHt, int[] chooseHtv,
630610
Paint.FontMetricsInt fm, boolean hasTabOrEmoji,
631-
boolean needMultiply, int pstart, byte[] chdirs,
632-
int dir, boolean easy, boolean last,
633-
boolean includePad, boolean trackPad,
634-
char[] chs, float[] widths, int widthStart,
635-
TextUtils.TruncateAt ellipsize, float ellipsisWidth,
636-
float textWidth, TextPaint paint, boolean moreChars) {
611+
boolean needMultiply, byte[] chdirs, int dir,
612+
boolean easy, int bufEnd, boolean includePad,
613+
boolean trackPad, char[] chs,
614+
float[] widths, int widthStart, TextUtils.TruncateAt ellipsize,
615+
float ellipsisWidth, float textWidth,
616+
TextPaint paint, boolean moreChars) {
637617
int j = mLineCount;
638618
int off = j * mColumns;
639619
int want = off + mColumns + TOP;
@@ -683,7 +663,7 @@ private int out(CharSequence text, int start, int end,
683663
above = top;
684664
}
685665
}
686-
if (last) {
666+
if (end == bufEnd) {
687667
if (trackPad) {
688668
mBottomPadding = bottom - below;
689669
}

0 commit comments

Comments
 (0)