module Reanimate.Svg.Constructors
(
mkCircle
, mkEllipse
, mkRect
, mkLine
, mkPath
, mkPathString
, mkPathText
, mkLinePath
, mkLinePathClosed
, mkClipPath
, mkText
, mkGroup
, mkDefinitions
, mkUse
, withId
, withStrokeColor
, withStrokeColorPixel
, withStrokeDashArray
, withStrokeLineJoin
, withFillColor
, withFillColorPixel
, withFillOpacity
, withGroupOpacity
, withStrokeWidth
, withClipPathRef
, center
, centerX
, centerY
, centerUsing
, translate
, rotate
, rotateAroundCenter
, rotateAround
, scale
, scaleToSize
, scaleToWidth
, scaleToHeight
, scaleXY
, flipXAxis
, flipYAxis
, aroundCenter
, aroundCenterX
, aroundCenterY
, withTransformations
, withViewBox
, mkColor
, mkBackground
, mkBackgroundPixel
, gridLayout
) where
import Codec.Picture (PixelRGBA8 (..))
import Control.Lens ((&), (.~), (?~))
import Data.Attoparsec.Text (parseOnly)
import qualified Data.Map as Map
import qualified Data.Text as T
import Graphics.SvgTree
import Graphics.SvgTree.NamedColors (svgNamedColors)
import Graphics.SvgTree.PathParser (pathParser)
import Linear.V2 (V2 (V2))
import Reanimate.Constants (screenHeight, screenWidth)
import Reanimate.Svg.BoundingBox (boundingBox)
withTransformations :: [Transformation] -> Tree -> Tree
withTransformations :: [Transformation] -> Tree -> Tree
withTransformations [Transformation]
transformations Tree
t =
[Tree] -> Tree
mkGroup [Tree
t] Tree -> (Tree -> Tree) -> Tree
forall a b. a -> (a -> b) -> b
& (Maybe [Transformation] -> Identity (Maybe [Transformation]))
-> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Maybe [Transformation])
transform ((Maybe [Transformation] -> Identity (Maybe [Transformation]))
-> Tree -> Identity Tree)
-> [Transformation] -> Tree -> Tree
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ [Transformation]
transformations
translate :: Double -> Double -> Tree -> Tree
translate :: Double -> Double -> Tree -> Tree
translate Double
x Double
y = [Transformation] -> Tree -> Tree
withTransformations [Double -> Double -> Transformation
Translate Double
x Double
y]
rotate :: Double -> Tree -> Tree
rotate :: Double -> Tree -> Tree
rotate Double
a = [Transformation] -> Tree -> Tree
withTransformations [Double -> Maybe (Double, Double) -> Transformation
Rotate Double
a Maybe (Double, Double)
forall a. Maybe a
Nothing]
rotateAround :: Double -> RPoint -> Tree -> Tree
rotateAround :: Double -> RPoint -> Tree -> Tree
rotateAround Double
a (V2 Double
x Double
y) = [Transformation] -> Tree -> Tree
withTransformations [Double -> Maybe (Double, Double) -> Transformation
Rotate Double
a ((Double, Double) -> Maybe (Double, Double)
forall a. a -> Maybe a
Just (Double
x,Double
y))]
rotateAroundCenter :: Double -> Tree -> Tree
rotateAroundCenter :: Double -> Tree -> Tree
rotateAroundCenter Double
a Tree
t =
Double -> RPoint -> Tree -> Tree
rotateAround Double
a (Double -> Double -> RPoint
forall a. a -> a -> V2 a
V2 (Double
xDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
wDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2) (Double
yDouble -> Double -> Double
forall a. Num a => a -> a -> a
+Double
hDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2)) Tree
t
where
(Double
x,Double
y,Double
w,Double
h) = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
aroundCenter :: (Tree -> Tree) -> Tree -> Tree
aroundCenter :: (Tree -> Tree) -> Tree -> Tree
aroundCenter Tree -> Tree
fn Tree
t =
Double -> Double -> Tree -> Tree
translate (-Double
offsetX) (-Double
offsetY) (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Tree -> Tree
fn (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Tree -> Tree
translate Double
offsetX Double
offsetY Tree
t
where
offsetX :: Double
offsetX = -Double
xDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
wDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2
offsetY :: Double
offsetY = -Double
yDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
hDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2
(Double
x,Double
y,Double
w,Double
h) = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
aroundCenterY :: (Tree -> Tree) -> Tree -> Tree
aroundCenterY :: (Tree -> Tree) -> Tree -> Tree
aroundCenterY Tree -> Tree
fn Tree
t =
Double -> Double -> Tree -> Tree
translate Double
0 (-Double
offsetY) (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Tree -> Tree
fn (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Tree -> Tree
translate Double
0 Double
offsetY Tree
t
where
offsetY :: Double
offsetY = -Double
yDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
hDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2
(Double
_x,Double
y,Double
_w,Double
h) = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
aroundCenterX :: (Tree -> Tree) -> Tree -> Tree
aroundCenterX :: (Tree -> Tree) -> Tree -> Tree
aroundCenterX Tree -> Tree
fn Tree
t =
Double -> Double -> Tree -> Tree
translate (-Double
offsetX) Double
0 (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Tree -> Tree
fn (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Tree -> Tree
translate Double
offsetX Double
0 Tree
t
where
offsetX :: Double
offsetX = -Double
xDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
wDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2
(Double
x,Double
_y,Double
w,Double
_h) = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
scale :: Double -> Tree -> Tree
scale :: Double -> Tree -> Tree
scale Double
a = [Transformation] -> Tree -> Tree
withTransformations [Double -> Maybe Double -> Transformation
Scale Double
a Maybe Double
forall a. Maybe a
Nothing]
scaleToSize :: Double -> Double -> Tree -> Tree
scaleToSize :: Double -> Double -> Tree -> Tree
scaleToSize Double
w Double
h Tree
t =
Double -> Double -> Tree -> Tree
scaleXY (Double
wDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
w') (Double
hDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
h') Tree
t
where
(Double
_x, Double
_y, Double
w', Double
h') = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
scaleToWidth :: Double -> Tree -> Tree
scaleToWidth :: Double -> Tree -> Tree
scaleToWidth Double
w Tree
t =
Double -> Tree -> Tree
scale (Double
wDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
w') Tree
t
where
(Double
_x, Double
_y, Double
w', Double
_h') = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
scaleToHeight :: Double -> Tree -> Tree
scaleToHeight :: Double -> Tree -> Tree
scaleToHeight Double
h Tree
t =
Double -> Tree -> Tree
scale (Double
hDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
h') Tree
t
where
(Double
_x, Double
_y, Double
_w', Double
h') = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
scaleXY :: Double -> Double -> Tree -> Tree
scaleXY :: Double -> Double -> Tree -> Tree
scaleXY Double
x Double
y = [Transformation] -> Tree -> Tree
withTransformations [Double -> Maybe Double -> Transformation
Scale Double
x (Double -> Maybe Double
forall a. a -> Maybe a
Just Double
y)]
flipXAxis :: Tree -> Tree
flipXAxis :: Tree -> Tree
flipXAxis = Double -> Double -> Tree -> Tree
scaleXY (-Double
1) Double
1
flipYAxis :: Tree -> Tree
flipYAxis :: Tree -> Tree
flipYAxis = Double -> Double -> Tree -> Tree
scaleXY Double
1 (-Double
1)
center :: Tree -> Tree
center :: Tree -> Tree
center Tree
t = Tree -> Tree -> Tree
centerUsing Tree
t Tree
t
centerX :: Tree -> Tree
centerX :: Tree -> Tree
centerX Tree
t = Double -> Double -> Tree -> Tree
translate (-Double
xDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
wDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2) Double
0 Tree
t
where
(Double
x, Double
_y, Double
w, Double
_h) = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
centerY :: Tree -> Tree
centerY :: Tree -> Tree
centerY Tree
t = Double -> Double -> Tree -> Tree
translate Double
0 (-Double
yDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
hDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2) Tree
t
where
(Double
_x, Double
y, Double
_w, Double
h) = Tree -> (Double, Double, Double, Double)
boundingBox Tree
t
centerUsing :: Tree -> Tree -> Tree
centerUsing :: Tree -> Tree -> Tree
centerUsing Tree
a = Double -> Double -> Tree -> Tree
translate (-Double
xDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
wDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2) (-Double
yDouble -> Double -> Double
forall a. Num a => a -> a -> a
-Double
hDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2)
where
(Double
x, Double
y, Double
w, Double
h) = Tree -> (Double, Double, Double, Double)
boundingBox Tree
a
mkColor :: String -> Texture
mkColor :: String -> Texture
mkColor String
name =
case Text -> Map Text PixelRGBA8 -> Maybe PixelRGBA8
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (String -> Text
T.pack String
name) Map Text PixelRGBA8
svgNamedColors of
Maybe PixelRGBA8
Nothing -> PixelRGBA8 -> Texture
ColorRef (Pixel8 -> Pixel8 -> Pixel8 -> Pixel8 -> PixelRGBA8
PixelRGBA8 Pixel8
240 Pixel8
248 Pixel8
255 Pixel8
255)
Just PixelRGBA8
c -> PixelRGBA8 -> Texture
ColorRef PixelRGBA8
c
withStrokeColor :: String -> Tree -> Tree
withStrokeColor :: String -> Tree -> Tree
withStrokeColor String
color = (Last Texture -> Identity (Last Texture)) -> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last Texture)
strokeColor ((Last Texture -> Identity (Last Texture))
-> Tree -> Identity Tree)
-> Last Texture -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Texture -> Last Texture
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Texture
mkColor String
color)
withStrokeColorPixel :: PixelRGBA8 -> Tree -> Tree
withStrokeColorPixel :: PixelRGBA8 -> Tree -> Tree
withStrokeColorPixel PixelRGBA8
color = (Last Texture -> Identity (Last Texture)) -> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last Texture)
strokeColor ((Last Texture -> Identity (Last Texture))
-> Tree -> Identity Tree)
-> Last Texture -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Texture -> Last Texture
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PixelRGBA8 -> Texture
ColorRef PixelRGBA8
color)
withStrokeDashArray :: [Double] -> Tree -> Tree
withStrokeDashArray :: [Double] -> Tree -> Tree
withStrokeDashArray [Double]
arr = (Last [Number] -> Identity (Last [Number]))
-> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last [Number])
strokeDashArray ((Last [Number] -> Identity (Last [Number]))
-> Tree -> Identity Tree)
-> Last [Number] -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Number] -> Last [Number]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Double -> Number) -> [Double] -> [Number]
forall a b. (a -> b) -> [a] -> [b]
map Double -> Number
Num [Double]
arr)
withStrokeLineJoin :: LineJoin -> Tree -> Tree
withStrokeLineJoin :: LineJoin -> Tree -> Tree
withStrokeLineJoin LineJoin
ljoin = (Last LineJoin -> Identity (Last LineJoin))
-> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last LineJoin)
strokeLineJoin ((Last LineJoin -> Identity (Last LineJoin))
-> Tree -> Identity Tree)
-> Last LineJoin -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ LineJoin -> Last LineJoin
forall (f :: * -> *) a. Applicative f => a -> f a
pure LineJoin
ljoin
withFillColor :: String -> Tree -> Tree
withFillColor :: String -> Tree -> Tree
withFillColor String
color = (Last Texture -> Identity (Last Texture)) -> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last Texture)
fillColor ((Last Texture -> Identity (Last Texture))
-> Tree -> Identity Tree)
-> Last Texture -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Texture -> Last Texture
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Texture
mkColor String
color)
withFillColorPixel :: PixelRGBA8 -> Tree -> Tree
withFillColorPixel :: PixelRGBA8 -> Tree -> Tree
withFillColorPixel PixelRGBA8
color = (Last Texture -> Identity (Last Texture)) -> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last Texture)
fillColor ((Last Texture -> Identity (Last Texture))
-> Tree -> Identity Tree)
-> Last Texture -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Texture -> Last Texture
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PixelRGBA8 -> Texture
ColorRef PixelRGBA8
color)
withFillOpacity :: Double -> Tree -> Tree
withFillOpacity :: Double -> Tree -> Tree
withFillOpacity Double
opacity = (Maybe Float -> Identity (Maybe Float)) -> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Maybe Float)
fillOpacity ((Maybe Float -> Identity (Maybe Float)) -> Tree -> Identity Tree)
-> Float -> Tree -> Tree
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Double -> Float
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
opacity
withGroupOpacity :: Double -> Tree -> Tree
withGroupOpacity :: Double -> Tree -> Tree
withGroupOpacity Double
opacity = (Maybe Float -> Identity (Maybe Float)) -> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Maybe Float)
groupOpacity ((Maybe Float -> Identity (Maybe Float)) -> Tree -> Identity Tree)
-> Float -> Tree -> Tree
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Double -> Float
forall a b. (Real a, Fractional b) => a -> b
realToFrac Double
opacity
withStrokeWidth :: Double -> Tree -> Tree
withStrokeWidth :: Double -> Tree -> Tree
withStrokeWidth Double
width = (Last Number -> Identity (Last Number)) -> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last Number)
strokeWidth ((Last Number -> Identity (Last Number)) -> Tree -> Identity Tree)
-> Last Number -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Number -> Last Number
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Double -> Number
Num Double
width)
withClipPathRef :: ElementRef
-> Tree
-> Tree
withClipPathRef :: ElementRef -> Tree -> Tree
withClipPathRef ElementRef
ref Tree
sub = [Tree] -> Tree
mkGroup [Tree
sub] Tree -> (Tree -> Tree) -> Tree
forall a b. a -> (a -> b) -> b
& (Last ElementRef -> Identity (Last ElementRef))
-> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last ElementRef)
clipPathRef ((Last ElementRef -> Identity (Last ElementRef))
-> Tree -> Identity Tree)
-> Last ElementRef -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ElementRef -> Last ElementRef
forall (f :: * -> *) a. Applicative f => a -> f a
pure ElementRef
ref
withId :: String -> Tree -> Tree
withId :: String -> Tree -> Tree
withId String
idTag = (Maybe String -> Identity (Maybe String)) -> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Maybe String)
attrId ((Maybe String -> Identity (Maybe String))
-> Tree -> Identity Tree)
-> String -> Tree -> Tree
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ String
idTag
mkRect :: Double -> Double -> Tree
mkRect :: Double -> Double -> Tree
mkRect Double
width Double
height = Double -> Double -> Tree -> Tree
translate (-Double
widthDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2) (-Double
heightDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2) (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Rectangle -> Tree
rectangleTree (Rectangle -> Tree) -> Rectangle -> Tree
forall a b. (a -> b) -> a -> b
$ Rectangle
forall a. WithDefaultSvg a => a
defaultSvg
Rectangle -> (Rectangle -> Rectangle) -> Rectangle
forall a b. a -> (a -> b) -> b
& (Point -> Identity Point) -> Rectangle -> Identity Rectangle
Lens' Rectangle Point
rectUpperLeftCorner ((Point -> Identity Point) -> Rectangle -> Identity Rectangle)
-> Point -> Rectangle -> Rectangle
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Double -> Number
Num Double
0, Double -> Number
Num Double
0)
Rectangle -> (Rectangle -> Rectangle) -> Rectangle
forall a b. a -> (a -> b) -> b
& (Maybe Number -> Identity (Maybe Number))
-> Rectangle -> Identity Rectangle
Lens' Rectangle (Maybe Number)
rectWidth ((Maybe Number -> Identity (Maybe Number))
-> Rectangle -> Identity Rectangle)
-> Number -> Rectangle -> Rectangle
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Double -> Number
Num Double
width
Rectangle -> (Rectangle -> Rectangle) -> Rectangle
forall a b. a -> (a -> b) -> b
& (Maybe Number -> Identity (Maybe Number))
-> Rectangle -> Identity Rectangle
Lens' Rectangle (Maybe Number)
rectHeight ((Maybe Number -> Identity (Maybe Number))
-> Rectangle -> Identity Rectangle)
-> Number -> Rectangle -> Rectangle
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Double -> Number
Num Double
height
mkCircle :: Double -> Tree
mkCircle :: Double -> Tree
mkCircle Double
radius = Circle -> Tree
circleTree (Circle -> Tree) -> Circle -> Tree
forall a b. (a -> b) -> a -> b
$ Circle
forall a. WithDefaultSvg a => a
defaultSvg
Circle -> (Circle -> Circle) -> Circle
forall a b. a -> (a -> b) -> b
& (Point -> Identity Point) -> Circle -> Identity Circle
Lens' Circle Point
circleCenter ((Point -> Identity Point) -> Circle -> Identity Circle)
-> Point -> Circle -> Circle
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Double -> Number
Num Double
0, Double -> Number
Num Double
0)
Circle -> (Circle -> Circle) -> Circle
forall a b. a -> (a -> b) -> b
& (Number -> Identity Number) -> Circle -> Identity Circle
Lens' Circle Number
circleRadius ((Number -> Identity Number) -> Circle -> Identity Circle)
-> Number -> Circle -> Circle
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Double -> Number
Num Double
radius
mkEllipse :: Double -> Double -> Tree
mkEllipse :: Double -> Double -> Tree
mkEllipse Double
rx Double
ry = Ellipse -> Tree
ellipseTree (Ellipse -> Tree) -> Ellipse -> Tree
forall a b. (a -> b) -> a -> b
$ Ellipse
forall a. WithDefaultSvg a => a
defaultSvg
Ellipse -> (Ellipse -> Ellipse) -> Ellipse
forall a b. a -> (a -> b) -> b
& (Point -> Identity Point) -> Ellipse -> Identity Ellipse
Lens' Ellipse Point
ellipseCenter ((Point -> Identity Point) -> Ellipse -> Identity Ellipse)
-> Point -> Ellipse -> Ellipse
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Double -> Number
Num Double
0, Double -> Number
Num Double
0)
Ellipse -> (Ellipse -> Ellipse) -> Ellipse
forall a b. a -> (a -> b) -> b
& (Number -> Identity Number) -> Ellipse -> Identity Ellipse
Lens' Ellipse Number
ellipseXRadius ((Number -> Identity Number) -> Ellipse -> Identity Ellipse)
-> Number -> Ellipse -> Ellipse
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Double -> Number
Num Double
rx
Ellipse -> (Ellipse -> Ellipse) -> Ellipse
forall a b. a -> (a -> b) -> b
& (Number -> Identity Number) -> Ellipse -> Identity Ellipse
Lens' Ellipse Number
ellipseYRadius ((Number -> Identity Number) -> Ellipse -> Identity Ellipse)
-> Number -> Ellipse -> Ellipse
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Double -> Number
Num Double
ry
mkLine :: (Double,Double) -> (Double, Double) -> Tree
mkLine :: (Double, Double) -> (Double, Double) -> Tree
mkLine (Double
x1,Double
y1) (Double
x2,Double
y2) = Line -> Tree
lineTree (Line -> Tree) -> Line -> Tree
forall a b. (a -> b) -> a -> b
$ Line
forall a. WithDefaultSvg a => a
defaultSvg
Line -> (Line -> Line) -> Line
forall a b. a -> (a -> b) -> b
& (Point -> Identity Point) -> Line -> Identity Line
Lens' Line Point
linePoint1 ((Point -> Identity Point) -> Line -> Identity Line)
-> Point -> Line -> Line
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Double -> Number
Num Double
x1, Double -> Number
Num Double
y1)
Line -> (Line -> Line) -> Line
forall a b. a -> (a -> b) -> b
& (Point -> Identity Point) -> Line -> Identity Line
Lens' Line Point
linePoint2 ((Point -> Identity Point) -> Line -> Identity Line)
-> Point -> Line -> Line
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Double -> Number
Num Double
x2, Double -> Number
Num Double
y2)
mkGroup :: [Tree] -> Tree
mkGroup :: [Tree] -> Tree
mkGroup [Tree]
forest = Group -> Tree
groupTree (Group -> Tree) -> Group -> Tree
forall a b. (a -> b) -> a -> b
$ Group
forall a. WithDefaultSvg a => a
defaultSvg
Group -> (Group -> Group) -> Group
forall a b. a -> (a -> b) -> b
& ([Tree] -> Identity [Tree]) -> Group -> Identity Group
Lens' Group [Tree]
groupChildren (([Tree] -> Identity [Tree]) -> Group -> Identity Group)
-> [Tree] -> Group -> Group
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Tree]
forest
mkDefinitions :: [Tree] -> Tree
mkDefinitions :: [Tree] -> Tree
mkDefinitions [Tree]
forest = Group -> Tree
definitionTree (Group -> Tree) -> Group -> Tree
forall a b. (a -> b) -> a -> b
$ Group
forall a. WithDefaultSvg a => a
defaultSvg
Group -> (Group -> Group) -> Group
forall a b. a -> (a -> b) -> b
& ([Tree] -> Identity [Tree]) -> Group -> Identity Group
Lens' Group [Tree]
groupChildren (([Tree] -> Identity [Tree]) -> Group -> Identity Group)
-> [Tree] -> Group -> Group
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Tree]
forest
mkUse :: String -> Tree
mkUse :: String -> Tree
mkUse String
name = Use -> Tree
useTree (Use
forall a. WithDefaultSvg a => a
defaultSvg Use -> (Use -> Use) -> Use
forall a b. a -> (a -> b) -> b
& (String -> Identity String) -> Use -> Identity Use
Lens' Use String
useName ((String -> Identity String) -> Use -> Identity Use)
-> String -> Use -> Use
forall s t a b. ASetter s t a b -> b -> s -> t
.~ String
name)
mkClipPath :: String
-> [Tree]
-> Tree
mkClipPath :: String -> [Tree] -> Tree
mkClipPath String
idTag [Tree]
forest = String -> Tree -> Tree
withId String
idTag (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ ClipPath -> Tree
clipPathTree (ClipPath -> Tree) -> ClipPath -> Tree
forall a b. (a -> b) -> a -> b
$ ClipPath
forall a. WithDefaultSvg a => a
defaultSvg
ClipPath -> (ClipPath -> ClipPath) -> ClipPath
forall a b. a -> (a -> b) -> b
& ([Tree] -> Identity [Tree]) -> ClipPath -> Identity ClipPath
Lens' ClipPath [Tree]
clipPathContent (([Tree] -> Identity [Tree]) -> ClipPath -> Identity ClipPath)
-> [Tree] -> ClipPath -> ClipPath
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Tree]
forest
mkPath :: [PathCommand] -> Tree
mkPath :: [PathCommand] -> Tree
mkPath [PathCommand]
cmds = Path -> Tree
pathTree (Path -> Tree) -> Path -> Tree
forall a b. (a -> b) -> a -> b
$ Path
forall a. WithDefaultSvg a => a
defaultSvg Path -> (Path -> Path) -> Path
forall a b. a -> (a -> b) -> b
& ([PathCommand] -> Identity [PathCommand]) -> Path -> Identity Path
Lens' Path [PathCommand]
pathDefinition (([PathCommand] -> Identity [PathCommand])
-> Path -> Identity Path)
-> [PathCommand] -> Path -> Path
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [PathCommand]
cmds
mkPathString :: String -> Tree
mkPathString :: String -> Tree
mkPathString = Text -> Tree
mkPathText (Text -> Tree) -> (String -> Text) -> String -> Tree
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
mkPathText :: T.Text -> Tree
mkPathText :: Text -> Tree
mkPathText Text
str =
case Parser [PathCommand] -> Text -> Either String [PathCommand]
forall a. Parser a -> Text -> Either String a
parseOnly Parser [PathCommand]
pathParser Text
str of
Left String
err -> String -> Tree
forall a. HasCallStack => String -> a
error String
err
Right [PathCommand]
cmds -> [PathCommand] -> Tree
mkPath [PathCommand]
cmds
mkLinePath :: [(Double, Double)] -> Tree
mkLinePath :: [(Double, Double)] -> Tree
mkLinePath [] = [Tree] -> Tree
mkGroup []
mkLinePath ((Double
startX, Double
startY):[(Double, Double)]
rest) =
Path -> Tree
pathTree (Path -> Tree) -> Path -> Tree
forall a b. (a -> b) -> a -> b
$ Path
forall a. WithDefaultSvg a => a
defaultSvg Path -> (Path -> Path) -> Path
forall a b. a -> (a -> b) -> b
& ([PathCommand] -> Identity [PathCommand]) -> Path -> Identity Path
Lens' Path [PathCommand]
pathDefinition (([PathCommand] -> Identity [PathCommand])
-> Path -> Identity Path)
-> [PathCommand] -> Path -> Path
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [PathCommand]
cmds
where
cmds :: [PathCommand]
cmds = [ Origin -> [RPoint] -> PathCommand
MoveTo Origin
OriginAbsolute [Double -> Double -> RPoint
forall a. a -> a -> V2 a
V2 Double
startX Double
startY]
, Origin -> [RPoint] -> PathCommand
LineTo Origin
OriginAbsolute [ Double -> Double -> RPoint
forall a. a -> a -> V2 a
V2 Double
x Double
y | (Double
x, Double
y) <- [(Double, Double)]
rest ] ]
mkLinePathClosed :: [(Double, Double)] -> Tree
mkLinePathClosed :: [(Double, Double)] -> Tree
mkLinePathClosed [] = [Tree] -> Tree
mkGroup []
mkLinePathClosed ((Double
startX, Double
startY):[(Double, Double)]
rest) =
Path -> Tree
pathTree (Path -> Tree) -> Path -> Tree
forall a b. (a -> b) -> a -> b
$ Path
forall a. WithDefaultSvg a => a
defaultSvg Path -> (Path -> Path) -> Path
forall a b. a -> (a -> b) -> b
& ([PathCommand] -> Identity [PathCommand]) -> Path -> Identity Path
Lens' Path [PathCommand]
pathDefinition (([PathCommand] -> Identity [PathCommand])
-> Path -> Identity Path)
-> [PathCommand] -> Path -> Path
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [PathCommand]
cmds
where
cmds :: [PathCommand]
cmds = [ Origin -> [RPoint] -> PathCommand
MoveTo Origin
OriginAbsolute [Double -> Double -> RPoint
forall a. a -> a -> V2 a
V2 Double
startX Double
startY]
, Origin -> [RPoint] -> PathCommand
LineTo Origin
OriginAbsolute [ Double -> Double -> RPoint
forall a. a -> a -> V2 a
V2 Double
x Double
y | (Double
x, Double
y) <- [(Double, Double)]
rest ]
, PathCommand
EndPath ]
mkBackground :: String -> Tree
mkBackground :: String -> Tree
mkBackground String
color = Double -> Tree -> Tree
withFillOpacity Double
1 (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Double -> Tree -> Tree
withStrokeWidth Double
0 (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$
String -> Tree -> Tree
withFillColor String
color (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Tree
mkRect Double
forall a. Fractional a => a
screenWidth Double
forall a. Fractional a => a
screenHeight
mkBackgroundPixel :: PixelRGBA8 -> Tree
mkBackgroundPixel :: PixelRGBA8 -> Tree
mkBackgroundPixel PixelRGBA8
pixel =
Double -> Tree -> Tree
withFillOpacity Double
1 (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Double -> Tree -> Tree
withStrokeWidth Double
0 (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$
PixelRGBA8 -> Tree -> Tree
withFillColorPixel PixelRGBA8
pixel (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$ Double -> Double -> Tree
mkRect Double
forall a. Fractional a => a
screenWidth Double
forall a. Fractional a => a
screenHeight
gridLayout :: [[Tree]] -> Tree
gridLayout :: [[Tree]] -> Tree
gridLayout [[Tree]]
rows = [Tree] -> Tree
mkGroup
[ Double -> Double -> Tree -> Tree
translate (-Double
forall a. Fractional a => a
screenWidthDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2Double -> Double -> Double
forall a. Num a => a -> a -> a
+Double
colSepDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
nCol Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
colSepDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
0.5)
(Double
forall a. Fractional a => a
screenHeightDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2Double -> Double -> Double
forall a. Num a => a -> a -> a
-Double
rowSepDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
nRow Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
rowSepDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
0.5)
Tree
elt
| (Double
nRow, [Tree]
row) <- [Double] -> [[Tree]] -> [(Double, [Tree])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Double
0..] [[Tree]]
rows
, let nCols :: Int
nCols = [Tree] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Tree]
row
colSep :: Double
colSep = Double
forall a. Fractional a => a
screenWidth Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nCols
, (Double
nCol, Tree
elt) <- [Double] -> [Tree] -> [(Double, Tree)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Double
0..] [Tree]
row ]
where
rowSep :: Double
rowSep = Double
forall a. Fractional a => a
screenHeight Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
nRows
nRows :: Int
nRows = [[Tree]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Tree]]
rows
mkText :: T.Text -> Tree
mkText :: Text -> Tree
mkText Text
str =
Tree -> Tree
flipYAxis
(Maybe TextPath -> Text -> Tree
TextTree Maybe TextPath
forall a. Maybe a
Nothing (Text -> Tree) -> Text -> Tree
forall a b. (a -> b) -> a -> b
$ Text
forall a. WithDefaultSvg a => a
defaultSvg
Text -> (Text -> Text) -> Text
forall a b. a -> (a -> b) -> b
& (TextSpan -> Identity TextSpan) -> Text -> Identity Text
Lens' Text TextSpan
textRoot ((TextSpan -> Identity TextSpan) -> Text -> Identity Text)
-> TextSpan -> Text -> Text
forall s t a b. ASetter s t a b -> b -> s -> t
.~ TextSpan
span_
Text -> (Text -> Text) -> Text
forall a b. a -> (a -> b) -> b
& (Last Number -> Identity (Last Number)) -> Text -> Identity Text
forall c. HasDrawAttributes c => Lens' c (Last Number)
fontSize ((Last Number -> Identity (Last Number)) -> Text -> Identity Text)
-> Last Number -> Text -> Text
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Number -> Last Number
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Double -> Number
Num Double
2))
Tree -> (Tree -> Tree) -> Tree
forall a b. a -> (a -> b) -> b
& (Last TextAnchor -> Identity (Last TextAnchor))
-> Tree -> Identity Tree
forall c. HasDrawAttributes c => Lens' c (Last TextAnchor)
textAnchor ((Last TextAnchor -> Identity (Last TextAnchor))
-> Tree -> Identity Tree)
-> Last TextAnchor -> Tree -> Tree
forall s t a b. ASetter s t a b -> b -> s -> t
.~ TextAnchor -> Last TextAnchor
forall (f :: * -> *) a. Applicative f => a -> f a
pure TextAnchor
TextAnchorMiddle
where
span_ :: TextSpan
span_ = TextSpan
forall a. WithDefaultSvg a => a
defaultSvg TextSpan -> (TextSpan -> TextSpan) -> TextSpan
forall a b. a -> (a -> b) -> b
& ([TextSpanContent] -> Identity [TextSpanContent])
-> TextSpan -> Identity TextSpan
Lens' TextSpan [TextSpanContent]
spanContent (([TextSpanContent] -> Identity [TextSpanContent])
-> TextSpan -> Identity TextSpan)
-> [TextSpanContent] -> TextSpan -> TextSpan
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Text -> TextSpanContent
SpanText Text
str]
withViewBox :: (Double, Double, Double, Double) -> Tree -> Tree
withViewBox :: (Double, Double, Double, Double) -> Tree -> Tree
withViewBox (Double, Double, Double, Double)
vbox Tree
child = Double -> Double -> Tree -> Tree
translate (-Double
forall a. Fractional a => a
screenWidthDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2) (-Double
forall a. Fractional a => a
screenHeightDouble -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2) (Tree -> Tree) -> Tree -> Tree
forall a b. (a -> b) -> a -> b
$
Document -> Tree
svgTree Document :: Maybe (Double, Double, Double, Double)
-> Maybe Number
-> Maybe Number
-> [Tree]
-> String
-> String
-> PreserveAspectRatio
-> Document
Document
{ _documentViewBox :: Maybe (Double, Double, Double, Double)
_documentViewBox = (Double, Double, Double, Double)
-> Maybe (Double, Double, Double, Double)
forall a. a -> Maybe a
Just (Double, Double, Double, Double)
vbox
, _documentWidth :: Maybe Number
_documentWidth = Number -> Maybe Number
forall a. a -> Maybe a
Just (Double -> Number
Num Double
forall a. Fractional a => a
screenWidth)
, _documentHeight :: Maybe Number
_documentHeight = Number -> Maybe Number
forall a. a -> Maybe a
Just (Double -> Number
Num Double
forall a. Fractional a => a
screenHeight)
, _documentElements :: [Tree]
_documentElements = [Tree
child]
, _documentDescription :: String
_documentDescription = String
""
, _documentLocation :: String
_documentLocation = String
""
, _documentAspectRatio :: PreserveAspectRatio
_documentAspectRatio = Bool -> Alignment -> Maybe MeetSlice -> PreserveAspectRatio
PreserveAspectRatio Bool
False Alignment
AlignNone Maybe MeetSlice
forall a. Maybe a
Nothing
}