Skip to content

Commit ee42693

Browse files
committed
Changed wrapping so if whitespace needs to wrap, it also takes the previous word with it.
closes #3234
1 parent 9b16dea commit ee42693

3 files changed

Lines changed: 17 additions & 26 deletions

File tree

gdx/src/com/badlogic/gdx/graphics/g2d/BitmapFont.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -788,12 +788,12 @@ public void getGlyphs (GlyphRun run, CharSequence str, int start, int end) {
788788
/** Returns the first valid glyph index to use to wrap to the next line, starting at the specified start index and
789789
* (typically) moving toward the beginning of the glyphs array. */
790790
public int getWrapIndex (Array<Glyph> glyphs, int start) {
791-
char ch = (char)glyphs.get(start).id;
792-
if (isWhitespace(ch)) return start;
793-
for (int i = start - 1; i >= 1; i--) {
794-
ch = (char)glyphs.get(i).id;
795-
if (isWhitespace(ch)) return i;
796-
if (isBreakChar(ch)) return i + 1;
791+
int i = start - 1;
792+
for (; i >= 1; i--)
793+
if (!isWhitespace((char)glyphs.get(i).id)) break;
794+
for (; i >= 1; i--) {
795+
char ch = (char)glyphs.get(i).id;
796+
if (isWhitespace(ch) || isBreakChar(ch)) return i + 1;
797797
}
798798
return 0;
799799
}

gdx/src/com/badlogic/gdx/graphics/g2d/GlyphLayout.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ else if (targetWidth <= font.data.spaceWidth) //
155155
break outer;
156156
}
157157

158-
int wrapIndex = fontData.getWrapIndex(run.glyphs, i - 1);
158+
int wrapIndex = fontData.getWrapIndex(run.glyphs, i);
159159
if ((run.x == 0 && wrapIndex == 0) // Require at least one glyph per line.
160160
|| wrapIndex >= run.glyphs.size) { // Wrap at least the glyph that didn't fit.
161161
wrapIndex = i - 1;
@@ -272,39 +272,30 @@ private GlyphRun wrap (BitmapFontData fontData, GlyphRun first, Pool<GlyphRun> g
272272
second.color.set(first.color);
273273
int glyphCount = first.glyphs.size;
274274

275-
// Determine index where first run ends, ignoring whitespace before the wrap index.
276-
int endIndex = wrapIndex;
277-
for (; endIndex > 0; endIndex--)
278-
if (!fontData.isWhitespace((char)first.glyphs.get(endIndex - 1).id)) break;
279-
280-
// Determine index where second run starts, ignoring whitespace after the wrap index.
281-
int startIndex = wrapIndex;
282-
for (; startIndex < glyphCount; startIndex++)
283-
if (!fontData.isWhitespace((char)first.glyphs.get(startIndex).id)) break;
284-
285275
// Copy wrapped glyphs and xAdvances to second run.
286-
if (startIndex < glyphCount) {
287-
second.glyphs.addAll(first.glyphs, startIndex, glyphCount - startIndex);
276+
if (wrapIndex < glyphCount) {
277+
second.glyphs.addAll(first.glyphs, wrapIndex, glyphCount - wrapIndex);
278+
// second.xAdvances.add(-second.glyphs.first().xoffset * fontData.scaleX - fontData.padLeft);
288279
second.xAdvances.add(-second.glyphs.first().xoffset * fontData.scaleX - fontData.padLeft);
289-
second.xAdvances.addAll(first.xAdvances, startIndex + 1, first.xAdvances.size - (startIndex + 1));
280+
second.xAdvances.addAll(first.xAdvances, wrapIndex + 1, first.xAdvances.size - (wrapIndex + 1));
290281
}
291282

292283
// Increase first run width up to the end index.
293-
while (widthIndex < endIndex)
284+
while (widthIndex < wrapIndex)
294285
first.width += first.xAdvances.get(widthIndex++);
295286

296287
// Reduce first run width by the wrapped glyphs that have contributed to the width.
297-
while (widthIndex > endIndex + 1)
288+
while (widthIndex > wrapIndex + 1)
298289
first.width -= first.xAdvances.get(--widthIndex);
299290

300-
if (endIndex == 0) {
291+
if (wrapIndex == 0) {
301292
// If the first run is now empty, remove it.
302293
glyphRunPool.free(first);
303294
runs.pop();
304295
} else {
305296
// Truncate wrapped glyphs from first run.
306-
first.glyphs.truncate(endIndex);
307-
first.xAdvances.truncate(endIndex + 1);
297+
first.glyphs.truncate(wrapIndex);
298+
first.xAdvances.truncate(wrapIndex + 1);
308299
adjustLastGlyph(fontData, first);
309300
}
310301
return second;

tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void render () {
110110
String text = "your new";
111111
// text = "How quickly [RED]daft jumping zebras vex.";
112112
// text = "Another font wrap is-sue, this time with multiple whitespace characters.";
113-
text = "test with AGWlWi AGWlWi issue";
113+
text = "test with AGWlWi AGWlWi issue";
114114
if (true) { // Test wrap.
115115
layout.setText(font, text, 0, text.length(), font.getColor(), w, Align.center, true, null);
116116
} else { // Test truncation.

0 commit comments

Comments
 (0)