/** * @return full height of this text run which is sum of ascent, descent and leading */ public float getHeight(){ double h = layout.getAscent() + layout.getDescent() + getLeading(); return (float)h; }
/** * @return the leading height before/after a text line */ public float getLeading() { // fix invalid leadings (leading == 0) double l = layout.getLeading(); if (l == 0) { // see https://stackoverflow.com/questions/925147 // we use a 115% value instead of the 120% proposed one, as this seems to be closer to LO/OO l = (layout.getAscent()+layout.getDescent())*0.15; } return (float)l; }
public void draw(Graphics2D graphics){ if(str == null) { return; } double yBaseline = y + layout.getAscent(); Integer textMode = (Integer)graphics.getRenderingHint(Drawable.TEXT_RENDERING_MODE); if(textMode != null && textMode == Drawable.TEXT_AS_SHAPES){ layout.draw(graphics, (float)x, (float)yBaseline); } else { graphics.drawString(str.getIterator(), (float)x, (float)yBaseline ); } }
nextY += layout.getAscent() + layout.getDescent() + layout.getLeading();
float ascent = layout.getAscent();
public void add(TextLayout line) { float advance = line.getAdvance(); if(advance > width) width = advance; height += line.getAscent(); list.add(line); }
float getAscent() { float ascent = Float.NEGATIVE_INFINITY; for (LineComponent component : components) { float ca = component.getLayout().getAscent(); if (ca > ascent) { return ascent; } } return ascent; }
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(); } }
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();
int x = (int) ((getWidth() - (int) bounds.getWidth()) / 2); int y = (int) ((getHeight() - (bounds.getHeight() - txt.getDescent())) / 2); y += txt.getAscent() - txt.getDescent();
float getLineOffset() { float offset = Float.NEGATIVE_INFINITY; for (LineComponent component : components) { float co = component.getLayout().getAscent() + component.getLayout().getDescent() + component.getLayout().getLeading(); if (co > offset) { offset = co; } } return offset; }
private void drawLines(WrappedLines lines, Graphics2D g) { Colors colors = look.colors(); float y = originY(lines); for(TextLayout line : lines.list) { float x = xFor(line); y += line.getAscent() - line.getDescent(); Color shadowColor = colors.shadow(); g.setColor(shadowColor); line.draw(g, x-1, y-1); g.setColor(colors.text()); line.draw(g, x, y); y += line.getDescent(); } }
float ascent = layout.getAscent(); yPos += ascent; remain -= ascent;
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; }
float ascent = textLayout.getAscent();
while (lineMeasurer.getPosition() < paragraphEnd) { TextLayout layout = lineMeasurer.nextLayout(maxTextWidth); layoutHeight += layout.getAscent() + layout.getDescent() + layout.getLeading(); 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) { posY += layout.getAscent() - layoutHeight * 0.5f; } else {
public void addLink(LinkSpan linkSpan, int linkStart, int linkEnd) { Rectangle linkBounds = textLayout.getLogicalHighlightShape(linkStart, linkEnd).getBounds(); linkBounds.translate(0, (int)(textLayout.getAscent() + textLayout.getLeading())); AttributedString linkString = new AttributedString(attributedString.getIterator()); linkString.addAttribute(TextAttribute.FOREGROUND, Color.MAGENTA.darker(), linkStart, linkEnd); linkString.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL, linkStart, linkEnd); LinkBox linkBox = new LinkBox(linkSpan.getLink()); linkBox.setBounds(linkBounds); add(linkBox); }
public void layout(FontRenderContext fontRenderContext) { this.textLayout = new TextLayout(attributedString.getIterator(), fontRenderContext); int textHeight = (int) (textLayout.getAscent() + textLayout.getDescent() + textLayout.getLeading()); int totalHeight = textHeight + getInsetsTop() + getInsetsBottom(); setHeight(totalHeight); }