public static JavaTextLayout[] layoutText (JavaGraphics gfx, String text, TextFormat format, TextWrap wrap) { // normalize newlines in the text (Windows: CRLF -> LF, Mac OS pre-X: CR -> LF) text = normalizeEOL(text); // we do some fiddling to work around the fact that TextLayout chokes on the empty string String ltext = text.length() == 0 ? " " : text; // set up an attributed character iterator so that we can measure the text AttributedString astring = new AttributedString(ltext); if (format.font != null) { astring.addAttribute(TextAttribute.FONT, gfx.resolveFont(format.font)); } List<JavaTextLayout> layouts = new ArrayList<JavaTextLayout>(); FontRenderContext frc = format.antialias ? gfx.aaFontContext() : gfx.aFontContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(astring.getIterator(), frc); int lastPos = ltext.length(), curPos = 0; char eol = '\n'; while (curPos < lastPos) { int nextRet = ltext.indexOf(eol, measurer.getPosition()+1); if (nextRet == -1) { nextRet = lastPos; } TextLayout layout = measurer.nextLayout(wrap.width, nextRet, false); int endPos = measurer.getPosition(); while (curPos < endPos && ltext.charAt(curPos) == eol) curPos += 1; // skip over EOLs layouts.add(new JavaTextLayout(ltext.substring(curPos, endPos), format, layout)); curPos = endPos; } return layouts.toArray(new JavaTextLayout[layouts.size()]); }
public static JavaTextLayout[] layoutText (JavaGraphics gfx, String text, TextFormat format, TextWrap wrap) { // normalize newlines in the text (Windows: CRLF -> LF, Mac OS pre-X: CR -> LF) text = normalizeEOL(text); // we do some fiddling to work around the fact that TextLayout chokes on the empty string String ltext = text.length() == 0 ? " " : text; // set up an attributed character iterator so that we can measure the text AttributedString astring = new AttributedString(ltext); if (format.font != null) { astring.addAttribute(TextAttribute.FONT, gfx.resolveFont(format.font)); } List<JavaTextLayout> layouts = new ArrayList<JavaTextLayout>(); FontRenderContext frc = format.antialias ? gfx.aaFontContext() : gfx.aFontContext(); LineBreakMeasurer measurer = new LineBreakMeasurer(astring.getIterator(), frc); int lastPos = ltext.length(), curPos = 0; char eol = '\n'; while (curPos < lastPos) { int nextRet = ltext.indexOf(eol, measurer.getPosition()+1); if (nextRet == -1) { nextRet = lastPos; } TextLayout layout = measurer.nextLayout(wrap.width, nextRet, false); int endPos = measurer.getPosition(); while (curPos < endPos && ltext.charAt(curPos) == eol) curPos += 1; // skip over EOLs layouts.add(new JavaTextLayout(ltext.substring(curPos, endPos), format, layout)); curPos = endPos; } return layouts.toArray(new JavaTextLayout[layouts.size()]); }
public static JavaTextLayout layoutText (JavaGraphics gfx, String text, TextFormat format) { // we do some fiddling to work around the fact that TextLayout chokes on the empty string AttributedString astring = new AttributedString(text.length() == 0 ? " " : text); if (format.font != null) { astring.addAttribute(TextAttribute.FONT, gfx.resolveFont(format.font)); } FontRenderContext frc = format.antialias ? gfx.aaFontContext() : gfx.aFontContext(); return new JavaTextLayout(text, format, new TextLayout(astring.getIterator(), frc)); }
public static JavaTextLayout layoutText (JavaGraphics gfx, String text, TextFormat format) { // we do some fiddling to work around the fact that TextLayout chokes on the empty string AttributedString astring = new AttributedString(text.length() == 0 ? " " : text); if (format.font != null) { astring.addAttribute(TextAttribute.FONT, gfx.resolveFont(format.font)); } FontRenderContext frc = format.antialias ? gfx.aaFontContext() : gfx.aFontContext(); return new JavaTextLayout(text, format, new TextLayout(astring.getIterator(), frc)); }