svg-tree-0.2: SVG file loader and serializer

Safe HaskellNone
LanguageHaskell2010

Graphics.Svg.Types

Contents

Description

This module define all the types used in the definition of a svg scene.

Most of the types are lensified.

Synopsis

Basic building types

type Coord = Float Source

Basic coordiante type.

data Origin Source

Tell if a path command is absolute (in the current user coordiante) or relative to the previous poitn.

Constructors

OriginAbsolute

Next point in absolute coordinate

OriginRelative

Next point relative to the previous

Instances

type Point = (Number, Number) Source

Possibly context dependant point.

type RPoint = V2 Coord Source

Real Point, fully determined and not dependant of the rendering context.

data PathCommand Source

Path command definition.

Constructors

MoveTo Origin [RPoint]

M or m command

LineTo Origin [RPoint]

Line to, L or l Svg path command.

HorizontalTo Origin [Coord]

Equivalent to the H or h svg path command.

VerticalTo Origin [Coord]

Equivalent to the V or v svg path command.

CurveTo Origin [(RPoint, RPoint, RPoint)]

Cubic bezier, C or c command

SmoothCurveTo Origin [(RPoint, RPoint)]

Smooth cubic bezier, equivalent to S or s command

QuadraticBezier Origin [(RPoint, RPoint)]

Quadratic bezier, Q or q command

SmoothQuadraticBezierCurveTo Origin [RPoint]

Quadratic bezier, T or t command

ElipticalArc Origin [(Coord, Coord, Coord, Coord, Coord, RPoint)]

Eliptical arc, A or a command.

EndPath

Close the path, Z or z svg path command.

data Transformation Source

Describe the content of the transformation attribute. see _transform and transform.

Constructors

TransformMatrix Coord Coord Coord Coord Coord Coord

Directly encode the translation matrix.

Translate Float Float

Translation along a vector

Scale Float (Maybe Float)

Scaling on both axis or on X axis and Y axis.

Rotate Float (Maybe (Float, Float))

Rotation around `(0, 0)` or around an optional point.

SkewX Float

Skew transformation along the X axis.

SkewY Float

Skew transformation along the Y axis.

TransformUnknown

Unkown transformation, like identity.

data ElementRef Source

Correspond to the possible values of the the attributes which are either none or `url(#elem)`

Constructors

RefNone

Value for none

Ref String

Equivalent to `url()` attribute.

data CoordinateUnits Source

Define the possible values of various *units attributes used in the definition of the gradients and masks.

Constructors

CoordUserSpace

userSpaceOnUse value

CoordBoundingBox

objectBoundingBox value

Building helpers

toPoint :: Number -> Number -> Point Source

Little helper function to build a point.

serializeNumber :: Number -> String Source

Encode the number to string which can be used in a CSS or a svg attributes.

serializeTransformation :: Transformation -> String Source

Convert the Transformation to a string which can be directly used in a svg attributes.

serializeTransformations :: [Transformation] -> String Source

Transform a list of transformations to a string for svg transform attributes.

Drawing control types

data Cap Source

Describe how the line should be terminated when stroking them. Describe the values of the `stroke-linecap` attribute. See _strokeLineCap

Constructors

CapRound

End with a round (round value)

CapButt

Define straight just at the end (butt value)

CapSquare

Straight further of the ends (square value)

Instances

data LineJoin Source

Define the possible values of the `stroke-linejoin` attribute. see _strokeLineJoin

Constructors

JoinMiter

miter value

JoinBevel

bevel value

JoinRound

round value

Instances

data Number Source

Encode complex number possibly dependant to the current render size.

Constructors

Num Float

Simple coordinate in current user coordinate.

Px Float

With suffix "px"

Em Float

Number relative to the current font size.

Percent Float

Number relative to the current viewport size.

Pc Float 
Mm Float

Number in millimeters, relative to DPI.

Cm Float

Number in centimeters, relative to DPI.

Point Float

Number in points, relative to DPI.

Inches Float

Number in inches, relative to DPI.

Instances

data Spread Source

Define the possible values for the spreadMethod values used for the gradient definitions.

Constructors

SpreadRepeat

reapeat value

SpreadPad

pad value

SpreadReflect

`reflect value`

Instances

data Texture Source

Describe the different value which can be used in the fill or stroke attributes.

Constructors

ColorRef PixelRGBA8

Direct solid color (rgb)

TextureRef String

