Description
Parse raster and SVG files for diagrams.
Description
Parse raster and SVG images for the diagrams DSL.
README.md
diagrams-input
diagrams-input provides functions to parse several input formats for diagrams:
- Raster images: PNG, JPG, ... (using Juicy.Pixels)
- SVG (using xml-conduit and attoparsec)
In the future it would be nice to have:
- HTML + CSS (We need CSS anyway if we want to implement the SVG parser properly). HTML could be a good exercise for developing layouting functions.
- Collada (3d)
- Obj (3d)
Usage
The main functions are loadimageEmbedded and loadImageExternal that return either a Left error-message or a Right diagram. See the diagrams manual.
The SVG Parser
The SVG parser evolved like maybe most others also did: By taking some SVG images, focussing on one image, getting it displayed correctly. See if the changes affect other images positively. Figuring out what is the most important thing to fix next. The SVG 1.1 spec was used. For testing purposes diagrams-input-test reads and writes all images in a directory.
Supported Tags
Support of SVG 1.1:
Tags | Support | Partial Support | Missing |
---|---|---|---|
Document Structure: | \<svg\>\<g\>\<defs\>\<desc\>\<title\>\<symbol\>\<use\> | \<image\> | \<switch\> |
Style tag: | Yes | CSS | |
Path tag: | All commands | ||
Basic Shapes: | \<rect\>\<circle\>\<ellipse\>\<line\>\<polyline\>\<polygon\> | ||
Text tag: | Pass the tag through to diagrams-svg or replace it with outlines from the font tag | ||
Filling, Stroking and Marker Symbols: | \<marker\> | ||
Gradients, Patterns: | Linear and Radial Gradients | \<pattern\> | |
Clipping, Masking: | Clipping | \<mask\> | |
Filter effects | No, not supported by diagrams | ||
Linking | No | ||
Animation | No, SVG Animation will likely be replaced by Web Animations | ||
Fonts: | It is planned to integrate SVGFonts | ||
Metadata | No | ||
Inkscape tags | No |
TODO
- [ ] fill = "none" => path open
- [ ] Scale viewbox to width and height
- [ ] marker tag
- [ ] Transform exceptions into Left values in Image.hs (if monad transformers and conduit is easy for you please help me)
- [ ] inherit-attribute
- [ ] display-attribute
- [ ] Integrate SVGFonts, because it is too much entangled with SVG to be separate
- [ ] loadIamgeExternal for SVG
A Walk through the Code
- Input.hs contains the main functions loadimageEmbedded and loadImageExternal. They call readimage from JuicyPixels and readSVGFile from ReadSVG.hs.
- In ReadSVG.hs the xml file is parsed and translated into a tree. That was necessary because there need to be at least two passes because of references with the \<use\>-tag. The tree has Constructors that take functions that expect data (like css) that is only known after the first pass.
- All the nodes of the tree are stored in a key value storage. Every node contains the whole subtree, but this is no problem because of lazy evaluation
- Gradients that have nested references to other gradients are flattened into one gradient.
- References are inserted using the key-value-storage and everything is combined into one diagram.
Other SVG Parsers
- Haskell: svg-tree (currently not targeted at
diagrams
) - Java: Processing
- Java: Apache Batik
- Javascript: canvg.