use of java.awt.font.TextLayout in project hid-serial by rayshobby.
the class StyledString method getLines.
/**
* Get the text layouts for display if the string has changed since last call
* to this method regenerate them.
*
* @param g2d Graphics2D display context
* @return a list of text layouts for rendering
*/
public LinkedList<TextLayoutInfo> getLines(Graphics2D g2d) {
if (font != g2d.getFont()) {
setFont(g2d.getFont());
invalidText = true;
}
if (invalidText) {
styledText = new AttributedString(plainText);
styledText = insertParagraphMarkers(plainText, styledText);
applyAttributes();
invalidText = false;
invalidLayout = true;
}
if (invalidLayout) {
linesInfo.clear();
if (plainText.length() > 0) {
textHeight = 0;
maxLineLength = 0;
maxLineHeight = 0;
nbrLines = 0;
AttributedCharacterIterator paragraph = styledText.getIterator(null, 0, plainText.length());
FontRenderContext frc = g2d.getFontRenderContext();
lineMeasurer = new LineBreakMeasurer(paragraph, frc);
float yposinpara = 0;
int charssofar = 0;
while (lineMeasurer.getPosition() < plainText.length()) {
TextLayout layout = lineMeasurer.nextLayout(wrapWidth);
float advance = layout.getVisibleAdvance();
if (justify) {
if (justify && advance > justifyRatio * wrapWidth) {
//System.out.println(layout.getVisibleAdvance() + " " + breakWidth + " "+ layout.get);
// If advance > breakWidth then we have a line break
float jw = (advance > wrapWidth) ? advance - wrapWidth : wrapWidth;
layout = layout.getJustifiedLayout(jw);
}
}
// Remember the longest and tallest value for a layout so far.
float lh = getHeight(layout);
if (lh > maxLineHeight)
maxLineHeight = lh;
textHeight += lh;
if (advance <= wrapWidth && advance > maxLineLength)
maxLineLength = advance;
// Store layout and line info
linesInfo.add(new TextLayoutInfo(nbrLines, layout, charssofar, layout.getCharacterCount(), yposinpara));
charssofar += layout.getCharacterCount();
yposinpara += lh;
nbrLines++;
}
}
invalidLayout = false;
}
return linesInfo;
}
use of java.awt.font.TextLayout in project processing by processing.
the class Toolkit method getAscent.
/**
* Synthesized replacement for FontMetrics.getAscent(), which is dreadfully
* inaccurate and inconsistent across platforms.
*/
public static double getAscent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
FontRenderContext frc = g2.getFontRenderContext();
//return new TextLayout("H", font, frc).getBounds().getHeight();
return new TextLayout("H", g.getFont(), frc).getBounds().getHeight();
}
use of java.awt.font.TextLayout in project playn by threerings.
the class JavaTextLayout method layoutText.
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, ((JavaFont) format.font).jfont);
}
FontRenderContext frc = format.antialias ? gfx.aaFontContext : gfx.aFontContext;
return new JavaTextLayout(text, format, new TextLayout(astring.getIterator(), frc));
}
use of java.awt.font.TextLayout in project limelight by slagyr.
the class TextPanel method paintOn.
public void paintOn(Graphics2D graphics) {
graphics.setColor(getStyle().getCompiledTextColor().getColor());
float y = 0;
if (lines == null)
return;
synchronized (this) {
for (TextLayout textLayout : lines) {
y += textLayout.getAscent();
int x = getStyle().getCompiledHorizontalAlignment().getX((int) widthOf(textLayout), new Box(0, 0, getWidth(), getHeight()));
textLayout.draw(graphics, x, y);
y += textLayout.getDescent() + textLayout.getLeading();
}
}
}
use of java.awt.font.TextLayout in project limelight by slagyr.
the class TextPanelTest method styledInheritsFromDefault.
@Test
public void styledInheritsFromDefault() {
createStyles();
parent.setSize(200, 100);
panel.setText("<size_only_style>This some text</size_only_style>", parent);
panel.buildLines();
List<TextLayout> lines = panel.getLines();
assertEquals(1, lines.size());
String onlyLine = lines.get(0).toString();
assertSubString("name=" + defaultFontFace, onlyLine);
assertSubString("size=" + "25", onlyLine);
assertSubString("style=" + defaultFontStyle, onlyLine);
StyledText first = panel.getTextChunks().get(0);
assertEquals(defaultTextColor, first.getColor());
}
Aggregations