Graphics2D g = ...; Point2D loc = ...; Font font = Font.getFont("Helvetica-bold-italic"); FontRenderContext frc = g.getFontRenderContext(); TextLayout layout = new TextLayout("This is a string", font, frc); layout.draw(g, (float)loc.getX(), (float)loc.getY()); Rectangle2D bounds = layout.getBounds(); bounds.setRect(bounds.getX()+loc.getX(), bounds.getY()+loc.getY(), bounds.getWidth(), bounds.getHeight()); g.draw(bounds);
/** * Check if the Fonts are installed correctly so that Java can compute the size of * columns. * * If a Cell uses a Font which is not available on the operating system then Java may * fail to return useful Font metrics and thus lead to an auto-computed size of 0. * * This method allows to check if computing the sizes for a given Font will succeed or not. * * @param font The Font that is used in the Cell * @return true if computing the size for this Font will succeed, false otherwise */ public static boolean canComputeColumnWidth(Font font) { // not sure what is the best value sample-here, only "1" did not work on some platforms... AttributedString str = new AttributedString("1w"); copyAttributes(font, str, 0, "1w".length()); TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); return (layout.getBounds().getWidth() > 0); }
bounds = layout.getOutline(trans).getBounds(); } else { bounds = layout.getBounds();
currentX = x + (centered ? ((boxWidth - ((Double) textLayout.getBounds().getWidth()).intValue()) / 2) : 0);
TextLayout tl = lbm.nextLayout(wrapWidth); textY += tl.getAscent(); Rectangle2D bb = tl.getBounds(); double tX = graphicInfo.getX(); if (centered) {
FontRenderContext context = g2d.getFontRenderContext(); Font font = new Font("Arial", Font.BOLD, 48); TextLayout txt = new TextLayout(text, font, context); Rectangle2D bounds = txt.getBounds(); int x = (int) ((getWidth() - (int) bounds.getWidth()) / 2); int y = (int) ((getHeight() - (bounds.getHeight() - txt.getDescent())) / 2); y += txt.getAscent() - txt.getDescent();
new TextLayout( dataPoint.label, styler.getToolTipFont(), new FontRenderContext(null, true, false)); Rectangle2D annotationRectangle = textLayout.getBounds();
private void paintSum(Graphics2D g, Rectangle2D pieBounds, double total) { // draw total value if visible if (pieStyler.isSumVisible()) { SumFormatter sumFormatter = pieStyler.getSumFormatter(); String annotation = sumFormatter.format(total); TextLayout textLayout = new TextLayout( annotation, pieStyler.getSumFont(), new FontRenderContext(null, true, false)); Shape shape = textLayout.getOutline(null); g.setColor(pieStyler.getChartFontColor()); // compute center Rectangle2D annotationRectangle = textLayout.getBounds(); double xCenter = pieBounds.getX() + pieBounds.getWidth() / 2 - annotationRectangle.getWidth() / 2; double yCenter = pieBounds.getY() + pieBounds.getHeight() / 2 + annotationRectangle.getHeight() / 2; // set text AffineTransform orig = g.getTransform(); AffineTransform at = new AffineTransform(); at.translate(xCenter, yCenter); g.transform(at); g.fill(shape); g.setTransform(orig); } } }
public Rect getMetrics() { if (text == null || text.length() == 0) return new Rect(); TextLayoutIterator iterator = new TextLayoutIterator(); Rectangle2D bounds = new Rectangle2D.Double(); while (iterator.hasNext()) { TextLayout layout = iterator.next(); // TODO: Compensate X, Y bounds = bounds.createUnion(layout.getBounds()); } return new Rect(bounds); }
TextLayout txt = new TextLayout(text, font, context); Rectangle2D bounds = txt.getBounds(); int x = (int) ((getWidth() - (int) bounds.getWidth()) / 2); int y = (int) ((getHeight() - (bounds.getHeight() - txt.getDescent())) / 2);
pieStyler.getAnnotationsFont(), new FontRenderContext(null, true, false)); Rectangle2D annotationRectangle = textLayout.getBounds();
axesChartStyler.getAxisTitleFont(), new FontRenderContext(null, true, false)); Rectangle2D rectangle = textLayout.getBounds(); titleHeight = rectangle.getHeight() + axesChartStyler.getAxisTitlePadding(); axesChartStyler.getAxisTickLabelsFont(), new FontRenderContext(null, true, false)); Rectangle2D rectangle = textLayout.getBounds();
/** * get the height of the chart title including the chart title padding * * @return a Rectangle2D defining the height of the chart title including the chart title padding */ private Rectangle2D getBoundsHint() { if (chart.getStyler().isChartTitleVisible() && chart.getTitle().length() > 0) { TextLayout textLayout = new TextLayout( chart.getTitle(), chart.getStyler().getChartTitleFont(), new FontRenderContext(null, true, false)); Rectangle2D rectangle = textLayout.getBounds(); double width = 2 * chart.getStyler().getChartTitlePadding() + rectangle.getWidth(); double height = 2 * chart.getStyler().getChartTitlePadding() + rectangle.getHeight(); return new Rectangle2D.Double( Double.NaN, Double.NaN, width, height); // Double.NaN indicates not sure yet. } else { return new Rectangle2D .Double(); // Constructs a new Rectangle2D, initialized to location (0, 0) and size (0, // 0). } }
TextLayout nonRotatedTextLayout = new TextLayout(yAxisTitle, chart.getStyler().getAxisTitleFont(), frc); Rectangle2D nonRotatedRectangle = nonRotatedTextLayout.getBounds(); TextLayout textLayout = new TextLayout(chart.getXAxisTitle(), chart.getStyler().getAxisTitleFont(), frc); Rectangle2D rectangle = textLayout.getBounds();
TextLayout textLayout = new TextLayout(chart.getTitle(), chart.getStyler().getChartTitleFont(), frc); Rectangle2D textBounds = textLayout.getBounds();
axesChartStyler.getAxisTitleFont(), new FontRenderContext(null, true, false)); Rectangle2D rectangle = textLayout.getBounds(); titleHeight = rectangle.getHeight() + axesChartStyler.getAxisTitlePadding();
TextLayout axisLabelTextLayout = new TextLayout(tickLabel, styler.getAxisTickLabelsFont(), frc); Rectangle2D tickLabelBounds = axisLabelTextLayout.getBounds(); double boundWidth = tickLabelBounds.getWidth(); if (boundWidth > maxTickLabelWidth) {
stylerCategory.getAnnotationsFont(), new FontRenderContext(null, true, false)); Rectangle2D annotationRectangle = textLayout.getBounds();
public static void main(final String[] args) throws Exception { final FontRenderContext frc = new FontRenderContext(null, true, true); final Font font = new Font("serif", Font.PLAIN, 18); final TextLayout layout = new TextLayout("This is a test", font, frc); final Rectangle2D bounds = layout.getBounds(); System.err.println((int) (bounds.getWidth() + .5)); }
@Override protected Params getParams (MusicFont font) { MyParams p = new MyParams(); // Full symbol (arc + dot) p.layout = font.layout(codes); p.rect = p.layout.getBounds().getBounds(); // Dot layout p.dotLayout = font.layout(dotSymbol); return p; }