use of java.awt.font.LineBreakMeasurer in project frostwire by frostwire.
the class SkinMultilineToolTipUI method calculatePreferredSize.
private Dimension calculatePreferredSize(JComponent c) {
Dimension dimension;
try {
String textAreaText = textArea.getText();
if (textAreaText == null || textAreaText.isEmpty()) {
dimension = new Dimension(0, 0);
} else {
AttributedString text = new AttributedString(textAreaText);
Font font = c.getFont();
FontMetrics fm = c.getFontMetrics(font);
FontRenderContext frc = fm.getFontRenderContext();
AttributedCharacterIterator charIt = text.getIterator();
LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(charIt, frc);
float formatWidth = (float) (TOOLTIP_WIDTH - 48);
lineMeasurer.setPosition(charIt.getBeginIndex());
int noLines = 0;
while (lineMeasurer.getPosition() < charIt.getEndIndex()) {
lineMeasurer.nextLayout(formatWidth);
noLines++;
}
if (lineMeasurer.getPosition() < textAreaText.length()) {
noLines++;
}
int width = TOOLTIP_WIDTH;
int height = fm.getHeight() * noLines + 10;
dimension = new Dimension(width, height);
}
} catch (Throwable e) {
// in case there is a problem with swing
dimension = new Dimension(0, 0);
}
return dimension;
}
use of java.awt.font.LineBreakMeasurer 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.LineBreakMeasurer in project OpenNotebook by jaltekruse.
the class AnswerBoxGUI method drawMathObject.
public void drawMathObject(AnswerBoxObject object, Graphics g, Point pageOrigin, float zoomLevel) {
ScaledSizeAndPosition sap = getSizeAndPositionWithFontSize(object, pageOrigin, zoomLevel, object.getFontSize());
// TODO - decide how extra whitespace should be handled, should it always be stored?
// students may use it to format a multi-line answer
// although useful whitespace will likely not coming at the very beginning or very end
// of an answer
List<? extends MathObjectAttribute> correctAnswers = object.getListWithName(AnswerBoxObject.CORRECT_ANSWERS).getValues();
if (!object.getStudentAnswer().trim().equals("") || !correctAnswers.isEmpty()) {
Font f = g.getFont();
g.setColor(new Color(150, 210, 255));
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
String message = object.getStudentAnswer();
for (MathObjectAttribute mAtt : correctAnswers) {
message += mAtt.getValue().toString() + ";";
}
message = message.substring(0, message.length() - 1);
if (message.isEmpty()) {
// cannot have an empty string in AttributedString
message = " ";
}
g.setFont(f.deriveFont(sap.getFontSize()));
g.setColor(Color.BLACK);
Graphics2D graphics2D = (Graphics2D) g;
GraphicsEnvironment.getLocalGraphicsEnvironment();
AttributedString messageAS = new AttributedString(message);
messageAS.addAttribute(TextAttribute.FONT, g.getFont());
AttributedCharacterIterator messageIterator = messageAS.getIterator();
FontRenderContext messageFRC = graphics2D.getFontRenderContext();
LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC);
Insets insets = new Insets(2, 2, 2, 2);
float wrappingWidth = sap.getWidth() - insets.left - insets.right;
float x = sap.getxOrigin() + insets.left;
float y = sap.getyOrigin() + insets.top;
while (messageLBM.getPosition() < messageIterator.getEndIndex()) {
TextLayout textLayout = messageLBM.nextLayout(wrappingWidth);
y += textLayout.getAscent();
textLayout.draw(graphics2D, x, y);
y += textLayout.getDescent() + textLayout.getLeading();
x = sap.getxOrigin() + insets.left;
}
g.setFont(f);
} else {
g.setColor(new Color(230, 230, 230));
g.fillRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
g.setColor(Color.BLACK);
g.drawRect(sap.getxOrigin(), sap.getyOrigin(), sap.getWidth(), sap.getHeight());
}
use of java.awt.font.LineBreakMeasurer in project java-swing-tips by aterai.
the class WrappedLabel method paintComponent.
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(getForeground());
Rectangle r = SwingUtilities.calculateInnerArea(this, null);
float x = r.x;
float y = r.y;
int w = r.width;
AttributedString as = new AttributedString(getText());
as.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator aci = as.getIterator();
FontRenderContext frc = g2.getFontRenderContext();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
while (lbm.getPosition() < aci.getEndIndex()) {
TextLayout tl = lbm.nextLayout(w);
tl.draw(g2, x, y + tl.getAscent());
y += tl.getDescent() + tl.getLeading() + tl.getAscent();
}
g2.dispose();
}
Aggregations