/** * * @return width if this text run */ public float getWidth(){ return layout.getAdvance(); }
/** * 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(); }
} else { layout.draw(graphics, (float) (bounds.width - layout.getAdvance()), nextY);
/** * 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(); }
float ascent = layout.getAscent(); float width = (float) Math.floor(layout.getAdvance());
bullet.draw(graphics); double bulletWidth = bullet.getLayout().getAdvance() + 1; penX = x + Math.max(leftMargin, leftMargin+indent+bulletWidth); } else {
public void add(TextLayout line) { float advance = line.getAdvance(); if(advance > width) width = advance; height += line.getAscent(); list.add(line); }
private float xFor(TextLayout line) { return (float) (label.getSize().getWidth() / 2.0 - line.getAdvance() / 2f); }
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(); } }
public TextLayout next() { if (first) { first = false; } else { y += ascent * lineHeight; } double layoutWidth = width == 0 ? Float.MAX_VALUE : width; TextLayout layout = currentMeasurer.nextLayout((float) layoutWidth); if (width == 0) { layoutWidth = layout.getAdvance(); if (align == Align.RIGHT) { x = -layoutWidth; } else if (align == Align.CENTER) { x = -layoutWidth / 2.0; } } else if (align == Align.RIGHT) { x = width - layout.getAdvance(); } else if (align == Align.CENTER) { x = (width - layout.getAdvance()) / 2.0; } else if (align == Align.JUSTIFY) { // Don't justify the last line. if (currentMeasurer.getPosition() < currentText.length()) { layout = layout.getJustifiedLayout((float) width); } } ascent = layout.getAscent(); // y += layout.getDescent() + layout.getLeading() + layout.getAscent(); return layout; }
/** * * @return width if this text run */ public float getWidth(){ return layout.getAdvance(); }
/** * * @return width if this text run */ public float getWidth(){ return _layout.getAdvance(); }
float posY = drawPosY; if (Position.CENTER == this.position) { posX += -layout.getAdvance() * 0.5f; posY += layout.getAscent() - layoutHeight * 0.5f; } else if (Position.BELOW == this.position) { posX += -layout.getAdvance() * 0.5f; posY += (layout.getAscent() + layout.getDescent() + layout.getLeading()) * 0.5f; } else if (Position.BELOW_LEFT == this.position) { posX += -layout.getAdvance(); posY += (layout.getAscent() + layout.getDescent() + layout.getLeading()) * 0.5f; } else if (Position.BELOW_RIGHT == this.position) { posY += (layout.getAscent() + layout.getDescent() + layout.getLeading()) * 0.5f; } else if (Position.ABOVE == this.position) { posX += -layout.getAdvance() * 0.5f; posY += layout.getAscent() + layout.getDescent() + layout.getLeading() - layoutHeight; } else if (Position.ABOVE_LEFT == this.position) { posX += -layout.getAdvance(); posY += layout.getAscent() + layout.getDescent() + layout.getLeading() - layoutHeight; } else if (Position.ABOVE_RIGHT == this.position) { posY += layout.getAscent() + layout.getDescent() + layout.getLeading() - layoutHeight; } else if (Position.LEFT == this.position) { posX += -layout.getAdvance(); posY += layout.getAscent() - layoutHeight * 0.5f; } else if (Position.RIGHT == this.position) {
protected int getWidthOfLine(String line) { initFontMetrics(); if (line == null || line.isEmpty()) { return 0; } TextLayout tl = new TextLayout( line, this.fm.getFont(), this.g.getFontRenderContext() ); int advance = (int) Math.ceil(tl.getAdvance()); return advance; }
@Override public void draw(Graphics2D graphics, Point location) { TextLayout tl = new TextLayout(text, graphics.getFont(), graphics.getFontRenderContext()); float x = location.x; if (alignment.equals(TextAlignment.RIGHT)) x -= tl.getAdvance(); tl.draw(graphics, x, location.y); }
@Override public void draw(Graphics2D graphics, Point location) { TextLayout tl = new TextLayout(text, graphics.getFont(), graphics.getFontRenderContext()); float x = location.x; if (alignment.equals(TextAlignment.RIGHT)) x -= tl.getAdvance(); tl.draw(graphics, x, location.y); }
@Override public void draw(Graphics2D graphics, Point location) { TextLayout tl = new TextLayout(text, graphics.getFont(), graphics.getFontRenderContext()); float x = location.x; if (alignment.equals(TextAlignment.RIGHT)) x -= tl.getAdvance(); tl.draw(graphics, x, location.y); }
@Override public Rectangle2D.Double getBounds() { TextLayout layout = getTextLayout(); Rectangle2D.Double r = new Rectangle2D.Double(origin.x, origin.y, layout.getAdvance(), layout.getAscent() + layout.getDescent()); return r; }
private int computeStringWidth(int fragmentIndex, Font font) { String text = myFragments.get(fragmentIndex); if (StringUtil.isEmpty(text)) return 0; TextLayout layout = getTextLayout(fragmentIndex, font, getFontMetrics(font).getFontRenderContext()); if (layout != null) { return (int)layout.getAdvance(); } else { return getFontMetrics(font).stringWidth(text); } }
static TextHitInfo x2RelOffset(TextLayout textLayout, float x) { TextHitInfo hit; x -= EXTRA_MARGIN_WIDTH; if (x >= textLayout.getAdvance()) { hit = TextHitInfo.trailing(textLayout.getCharacterCount()); } else { hit = textLayout.hitTestChar(x, 0); // What about backward bias -> with higher offsets it may go back visually } return hit; }