Java Code Examples for java.awt.font.LineMetrics#getAscent()

The following examples show how to use java.awt.font.LineMetrics#getAscent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextFragment.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 == 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;                                             
}
 
Example 2
Source File: StandardGlyphVector.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override protected void paintComponent(Graphics g) {
  Graphics2D g2 = (Graphics2D) g.create();
  Rectangle r = list.getVisibleRect();
  int cw = r.width - icon.getPreferredSize().width;
  FontRenderContext frc = g2.getFontRenderContext();
  GlyphVector gv = getFont().createGlyphVector(frc, getText());
  if (isAnimatingCell() && gv.getVisualBounds().getWidth() > cw) {
    LineMetrics lm = getFont().getLineMetrics(getText(), frc);
    float yy = lm.getAscent() / 2f + (float) gv.getVisualBounds().getY();
    g2.drawGlyphVector(gv, cw - xx, getHeight() / 2f - yy);
    xx = cw + gv.getVisualBounds().getWidth() - xx > 0 ? xx + 8f : 0f;
  } else {
    super.paintComponent(g);
  }
  g2.dispose();
}
 
Example 4
Source File: StandardGlyphVector.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 5
Source File: TextFragment.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 == 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;
}
 
Example 6
Source File: StandardGlyphVector.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 7
Source File: StandardGlyphVector.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 8
Source File: UnderlineTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void paintComponent(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, fpd.width, fpd.height);

    g.setColor(Color.RED);
    FontRenderContext frc = ((Graphics2D)g).getFontRenderContext();
    LineMetrics lm = f.getLineMetrics(fps, frc);
    int h = (int)(fpd.height - 20 - lm.getAscent());
    g.drawLine(20, h, fpd.width - 20, h);
    h = fpd.height - 20;
    g.drawLine(20, h, fpd.width - 20, h);
    h = (int)(fpd.height - 20 + lm.getDescent());
    g.drawLine(20, h, fpd.width - 20, h);

    g.setColor(Color.BLACK);
    g.setFont(f);
    g.drawString(fps, 50, fpd.height - 20);
}
 
Example 9
Source File: StandardGlyphVector.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public Rectangle2D getLogicalBounds() {
    setFRCTX();
    initPositions();

    LineMetrics lm = font.getLineMetrics("", frc);

    float minX, minY, maxX, maxY;
    // horiz only for now...
    minX = 0;
    minY = -lm.getAscent();
    maxX = 0;
    maxY = lm.getDescent() + lm.getLeading();
    if (glyphs.length > 0) {
        maxX = positions[positions.length - 2];
    }

    return new Rectangle2D.Float(minX, minY, maxX - minX, maxY - minY);
}
 
Example 10
Source File: SpiderWebPlot.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example 11
Source File: TextBlock.java    From pumpernickel with MIT License 5 votes vote down vote up
protected void paintText(Graphics2D g, GeneralPath bodyOutline) {
	Font font = getFont();
	String text = getText();
	g.setFont(font);
	FontRenderContext frc = g.getFontRenderContext();
	Rectangle2D r = font.getStringBounds(text, frc);
	LineMetrics lineMetrics = font.getLineMetrics(text, frc);

	Rectangle2D shapeBounds = ShapeBounds.getBounds(bodyOutline);
	float shapeCenterX = (float) shapeBounds.getCenterX();
	float shapeCenterY = (float) shapeBounds.getCenterY();
	float textX = (float) (shapeCenterX - r.getWidth() / 2f);
	float textY = (float) (shapeCenterY + lineMetrics.getAscent() / 2f - lineMetrics
			.getDescent() / 3f);

	if (isTextShadowActive()) {
		Paint paint = this.getTextPaint();
		boolean isTextDark = true;
		if (paint instanceof Color) {
			Color color = (Color) paint;
			isTextDark = (color.getRed() + color.getGreen() + color
					.getBlue()) / 3 < 120;
		}
		g.setColor(isTextDark ? new Color(255, 255, 255, 40) : new Color(0,
				0, 0, 40));
		g.translate(0, 1);
		g.drawString(text, textX, textY);
		g.translate(0, 1);
		g.drawString(text, textX, textY);
		g.translate(0, -2);
	}

	g.setPaint(getTextPaint());
	g.drawString(text, textX, textY);
}
 
