Permalink
Browse files

Changed wrapping so if whitespace needs to wrap, it also takes the pr…

…evious word with it.

closes #3234
  • Loading branch information...
NathanSweet committed Jun 27, 2015
1 parent 9b16dea commit ee42693da067da7c5ddd747f051c1423d262cb96
@@ -788,12 +788,12 @@ public void getGlyphs (GlyphRun run, CharSequence str, int start, int end) {
/** Returns the first valid glyph index to use to wrap to the next line, starting at the specified start index and
* (typically) moving toward the beginning of the glyphs array. */
public int getWrapIndex (Array<Glyph> glyphs, int start) {
char ch = (char)glyphs.get(start).id;
if (isWhitespace(ch)) return start;
for (int i = start - 1; i >= 1; i--) {
ch = (char)glyphs.get(i).id;
if (isWhitespace(ch)) return i;
if (isBreakChar(ch)) return i + 1;
int i = start - 1;
for (; i >= 1; i--)
if (!isWhitespace((char)glyphs.get(i).id)) break;
for (; i >= 1; i--) {
char ch = (char)glyphs.get(i).id;
if (isWhitespace(ch) || isBreakChar(ch)) return i + 1;
}
return 0;
}
@@ -155,7 +155,7 @@ else if (targetWidth <= font.data.spaceWidth) //
break outer;
}
int wrapIndex = fontData.getWrapIndex(run.glyphs, i - 1);
int wrapIndex = fontData.getWrapIndex(run.glyphs, i);
if ((run.x == 0 && wrapIndex == 0) // Require at least one glyph per line.
|| wrapIndex >= run.glyphs.size) { // Wrap at least the glyph that didn't fit.
wrapIndex = i - 1;
@@ -272,39 +272,30 @@ private GlyphRun wrap (BitmapFontData fontData, GlyphRun first, Pool<GlyphRun> g
second.color.set(first.color);
int glyphCount = first.glyphs.size;
// Determine index where first run ends, ignoring whitespace before the wrap index.
int endIndex = wrapIndex;
for (; endIndex > 0; endIndex--)
if (!fontData.isWhitespace((char)first.glyphs.get(endIndex - 1).id)) break;
// Determine index where second run starts, ignoring whitespace after the wrap index.
int startIndex = wrapIndex;
for (; startIndex < glyphCount; startIndex++)
if (!fontData.isWhitespace((char)first.glyphs.get(startIndex).id)) break;
// Copy wrapped glyphs and xAdvances to second run.
if (startIndex < glyphCount) {
second.glyphs.addAll(first.glyphs, startIndex, glyphCount - startIndex);
if (wrapIndex < glyphCount) {
second.glyphs.addAll(first.glyphs, wrapIndex, glyphCount - wrapIndex);
// second.xAdvances.add(-second.glyphs.first().xoffset * fontData.scaleX - fontData.padLeft);
second.xAdvances.add(-second.glyphs.first().xoffset * fontData.scaleX - fontData.padLeft);
second.xAdvances.addAll(first.xAdvances, startIndex + 1, first.xAdvances.size - (startIndex + 1));
second.xAdvances.addAll(first.xAdvances, wrapIndex + 1, first.xAdvances.size - (wrapIndex + 1));
}
// Increase first run width up to the end index.
while (widthIndex < endIndex)
while (widthIndex < wrapIndex)
first.width += first.xAdvances.get(widthIndex++);
// Reduce first run width by the wrapped glyphs that have contributed to the width.
while (widthIndex > endIndex + 1)
while (widthIndex > wrapIndex + 1)
first.width -= first.xAdvances.get(--widthIndex);
if (endIndex == 0) {
if (wrapIndex == 0) {
// If the first run is now empty, remove it.
glyphRunPool.free(first);
runs.pop();
} else {
// Truncate wrapped glyphs from first run.
first.glyphs.truncate(endIndex);
first.xAdvances.truncate(endIndex + 1);
first.glyphs.truncate(wrapIndex);
first.xAdvances.truncate(wrapIndex + 1);
adjustLastGlyph(fontData, first);
}
return second;
@@ -110,7 +110,7 @@ public void render () {
String text = "your new";
// text = "How quickly [RED]daft jumping zebras vex.";
// text = "Another font wrap is-sue, this time with multiple whitespace characters.";
text = "test with AGWlWi AGWlWi issue";
text = "test with AGWlWi AGWlWi issue";
if (true) { // Test wrap.
layout.setText(font, text, 0, text.length(), font.getColor(), w, Align.center, true, null);
} else { // Test truncation.

0 comments on commit ee42693

Please sign in to comment.