package com.testcase;

// Created 2001-Aug-13 Mon by Randall Glenn Parker
// The purpose of this is to serve as a test bed for
// problems with Batik and FOP.


import javax.swing.JFrame;
import javax.swing.JPanel;
//import java.awt.GraphicsConfiguration;

import java.awt.Color; // implements Paint
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Paint;
import java.awt.BorderLayout;



import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.fop.svg.PDFDocumentGraphics2D;

   // Things we need to import these to support getRenderContext

import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;

import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.gvt.GraphicsNodeRenderContext;
import org.apache.batik.gvt.event.EventDispatcher;
import org.apache.batik.gvt.renderer.ImageRenderer;
import org.apache.batik.gvt.renderer.ImageRendererFactory;
import org.apache.batik.gvt.filter.ConcreteGraphicsNodeRableFactory;
import org.apache.batik.gvt.filter.GraphicsNodeRableFactory;
   // get TextPainter and StrokingTextPainter from these.
import org.apache.batik.gvt.*;
import org.apache.batik.gvt.renderer.*;


public class TestCase1 //implements RenderDrawingInterface
{
   Font FirstLineFnt    = new Font("sans-serif",Font.BOLD,14);
   Font SecondLineFnt   = new Font("Helvetica",Font.PLAIN,12);

   Font LgdTopLblFnt = new Font("Helvetica",Font.BOLD,11);
   Font LgdBotLblFnt = new Font("Helvetica",Font.ITALIC,9);
   Font LgdLftLblFnt = new Font("Helvetica",Font.PLAIN,14);
   //Font LgdLftLblFnt = new Font("SansSerif",Font.PLAIN,14);
   Font LgdRgtLblFnt = new Font("Times Roman",Font.ITALIC | Font.PLAIN,14);

   Font LgdLblFnt    = new Font("sans-serif",Font.PLAIN,10);
   Font ChartLblFnt  = new Font("Times-Roman",Font.PLAIN,10);
   Font SliceLblFnt  = new Font("sans-serif",Font.PLAIN,7);

   Font BarNumFnt    = new Font("sanserif",Font.PLAIN,8);
   Font BarLblFnt    = new Font("Times",Font.PLAIN,8);
   Font XAxisFnt     = new Font("Times",Font.PLAIN,10);
   Font YAxisFnt     = new Font("Times",Font.PLAIN,10);

   Font[] FontArr = new Font[20];


   java.awt.Graphics2D gr2d;
   java.awt.font.FontRenderContext frc;



   public void initFonts()
   {
      for (int ii=0; ii<12; ii++)
         FontArr[ii]   = new Font("Times",Font.PLAIN,14);


      //FontArr[0]   = new Font("sans-serif",Font.BOLD,14);
      //FontArr[1]   = new Font("sans-serif",Font.PLAIN,12);

      //FontArr[2] = new Font("sans-serif",Font.BOLD,11);
      //FontArr[3] = new Font("Times",Font.ITALIC,9);
      //FontArr[4] = new Font("sans-serif",Font.PLAIN,14);
      //FontArr[5] = new Font("Times Roman",Font.ITALIC | Font.PLAIN,14);

      //FontArr[6]  = new Font("sans-serif",Font.PLAIN,10);
      //FontArr[7]  = new Font("Times-Roman",Font.PLAIN,10);
      //FontArr[8]  = new Font("sans-serif",Font.PLAIN,7);

      //FontArr[9]    = new Font("sanserif",Font.PLAIN,8);
      //FontArr[10]    = new Font("Times",Font.PLAIN,8);
      //FontArr[11]    = new Font("Times",Font.PLAIN,10);
      //FontArr[12]    = new Font("Times",Font.PLAIN,10);

   }


