rasterific-svg-0.1.0.1: SVG renderer based on Rasterific.

Safe HaskellNone

Graphics.Rasterific.Svg

Contents

Description

Svg renderer based on Rasterific.

Here is a simple example of loading a SVG file (using svg-tree) rendering it to a picture, and saving it to a PNG (using Juicy.Pixels)

 import Codec.Picture( writePng )
 import Graphics.Svg( loadSvgFile )
 import Graphics.Rasterific.Svg( loadCreateFontCache
                               , renderSvgDocument
                               )
 loadRender :: FilePath -> FilePath -> IO ()
 loadRender svgfilename pngfilename = do
   f <- loadSvgFile svgfilename
   case f of
     Nothing -> putStrLn Error while loading SVG
     Just doc -> do
       cache <- loadCreateFontCache fonty-texture-cache
       (finalImage, _) <- renderSvgDocument cache Nothing 96 doc
       writePng pngfilename finalImage

Synopsis

Main functions

drawingOfSvgDocumentSource

Arguments

:: FontCache

Structure used to access fonts

-> Maybe (Int, Int)

Optional document size

-> Dpi

Current resolution for text and elements

-> Document

Svg document

-> IO (DrawResult, LoadedElements) 

Render an svg document to a Rasterific Drawing. If you provide a size, the document will be stretched to match the provided size.

The DPI parameter really should depend of your screen, but a good default value is 96

The use of the IO Monad is there to allow loading of fonts and referenced images.

renderSvgDocumentSource

Arguments

:: FontCache

Structure used to access fonts

-> Maybe (Int, Int)

Optional document size

-> Dpi

Current resolution for text and elements

-> Document

Svg document

-> IO (Image PixelRGBA8, LoadedElements) 

Render an svg document to an image. If you provide a size, the document will be stretched to match the provided size.

The DPI parameter really should depend of your screen, but a good default value is 96

The use of the IO Monad is there to allow loading of fonts and referenced images.

loadCreateFontCache :: FilePath -> IO FontCacheSource

This function will create a font cache, a structure allowing to quickly match a font family name and style to a specific true type font on disk.

The cache is saved on disk at the filepath given as parameter. If a cache is found it is automatically loaded from the file.

Creating the cache is a rather long operation (especially on Windows), that's why you may want to keep the cache around.

Types

data LoadedElements Source

Constructors

LoadedElements 

Fields

_loadedFonts :: Map FilePath Font
 
_loadedImages :: Map FilePath (Image PixelRGBA8)
 

data Result Source

Rendering status.

Constructors

ResultSuccess

No problem found

ResultError String

Error with message

Instances

Eq Result 
Show Result 

data DrawResult Source

Represent a Rasterific drawing with the associated image size.

Constructors

DrawResult 

Fields

_drawAction :: Drawing PixelRGBA8 ()

Rasterific drawing, can be reused and composed with other elements for scene drawing.

_drawWidth :: !Int

Supposed drawing width of the drawing, ideally represent the final image width.

_drawHeight :: !Int

Supposed drawing height of the drawing, ideally represent the final image height.

type Dpi = Int

Express device resolution in dot per inch.

Other helper functions

renderSvgFile :: FilePath -> FilePath -> IO ResultSource

Convert an SVG file to a PNG file, return True if the operation went without problems.

This function will call loadCreateFontCache with the filename fonty-texture-cache