Example 12
Source File: SpiderWebPlot.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws the label for one axis.
 * 
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value, 
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();
 
    String label = null;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }
 
    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent, 
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(), 
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example 13
Source File: SpiderWebPlot.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example 14
Source File: TextUtilities.java    From ccu-historian with GNU General Public License v3.0 4 votes vote down vote up
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 * @param textBounds  the text bounds (if not <code>null</code>, this
 *                    object will be updated by this method to match the
 *                    string bounds).
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor, Rectangle2D textBounds) {

    float[] result = new float[3];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = TextUtilities.getTextBounds(text, g2, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    result[2] = -ascent;
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    if (textBounds != null) {
        textBounds.setRect(bounds);
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 15
Source File: TextUtilities.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * A utility method that calculates the anchor offsets for a string.  
 * Normally, the (x, y) coordinate for drawing text is a point on the 
 * baseline at the left of the text string.  If you add these offsets to 
 * (x, y) and draw the string, then the anchor point should coincide with 
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 * @param textBounds  the text bounds (if not <code>null</code>, this 
 *                    object will be updated by this method to match the 
 *                    string bounds).
 * 
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor, Rectangle2D textBounds) {

    float[] result = new float[3];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = TextUtilities.getTextBounds(text, g2, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    result[2] = -ascent;
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor == TextAnchor.TOP_CENTER
            || anchor == TextAnchor.CENTER
            || anchor == TextAnchor.BOTTOM_CENTER
            || anchor == TextAnchor.BASELINE_CENTER
            || anchor == TextAnchor.HALF_ASCENT_CENTER) {

        xAdj = (float) -bounds.getWidth() / 2.0f;

    }
    else if (anchor == TextAnchor.TOP_RIGHT
            || anchor == TextAnchor.CENTER_RIGHT
            || anchor == TextAnchor.BOTTOM_RIGHT
            || anchor == TextAnchor.BASELINE_RIGHT
            || anchor == TextAnchor.HALF_ASCENT_RIGHT) {

        xAdj = (float) -bounds.getWidth();

    }

    if (anchor == TextAnchor.TOP_LEFT
            || anchor == TextAnchor.TOP_CENTER
            || anchor == TextAnchor.TOP_RIGHT) {

        yAdj = -descent - leading + (float) bounds.getHeight();

    }
    else if (anchor == TextAnchor.HALF_ASCENT_LEFT
            || anchor == TextAnchor.HALF_ASCENT_CENTER
            || anchor == TextAnchor.HALF_ASCENT_RIGHT) {

        yAdj = halfAscent;

    }
    else if (anchor == TextAnchor.CENTER_LEFT
            || anchor == TextAnchor.CENTER
            || anchor == TextAnchor.CENTER_RIGHT) {

        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);

    }
    else if (anchor == TextAnchor.BASELINE_LEFT
            || anchor == TextAnchor.BASELINE_CENTER
            || anchor == TextAnchor.BASELINE_RIGHT) {

        yAdj = 0.0f;

    }
    else if (anchor == TextAnchor.BOTTOM_LEFT
            || anchor == TextAnchor.BOTTOM_CENTER
            || anchor == TextAnchor.BOTTOM_RIGHT) {

        yAdj = -metrics.getDescent() - metrics.getLeading();

    }
    if (textBounds != null) {
        textBounds.setRect(bounds);
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 16
Source File: TextUtils.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor) {

    float[] result = new float[2];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 17
Source File: TextUtils.java    From ECG-Viewer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 * @param textBounds  the text bounds (if not <code>null</code>, this
 *                    object will be updated by this method to match the
 *                    string bounds).
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor, Rectangle2D textBounds) {

    float[] result = new float[3];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    result[2] = -ascent;
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isHorizontalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    if (textBounds != null) {
        textBounds.setRect(bounds);
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;
}
 
Example 18
Source File: TextUtils.java    From ECG-Viewer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the (x, y) coordinate for drawing text is a point on the
 * baseline at the left of the text string.  If you add these offsets to
 * (x, y) and draw the string, then the anchor point should coincide with
 * the (x, y) point.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param text  the text.
 * @param anchor  the anchor point.
 *
 * @return  The offsets.
 */
private static float[] deriveTextBoundsAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor) {

    float[] result = new float[2];
    FontRenderContext frc = g2.getFontRenderContext();
    Font f = g2.getFont();
    FontMetrics fm = g2.getFontMetrics(f);
    Rectangle2D bounds = getTextBounds(text, fm);
    LineMetrics metrics = f.getLineMetrics(text, frc);
    float ascent = metrics.getAscent();
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor.isHorizontalCenter()) {
        xAdj = (float) -bounds.getWidth() / 2.0f;
    }
    else if (anchor.isRight()) {
        xAdj = (float) -bounds.getWidth();
    }

    if (anchor.isTop()) {
        yAdj = -descent - leading + (float) bounds.getHeight();
    }
    else if (anchor.isHalfAscent()) {
        yAdj = halfAscent;
    }
    else if (anchor.isVerticalCenter()) {
        yAdj = -descent - leading + (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isBaseline()) {
        yAdj = 0.0f;
    }
    else if (anchor.isBottom()) {
        yAdj = -metrics.getDescent() - metrics.getLeading();
    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 19
Source File: TextUtilities.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * A utility method that calculates the rotation anchor offsets for a
 * string.  These offsets are relative to the text starting coordinate
 * (BASELINE_LEFT).
 *
 * @param g2  the graphics device.
 * @param text  the text.
 * @param anchor  the anchor point.
 *
 * @return  The offsets.
 */
private static float[] deriveRotationAnchorOffsets(Graphics2D g2,
        String text, TextAnchor anchor) {

    float[] result = new float[2];
    FontRenderContext frc = g2.getFontRenderContext();
    LineMetrics metrics = g2.getFont().getLineMetrics(text, frc);
    FontMetrics fm = g2.getFontMetrics();
    Rectangle2D bounds = TextUtilities.getTextBounds(text, g2, fm);
    float ascent = metrics.getAscent();
    float halfAscent = ascent / 2.0f;
    float descent = metrics.getDescent();
    float leading = metrics.getLeading();
    float xAdj = 0.0f;
    float yAdj = 0.0f;

    if (anchor == TextAnchor.TOP_LEFT
            || anchor == TextAnchor.CENTER_LEFT
            || anchor == TextAnchor.BOTTOM_LEFT
            || anchor == TextAnchor.BASELINE_LEFT
            || anchor == TextAnchor.HALF_ASCENT_LEFT) {

        xAdj = 0.0f;

    }
    else if (anchor == TextAnchor.TOP_CENTER
            || anchor == TextAnchor.CENTER
            || anchor == TextAnchor.BOTTOM_CENTER
            || anchor == TextAnchor.BASELINE_CENTER
            || anchor == TextAnchor.HALF_ASCENT_CENTER) {

        xAdj = (float) bounds.getWidth() / 2.0f;

    }
    else if (anchor == TextAnchor.TOP_RIGHT
            || anchor == TextAnchor.CENTER_RIGHT
            || anchor == TextAnchor.BOTTOM_RIGHT
            || anchor == TextAnchor.BASELINE_RIGHT
            || anchor == TextAnchor.HALF_ASCENT_RIGHT) {

        xAdj = (float) bounds.getWidth();

    }

    if (anchor == TextAnchor.TOP_LEFT
            || anchor == TextAnchor.TOP_CENTER
            || anchor == TextAnchor.TOP_RIGHT) {

        yAdj = descent + leading - (float) bounds.getHeight();

    }
    else if (anchor == TextAnchor.CENTER_LEFT
            || anchor == TextAnchor.CENTER
            || anchor == TextAnchor.CENTER_RIGHT) {

        yAdj = descent + leading - (float) (bounds.getHeight() / 2.0);

    }
    else if (anchor == TextAnchor.HALF_ASCENT_LEFT
            || anchor == TextAnchor.HALF_ASCENT_CENTER
            || anchor == TextAnchor.HALF_ASCENT_RIGHT) {

        yAdj = -halfAscent;

    }
    else if (anchor == TextAnchor.BASELINE_LEFT
            || anchor == TextAnchor.BASELINE_CENTER
            || anchor == TextAnchor.BASELINE_RIGHT) {

        yAdj = 0.0f;

    }
    else if (anchor == TextAnchor.BOTTOM_LEFT
            || anchor == TextAnchor.BOTTOM_CENTER
            || anchor == TextAnchor.BOTTOM_RIGHT) {

        yAdj = metrics.getDescent() + metrics.getLeading();

    }
    result[0] = xAdj;
    result[1] = yAdj;
    return result;

}
 
Example 20
Source File: ImageWorker.java    From jrobin with GNU Lesser General Public License v2.1 4 votes vote down vote up
double getFontAscent(Font font) {
    LineMetrics lm = font.getLineMetrics(DUMMY_TEXT, gd.getFontRenderContext());
    return lm.getAscent();
}