| 1 |
module Text.Blaze.Svg.Internal where |
| 2 |
|
| 3 |
import Control.Monad (join) |
| 4 |
import Control.Monad.State |
| 5 |
import Data.Monoid (mappend, mempty) |
| 6 |
|
| 7 |
import Text.Blaze |
| 8 |
|
| 9 |
-- | Type to represent an SVG document fragment. |
| 10 |
type Svg = Markup |
| 11 |
|
| 12 |
toSvg :: ToMarkup a => a -> Svg |
| 13 |
toSvg = toMarkup |
| 14 |
|
| 15 |
-- | Type to accumulate an SVG path. |
| 16 |
type Path = State AttributeValue () |
| 17 |
|
| 18 |
-- | Construct SVG path values using path instruction combinators. |
| 19 |
-- See simple example below of how you can use @mkPath@ to |
| 20 |
-- specify a path using the path instruction combinators |
| 21 |
-- that are included as part of the same module. |
| 22 |
-- |
| 23 |
-- More information available at: <http://www.w3.org/TR/SVG/paths.html> |
| 24 |
-- |
| 25 |
-- > import Text.Blaze.Svg11 ((!), mkPath, l, m) |
| 26 |
-- > import qualified Text.Blaze.Svg11 as S |
| 27 |
-- > import qualified Text.Blaze.Svg11.Attributes as A |
| 28 |
-- > |
| 29 |
-- > svgDoc :: S.Svg |
| 30 |
-- > svgDoc = S.docTypeSvg ! A.version "1.1" ! A.width "150" ! A.height "100" $ do |
| 31 |
-- > S.path ! A.d makeSimplePath |
| 32 |
-- > |
| 33 |
-- > makeSimplePath :: S.AttributeValue |
| 34 |
-- > makeSimplePath = mkPath do |
| 35 |
-- > l 2 3 |
| 36 |
-- > m 4 5 |
| 37 |
mkPath :: Path -> AttributeValue |
| 38 |
mkPath path = snd $ runState path mempty |
| 39 |
|
| 40 |
appendToPath :: [String] -> Path |
| 41 |
appendToPath = modify . flip mappend . toValue . join |
| 42 |
|
| 43 |
-- | Moveto |
| 44 |
m :: Show a => a -> a -> Path |
| 45 |
m x y = appendToPath |
| 46 |
[ "M " |
| 47 |
, show x, ",", show y |
| 48 |
, " " |
| 49 |
] |
| 50 |
|
| 51 |
-- | Moveto (relative) |
| 52 |
mr :: Show a => a -> a -> Path |
| 53 |
mr dx dy = appendToPath |
| 54 |
[ "m " |
| 55 |
, show dx, ",", show dy |
| 56 |
, " " |
| 57 |
] |
| 58 |
|
| 59 |
-- | ClosePath |
| 60 |
z :: Path |
| 61 |
z = modify (`mappend` toValue "Z") |
| 62 |
|
| 63 |
-- | Lineto |
| 64 |
l :: Show a => a -> a -> Path |
| 65 |
l x y = appendToPath |
| 66 |
[ "L " |
| 67 |
, show x, ",", show y |
| 68 |
, " " |
| 69 |
] |
| 70 |
|
| 71 |
-- | Lineto (relative) |
| 72 |
lr :: Show a => a -> a -> Path |
| 73 |
lr dx dy = appendToPath |
| 74 |
[ "l " |
| 75 |
, show dx, ",", show dy |
| 76 |
, " " |
| 77 |
] |
| 78 |
|
| 79 |
-- | Horizontal lineto |
| 80 |
h :: Show a => a -> Path |
| 81 |
h x = appendToPath |
| 82 |
[ "H " |
| 83 |
, show x |
| 84 |
, " " |
| 85 |
] |
| 86 |
|
| 87 |
-- | Horizontal lineto (relative) |
| 88 |
hr :: Show a => a -> Path |
| 89 |
hr dx = appendToPath |
| 90 |
[ "h " |
| 91 |
, show dx |
| 92 |
, " " |
| 93 |
] |
| 94 |
|
| 95 |
|
| 96 |
-- | Vertical lineto |
| 97 |
v :: Show a => a -> Path |
| 98 |
v y = appendToPath |
| 99 |
[ "V " |
| 100 |
, show y |
| 101 |
, " " |
| 102 |
] |
| 103 |
|
| 104 |
-- | Vertical lineto (relative) |
| 105 |
vr :: Show a => a -> Path |
| 106 |
vr dy = appendToPath |
| 107 |
[ "v " |
| 108 |
, show dy |
| 109 |
, " " |
| 110 |
] |
| 111 |
|
| 112 |
-- | Cubic Bezier curve |
| 113 |
c :: Show a => a -> a -> a -> a -> a -> a -> Path |
| 114 |
c c1x c1y c2x c2y x y = appendToPath |
| 115 |
[ "C " |
| 116 |
, show c1x, ",", show c1y |
| 117 |
, " " |
| 118 |
, show c2x, ",", show c2y |
| 119 |
, " " |
| 120 |
, show x, " ", show y |
| 121 |
] |
| 122 |
|
| 123 |
-- | Cubic Bezier curve (relative) |
| 124 |
cr :: Show a => a -> a -> a -> a -> a -> a -> Path |
| 125 |
cr dc1x dc1y dc2x dc2y dx dy = appendToPath |
| 126 |
[ "c " |
| 127 |
, show dc1x, ",", show dc1y |
| 128 |
, " " |
| 129 |
, show dc2x, ",", show dc2y |
| 130 |
, " " |
| 131 |
, show dx, " ", show dy |
| 132 |
] |
| 133 |
|
| 134 |
-- | Smooth Cubic Bezier curve |
| 135 |
s :: Show a => a -> a -> a -> a -> Path |
| 136 |
s c2x c2y x y = appendToPath |
| 137 |
[ "S " |
| 138 |
, show c2x, ",", show c2y |
| 139 |
, " " |
| 140 |
, show x, ",", show y |
| 141 |
, " " |
| 142 |
] |
| 143 |
|
| 144 |
-- | Smooth Cubic Bezier curve (relative) |
| 145 |
sr :: Show a => a -> a -> a -> a -> Path |
| 146 |
sr dc2x dc2y dx dy = appendToPath |
| 147 |
[ "s " |
| 148 |
, show dc2x, ",", show dc2y |
| 149 |
, " " |
| 150 |
, show dx, ",", show dy |
| 151 |
, " " |
| 152 |
] |
| 153 |
|
| 154 |
-- | Quadratic Bezier curve |
| 155 |
q :: Show a => a -> a -> a -> a -> Path |
| 156 |
q cx cy x y = appendToPath |
| 157 |
[ "Q " |
| 158 |
, show cx, ",", show cy |
| 159 |
, " " |
| 160 |
, show x, ",", show y |
| 161 |
, " " |
| 162 |
] |
| 163 |
|
| 164 |
-- | Quadratic Bezier curve (relative) |
| 165 |
qr :: Show a => a -> a -> a -> a -> Path |
| 166 |
qr dcx dcy dx dy = appendToPath |
| 167 |
[ "q " |
| 168 |
, show dcx, ",", show dcy |
| 169 |
, " " |
| 170 |
, show dx, ",", show dy |
| 171 |
, " " |
| 172 |
] |
| 173 |
|
| 174 |
-- | Smooth Quadratic Bezier curve |
| 175 |
t :: Show a => a -> a -> Path |
| 176 |
t x y = appendToPath |
| 177 |
[ "T " |
| 178 |
, " " |
| 179 |
, show x, ",", show y |
| 180 |
, " " |
| 181 |
] |
| 182 |
|
| 183 |
-- | Smooth Quadratic Bezier curve (relative) |
| 184 |
tr :: Show a => a -> a -> Path |
| 185 |
tr x y = appendToPath |
| 186 |
[ "t " |
| 187 |
, " " |
| 188 |
, show x, ",", show y |
| 189 |
, " " |
| 190 |
] |
| 191 |
|
| 192 |
-- | Elliptical Arc (absolute). |
| 193 |
-- |
| 194 |
-- Note that this function is an alias for the function |
| 195 |
-- 'Text.Blaze.Svg.Internal.a', defined in |
| 196 |
-- "Text.Blaze.Svg.Internal". 'aa' is exported from "Text.Blaze.Svg" |
| 197 |
-- instead of 'a' due to naming conflicts with 'Text.Blaze.SVG11.a' |
| 198 |
-- from "Text.Blaze.SVG11". |
| 199 |
aa |
| 200 |
:: Show a |
| 201 |
=> a -- ^ Radius in the x-direction |
| 202 |
-> a -- ^ Radius in the y-direction |
| 203 |
-> a -- ^ The rotation of the arc's x-axis compared to the normal x-axis |
| 204 |
-> Bool -- ^ Draw the smaller or bigger arc satisfying the start point |
| 205 |
-> Bool -- ^ To mirror or not |
| 206 |
-> a -- ^ The x-coordinate of the end point |
| 207 |
-> a -- ^ The y-coordinate of the end point |
| 208 |
-> Path |
| 209 |
aa = a |
| 210 |
|
| 211 |
-- | Elliptical Arc (absolute). This is the internal definition for absolute |
| 212 |
-- arcs. It is not exported but instead exported as 'aa' due to naming |
| 213 |
-- conflicts with 'Text.Blaze.SVG11.a'. |
| 214 |
a |
| 215 |
:: Show a |
| 216 |
=> a -- ^ Radius in the x-direction |
| 217 |
-> a -- ^ Radius in the y-direction |
| 218 |
-> a -- ^ The rotation of the arc's x-axis compared to the normal x-axis |
| 219 |
-> Bool -- ^ True to draw the larger of the two arcs satisfying constraints. |
| 220 |
-> Bool -- ^ To mirror or not |
| 221 |
-> a -- ^ The x-coordinate of the end point |
| 222 |
-> a -- ^ The y-coordinate of the end point |
| 223 |
-> Path |
| 224 |
a rx ry xAxisRotation largeArcFlag sweepFlag x y = appendToPath |
| 225 |
[ "A " |
| 226 |
, show rx, ",", show ry, " " |
| 227 |
, show xAxisRotation, " " |
| 228 |
, if largeArcFlag then "1" else "0", ",", if sweepFlag then "1" else "0", " " |
| 229 |
, show x, ",", show y, " " |
| 230 |
] |
| 231 |
|
| 232 |
-- | Elliptical Arc (relative) |
| 233 |
ar |
| 234 |
:: Show a |
| 235 |
=> a -- ^ Radius in the x-direction |
| 236 |
-> a -- ^ Radius in the y-direction |
| 237 |
-> a -- ^ The rotation of the arc's x-axis compared to the normal x-axis |
| 238 |
-> Bool -- ^ True to draw the larger of the two arcs satisfying constraints. |
| 239 |
-> Bool -- ^ To mirror or not |
| 240 |
-> a -- ^ The x-coordinate of the end point |
| 241 |
-> a -- ^ The y-coordinate of the end point |
| 242 |
-> Path |
| 243 |
ar rx ry xAxisRotation largeArcFlag sweepFlag x y = appendToPath |
| 244 |
[ "a " |
| 245 |
, show rx, ",", show ry, " " |
| 246 |
, show xAxisRotation, " " |
| 247 |
, if largeArcFlag then "1" else "0", ",", if sweepFlag then "1" else "0", " " |
| 248 |
, show x, ",", show y, " " |
| 249 |
] |
| 250 |
|
| 251 |
-- | Specifies a translation by @x@ and @y@ |
| 252 |
translate :: Show a => a -> a -> AttributeValue |
| 253 |
translate x y = toValue . join $ |
| 254 |
[ "translate(" |
| 255 |
, show x, " ", show y |
| 256 |
, ")" |
| 257 |
] |
| 258 |
|
| 259 |
-- | Specifies a scale operation by @x@ and @y@ |
| 260 |
scale :: Show a => a -> a -> AttributeValue |
| 261 |
scale x y = toValue . join $ |
| 262 |
[ "scale(" |
| 263 |
, show x, " ", show y |
| 264 |
, ")" |
| 265 |
] |
| 266 |
|
| 267 |
-- | Specifies a rotation by @rotate-angle@ degrees |
| 268 |
rotate :: Show a => a -> AttributeValue |
| 269 |
rotate rotateAngle = toValue . join $ |
| 270 |
[ "rotate(" |
| 271 |
, show rotateAngle |
| 272 |
, ")" |
| 273 |
] |
| 274 |
|
| 275 |
-- | Specifies a rotation by @rotate-angle@ degrees about the given time @rx,ry@ |
| 276 |
rotateAround :: Show a => a -> a -> a -> AttributeValue |
| 277 |
rotateAround rotateAngle rx ry = toValue . join $ |
| 278 |
[ "rotate(" |
| 279 |
, show rotateAngle, "," |
| 280 |
, show rx, ",", show ry |
| 281 |
, ")" |
| 282 |
] |
| 283 |
|
| 284 |
-- | Skew tansformation along x-axis |
| 285 |
skewX :: Show a => a -> AttributeValue |
| 286 |
skewX skewAngle = toValue . join $ |
| 287 |
[ "skewX(" |
| 288 |
, show skewAngle |
| 289 |
, ")" |
| 290 |
] |
| 291 |
|
| 292 |
-- | Skew tansformation along y-axis |
| 293 |
skewY :: Show a => a -> AttributeValue |
| 294 |
skewY skewAngle = toValue . join $ |
| 295 |
[ "skewY(" |
| 296 |
, show skewAngle |
| 297 |
, ")" |
| 298 |
] |
| 299 |
|
| 300 |
-- | Specifies a transform in the form of a transformation matrix |
| 301 |
matrix :: Show a => a -> a -> a -> a -> a -> a -> AttributeValue |
| 302 |
matrix a_ b c_ d e f = toValue . join $ |
| 303 |
[ "matrix(" |
| 304 |
, show a_, "," |
| 305 |
, show b, "," |
| 306 |
, show c_, "," |
| 307 |
, show d, "," |
| 308 |
, show e, "," |
| 309 |
, show f |
| 310 |
, ")" |
| 311 |
] |