Skip to content
This repository has been archived by the owner on Sep 9, 2019. It is now read-only.

Commit

Permalink
Exposing a Path type to represent the monad that records SVG paths
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakjois committed Mar 11, 2012
1 parent 0ced7e5 commit e6f27f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions Text/Blaze/Svg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Text.Blaze.Svg
(
Svg
, Path
, mkPath
, m, mr
, z
Expand Down
39 changes: 20 additions & 19 deletions Text/Blaze/Svg/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Text.Blaze
import Text.Blaze.Internal

type Svg = HtmlM ()
type Path = State AttributeValue ()

-- | Construct SVG path values using path instruction combinators.
-- See simple example below of how you can use @mkPath@ to
Expand All @@ -25,58 +26,58 @@ type Svg = HtmlM ()
-- > makeSimplePath = mkPath do
-- > l 2 3
-- > m 4 5
mkPath :: State AttributeValue () -> AttributeValue
mkPath :: Path -> AttributeValue
mkPath path = snd $ runState path mempty

appendToPath :: [String] -> State AttributeValue ()
appendToPath :: [String] -> Path
appendToPath = modify . flip mappend . toValue . join

-- Moveto
m :: Show a => a -> a -> State AttributeValue ()
m :: Show a => a -> a -> Path
m x y = appendToPath
[ "M "
, show x, ",", show y
, " "
]

-- Moveto (relative)
mr :: Show a => a -> a -> State AttributeValue ()
mr :: Show a => a -> a -> Path
mr dx dy = appendToPath
[ "m "
, show dx, ",", show dy
, " "
]

-- ClosePath
z :: State AttributeValue ()
z :: Path
z = modify (`mappend` toValue "Z")

-- Lineto
l :: Show a => a -> a -> State AttributeValue ()
l :: Show a => a -> a -> Path
l x y = appendToPath
[ "L "
, show x, ",", show y
, " "
]

-- Lineto (relative)
lr :: Show a => a -> a -> State AttributeValue ()
lr :: Show a => a -> a -> Path
lr dx dy = appendToPath
[ "l "
, show dx, ",", show dy
, " "
]

-- Horizontal lineto
h :: Show a => a -> State AttributeValue ()
h :: Show a => a -> Path
h x = appendToPath
[ "H "
, show x
, " "
]

-- Horizontal lineto (relative)
hr :: Show a => a -> State AttributeValue ()
hr :: Show a => a -> Path
hr dx = appendToPath
[ "h "
, show dx
Expand All @@ -85,23 +86,23 @@ hr dx = appendToPath


-- Vertical lineto
v :: Show a => a -> State AttributeValue ()
v :: Show a => a -> Path
v y = appendToPath
[ "V "
, show y
, " "
]

-- Vertical lineto (relative)
vr :: Show a => a -> State AttributeValue ()
vr :: Show a => a -> Path
vr dy = appendToPath
[ "v "
, show dy
, " "
]

-- Cubic Bezier curve
c :: Show a => a -> a -> a -> a -> a -> a -> State AttributeValue ()
c :: Show a => a -> a -> a -> a -> a -> a -> Path
c c1x c1y c2x c2y x y = appendToPath
[ "C "
, show c1x, ",", show c1y
Expand All @@ -112,7 +113,7 @@ c c1x c1y c2x c2y x y = appendToPath
]

-- Cubic Bezier curve (relative)
cr :: Show a => a -> a -> a -> a -> a -> a -> State AttributeValue ()
cr :: Show a => a -> a -> a -> a -> a -> a -> Path
cr dc1x dc1y dc2x dc2y dx dy = appendToPath
[ "c "
, show dc1x, ",", show dc1y
Expand All @@ -123,7 +124,7 @@ cr dc1x dc1y dc2x dc2y dx dy = appendToPath
]

-- Smooth Cubic Bezier curve
s :: Show a => a -> a -> a -> a -> State AttributeValue ()
s :: Show a => a -> a -> a -> a -> Path
s c2x c2y x y = appendToPath
[ "S "
, show c2x, ",", show c2y
Expand All @@ -133,7 +134,7 @@ s c2x c2y x y = appendToPath
]

-- Smooth Cubic Bezier curve (relative)
sr :: Show a => a -> a -> a -> a -> State AttributeValue ()
sr :: Show a => a -> a -> a -> a -> Path
sr dc2x dc2y dx dy = appendToPath
[ "s "
, show dc2x, ",", show dc2y
Expand All @@ -143,7 +144,7 @@ sr dc2x dc2y dx dy = appendToPath
]

-- Quadratic Bezier curve
q :: Show a => a -> a -> a -> a -> State AttributeValue ()
q :: Show a => a -> a -> a -> a -> Path
q cx cy x y = appendToPath
[ "Q "
, show cx, ",", show cy
Expand All @@ -153,7 +154,7 @@ q cx cy x y = appendToPath
]

-- Quadratic Bezier curve (relative)
qr :: Show a => a -> a -> a -> a -> State AttributeValue ()
qr :: Show a => a -> a -> a -> a -> Path
qr dcx dcy dx dy = appendToPath
[ "q "
, show dcx, ",", show dcy
Expand All @@ -163,7 +164,7 @@ qr dcx dcy dx dy = appendToPath
]

-- Smooth Quadratic Bezier curve
t :: Show a => a -> a -> State AttributeValue ()
t :: Show a => a -> a -> Path
t x y = appendToPath
[ "T "
, " "
Expand All @@ -172,7 +173,7 @@ t x y = appendToPath
]

-- Smooth Quadratic Bezier curve (relative)
tr :: Show a => a -> a -> State AttributeValue ()
tr :: Show a => a -> a -> Path
tr x y = appendToPath
[ "t "
, " "
Expand Down

0 comments on commit e6f27f1

Please sign in to comment.