   public void drawSomeStuff(java.awt.Graphics2D gr2d,
                        double XStart,
                        double YStart,
                        double Width,
                        double Height
                        )
   {
      AffineTransform tmpTransform = gr2d.getTransform(); // save to restore when finished here.

      double Radius;
      if (Height < Width)
         Radius = Height * 0.7 * 0.5;
      else
         Radius = Width * 0.7 * 0.5;

      double XCenter = XStart + (0.5*Width);
      double YCenter = YStart + (0.5*Height);

      double QuarterPi = Math.PI*0.25; // 45 degrees.
      double CurrPi = 0.0;
      for (int ii=0; ii<8; ii++)
      {
         AffineTransform twister = AffineTransform.getRotateInstance(CurrPi); //
         gr2d.setTransform(twister); // add this transform temporarily

         double TxtXStart = XCenter + (java.lang.Math.cos(CurrPi) * Radius);   //
         double TxtYStart = YCenter + (java.lang.Math.sin(CurrPi) * Radius);  //

         java.awt.geom.Point2D OrigP = new java.awt.geom.Point2D.Double();
         OrigP.setLocation(TxtXStart,TxtYStart);
         java.awt.geom.Point2D XformP = new java.awt.geom.Point2D.Double();
         try {
            twister.inverseTransform(OrigP,XformP);
         }
         catch (java.awt.geom.NoninvertibleTransformException ex)
         {
               // do nothing?
               XformP = OrigP; // can't transform it for some unexpected reason.
         }

         //float XPoint = (XCenter + XformP.getX());
         //float YPoint = (YCenter + XformP.getY());
         float XPoint = (float)(XformP.getX());
         float YPoint = (float)(XformP.getY());

         // now draw up the left text label.
         gr2d.setFont(FontArr[ii]);
         gr2d.drawString("HelloAAA"+ new Integer(ii).toString(),
            (float)XPoint,
            (float)YPoint);

         //gr2d.drawString("HelloBBB"+ new Integer(ii).toString(),
         //   (float)TxtXStart,
         //   (float)TxtYStart);

         //gr2d.drawString("HelloCCC"+ new Integer(ii).toString(),
         //   (float)XCenter,
         //   (float)YCenter);


         CurrPi = CurrPi + QuarterPi;
      }

      gr2d.setTransform(tmpTransform); // restore original transform.
   }


   public void genPdfOutFile(
      float DrawingWidth,
      float DrawingHeight,
      boolean TextAsShapesFlag,
      String PdfFileName
      )
   {
      try
      {
        java.io.FileOutputStream PdfOutStream = new java.io.FileOutputStream(PdfFileName); //, "UTF-8");
        //java.io.Writer PdfOutWriter = new java.io.OutputStreamWriter(PdfOutStream, "UTF-8");

        PDFDocumentGraphics2D PdfGr2d =
           new PDFDocumentGraphics2D(TextAsShapesFlag, PdfOutStream , (int)DrawingWidth,(int)DrawingHeight);
        PdfGr2d.setSVGDimension(DrawingWidth,DrawingHeight);
        GraphicsNodeRenderContext rc = getRenderContext(true);
        PdfGr2d.setGraphicContext(
           new org.apache.batik.ext.awt.g2d.GraphicContext());
        PdfGr2d.setRenderingHints(rc.getRenderingHints());
        // Ask the component to render into the PDF Graphics2D implementation.
        //((Graphics2D)PdfGr2d).setClip(0, 0, width, height);

        drawSomeStuff(PdfGr2d,0,0,DrawingWidth,DrawingHeight);

        // What is this thing?
        //component.export((Graphics2D)pdfGenerator, pageFormat, false);

        //boolean useCSS = true; // we want to use CSS style attribute
        PdfGr2d.finish(); // this will stream out the results.
        PdfOutStream.close();
      }
      catch (java.io.FileNotFoundException exc)
      {
         System.out.println("FileNotFoundException opening pdf output file: " + PdfFileName + ". " + exc.getMessage());
      }
      catch (java.io.IOException exc)
      {
         System.out.println("IOException opening pdf output file: " + PdfFileName);
      }
      catch (java.lang.SecurityException exc)
      {
         System.out.println("SecurityException opening pdf output file: " + PdfFileName);
      }
   }


