afreechart/src/main/java/org/afree/chart/text/TextUtilities.java
/**
     * 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 canvas  the graphics device (not <code>null</code>).
     * @param text  the text.
     * @param anchor  the anchor point.
     *
     * @return  The offsets.
     */
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 canvas  the graphics device (not null).
@param text  the text.
@param anchor  the anchor point.

@return  The offsets.
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 canvas the graphics device ( not null ) . @param text the text . @param anchor the anchor point . @return The offsets .
private static float[] deriveTextBoundsAnchorOffsets(final Paint paint,
            final String text, final TextAnchor anchor) {
        final float[] result = new float[2];
        final FontMetrics fm = paint.getFontMetrics();
        final RectShape bounds = TextUtilities.getTextBounds(text, paint);
        final float ascent = Math.abs(fm.ascent);
        final float halfAscent = ascent / 2.0f;
        final float descent = Math.abs(fm.descent);
        final float leading = Math.abs(fm.leading);
        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 = -Math.abs(fm.descent) - Math.abs(fm.leading);
        }
        result[0] = xAdj;
        result[1] = yAdj;
        return result;
    }
private static float [ ] deriveTextBoundsAnchorOffsets ( final Paint paint , final String text , final TextAnchor anchor ) { final float [ ] result = new float [ 2 ] ; final FontMetrics fm = paint . getFontMetrics ( ) ; final RectShape bounds = TextUtilities . getTextBounds ( text , paint ) ; final float ascent = Math . abs ( fm . ascent ) ; final float halfAscent = ascent / 2.0f ; final float descent = Math . abs ( fm . descent ) ; final float leading = Math . abs ( fm . leading ) ; 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 = - Math . abs ( fm . descent ) - Math . abs ( fm . leading ) ; } result [ 0 ] = xAdj ; result [ 1 ] = yAdj ; return result ; }
A utility method that calculates the anchor offsets for a string.
A utility method that calculates the anchor offsets for a string .