private static float[] deriveRotationAnchorOffsets(final Graphics2D g2,
final String text, final TextAnchor anchor) {
final float[] result = new float[2];
final FontRenderContext frc = g2.getFontRenderContext();
final LineMetrics metrics = g2.getFont().getLineMetrics(text, frc);
final FontMetrics fm = g2.getFontMetrics();
final Rectangle2D bounds = TextUtilities.getTextBounds(text, g2, fm);
final float ascent = metrics.getAscent();
final float halfAscent = ascent / 2.0f;
final float descent = metrics.getDescent();
final 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;