   public TestCase1()
   {
      initFonts();

      float DrawingWidth=540, DrawingHeight=720;
      int DrawingWidthInt=(int)DrawingWidth, DrawingHeightInt=(int)DrawingHeight;


      org.apache.batik.transcoder.Transcoder tc;
      org.w3c.dom.DOMImplementation domImpl =
         org.apache.batik.dom.GenericDOMImplementation.getDOMImplementation();
      org.w3c.dom.Document document = domImpl.createDocument(null,"svg",null);
      SVGGraphics2D svggr2d = new SVGGraphics2D(document);
        svggr2d.setSVGCanvasSize(new Dimension(DrawingWidthInt, DrawingHeightInt));

      drawSomeStuff(svggr2d,0,0,DrawingWidth,DrawingHeight);

      String SvgOutputFileName = new String("\\tmp\\svgtest.svg");
      String PdfTextAsShapesFileName = new String("\\tmp\\pdfasshapes.pdf");
      String PdfTextNotShapesFileName = new String("\\tmp\\pdfnotshapes.pdf");
      try
      {
        //java.io.Writer SvgOut = new java.io.FileWriter("\\tmp\\svgout.svg"); //, "UTF-8");
        java.io.FileOutputStream SvgOut = new java.io.FileOutputStream(SvgOutputFileName); //, "UTF-8");
        boolean useCSS = true; // we want to use CSS style attribute
        java.io.Writer StreamOut = new java.io.OutputStreamWriter(SvgOut, "UTF-8");
        svggr2d.stream(StreamOut, useCSS);
        SvgOut.close();
      }
      catch (java.io.FileNotFoundException exc)
      {
         System.out.println("FileNotFoundException opening svg output file: " + exc.getMessage());
      }
      catch (java.io.IOException exc)
      {
         System.out.println("IOException opening svg output file");
      }
      catch (java.lang.SecurityException exc)
      {
         System.out.println("SecurityException opening svg output file");
      }

      genPdfOutFile(
         DrawingWidth,
         DrawingHeight,
         true,
         PdfTextAsShapesFileName
         );

      genPdfOutFile(
         DrawingWidth,
         DrawingHeight,
         false,
         PdfTextNotShapesFileName
         );


      System.out.println("Font Family, FontName, Name, PSName, Style= "
                      + "  " + FirstLineFnt.getFamily()
                      + "  " + FirstLineFnt.getFontName()
                      + "  " + FirstLineFnt.getName()
                      + "  " + FirstLineFnt.getPSName()
                      + "  " + FirstLineFnt.getStyle());

      System.out.println("Font Family, FontName, Name, PSName, Style= "
                      + "  " + SecondLineFnt.getFamily()
                      + "  " + SecondLineFnt.getFontName()
                      + "  " + SecondLineFnt.getName()
                      + "  " + SecondLineFnt.getPSName()
                      + "  " + SecondLineFnt.getStyle());

      System.out.println("Font Family, FontName, Name, PSName, Style= "
                      + "  " + LgdBotLblFnt.getFamily()
                      + "  " + LgdBotLblFnt.getFontName()
                      + "  " + LgdBotLblFnt.getName()
                      + "  " + LgdBotLblFnt.getPSName()
                      + "  " + LgdBotLblFnt.getStyle());

      System.out.println("Font Family, FontName, Name, PSName, Style= "
                      + "  " + LgdTopLblFnt.getFamily()
                      + "  " + LgdTopLblFnt.getFontName()
                      + "  " + LgdTopLblFnt.getName()
                      + "  " + LgdTopLblFnt.getPSName()
                      + "  " + LgdTopLblFnt.getStyle());

      System.out.println("Font Family, FontName, Name, PSName, Style= "
                      + "  " + LgdLblFnt.getFamily()
                      + "  " + LgdLblFnt.getFontName()
                      + "  " + LgdLblFnt.getName()
                      + "  " + LgdLblFnt.getPSName()
                      + "  " + LgdLblFnt.getStyle());

   }


   public static void main(String args[])
   {
      // For now use it to paint up a set of test charts with test data.

      TestCase1 ct = new TestCase1();
   }



   // Copied out of org.apache.fop.svg.PDFTranscoder.java
   public GraphicsNodeRenderContext getRenderContext(boolean stroke) {
       GraphicsNodeRenderContext nodeRenderContext = null;
       if (nodeRenderContext == null) {
           RenderingHints hints = new RenderingHints(null);
           hints.put(RenderingHints.KEY_ANTIALIASING,
                     RenderingHints.VALUE_ANTIALIAS_ON);

           hints.put(RenderingHints.KEY_INTERPOLATION,
                     RenderingHints.VALUE_INTERPOLATION_BILINEAR);

           FontRenderContext fontRenderContext =
               new FontRenderContext(new AffineTransform(), true, true);

           TextPainter textPainter = null;
           textPainter = new StrokingTextPainter();

           GraphicsNodeRableFactory gnrFactory =
               new ConcreteGraphicsNodeRableFactory();

           nodeRenderContext =
               new GraphicsNodeRenderContext(new AffineTransform(), null,
                                             hints, fontRenderContext,
                                             textPainter, gnrFactory);
       }

       return nodeRenderContext;
   }



}
