MIT licensed by goolord
Maintained by [email protected]
This version can be pinned in stack with:nano-svg-0.2.0.0@sha256:72f59584460b68cc33bbc1ef4f57c9ed3a246d0a5db96e8b3122c173dceed711,2652

Module documentation for 0.2.0.0

nano-svg

A fast SVG parser for Haskell. Bytes in, a flat array of shapes out.

For geometry renderers, rasterizers and UI toolkits that need SVG icons and static vector artwork as paths. The library parses and normalizes geometry; the caller handles rendering and viewport mapping.

Quick start

parseSvg :: ByteString -> Either String Document reads UTF-8 SVG directly from a strict ByteString—no text-decoding step needed.

import Graphics.NanoSvg
import qualified Data.ByteString as BS

main :: IO ()
main = do
  bytes <- BS.readFile "clock.svg"
  case parseSvg bytes of
    Left err -> putStrLn err
    Right doc -> print (documentSize doc, length (documentShapes doc))

A Document contains the viewport size, the viewBox and a SmallArray of Shapes in paint order. Each shape carries its path, the Matrix that maps it into the viewBox, and its resolved Style. Relative path commands, shorthands and basic shapes become a common set of absolute path segments. The renderer walks segments without resolving an SVG tree, style inheritance or a transform stack. It still tracks the current point and subpath start.

encodeSvg :: Document -> ByteString writes a document back out as SVG, one path per shape; svgBuilder gives the same as a bytestring Builder. Parsing the result gives back the same shapes.

SVG scope

Supports paths, rectangles, circles, ellipses, lines, polylines and polygons; groups and use references; fills, strokes and inline styles; and SVG transforms. The focus is the static geometry used in icons and vector assets.

Not supported: gradients, patterns and other paint servers; text; clipPath, mask and filter; marker; CSS in a style element or an external sheet, and so class selectors; animation; and preserveAspectRatio, which is left to the renderer.

Unknown elements are skipped along with their children. Group opacity is multiplied into each shape rather than preserved as a compositing group. Nested svg and referenced symbol elements do not establish viewports.

Modules

Module Purpose
Graphics.NanoSvg parseSvg and encodeSvg, re-exporting the two below
Graphics.NanoSvg.Types the document model and matrix helpers
Graphics.NanoSvg.Attribute standalone parsers for paths, transforms, colors, numbers and lengths

Build

cabal build
cabal test
cabal haddock --open

Changes

Revision history for nano-svg

0.2.0.0

  • Breaking: collapse into three modules. Graphics.NanoSvg.Color, .Number, .Path, .Xml and .Internal.Parser are gone; their value parsers live in Graphics.NanoSvg.Attribute.
  • Breaking: remove documentWidth/documentHeight, the RGBA channel accessors, withAlpha, transparent, Length/Unit and the raw flatparse parsers. parseLength now returns user units.
  • Add encodeSvg and svgBuilder, which write a Document back out as SVG with bytestring builders. parseSvg reads the result back to the same shapes.
  • use elements nest at most maxUseDepth (8) deep, so a small file cannot expand exponentially.
  • Entity decoding is linear in the attribute length, and numeric character references no longer build an Integer from any number of digits.
  • Look up use targets and named colors in maps, and share a target’s geometry across its uses.
  • 0e400 and long mantissas parse as finite numbers.

0.1.0.0

  • First version.