Link to a complex texture (url(#name))

FillNone

Equivalent to the none value.

Instances

data Element Source

Sum types helping keeping track of all the namable elemens in a SVG document.

Instances

data FillRule Source

Describe the possile filling algorithms. Map the values of the `fill-rule` attributes.

Constructors

FillEvenOdd

Correspond to the evenodd value.

FillNonZero

Correspond to the nonzero value.

Instances

data FontStyle Source

Classify the font style, used to search a matching font in the FontCache.

type Dpi = Int Source

Alias describing a "dot per inch" information used for size calculation (see toUserUnit).

Main type

data Document Source

Represent a full svg document with style, geometry and named elements.

documentSize :: Dpi -> Document -> (Int, Int) Source

Calculate the document size in function of the different available attributes in the document.

Drawing attributes

data DrawAttributes Source

This type define how to draw any primitives, which color to use, how to stroke the primitives and the potential transformations to use.

All these attributes are propagated to the children.

Constructors

DrawAttributes 

Fields

_strokeWidth :: !(Last Number)

Attribute corresponding to the `stroke-width` SVG attribute.

_strokeColor :: !(Last Texture)

Correspond to the stroke attribute.

_strokeOpacity :: !(Maybe Float)

Define the `stroke-opacity` attribute, the transparency for the "border".

_strokeLineCap :: !(Last Cap)

Correspond to the `stroke-linecap` SVG attribute

_strokeLineJoin :: !(Last LineJoin)

Correspond to the `stroke-linejoin` SVG attribute

_strokeMiterLimit :: !(Last Float)

Define the distance of the miter join, correspond to the `stroke-miterlimit` attritbue.

_fillColor :: !(Last Texture)

Define the filling color of the elements. Corresponding to the fill attribute.

_fillOpacity :: !(Maybe Float)

Define the `fill-opacity` attribute, the transparency for the "content".

_groupOpacity :: !(Maybe Float)

Define the global or group opacity attribute.

_transform :: !(Maybe [Transformation])

Content of the transform attribute

_fillRule :: !(Last FillRule)

Define the `fill-rule` used during the rendering.

_maskRef :: !(Last ElementRef)

Define the mask attribute.

_clipPathRef :: !(Last ElementRef)

Define the `clip-path` attribute.

_clipRule :: !(Last FillRule)

Define the `clip-rule` attribute.

_attrClass :: ![Text]

Map to the `class` attribute. Used for the CSS rewriting.

_attrId :: !(Maybe String)

Map to the id attribute. Used for the CSS rewriting.

_strokeOffset :: !(Last Number)

Define the start distance of the dashing pattern. Correspond to the `stroke-dashoffset` attribute.

_strokeDashArray :: !(Last [Number])

Define the dashing pattern for the lines. Correspond to the `stroke-dasharray` attribute.

_fontSize :: !(Last Number)

Current size of the text, correspond to the `font-size` SVG attribute.

_fontFamily :: !(Last [String])

Define the possible fonts to be used for text rendering. Map to the `font-family` attribute.

_fontStyle :: !(Last FontStyle)

Map to the `font-style` attribute.

_textAnchor :: !(Last TextAnchor)

Define how to interpret the text position, correspond to the `text-anchor` attribute.

_markerStart :: !(Last ElementRef)

Define the marker used for the start of the line. Correspond to the `marker-start` attribute.

_markerMid :: !(Last ElementRef)

Define the marker used for every point of the polyline/path Correspond to the `marker-mid` attribute.

_markerEnd :: !(Last ElementRef)

Define the marker used for the end of the line. Correspond to the `marker-end` attribute.

SVG drawing primitives

Rectangle

data Rectangle Source

Define a rectangle. Correspond to `<rectangle>` svg tag.

Constructors

Rectangle 

Fields

_rectDrawAttributes :: DrawAttributes

Rectangle drawing attributes.

_rectUpperLeftCorner :: Point

Upper left corner of the rectangle, correspond to the attributes x and y.

_rectWidth :: Number

Rectangle width, correspond, strangely, to the width attribute.

_rectHeight :: Number

Rectangle height, correspond, amazingly, to the height attribute.

_rectCornerRadius :: (Number, Number)

Define the rounded corner radius radius of the rectangle. Correspond to the rx and ry attributes.

Line

data Line Source

Define a simple line. Correspond to the `<line>` tag.

Constructors

Line 

Fields

_lineDrawAttributes :: DrawAttributes

Drawing attributes of line.

_linePoint1 :: Point

First point of the the line, correspond to the x1 and y1 attributes.

_linePoint2 :: Point

Second point of the the line, correspond to the x2 and y2 attributes.

class HasLine c where Source

Lenses for the Line type.

Minimal complete definition

line

Instances

Polygon

data Polygon Source

Primitive decriving polygon composed of segements. Correspond to the `<polygon>` tag

Constructors

Polygon 

Fields

_polygonDrawAttributes :: DrawAttributes

Drawing attributes for the polygon.

_polygonPoints :: [RPoint]

Points of the polygon. Correspond to the points attributes.

class HasPolygon c where Source

Lenses for the Polygon type

Minimal complete definition

polygon

Instances

Polyline

data PolyLine Source

This primitive describe an unclosed suite of segments. Correspond to the `<polyline>` tag.

Constructors

PolyLine 

Fields

_polyLineDrawAttributes :: DrawAttributes

drawing attributes of the polyline.

_polyLinePoints :: [RPoint]

Geometry definition of the polyline. correspond to the points attribute

class HasPolyLine c where Source

Lenses for the PolyLine type.

Minimal complete definition

polyLine

Path

data Path Source

Type mapping the `<path>` svg tag.

Constructors

Path 

Fields

_pathDrawAttributes :: DrawAttributes

Drawing attributes of the path.

_pathDefinition :: [PathCommand]

Definition of the path, correspond to the d attributes.

class HasPath c where Source

Lenses for the Path type

Minimal complete definition

path

Instances

Circle

data Circle Source

Define a `<circle>`.

Constructors

Circle 

Fields

_circleDrawAttributes :: DrawAttributes

Drawing attributes of the circle.

_circleCenter :: Point

Define the center of the circle, describe the cx and cy attributes.

_circleRadius :: Number

Radius of the circle, equivalent to the r attribute.

class HasCircle c where Source

Lenses for the Circle type.

Minimal complete definition

circle

Instances

Ellipse

data Ellipse Source

Define an `<ellipse>`

Constructors

Ellipse 

Fields

_ellipseDrawAttributes :: DrawAttributes

Drawing attributes of the ellipse.

_ellipseCenter :: Point

Center of the ellipse, map to the cx and cy attributes.

_ellipseXRadius :: Number

Radius along the X axis, map the rx attribute.

_ellipseYRadius :: Number

Radius along the Y axis, map the ry attribute.

Image

data Image Source

Define an `<image>` tag.

Constructors

Image 

Fields

_imageDrawAttributes :: DrawAttributes

Drawing attributes of the image

_imageCornerUpperLeft :: Point

Position of the image referenced by its upper left corner.

_imageWidth :: Number

Image width

_imageHeight :: Number

Image Height

_imageHref :: String

Image href, pointing to the real image.

Use

data Use Source

Define an `<use>` for a named content. Every named content can be reused in the document using this element.

Constructors

Use 

Fields

_useBase :: Point

Position where to draw the "used" element. Correspond to the x and y attributes.

_useName :: String

Referenced name, correspond to `xlink:href` attribute.

_useWidth :: Maybe Number

Define the width of the region where to place the element. Map to the width attribute.

_useHeight :: Maybe Number

Define the height of the region where to place the element. Map to the height attribute.

_useDrawAttributes :: DrawAttributes

Use draw attributes.

class HasUse c where Source

Lenses for the Use type.

Minimal complete definition

use

Instances

Grouping primitives

Group

data Group a Source

Define a SVG group, corresponding `<g>` tag.

Constructors

Group 

Fields

_groupDrawAttributes :: !DrawAttributes

Group drawing attributes, propagated to all of its children.

_groupChildren :: ![a]

Content of the group, corresponding to all the tags inside the `<g>` tag.

_groupViewBox :: !(Maybe (Int, Int, Int, Int))

Mapped to the attribute viewBox

Instances

class HasGroup c a | c -> a where Source

Lenses associated to the Group type.

Minimal complete definition

group

Instances

HasGroup (Group a) a 

Symbol

newtype Symbol a Source

Define the `<symbol>` tag, equivalent to a named group.

Constructors

Symbol 

Fields

_groupOfSymbol :: Group a
 

Instances

groupOfSymbol :: forall a a. Iso (Symbol a) (Symbol a) (Group a) (Group a) Source

Lenses associated with the Symbol type.

Text related types

Text

data Text Source

Define the global `<tag>` SVG tag.

Constructors

Text 

Fields

_textAdjust :: !TextAdjust

Define the lengthAdjust attribute.

_textRoot :: !TextSpan

Root of the text content.

class HasText c where Source

Lenses for the Text type.

Minimal complete definition

text

Instances

data TextAnchor Source

Tell where to anchor the text, where the position given is realative to the text.

Constructors

TextAnchorStart

The text with left aligned, or start at the postion If the point is the * then the text will be printed this way:

 *THE_TEXT_TO_PRINT

Equivalent to the start value.

TextAnchorMiddle

The text is middle aligned, so the text will be at the left and right of the position:

  THE_TEXT*TO_PRINT

Equivalent to the middle value.

TextAnchorEnd

The text is right aligned.

  THE_TEXT_TO_PRINT*

Equivalent to the end value.

textAt :: Point -> Text -> Text Source

Little helper to create a SVG text at a given baseline position.

Text path

data TextPath Source

Describe the `<textpath>` SVG tag.

Constructors

TextPath 

Fields

_textPathStartOffset :: !Number

Define the beginning offset on the path, the startOffset attribute.

_textPathName :: !String

Define the `xlink:href` attribute.

_textPathMethod :: !TextPathMethod

Correspond to the method attribute.

_textPathSpacing :: !TextPathSpacing

Correspond to the spacing attribute.

_textPathData :: ![PathCommand]

Real content of the path.

data TextPathSpacing Source

Describe the content of the spacing text path attribute.

Constructors

TextPathSpacingExact

Map to the exact value.

TextPathSpacingAuto

Map to the auto value.

data TextPathMethod Source

Describe the content of the method attribute on text path.

Constructors

TextPathAlign

Map to the align value.

TextPathStretch

Map to the stretch value.

Text span.

data TextSpanContent Source

Define the content of a `<tspan>` tag.

Constructors

SpanText !Text

Raw text

SpanTextRef !String

Equivalent to a `<tref>`

SpanSub !TextSpan

Define a `<tspan>`

data TextSpan Source

Define a `<tspan>` tag.

Constructors

TextSpan 

Fields

_spanInfo :: !TextInfo

Placing information for the text.

_spanDrawAttributes :: !DrawAttributes

Drawing attributes for the text span.

_spanContent :: ![TextSpanContent]

Content of the span.

class HasTextSpan c where Source

Lenses for the TextSpan type.

Minimal complete definition

textSpan

data TextInfo Source

Define position information associated to `<text>` or `<tspan>` svg tag.

Constructors

TextInfo 

Fields

_textInfoX :: ![Number]

x attribute.

_textInfoY :: ![Number]

y attribute.

_textInfoDX :: ![Number]

dx attribute.

_textInfoDY :: ![Number]

dy attribute.

_textInfoRotate :: ![Float]

rotate attribute.

_textInfoLength :: !(Maybe Number)

textLength attribute.

data TextAdjust Source

Define the possible values of the lengthAdjust attribute.

Constructors

TextAdjustSpacing

Value spacing

TextAdjustSpacingAndGlyphs

Value spacingAndGlyphs

Marker definition

data Marker Source

Define the `<marker>` tag.

Constructors

Marker 

Fields

_markerDrawAttributes :: DrawAttributes

Draw attributes of the marker.

_markerRefPoint :: (Number, Number)

Define the reference point of the marker. correspond to the refX and refY attributes.

_markerWidth :: Maybe Number

Define the width of the marker. Correspond to the markerWidth attribute.

_markerHeight :: Maybe Number

Define the height of the marker. Correspond to the markerHeight attribute.

_markerOrient :: Maybe MarkerOrientation

Correspond to the orient attribute.

_markerUnits :: Maybe MarkerUnit

Map the markerUnits attribute.

_markerViewBox :: !(Maybe (Int, Int, Int, Int))

Optional viewbox

_markerElements :: [Tree]

Elements defining the marker.

data MarkerOrientation Source

Define the orientation, associated to the orient attribute on the Marker

Constructors

OrientationAuto

Auto value

OrientationAngle Coord

Specific angle.

data MarkerUnit Source

Define the content of the markerUnits attribute on the Marker.

Constructors

MarkerUnitStrokeWidth

Value strokeWidth

MarkerUnitUserSpaceOnUse

Value userSpaceOnUse

Gradient definition

data GradientStop Source

Define a color stop for the gradients. Represent the `<stop>` SVG tag.

Constructors

GradientStop 

Fields

_gradientOffset :: Float

Gradient offset between 0 and 1, correspond to the offset attribute.

_gradientColor :: PixelRGBA8

Color of the gradient stop. Correspond to the `stop-color` attribute.

class HasGradientStop c where Source

Lenses for the GradientStop type.

Minimal complete definition

gradientStop

Linear Gradient

data LinearGradient Source

Define a `<linearGradient>` tag.

Constructors

LinearGradient 

Fields

_linearGradientUnits :: CoordinateUnits

Define coordinate system of the gradient, associated to the gradientUnits attribute.

_linearGradientStart :: Point

Point defining the beginning of the line gradient. Associated to the x1 and y1 attribute.

_linearGradientStop :: Point

Point defining the end of the line gradient. Associated to the x2 and y2 attribute.

_linearGradientSpread :: Spread

Define how to handle the values outside the gradient start and stop. Associated to the spreadMethod attribute.

_linearGradientTransform :: [Transformation]

Define the transformation to apply to the gradient points. Associated to the gradientTransform attribute.

_linearGradientStops :: [GradientStop]

List of color stops of the linear gradient.

Radial Gradient

data RadialGradient Source

Define a `<radialGradient>` tag.

Constructors

RadialGradient 

Fields

_radialGradientUnits :: CoordinateUnits

Define coordinate system of the gradient, associated to the gradientUnits attribute.

_radialGradientCenter :: Point

Center of the radial gradient. Associated to the cx and cy attributes.

_radialGradientRadius :: Number

Radius of the radial gradient. Associated to the r attribute.

_radialGradientFocusX :: Maybe Number

X coordinate of the focus point of the radial gradient. Associated to the fx attribute.

_radialGradientFocusY :: Maybe Number

Y coordinate of the focus point of the radial gradient. Associated to the fy attribute.

_radialGradientSpread :: Spread

Define how to handle the values outside the gradient start and stop. Associated to the spreadMethod attribute.

_radialGradientTransform :: [Transformation]

Define the transformation to apply to the gradient points. Associated to the gradientTransform attribute.

_radialGradientStops :: [GradientStop]

List of color stops of the radial gradient.

Pattern definition

data Pattern Source

Define a `<pattern>` tag.

Constructors

Pattern 

Fields

_patternDrawAttributes :: DrawAttributes

Pattern drawing attributes.

_patternViewBox :: Maybe (Int, Int, Int, Int)

Possible viewBox.

_patternWidth :: Number

Width of the pattern tile, mapped to the width attribute

_patternHeight :: Number

Height of the pattern tile, mapped to the height attribute

_patternPos :: Point

Pattern tile base, mapped to the x and y attributes.

_patternElements :: [Tree]

Elements used in the pattern.

_patternUnit :: CoordinateUnits

Define the cordinate system to use for the pattern. Mapped to the patternUnits attribute.

Mask definition

data Mask Source

Define a SVG `<mask>` tag.

Constructors

Mask 

Fields

_maskDrawAttributes :: DrawAttributes

Drawing attributes of the Mask

_maskContentUnits :: CoordinateUnits

Correspond to the maskContentUnits attributes.

_maskUnits :: CoordinateUnits

Mapping to the maskUnits attribute.

_maskPosition :: Point

Map to the x and y attributes.

_maskWidth :: Number

Map to the width attribute

_maskHeight :: Number

Map to the height attribute.

_maskContent :: [Tree]

Children of the `<mask>` tag.

Clip path definition

data ClipPath Source

Define a `<clipPath>` tag.

Constructors

ClipPath 

Fields

_clipPathDrawAttributes :: DrawAttributes
 
_clipPathUnits :: CoordinateUnits

Maps to the clipPathUnits attribute

_clipPathContent :: [Tree]

Maps to the content of the tree

MISC functions

isPathArc :: PathCommand -> Bool Source

Tell if the path command is an ElipticalArc.

isPathWithArc :: Foldable f => f PathCommand -> Bool Source

Tell if a full path contain an ElipticalArc.

nameOfTree :: Tree -> Text Source

For every element of a svg tree, associate it's SVG tag name.

zipTree :: ([[Tree]] -> Tree) -> Tree -> Tree Source

Map a tree while propagating context information. The function passed in parameter receive a list representing the the path used to go arrive to the current node.

mapTree :: (Tree -> Tree) -> Tree -> Tree Source

Helper function mapping every tree element.

foldTree :: (a -> Tree -> a) -> a -> Tree -> a Source

Fold all nodes of a SVG tree.

toUserUnit :: Dpi -> Number -> Number Source

This function replace all device dependant units to user units given it's DPI configuration. Preserve percentage and "em" notation.

mapNumber :: (Float -> Float) -> Number -> Number Source

Helper function to modify inner value of a number