@Test public void testCalculateMaxCharactersBeforeWrap() { final int estimate = 5; final float targetWidth = (5 * FONT_CHARACTER_WIDTH) + (4 * FONT_SPACING); Assert.assertEquals(3, glyphLayout.calculateMaxCharactersBeforeWrap("abc\ndef", 0, estimate, targetWidth)); Assert.assertEquals(3, glyphLayout.calculateMaxCharactersBeforeWrap("abc def", 0, estimate, targetWidth)); Assert.assertEquals(3, glyphLayout.calculateMaxCharactersBeforeWrap("abc def ghi", 4, estimate, targetWidth)); Assert.assertEquals(5, glyphLayout.calculateMaxCharactersBeforeWrap("abcdef", 0, estimate, targetWidth)); Assert.assertEquals(2, glyphLayout.calculateMaxCharactersBeforeWrap("ab\ncdef", 0, estimate, targetWidth)); Assert.assertEquals(2, glyphLayout.calculateMaxCharactersBeforeWrap("abc def", 5, estimate, targetWidth)); Assert.assertEquals(0, glyphLayout.calculateMaxCharactersBeforeWrap("\n", 0, estimate, targetWidth)); Assert.assertEquals(0, glyphLayout.calculateMaxCharactersBeforeWrap("", 0, estimate, targetWidth)); Assert.assertEquals(0, glyphLayout.calculateMaxCharactersBeforeWrap("a", 0, estimate, FONT_CHARACTER_WIDTH - 1f)); }
public int calculateMaxCharactersBeforeWrap(CharSequence str, int from, int estimate, float targetWidth) { float x = 0f; for(int i = from; i < str.length(); i++) { final char c = str.charAt(i); switch(c) { case '\r': case '\n': return i - from; } if(x + fontParameters.characterWidth > targetWidth) { if(Character.isWhitespace(c)) { return i - from; } if(c == '\n' || c == '\r') { return i - from; } for(int j = i - 1; j >= from; j--) { final char previousChar = str.charAt(j); if(Character.isWhitespace(previousChar)) { return j - from; } } return i - from; } x += fontParameters.characterWidth + fontParameters.spacing; } return Math.min(estimate, str.length() - from); }
MonospaceFontGlyphLayout implements FontGlyphLayout { public int calculateMaxCharactersBeforeWrap(CharSequence str, int from, int estimate, float targetWidth) { float x = 0f; for(int i = from; i < str.length(); i++) { final char c = str.charAt(i); switch(c) { case '\r': case '\n': return i - from; } if(x + fontParameters.characterWidth > targetWidth) { if(Character.isWhitespace(c)) { return i - from; } if(c == '\n' || c == '\r') { return i - from; } for(int j = i - 1; j >= from; j--) { final char previousChar = str.charAt(j); if(Character.isWhitespace(previousChar)) { return j - from; } } return i - from; } x += fontParameters.characterWidth + fontParameters.spacing; } return Math.min(estimate, str.length() - from); } }
MonospaceFontGlyphLayout implements FontGlyphLayout { public int calculateMaxCharactersBeforeWrap(CharSequence str, int from, int estimate, float targetWidth) { float x = 0f; for(int i = from; i < str.length(); i++) { final char c = str.charAt(i); switch(c) { case '\r': case '\n': return i - from; } if(x + fontParameters.characterWidth > targetWidth) { if(Character.isWhitespace(c)) { return i - from; } if(c == '\n' || c == '\r') { return i - from; } for(int j = i - 1; j >= from; j--) { final char previousChar = str.charAt(j); if(Character.isWhitespace(previousChar)) { return j - from; } } return i - from; } x += fontParameters.characterWidth + fontParameters.spacing; } return Math.min(estimate, str.length() - from); } MonospaceFontGlyphLayout(MonospaceGameFont monospaceFont); }
MonospaceFontGlyphLayout implements FontGlyphLayout { public int calculateMaxCharactersBeforeWrap(CharSequence str, int from, int estimate, float targetWidth) { float x = 0f; for(int i = from; i < str.length(); i++) { final char c = str.charAt(i); switch(c) { case '\r': case '\n': return i - from; } if(x + fontParameters.characterWidth > targetWidth) { if(Character.isWhitespace(c)) { return i - from; } if(c == '\n' || c == '\r') { return i - from; } for(int j = i - 1; j >= from; j--) { final char previousChar = str.charAt(j); if(Character.isWhitespace(previousChar)) { return j - from; } } return i - from; } x += fontParameters.characterWidth + fontParameters.spacing; } return Math.min(estimate, str.length() - from); } MonospaceFontGlyphLayout(MonospaceGameFont monospaceFont); @Override void setText(CharSequence str); @Override void setText(CharSequence str, Color color, float targetWidth, int halign, boolean wrap); int calculateMaxCharactersBeforeWrap(CharSequence str, int from, int estimate, float targetWidth); @Override void reset(); @Override void dispose(); @Override float getWidth(); @Override float getHeight(); @Override GameFont getFont(); void transferGlyphsTo(Array<MonospaceGlyph> result, float x, float y); Array<MonospaceGlyph> getGlyphs(); }
MonospaceFontGlyphLayout implements FontGlyphLayout { public int calculateMaxCharactersBeforeWrap(CharSequence str, int from, int estimate, float targetWidth) { float x = 0f; for(int i = from; i < str.length(); i++) { final char c = str.charAt(i); switch(c) { case '\r': case '\n': return i - from; } if(x + fontParameters.characterWidth > targetWidth) { if(Character.isWhitespace(c)) { return i - from; } if(c == '\n' || c == '\r') { return i - from; } for(int j = i - 1; j >= from; j--) { final char previousChar = str.charAt(j); if(Character.isWhitespace(previousChar)) { return j - from; } } return i - from; } x += fontParameters.characterWidth + fontParameters.spacing; } return Math.min(estimate, str.length() - from); } MonospaceFontGlyphLayout(MonospaceGameFont monospaceFont); @Override void setText(CharSequence str); @Override void setText(CharSequence str, Color color, float targetWidth, int halign, boolean wrap); int calculateMaxCharactersBeforeWrap(CharSequence str, int from, int estimate, float targetWidth); @Override void reset(); @Override void dispose(); @Override float getWidth(); @Override float getHeight(); @Override GameFont getFont(); void transferGlyphsTo(Array<MonospaceGlyph> result, float x, float y); Array<MonospaceGlyph> getGlyphs();  }