svgutils-0.1: Helper functions for dealing with SVG files



A module containing all the basic types for dealing with SVG files.



newtype MM Source

A wrapper around Double for measurements in millimetres.

The Show instance appends "mm" to the value.


MM Double 


data Size Source

A size (width and height) measured in millimetres.




mmWidth :: MM
mmHeight :: MM


newtype DPI Source

A dots-per-inch measurement for dealing with graphics.

(To get dots per millimetre, divide by 25.4)


DPI Double 

data SVG Source

A container for SVG documents. See the makeSVG function for creating them, and the getSVGElement function for accessing them.

The Show instance prints this as a complete XML document.


getSVGElement :: SVG -> ElementSource

Gets the top-level "svg" element.

makeSVG :: Element -> Maybe SVGSource

Creates an SVG item from an XML element.

If the element is named "svg", this function will return a Just result. If the element is named anything else, this function will return Nothing.

makeBlankSVG :: Size -> SVGSource

Makes a blank SVG file of the given size.

parseSVG :: String -> Maybe SVGSource

Parses a String containing a complete XML document into an SVG.

This function can fail in two ways: it will fail either if the String is not a complete valid XML document, or if the top-level element is not an "svg" element.

getSVGSize :: DPI -> SVG -> Maybe SizeSource

Gets the size of the SVG document.

In an ideal world, this size would be some measurement in centimetres, etc. that would be trivial to convert to millimetres.

Unfortunately, some programs (most notably Inkscape) record the document size in pixels, which is very unhelpful when trying to get the size of the document for printing, etc. Therefore you must supply a DPI parameter for converting this pixel size into millimetres. On my system, Inkscape uses a DPI of 90 but I am not sure if this is system-specific or a constant that is used on all machines.

The method will fail if either the width or height attributes are missing at the top-level, or they cannot be parsed using parseCoord.

namespaces :: SVG -> [(QName, String)]Source

Gets all the namespaces from the header of the SVG file.

parseCoord :: DPI -> String -> Maybe MMSource

Parses a coordinate/length value from an SVG file.

All valid units are supported, except "em" and "ex" which depend on the size of the current font.

The DPI parameter is needed in order to convert user coordinate units (pixels) to millimetres.

This method assumes that no transformation is currently in place on the size. It is primarily intended for parsing the size of the document, where there can be no transformations present.

placeAt :: DPI -> (MM, MM) -> [Content] -> ElementSource

Places the given XML content (which is assumed to be a valid SVG fragment) at the given (x, y) coordinates by wrapping them in an appropriate SVG transformation (<g> element with transform attribute).

Note that if you place the resulting element inside a transformation, that transformation will of course apply to this element as is standard in SVG. So if you place something at (20, 20) then wrap that in a scale transformation with factor 0.1, it will end up placed at (2, 2).