package batictest;

import java.awt.geom.AffineTransform;
import java.io.File;
import javax.swing.JFrame;
import org.apache.batik.bridge.ViewBox;
import org.apache.batik.swing.JSVGCanvas;
import org.w3c.dom.svg.SVGPreserveAspectRatio;
import org.w3c.dom.svg.SVGRect;
import org.w3c.dom.svg.SVGSVGElement;

public class ScaleTest extends JFrame {
    public class MyCanvas extends JSVGCanvas{
        @Override
        protected AffineTransform calculateViewingTransform(String fragIdent, SVGSVGElement svgElt) {
            final float[] viewBox= this.getViewBox(svgElt);
            return ViewBox.getPreserveAspectRatioTransform(viewBox, SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN, true, super.getWidth(), super.getHeight());
        }

        private float[] getViewBox(final SVGSVGElement svgElt){
            final SVGRect rc = svgElt.getBBox();
            if (rc == null){
                return new float[]{0, 0, 0, 0};
            }else{
                return new float[]{0, 0, rc.getWidth(), rc.getHeight()};
            }
        }
    }

    private static final File SVG_FILE= new File("scaletest.svg");
    private MyCanvas canvas= new MyCanvas();

    public ScaleTest(){
        this.add(canvas);
        canvas.setURI(SVG_FILE.toURI().toString());        
    }

    public static void main(String[] args){
        ScaleTest frame= new ScaleTest();
        frame.setSize(500, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
