afreechart/src/main/java/org/afree/chart/text/TextUtilities.java
/**
     * A utility method that calculates the rotation anchor offsets for a
     * string.  These offsets are relative to the text starting coordinate
     * (BASELINE_LEFT).
     *
     * @param canvas  the graphics device.
     * @param text  the text.
     * @param anchor  the anchor point.
     *
     * @return  The offsets.
     */
A utility method that calculates the rotation anchor offsets for a
string.  These offsets are relative to the text starting coordinate
(BASELINE_LEFT).

@param canvas  the graphics device.
@param text  the text.
@param anchor  the anchor point.

@return  The offsets.
A utility method that calculates the rotation anchor offsets for a string . These offsets are relative to the text starting coordinate ( BASELINE_LEFT ) . @param canvas the graphics device . @param text the text . @param anchor the anchor point . @return The offsets .
private static float[] deriveRotationAnchorOffsets(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 = fm.ascent;
        final float halfAscent = ascent / 2.0f;
        final float descent = fm.descent;
        final float leading = fm.leading;
        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 = fm.descent + fm.leading;
        }
        result[0] = xAdj;
        result[1] = yAdj;
        return result;
    }
private static float [ ] deriveRotationAnchorOffsets ( 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 = fm . ascent ; final float halfAscent = ascent / 2.0f ; final float descent = fm . descent ; final float leading = fm . leading ; 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 = fm . descent + fm . leading ; } result [ 0 ] = xAdj ; result [ 1 ] = yAdj ; return result ; }
A utility method that calculates the rotation anchor offsets for a
 string.
A utility method that calculates the rotation anchor offsets for a string .