Package pl.poznan.put.utility.svg
Class SVGHelper
java.lang.Object
pl.poznan.put.utility.svg.SVGHelper
A collection of methods to work with SVG images.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Rectangle2D
Calculates a bounding box for given node of SVG document.static org.w3c.dom.svg.SVGDocument
static byte[]
Exports SVG image to a given output image format.static org.w3c.dom.svg.SVGDocument
Loads an SVG image from file.static org.w3c.dom.svg.SVGDocument
Loads an SVG image from URI.static double
getHeight
(org.w3c.dom.svg.SVGDocument document) Retrieves the height of the SVG document.static LineMetrics
getLineMetrics
(org.apache.batik.svggen.SVGGraphics2D svg) Gets metrics for a line of ASCII letters in the context of SVG image.static double
getWidth
(org.w3c.dom.svg.SVGDocument document) Retrieves the width of the SVG document.static org.w3c.dom.svg.SVGDocument
Merges several SVG images into one, by putting one next to another (from left to right).static NamespaceContext
-
Method Details
-
fromFile
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
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
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
- 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
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
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.
-