Class SVGHelper


  • public final class SVGHelper
    extends Object
    A collection of methods to work with SVG images.
    • Method Detail

      • fromFile

        public static org.w3c.dom.svg.SVGDocument fromFile​(File file)
                                                    throws IOException
        Loads an SVG image from file.
        Parameters:
        file - The path to the SVG file.
        Returns:
        An instance of SVGDocument parsed from the file.
        Throws:
        IOException - When reading of file fails.
      • fromUri

        public static org.w3c.dom.svg.SVGDocument fromUri​(URI uri)
                                                   throws IOException
        Loads an SVG image from URI.
        Parameters:
        uri - The URI of the SVG image.
        Returns:
        An instance of SVGDocument parsed from the URI.
        Throws:
        IOException - When reading of the URI fails.
      • getLineMetrics

        public static LineMetrics getLineMetrics​(org.apache.batik.svggen.SVGGraphics2D svg)
        Gets metrics for a line of ASCII letters in the context of SVG image.
        Parameters:
        svg - An instance of SVG graphics.
        Returns:
        Detailed metrics for the specific font in the SVG graphics.
      • svgNamespaceContext

        public static NamespaceContext svgNamespaceContext()
        Returns:
        A singleton instance of SVG namespace.
      • export

        public static byte[] export​(org.w3c.dom.svg.SVGDocument svgDocument,
                                    Format format)
                             throws IOException
        Exports SVG image to a given output image format. When exporting to SVG, this method uses Batik's encoder. For other output formats, by default this method will try to use Inkscape (i.e. run an external process) and if it fails, this method will resort back to Batik's transcoder capabilities. This is due to the fact, that Batik sometimes has problems with rasterization of complex SVG images and Inkscape seems to perform better.
        Parameters:
        svgDocument - The image to export.
        format - The output format.
        Returns:
        An array of bytes with the image data.
        Throws:
        IOException - When exporting fails.
      • merge

        public static org.w3c.dom.svg.SVGDocument merge​(List<org.w3c.dom.svg.SVGDocument> svgs)
        Merges several SVG images into one, by putting one next to another (from left to right).
        Parameters:
        svgs - The list of SVGs.
        Returns:
        An instance of SVGDocument with all input images combined.
      • emptyDocument

        public static org.w3c.dom.svg.SVGDocument emptyDocument()
        Returns:
        An empty SVG image.
      • calculateBoundingBox

        public static Rectangle2D calculateBoundingBox​(Document doc)
        Calculates a bounding box for given node of SVG document.
        Parameters:
        doc - A node in SVG document.
        Returns:
        Bounding box of this node.
      • getWidth

        public static double getWidth​(org.w3c.dom.svg.SVGDocument document)
        Retrieves the width of the SVG document.
        Parameters:
        document - The SVG image.
        Returns:
        The width of the SVG image.
      • getHeight

        public static double getHeight​(org.w3c.dom.svg.SVGDocument document)
        Retrieves the height of the SVG document.
        Parameters:
        document - The SVG image.
        Returns:
        The height of the SVG image.