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

The following examples show how to use java.awt.font.LineMetrics#getLeading() . 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: StandardGlyphVector.java    From jdk8u-jdk 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 2
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 3
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 4
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 5
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 6
Source File: TextUtils.java    From orson-charts with GNU General Public License v3.0 5 votes vote down vote up
/**
 * A utility method that calculates the anchor offsets for a string.
 * Normally, the {@code (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 {@code (x, y)} and draw the string, then the anchor point should 
 * coincide with the {@code (x, y)} point.
 *
 * @param g2  the graphics device (not {@code null}).
 * @param text  the text.
 * @param anchor  the anchor point ({@code null} not permitted).
 *
 * @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.isHalfHeight()) {
        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 7
Source File: TextUtils.java    From orson-charts with GNU General Public License v3.0 5 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
 * ({@code BASELINE_LEFT}).
 *
 * @param g2  the graphics device ({@code null} not permitted).
 * @param text  the text ({@code null} not permitted).
 * @param anchor  the anchor point ({@code null} not permitted).
 *
 * @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 = TextUtils.getTextBounds(text, 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.isLeft()) {
        xAdj = 0.0f;
    } else 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.isHalfHeight()) {
        yAdj = descent + leading - (float) (bounds.getHeight() / 2.0);
    } else if (anchor.isHalfAscent()) {
        yAdj = -halfAscent;
    } 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 8
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.
 *
 * @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 = TextUtilities.getTextBounds(text, g2, 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 == 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();

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

}
 
Example 9
Source File: EditorView.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void initMetricsIfNeeded() {
  if (myPlainSpaceWidth >= 0) return;

  Font font = myEditor.getColorsScheme().getFont(EditorFontType.PLAIN);
  FontMetrics fm = FontInfo.getFontMetrics(font, myFontRenderContext);

  float width = FontLayoutService.getInstance().charWidth2D(fm, ' ');
  myPlainSpaceWidth = width > 0 ? width : 1;

  myCharHeight = FontLayoutService.getInstance().charWidth(fm, 'a');

  float verticalScalingFactor = getVerticalScalingFactor();

  int fontMetricsHeight = FontLayoutService.getInstance().getHeight(fm);
  int lineHeight;
  if (Registry.is("editor.text.xcode.vertical.spacing")) {
    //Here we approximate line calculation to the variant used in Xcode 9 editor
    LineMetrics metrics = font.getLineMetrics("", myFontRenderContext);

    double height = Math.ceil(metrics.getHeight()) + metrics.getLeading();
    double delta = verticalScalingFactor - 1;
    int spacing;
    if (Math.round((height * delta) / 2) <= 1) {
      spacing = delta > 0 ? 2 : 0;
    }
    else {
      spacing = ((int)Math.ceil((height * delta) / 2)) * 2;
    }
    lineHeight = (int)Math.ceil(height) + spacing;
  }
  else if (Registry.is("editor.text.vertical.spacing.correct.rounding")) {
    if (verticalScalingFactor == 1f) {
      lineHeight = fontMetricsHeight;
    }
    else {
      Font scaledFont = font.deriveFont(font.getSize() * verticalScalingFactor);
      FontMetrics scaledMetrics = FontInfo.getFontMetrics(scaledFont, myFontRenderContext);
      lineHeight = FontLayoutService.getInstance().getHeight(scaledMetrics);
    }
  }
  else {
    lineHeight = (int)Math.ceil(fontMetricsHeight * verticalScalingFactor);
  }
  myLineHeight = Math.max(1, lineHeight);
  int descent = FontLayoutService.getInstance().getDescent(fm);
  myDescent = descent + (myLineHeight - fontMetricsHeight) / 2;
  myTopOverhang = fontMetricsHeight - myLineHeight + myDescent - descent;
  myBottomOverhang = descent - myDescent;

  // assuming that bold italic 'W' gives a good approximation of font's widest character
  FontMetrics fmBI = FontInfo.getFontMetrics(myEditor.getColorsScheme().getFont(EditorFontType.BOLD_ITALIC), myFontRenderContext);
  myMaxCharWidth = FontLayoutService.getInstance().charWidth2D(fmBI, 'W');
}
 
Example 10
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.
 *
 * @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 = TextUtilities.getTextBounds(text, g2, 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 == 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();

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

}
 
Example 11
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 rotation anchor offsets for a
 * string.  These offsets are relative to the text starting coordinate
 * (<code>BASELINE_LEFT</code>).
 *
 * @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.isLeft()) {
        xAdj = 0.0f;
    }
    else 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.isVerticalCenter()) {
        yAdj = descent + leading - (float) (bounds.getHeight() / 2.0);
    }
    else if (anchor.isHalfAscent()) {
        yAdj = -halfAscent;
    }
    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 12
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 13
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 14
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 15
Source File: TextUtils.java    From buffer_bci 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 = 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 16
Source File: SurveyScale.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void drawScaleValues( final Graphics2D g2, final Rectangle2D area ) {
  g2.setPaint( getScaleValuePaint() );
  final Font valueFont = getScaleValueFont();
  if ( valueFont != null ) {
    g2.setFont( valueFont );
  } else if ( styleSheet != null ) {
    final String fontName = (String) styleSheet.getStyleProperty( TextStyleKeys.FONT );
    final int fontSize = styleSheet.getIntStyleProperty( TextStyleKeys.FONTSIZE, 10 );
    final boolean bold = styleSheet.getBooleanStyleProperty( TextStyleKeys.BOLD );
    final boolean italic = styleSheet.getBooleanStyleProperty( TextStyleKeys.ITALIC );
    int style = 0;
    if ( bold ) {
      style |= Font.BOLD;
    }
    if ( italic ) {
      style |= Font.ITALIC;
    }
    g2.setFont( new Font( fontName, style, fontSize ) );
  }

  final Font f = g2.getFont();
  final FontMetrics fm = g2.getFontMetrics( f );
  final FontRenderContext frc = g2.getFontRenderContext();
  final double y = area.getCenterY();

  final int highest = getHighest();
  for ( int i = getLowest(); i <= highest; i++ ) {
    final double x = valueToJava2D( i, area );
    final String text = String.valueOf( i );

    final float width;
    if ( useFontMetricsGetStringBounds ) {
      final Rectangle2D bounds = fm.getStringBounds( text, g2 );
      // getStringBounds() can return incorrect height for some Unicode
      // characters...see bug parade 6183356, let's replace it with
      // something correct
      width = (float) bounds.getWidth();
    } else {
      width = fm.stringWidth( text );
    }

    final LineMetrics metrics = f.getLineMetrics( text, frc );
    final float descent = metrics.getDescent();
    final float leading = metrics.getLeading();
    final float yAdj = -descent - leading + (float) ( metrics.getHeight() / 2.0 );
    final float xAdj = -width / 2.0f;
    g2.drawString( text, (float) ( x + xAdj ), (float) ( y + yAdj ) );
  }
}
 
Example 17
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 18
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 19
Source File: TextUtils.java    From orson-charts 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 {@code (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 {@code (x, y)} and draw the string, then the anchor point should 
 * coincide with the {@code (x, y)} point.
 *
 * @param g2  the graphics device (not {@code null}).
 * @param text  the text.
 * @param anchor  the anchor point ({@code null} not permitted).
 * @param textBounds  the text bounds (if not {@code null}, 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 20
Source File: TextUtils.java    From openstock 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 = 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;
}