A module containing all the basic types for dealing with SVG files.
- newtype MM = MM Double
- data Size = Size {}
- newtype DPI = DPI Double
- data SVG
- getSVGElement :: SVG -> Element
- makeSVG :: Element -> Maybe SVG
- makeBlankSVG :: Size -> SVG
- parseSVG :: String -> Maybe SVG
- getSVGSize :: DPI -> SVG -> Maybe Size
- namespaces :: SVG -> [(QName, String)]
- parseCoord :: DPI -> String -> Maybe MM
- placeAt :: DPI -> (MM, MM) -> [Content] -> Element
Documentation
A size (width and height) measured in millimetres.
A dots-per-inch measurement for dealing with graphics.
(To get dots per millimetre, divide by 25.4)
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.
makeBlankSVG :: Size -> SVGSource
Makes a blank SVG file of the given size.
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).