use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.
the class TestLineBreakWithFontSub method checkMeasurer.
/**
* Iterate through measurer and check that every line is
* not too long and not too short, but just right.
*/
private void checkMeasurer(LineBreakMeasurer measurer, float wrappingWidth, float sequenceAdvance, int endPosition) {
do {
TextLayout layout = measurer.nextLayout(wrappingWidth);
float visAdvance = layout.getVisibleAdvance();
if (visAdvance > wrappingWidth) {
// line is too long for given wrapping width
throw new Error("layout is too long");
}
if (measurer.getPosition() < endPosition) {
if (visAdvance <= wrappingWidth - sequenceAdvance) {
// another word would have fit
throw new Error("room for more words on line. diff=" + (wrappingWidth - sequenceAdvance - visAdvance));
}
}
} while (measurer.getPosition() != endPosition);
}
use of java.awt.font.TextLayout in project JMRI by JMRI.
the class ToolTip method paint.
public void paint(Graphics2D g2d, double scale) {
if (_tip == null || _tip.trim().length() == 0) {
return;
}
Color color = g2d.getColor();
Font font = g2d.getFont();
TextLayout tl = new TextLayout(_tip, _tFont, g2d.getFontRenderContext());
Rectangle2D bds = tl.getBounds();
int x0 = Math.max((int) (bds.getX() + _tx - bds.getWidth() / 2 - 9), 0);
bds.setRect(x0, _ty + (bds.getHeight() - 9) / scale, bds.getWidth() + 9, bds.getHeight() + 8);
g2d.setColor(_backgroundColor);
g2d.fill(bds);
g2d.setColor(_borderColor);
g2d.draw(bds);
g2d.setColor(_fontColor);
tl.draw(g2d, x0 + 3f, (float) (_ty + bds.getHeight() - 4));
g2d.setColor(color);
g2d.setFont(font);
}
use of java.awt.font.TextLayout in project jdk8u_jdk by JetBrains.
the class CompositionArea method setText.
/**
* Sets the text and caret to be displayed in this composition area.
* Shows the window if it contains text, hides it if not.
*/
void setText(AttributedCharacterIterator composedText, TextHitInfo caret) {
composedTextLayout = null;
if (composedText == null) {
// there's no composed text to display, so hide the window
compositionWindow.setVisible(false);
this.caret = null;
} else {
/* since we have composed text, make sure the window is shown.
This is necessary to get a valid graphics object. See 6181385.
*/
if (!compositionWindow.isVisible()) {
compositionWindow.setVisible(true);
}
Graphics g = getGraphics();
if (g == null) {
return;
}
try {
updateWindowLocation();
FontRenderContext context = ((Graphics2D) g).getFontRenderContext();
composedTextLayout = new TextLayout(composedText, context);
Rectangle2D bounds = composedTextLayout.getBounds();
this.caret = caret;
// Resize the composition area to just fit the text.
FontMetrics metrics = g.getFontMetrics();
Rectangle2D maxCharBoundsRec = metrics.getMaxCharBounds(g);
int newHeight = (int) maxCharBoundsRec.getHeight() + HEIGHT_MARGIN;
int newFrameHeight = newHeight + compositionWindow.getInsets().top + compositionWindow.getInsets().bottom;
// If it's a passive client, set the width always to PASSIVE_WIDTH (480px)
InputMethodRequests req = handler.getClientInputMethodRequests();
int newWidth = (req == null) ? PASSIVE_WIDTH : (int) bounds.getWidth() + WIDTH_MARGIN;
int newFrameWidth = newWidth + compositionWindow.getInsets().left + compositionWindow.getInsets().right;
setPreferredSize(new Dimension(newWidth, newHeight));
compositionWindow.setSize(new Dimension(newFrameWidth, newFrameHeight));
// show the composed text
paint(g);
} finally {
g.dispose();
}
}
}
use of java.awt.font.TextLayout in project poi by apache.
the class XDGFText method draw.
/**
* When calling this to draw text, it assumes graphics is set properly
* to draw in the right style.
*/
public void draw(Graphics2D graphics) {
String textContent = getTextContent();
if (textContent.length() == 0)
return;
Rectangle2D.Double bounds = getTextBounds();
String[] lines = textContent.trim().split("\n");
FontRenderContext frc = graphics.getFontRenderContext();
Font font = graphics.getFont();
AffineTransform oldTr = graphics.getTransform();
// visio is in flipped coordinates, so translate the text to be in the
// right place
Boolean flipX = _parent.getFlipX();
Boolean flipY = _parent.getFlipY();
if (flipY == null || !_parent.getFlipY()) {
graphics.translate(bounds.x, bounds.y);
graphics.scale(1, -1);
graphics.translate(0, -bounds.height + graphics.getFontMetrics().getMaxCharBounds(graphics).getHeight());
}
if (flipX != null && _parent.getFlipX()) {
graphics.scale(-1, 1);
graphics.translate(-bounds.width, 0);
}
Double txtAngle = _parent.getTxtAngle();
if (txtAngle != null && Math.abs(txtAngle) > 0.01)
graphics.rotate(txtAngle);
float nextY = 0;
for (String line : lines) {
if (line.length() == 0)
continue;
TextLayout layout = new TextLayout(line, font, frc);
if (layout.isLeftToRight())
layout.draw(graphics, 0, nextY);
else
layout.draw(graphics, (float) (bounds.width - layout.getAdvance()), nextY);
nextY += layout.getAscent() + layout.getDescent() + layout.getLeading();
}
graphics.setTransform(oldTr);
}
use of java.awt.font.TextLayout in project jgnash by ccavanaugh.
the class PrintableCheckLayout method drawPayee.
private void drawPayee(final Graphics2D g2, final CheckObject object, final float offset, final String payee) {
float payeeX = object.getX();
float payeeY = object.getY() + offset;
if (payee != null && !payee.isEmpty()) {
TextLayout textLayout = new TextLayout(payee, font, frc);
textLayout.draw(g2, payeeX, payeeY);
}
if (testPrint) {
TextLayout payeeText = new TextLayout("ORDER OF", testPrintFont, frc);
double width = payeeText.getBounds().getWidth();
payeeText.draw(g2, (float) (payeeX - width - space), payeeY);
LineMetrics metrics = testPrintFont.getLineMetrics("PAY TO THE", frc);
float y = payeeY - (float) payeeText.getBounds().getHeight() - metrics.getDescent() - metrics.getLeading();
payeeText = new TextLayout("PAY TO THE", testPrintFont, frc);
payeeText.draw(g2, (float) (payeeX - width - space), y);
}
}
Aggregations