Search code examples
javaswingsvgscalebatik

Java Batik resize SVG to panel size keeping aspect ratio


I'm trying to display an SVG Image on my swing application, since i don't have an SVG file but a path i'm converting the path to a valid svg document with:

private static Document buildSVGDocument(Color svgColor, /*double svgWidth, double svgHeight,*/ String svgPath) {
    DOMImplementation svgDocumentImplementation = SVGDOMImplementation.getDOMImplementation();
    Document svgDocument = svgDocumentImplementation.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null);

    Element svgDocumentElement = svgDocument.getDocumentElement();
    //svgDocumentElement.setAttribute("height", String.valueOf(svgHeight));
    //svgDocumentElement.setAttribute("width", String.valueOf(svgWidth));

    Element svgDocumentPath = svgDocument.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "path");
    svgDocumentPath.setAttribute("style", String.format("fill:rgb(%s, %s, %s);", svgColor.getRed(), svgColor.getGreen(), svgColor.getBlue()));
    svgDocumentPath.setAttribute("d", svgPath);
    svgDocumentElement.appendChild(svgDocumentPath);

    return svgDocument;
}

Then I display the SVG Document on a Batik Canvas:

JSVGCanvas panel = new JSVGCanvas();
panel.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
panel.setDisableInteractions(true);
panel.setDocument(buildSVGDocument(/*etc*/));
container.add(panel, BorderLayout.WEST);

Now my question is: how do I resize the svg to the panel size keeping the aspect-ratio?


Solution

  • I figured out how to do it!, Browsing the source I found the method used to calculate the image scale, it's:

    calculateViewingTransform
    

    Then I've implemented a simple class to scale the image to the container

    public class SVGCanvas extends JSVGCanvas {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * The type of scale
         */
        private short svgScale;
    
        /**
         * Image padding
         */
        private int svgPadding;
    
        public SVGCanvas() {
            this.svgScale = SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMIDYMAX;
            this.svgPadding = 5;
        }
    
        @Override
        protected AffineTransform calculateViewingTransform(String svgElementIdentifier, SVGSVGElement svgElement) {
            SVGRect svgElementBounds = svgElement.getBBox();
            float[] svgElementBoundsVector = new float[] {
                svgElementBounds.getX(),
                svgElementBounds.getY(),
                svgElementBounds.getWidth(),
                svgElementBounds.getHeight()
            };
    
            float svgEemenetScaleToHeight = getHeight() - svgPadding;
            float svgElementScaleToWidth = getWidth() - svgPadding;
    
            return ViewBox.getPreserveAspectRatioTransform(
                svgElementBoundsVector, svgScale, true,
                svgElementScaleToWidth,
                svgEemenetScaleToHeight
            );
        }
    
        public void setSvgScale(short svgScale) {
            this.svgScale = svgScale;
        }
    
        public void setSvgPadding(int svgPadding) {
            this.svgPadding = svgPadding;
        }
    
    }