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);
public String getCharDesc(char c) { final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext()); final Shape sh = t.getOutline(null); final double current[] = new double[6]; final PathIterator it = sh.getPathIterator(null); int sum = 0; final StringBuilder result = new StringBuilder(); while (it.isDone() == false) { final double oldX = current[0]; final double oldY = current[1]; final int nb = it.currentSegment(current); sum += nb; result.append(getType(nb, oldX, oldY, current[0], current[1])); it.next(); } return result.toString(); }
public String getCharDescVerbose(char c) { final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext()); final Shape sh = t.getOutline(null); final double current[] = new double[6]; final PathIterator it = sh.getPathIterator(null); int sum = 0; final StringBuilder result = new StringBuilder(); while (it.isDone() == false) { final double oldX = current[0]; final double oldY = current[1]; final int nb = it.currentSegment(current); sum += nb; result.append(getType(nb, oldX, oldY, current[0], current[1])); appendValue(result, current); it.next(); } return result.toString(); }
/** * 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); }
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); final Rectangle2D bounds; if(style.getRotation() != 0){
/** * Get default character width using the Workbook's default font * * @param wb the workbook to get the default character width from * @return default character width in pixels */ @Internal public static int getDefaultCharWidth(final Workbook wb) { Font defaultFont = wb.getFontAt( 0); AttributedString str = new AttributedString(String.valueOf(defaultChar)); copyAttributes(defaultFont, str, 0, 1); TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); return (int) layout.getAdvance(); }
TextLayout layout = new TextLayout(line, font, frc);
public void drawString(AttributedCharacterIterator attributedcharacteriterator, float x, float y) { TextLayout textlayout = new TextLayout(attributedcharacteriterator, getFontRenderContext()); Paint paint1 = getPaint(); setColor(getColor()); fill(textlayout.getOutline(AffineTransform.getTranslateInstance(x, y))); setPaint(paint1); }
/** * Replace a tab with the effective number of white spaces. */ private String tab2space(TextRun tr) { AttributedString string = new AttributedString(" "); String fontFamily = tr.getFontFamily(); if (fontFamily == null) { fontFamily = "Lucida Sans"; } string.addAttribute(TextAttribute.FAMILY, fontFamily); Double fs = tr.getFontSize(); if (fs == null) { fs = 12d; } string.addAttribute(TextAttribute.SIZE, fs.floatValue()); TextLayout l = new TextLayout(string.getIterator(), new FontRenderContext(null, true, true)); double wspace = l.getAdvance(); Double tabSz = paragraph.getDefaultTabSize(); if (tabSz == null) { tabSz = wspace*4; } int numSpaces = (int)Math.ceil(tabSz / wspace); StringBuilder buf = new StringBuilder(); for(int i = 0; i < numSpaces; i++) { buf.append(' '); } return buf.toString(); }
lastLine.length() - 4) + "..."; layouts.add(new TextLayout(lastLine, g.getFont(), g.getFontRenderContext()));
final TextLayout t = new TextLayout(shape.getText(), font.getFont(), fontRenderContext); g2d.translate(x, y); g2d.fill(t.getOutline(null));
public void draw(UShape ushape, double x, double y, ColorMapper mapper, UParam param, SvgGraphics svg) { final UClip clip = clipContainer.getClip(); if (clip != null && clip.isInside(x, y) == false) { return; } final UText shape = (UText) ushape; final FontConfiguration fontConfiguration = shape.getFontConfiguration(); final UFont font = fontConfiguration.getFont(); final TextLayout t = new TextLayout(shape.getText(), font.getFont(), fontRenderContext); drawPathIterator(svg, x, y, t.getOutline(null).getPathIterator(null)); }
public void draw(UShape ushape, double x, double y, ColorMapper mapper, UParam param, EpsGraphics eps) { final UCenteredCharacter centeredCharacter = (UCenteredCharacter) ushape; final char c = centeredCharacter.getChar(); final UFont font = centeredCharacter.getFont(); final UnusedSpace unusedSpace = UnusedSpace.getUnusedSpace(font, c); final double xpos = x - unusedSpace.getCenterX() - 0.5; final double ypos = y - unusedSpace.getCenterY() - 0.5; final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext()); eps.setStrokeColor(mapper.getMappedColor(param.getColor())); DriverTextEps.drawPathIterator(eps, xpos, ypos, t.getOutline(null)); }
TextLayout layout = new TextLayout(s, _font, getFontRenderContext()); float ascent = layout.getAscent();
public void draw(UShape ushape, double x, double y, ColorMapper mapper, UParam param, TikzGraphics tikz) { final UCenteredCharacter centeredCharacter = (UCenteredCharacter) ushape; final char c = centeredCharacter.getChar(); final UFont font = centeredCharacter.getFont(); final UnusedSpace unusedSpace = UnusedSpace.getUnusedSpace(font, c); final double xpos = x - unusedSpace.getCenterX() - 0.5; final double ypos = y - unusedSpace.getCenterY() - 0.5; final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext()); tikz.setStrokeColor(mapper.getMappedColor(param.getColor())); tikz.drawPathIterator(xpos, ypos, t.getOutline(null).getPathIterator(null)); }
public void draw(UShape ushape, double x, double y, ColorMapper mapper, UParam param, SvgGraphics svg) { final UCenteredCharacter characterCircled = (UCenteredCharacter) ushape; final char c = characterCircled.getChar(); final UFont font = characterCircled.getFont(); final UnusedSpace unusedSpace = UnusedSpace.getUnusedSpace(font, c); final double xpos = x - unusedSpace.getCenterX() - 0.5; final double ypos = y - unusedSpace.getCenterY() - 0.5; final TextLayout t = new TextLayout("" + c, font.getFont(), TextBlockUtils.getFontRenderContext()); svg.setStrokeColor(StringUtils.getAsHtml(mapper.getMappedColor(param.getColor()))); DriverTextAsPathSvg.drawPathIterator(svg, xpos, ypos, t.getOutline(null).getPathIterator(null)); } }
public void drawCentralizedText(String text) { Graphics2D graphics = image.createGraphics(); try { graphics.setFont(font); graphics.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON); FontRenderContext context = graphics.getFontRenderContext(); TextLayout layout = new TextLayout(text, font, context); int x = width / 2 - (int) (layout.getAdvance() / 2f); int y = height / 2 + (int) (layout.getAscent() / 2f) - 1; graphics.setColor(Color.DARK_GRAY.darker()); graphics.drawString(text, x-1, y-1); graphics.setColor(Color.WHITE); graphics.drawString(text, x, y); } finally { graphics.dispose(); } }
str.addAttribute(TextAttribute.SIZE, fontSize); TextLayout layout = new TextLayout(str.getIterator(), graphics.getFontRenderContext()); DrawFactory fact = DrawFactory.getInstance(graphics); return fact.getTextFragment(layout, str);
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();
final UFont font = fontConfiguration.getFont(); final TextLayout textLayout = new TextLayout(shape.getText(), font.getFont(), fontRenderContext);