/**
 * Copyright 2011 - 2015 Xeiam LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xeiam.xchart.internal.chartpart;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

/**
 * AxisTitle
 */
public class AxisTitle implements ChartPart {

  /** parent */
  private final Axis axis;

  /** the title text */
  private String text = ""; // default to ""

  /** the bounds */
  private Rectangle2D bounds;

  /**
   * Constructor
   * 
   * @param axis the axis
   */
  protected AxisTitle(Axis axis) {

    this.axis = axis;
  }

  @Override
  public Rectangle2D getBounds() {

    return bounds;
  }

  @Override
  public void paint(Graphics2D g) {

    bounds = new Rectangle2D.Double();

    g.setColor(getChartPainter().getStyleManager().getChartFontColor());
    g.setFont(getChartPainter().getStyleManager().getAxisTitleFont());

    if (axis.getDirection() == Axis.Direction.Y) {

      if (text != null && !text.trim().equalsIgnoreCase("") && getChartPainter().getStyleManager().isYAxisTitleVisible()) {

        FontRenderContext frc = g.getFontRenderContext();
        TextLayout nonRotatedTextLayout = new TextLayout(text, getChartPainter().getStyleManager().getAxisTitleFont(), frc);
        Rectangle2D nonRotatedRectangle = nonRotatedTextLayout.getBounds();

        // ///////////////////////////////////////////////

        int xOffset = (int) (axis.getPaintZone().getX() + nonRotatedRectangle.getHeight());
        int yOffset = (int) ((axis.getPaintZone().getHeight() + nonRotatedRectangle.getWidth()) / 2.0 + axis.getPaintZone().getY());
        AffineTransform orig = g.getTransform();
        AffineTransform at = new AffineTransform();
        at.rotate(Math.PI / -2.0, xOffset, yOffset);
        g.transform(at);
        g.drawString(text, xOffset, yOffset);
        g.setTransform(orig);

        // ///////////////////////////////////////////////
        // System.out.println(nonRotatedRectangle.getHeight());

        // bounds
        bounds =
            new Rectangle2D.Double(xOffset - nonRotatedRectangle.getHeight(), yOffset - nonRotatedRectangle.getWidth(), nonRotatedRectangle.getHeight()
                + getChartPainter().getStyleManager().getAxisTitlePadding(), nonRotatedRectangle.getWidth());
        // g.setColor(Color.blue);
        // g.draw(bounds);
      }
      else {
        bounds = new Rectangle2D.Double(axis.getPaintZone().getX(), axis.getPaintZone().getY(), 0, axis.getPaintZone().getHeight());
      }

    }
    else {

      if (text != null && !text.trim().equalsIgnoreCase("") && getChartPainter().getStyleManager().isXAxisTitleVisible()) {

        FontRenderContext frc = g.getFontRenderContext();
        TextLayout textLayout = new TextLayout(text, getChartPainter().getStyleManager().getAxisTitleFont(), frc);
        Rectangle2D rectangle = textLayout.getBounds();
        // System.out.println(rectangle);

        double xOffset = axis.getPaintZone().getX() + (axis.getPaintZone().getWidth() - rectangle.getWidth()) / 2.0;
        double yOffset = axis.getPaintZone().getY() + axis.getPaintZone().getHeight() - rectangle.getHeight();

        // textLayout.draw(g, (float) xOffset, (float) (yOffset - rectangle.getY()));
        Shape shape = textLayout.getOutline(null);
        AffineTransform orig = g.getTransform();
        AffineTransform at = new AffineTransform();
        at.translate((float) xOffset, (float) (yOffset - rectangle.getY()));
        g.transform(at);
        g.fill(shape);
        g.setTransform(orig);

        bounds =
            new Rectangle2D.Double(xOffset, yOffset - getChartPainter().getStyleManager().getAxisTitlePadding(), rectangle.getWidth(), rectangle.getHeight()
                + getChartPainter().getStyleManager().getAxisTitlePadding());
        // g.setColor(Color.blue);
        // g.draw(bounds);

      }
      else {
        bounds = new Rectangle2D.Double(axis.getPaintZone().getX(), axis.getPaintZone().getY() + axis.getPaintZone().getHeight(), axis.getPaintZone().getWidth(), 0);
        // g.setColor(Color.blue);
        // g.draw(bounds);

      }
    }
  }

  @Override
  public ChartPainter getChartPainter() {

    return axis.getChartPainter();
  }

  // Getters /////////////////////////////////////////////////

  public String getText() {

    return text;
  }

  public void setText(String text) {

    this.text = text;
  }
}

@@ -1138,17 +1138,16 @@
 or%0D%0A   *
- 
 %0D%0A   * @
@@ -2292,24 +2292,179 @@
 ).getY());%0D%0A
+%0D%0A        AffineTransform rot = AffineTransform.getRotateInstance(-1 * Math.PI / 2, 0, 0);%0D%0A        Shape shape = nonRotatedTextLayout.getOutline(rot);%0D%0A%0D%0A
         Affi
@@ -2546,32 +2546,34 @@
 neTransform();%0D%0A
+%0D%0A
         at.rotat
@@ -2571,31 +2571,18 @@
  at.
-rotate(Math.PI / -2.0, 
+translate(
 xOff
@@ -2637,41 +2637,18 @@
   g.
-drawString(text, xOffset, yOffset
+fill(shape
 );%0D%0A
@@ -2838,37 +2838,24 @@
     bounds =
-%0D%0A           
  new Rectang
@@ -2982,33 +2982,16 @@
 Height()
-%0D%0A               
  + getCh
@@ -3012,32 +3012,46 @@
 etStyleManager()
+%0D%0A            
 .getAxisTitlePad
@@ -4400,29 +4400,16 @@
 bounds =
-%0D%0A           
  new Rec
@@ -4549,25 +4549,8 @@
 ht()
-%0D%0A               
  + g
@@ -4575,32 +4575,46 @@
 etStyleManager()
+%0D%0A            
 .getAxisTitlePad

fix for issue #105 PDF rendering bug