/** * Vertical centering is not trivial, because visually we want centering on characters such as * a,m,e, and not centering on d,g whose center is affected by the full ascent or the full * descent. This method tries to computes the y anchor taking into account those. */ public double getLinePlacementYAnchor() { TextStyle2D textStyle = getLabel().getTextStyle(); LineMetrics lm = textStyle .getFont() .getLineMetrics(textStyle.getLabel(), graphics.getFontRenderContext()); // gracefully handle font size = 0 if (lm.getHeight() > 0) { return (Math.abs(lm.getStrikethroughOffset()) + lm.getDescent() + lm.getLeading()) / lm.getHeight(); } else { return 0; } }
public String toString() { StringBuffer buf = new StringBuffer(); buf.append("{text="); for (int i = 0; i < length(); i++) { buf.append(aStrings[i].getIterator().current()); } final String RS = "\n\t"; buf.append(RS); for (int i = 0; i < length(); i++) { buf.append(bounds[i].toString()); final String FS = " "; final LineMetrics m = metrics[i]; // height = ascent + descent + leading buf.append(" ascent=").append(m.getAscent()).append(FS); buf.append("descent=").append(m.getDescent()).append(FS); buf.append("leading=").append(m.getLeading()).append(FS); buf.append(RS); } buf.append("}"); return buf.toString(); }
@Override public int getPreferredHeight(int width) { int preferredHeight = thickness; Separator separator = (Separator)getComponent(); String heading = separator.getHeading(); if (heading != null && heading.length() > 0) { FontRenderContext fontRenderContext = Platform.getFontRenderContext(); LineMetrics lm = font.getLineMetrics(heading, fontRenderContext); preferredHeight = Math.max((int)Math.ceil(lm.getAscent() + lm.getDescent() + lm.getLeading()), preferredHeight); } preferredHeight += (padding.top + padding.bottom); return preferredHeight; }
@Override public int getPreferredHeight(int width) { int preferredHeight = thickness; Separator separator = (Separator)getComponent(); String heading = separator.getHeading(); if (heading != null && heading.length() > 0) { FontRenderContext fontRenderContext = Platform.getFontRenderContext(); LineMetrics lm = font.getLineMetrics(heading, fontRenderContext); preferredHeight = Math.max((int)Math.ceil(lm.getAscent() + lm.getDescent() + lm.getLeading()), preferredHeight); } preferredHeight += (padding.top + padding.bottom); return preferredHeight; }
/** * Calculates the vertical offset between the baseline and the specified * text anchor. * * @param g2 the graphics device. * @param anchor the anchor. * * @return the offset. */ public float calculateBaselineOffset(final Graphics2D g2, final TextAnchor anchor) { float result = 0.0f; final FontMetrics fm = g2.getFontMetrics(this.font); final LineMetrics lm = fm.getLineMetrics("ABCxyz", g2); if (anchor == TextAnchor.TOP_LEFT || anchor == TextAnchor.TOP_CENTER || anchor == TextAnchor.TOP_RIGHT) { result = lm.getAscent(); } else if (anchor == TextAnchor.BOTTOM_LEFT || anchor == TextAnchor.BOTTOM_CENTER || anchor == TextAnchor.BOTTOM_RIGHT) { result = -lm.getDescent() - lm.getLeading(); } return result; }
/** * Calculates the vertical offset between the baseline and the specified * text anchor. * * @param g2 the graphics device. * @param anchor the anchor. * * @return the offset. */ public float calculateBaselineOffset(final Graphics2D g2, final TextAnchor anchor) { float result = 0.0f; final FontMetrics fm = g2.getFontMetrics(this.font); final LineMetrics lm = fm.getLineMetrics("ABCxyz", g2); if (anchor == TextAnchor.TOP_LEFT || anchor == TextAnchor.TOP_CENTER || anchor == TextAnchor.TOP_RIGHT) { result = lm.getAscent(); } else if (anchor == TextAnchor.BOTTOM_LEFT || anchor == TextAnchor.BOTTOM_CENTER || anchor == TextAnchor.BOTTOM_RIGHT) { result = -lm.getDescent() - lm.getLeading(); } return result; }
@Override public Dimensions getPreferredSize() { int preferredWidth = 0; int preferredHeight = thickness; Separator separator = (Separator)getComponent(); String heading = separator.getHeading(); if (heading != null && heading.length() > 0) { FontRenderContext fontRenderContext = Platform.getFontRenderContext(); Rectangle2D headingBounds = font.getStringBounds(heading, fontRenderContext); LineMetrics lm = font.getLineMetrics(heading, fontRenderContext); preferredWidth = (int)Math.ceil(headingBounds.getWidth()); preferredHeight = Math.max((int)Math.ceil(lm.getAscent() + lm.getDescent() + lm.getLeading()), preferredHeight); } preferredHeight += (padding.top + padding.bottom); preferredWidth += (padding.left + padding.right); return new Dimensions(preferredWidth, preferredHeight); }
float ah = ch - insets.top - insets.bottom; float th = lm.getAscent() + lm.getDescent() + lm.getLeading();
@Override public Dimensions getPreferredSize() { int preferredWidth = 0; int preferredHeight = thickness; Separator separator = (Separator)getComponent(); String heading = separator.getHeading(); if (heading != null && heading.length() > 0) { FontRenderContext fontRenderContext = Platform.getFontRenderContext(); Rectangle2D headingBounds = font.getStringBounds(heading, fontRenderContext); LineMetrics lm = font.getLineMetrics(heading, fontRenderContext); preferredWidth = (int)Math.ceil(headingBounds.getWidth()); preferredHeight = Math.max((int)Math.ceil(lm.getAscent() + lm.getDescent() + lm.getLeading()), preferredHeight); } preferredHeight += (padding.top + padding.bottom); preferredWidth += (padding.left + padding.right); return new Dimensions(preferredWidth, preferredHeight); }
private void writeFootline(AffineTransform pageTrafo) throws IOException { if (footerText != null) { LineMetrics metrics = footerFont.getLineMetrics("mM", getFontRenderContext()); double y = getHeight() + footerFont.getSize2D() / 2; writeLine(pageTrafo, footerFont, footerText, y + metrics.getLeading(), TEXT_TOP, y, footerUnderline); } }
private void writeHeadline(AffineTransform pageTrafo) throws IOException { if (headerText != null) { LineMetrics metrics = headerFont.getLineMetrics("mM", getFontRenderContext()); writeLine(pageTrafo, headerFont, headerText, -metrics.getLeading() - headerFont.getSize2D() / 2, TEXT_BOTTOM, -headerFont .getSize2D() / 2, headerUnderline); } }
/** * Vertical centering is not trivial, because visually we want centering on * characters such as a,m,e, and not centering on d,g whose center is * affected by the full ascent or the full descent. This method tries to * computes the y anchor taking into account those. */ public double getLinePlacementYAnchor() { TextStyle2D textStyle = getLabel().getTextStyle(); LineMetrics lm = textStyle.getFont().getLineMetrics(textStyle.getLabel(), graphics.getFontRenderContext()); // gracefully handle font size = 0 if(lm.getHeight() > 0) return (Math.abs(lm.getStrikethroughOffset()) + lm.getDescent() + lm.getLeading()) / lm.getHeight(); else return 0; }
public JavaFont(String fontName, int fontStyle, int fontSize) { super(fontName, fontStyle, fontSize); myFont = UIManager.getCachedJavaFont(fontName, fontStyle, fontSize); frc = new java.awt.font.FontRenderContext(null, true, false); java.awt.font.LineMetrics metrics = myFont.getLineMetrics("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopwrstuvwxyz0123456789", frc); height = metrics.getHeight(); ascent = metrics.getAscent(); leading = metrics.getLeading(); descent = metrics.getDescent(); } public MetaFont deriveFontSize(int newSize, UIManager uiLoader)
/** * Calculates the vertical offset between the baseline and the specified * text anchor. * * @param g2 the graphics device. * @param anchor the anchor. * * @return the offset. */ public float calculateBaselineOffset(Graphics2D g2, TextAnchor anchor) { float result = 0.0f; FontMetrics fm = g2.getFontMetrics(this.font); LineMetrics lm = fm.getLineMetrics("ABCxyz", g2); if (anchor.isTop()) { result = lm.getAscent(); } else if (anchor.isHalfAscent()) { result = lm.getAscent() / 2.0f; } else if (anchor.isVerticalCenter()) { result = lm.getAscent() / 2.0f - lm.getDescent() / 2.0f; } else if (anchor.isBottom()) { result = -lm.getDescent() - lm.getLeading(); } return result; }
/** * Calculates the vertical offset between the baseline and the specified * text anchor. * * @param g2 the graphics device. * @param anchor the anchor. * * @return the offset. */ public float calculateBaselineOffset(Graphics2D g2, TextAnchor anchor) { float result = 0.0f; final FontMetrics fm = g2.getFontMetrics(this.font); final LineMetrics lm = fm.getLineMetrics("ABCxyz", g2); if (anchor.isTop()) { result = lm.getAscent(); } else if (anchor.isHalfAscent()) { result = lm.getAscent() / 2.0f; } else if (anchor.isVerticalCenter()) { result = lm.getAscent() / 2.0f - lm.getDescent() / 2.0f; } else if (anchor.isBottom()) { result = -lm.getDescent() - lm.getLeading(); } return result; }
/** * Constructs a GVTLineMetrics object based on the specified line metrics. * * @param lineMetrics The lineMetrics object that this metrics object will * be based upon. */ public GVTLineMetrics(LineMetrics lineMetrics) { this.ascent = lineMetrics.getAscent(); this.baselineIndex = lineMetrics.getBaselineIndex(); this.baselineOffsets = lineMetrics.getBaselineOffsets(); this.descent = lineMetrics.getDescent(); this.height = lineMetrics.getHeight(); this.leading = lineMetrics.getLeading(); this.numChars = lineMetrics.getNumChars(); this.strikethroughOffset = lineMetrics.getStrikethroughOffset(); this.strikethroughThickness = lineMetrics.getStrikethroughThickness(); this.underlineOffset = lineMetrics.getUnderlineOffset(); this.underlineThickness = lineMetrics.getUnderlineThickness(); this.overlineOffset = -this.ascent; this.overlineThickness = this.underlineThickness; }
/** * Constructs a GVTLineMetrics object based on the specified line metrics. * * @param lineMetrics The lineMetrics object that this metrics object will * be based upon. */ public GVTLineMetrics(LineMetrics lineMetrics) { this.ascent = lineMetrics.getAscent(); this.baselineIndex = lineMetrics.getBaselineIndex(); this.baselineOffsets = lineMetrics.getBaselineOffsets(); this.descent = lineMetrics.getDescent(); this.height = lineMetrics.getHeight(); this.leading = lineMetrics.getLeading(); this.numChars = lineMetrics.getNumChars(); this.strikethroughOffset = lineMetrics.getStrikethroughOffset(); this.strikethroughThickness = lineMetrics.getStrikethroughThickness(); this.underlineOffset = lineMetrics.getUnderlineOffset(); this.underlineThickness = lineMetrics.getUnderlineThickness(); this.overlineOffset = -this.ascent; this.overlineThickness = this.underlineThickness; }
/** * Constructs a GVTLineMetrics object based on the specified line metrics. * * @param lineMetrics The lineMetrics object that this metrics object will * be based upon. */ public GVTLineMetrics(LineMetrics lineMetrics) { this.ascent = lineMetrics.getAscent(); this.baselineIndex = lineMetrics.getBaselineIndex(); this.baselineOffsets = lineMetrics.getBaselineOffsets(); this.descent = lineMetrics.getDescent(); this.height = lineMetrics.getHeight(); this.leading = lineMetrics.getLeading(); this.numChars = lineMetrics.getNumChars(); this.strikethroughOffset = lineMetrics.getStrikethroughOffset(); this.strikethroughThickness = lineMetrics.getStrikethroughThickness(); this.underlineOffset = lineMetrics.getUnderlineOffset(); this.underlineThickness = lineMetrics.getUnderlineThickness(); this.overlineOffset = -this.ascent; this.overlineThickness = this.underlineThickness; }
/** * Constructs a GVTLineMetrics object based on the specified line metrics * with a scale factor applied. * * @param lineMetrics The lineMetrics object that this metrics object will * be based upon. * @param scaleFactor The scale factor to apply to all metrics. */ public GVTLineMetrics(LineMetrics lineMetrics, float scaleFactor) { this.ascent = lineMetrics.getAscent() * scaleFactor; this.baselineIndex = lineMetrics.getBaselineIndex(); this.baselineOffsets = lineMetrics.getBaselineOffsets(); for (int i=0; i<baselineOffsets.length; i++) { this.baselineOffsets[i] *= scaleFactor; } this.descent = lineMetrics.getDescent() * scaleFactor; this.height = lineMetrics.getHeight() * scaleFactor; this.leading = lineMetrics.getLeading(); this.numChars = lineMetrics.getNumChars(); this.strikethroughOffset = lineMetrics.getStrikethroughOffset() * scaleFactor; this.strikethroughThickness = lineMetrics.getStrikethroughThickness() * scaleFactor; this.underlineOffset = lineMetrics.getUnderlineOffset() * scaleFactor; this.underlineThickness = lineMetrics.getUnderlineThickness() * scaleFactor; this.overlineOffset = -this.ascent; this.overlineThickness = this.underlineThickness; }
/** * Constructs a GVTLineMetrics object based on the specified line metrics * with a scale factor applied. * * @param lineMetrics The lineMetrics object that this metrics object will * be based upon. * @param scaleFactor The scale factor to apply to all metrics. */ public GVTLineMetrics(LineMetrics lineMetrics, float scaleFactor) { this.ascent = lineMetrics.getAscent() * scaleFactor; this.baselineIndex = lineMetrics.getBaselineIndex(); this.baselineOffsets = lineMetrics.getBaselineOffsets(); for (int i=0; i<baselineOffsets.length; i++) { this.baselineOffsets[i] *= scaleFactor; } this.descent = lineMetrics.getDescent() * scaleFactor; this.height = lineMetrics.getHeight() * scaleFactor; this.leading = lineMetrics.getLeading(); this.numChars = lineMetrics.getNumChars(); this.strikethroughOffset = lineMetrics.getStrikethroughOffset() * scaleFactor; this.strikethroughThickness = lineMetrics.getStrikethroughThickness() * scaleFactor; this.underlineOffset = lineMetrics.getUnderlineOffset() * scaleFactor; this.underlineThickness = lineMetrics.getUnderlineThickness() * scaleFactor; this.overlineOffset = -this.ascent; this.overlineThickness = this.underlineThickness; }