-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Svg.scala
355 lines (311 loc) · 11 KB
/
Svg.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright 2015 Creative Scala
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package doodle
package svg
package algebra
import cats.Applicative
import cats.effect.IO
import doodle.algebra.Picture
import doodle.algebra.generic.Fill
import doodle.algebra.generic.Stroke
import doodle.core._
import doodle.core.font._
import doodle.svg.effect.Size
import scala.collection.mutable
trait SvgModule { self: Base =>
object Svg {
implicit val svgResultApplicative: Applicative[SvgResult] =
new Applicative[SvgResult] {
def ap[A, B](
ff: SvgResult[(A) => B]
)(fa: SvgResult[A]): SvgResult[B] = {
val (t1, s1, fab) = ff
val (t2, s2, a) = fa
(svg.g(t1, t2), s1.union(s2), fab(a))
}
override def map[A, B](fa: SvgResult[A])(f: (A) => B): SvgResult[B] = {
val (t, s, a) = fa
(t, s, f(a))
}
def pure[A](x: A): SvgResult[A] =
(bundle.svgTags.circle(), mutable.Set.empty, x)
}
val svg = bundle.svgTags
val svgAttrs = bundle.svgAttrs
val implicits = bundle.implicits
import implicits.{Tag => _, _}
def render[Alg <: self.Algebra, A](
frame: Frame,
algebra: Alg,
picture: Picture[Alg, A]
): IO[(Output, A)] = {
renderWithoutRootTag(algebra, picture)
.map { case (bb, tags, a) => (svgTag(bb, frame)(tags).render, a) }
}
/** Render to SVG without wrapping with a root <svg> tag. */
def renderWithoutRootTag[Alg <: self.Algebra, A](
algebra: Alg,
picture: Picture[Alg, A]
): IO[(BoundingBox, Tag, A)] = {
for {
drawing <- IO { picture(algebra) }
(bb, rdr) = drawing.runA(List.empty).value
(_, (tags, set, a)) = rdr.run(Transform.verticalReflection).value
tagsWithGradients = svg.g(svg.defs(set.toList: _*), tags)
} yield (bb, tagsWithGradients, a)
}
/** Given a bounding box and a size specification create a <svg> tag that
* has the correct size and viewbox
*/
def svgTag(bb: BoundingBox, frame: Frame): Tag =
frame.size match {
case Size.FitToPicture(border) =>
val w = bb.width + (2 * border)
val h = bb.height + (2 * border)
svg.svg(
svgAttrs.xmlns := s"http://www.w3.org/2000/svg",
svgAttrs.width := w,
svgAttrs.height := h,
svgAttrs.viewBox := s"${bb.left - border} ${bb.bottom - border} ${w} ${h}",
bundle.attrs.style :=
frame.background
.map(c => s"background-color: ${Svg.toHSLA(c)};")
.getOrElse("")
)
case Size.FixedSize(w, h) =>
svg.svg(
svgAttrs.width := w,
svgAttrs.height := h,
svgAttrs.viewBox := s"${-w / 2} ${-h / 2} ${w} ${h}",
bundle.attrs.style :=
frame.background
.map(c => s"background-color: ${Svg.toHSLA(c)};")
.getOrElse("")
)
}
def textTag(text: String, font: Font): Tag = {
val fontFamily =
font.family match {
case FontFamily.Serif => "serif"
case FontFamily.SansSerif => "sans-serif"
case FontFamily.Monospaced => "monospaced"
case FontFamily.Named(name) => name
}
val fontStyle =
font.style match {
case FontStyle.Italic => "italic"
case FontStyle.Normal => "normal"
}
val fontWeight =
font.weight match {
case FontWeight.Bold => "bold"
case FontWeight.Normal => "normal"
}
val style = s"""
|font-family: ${fontFamily};
|font-style: ${fontStyle};
|font-weight: ${fontWeight};
""".stripMargin
svg.text(svgAttrs.style := style, text)
}
/** Transform from client coordinates to local coordinates
*/
def inverseClientTransform(bb: BoundingBox, size: Size): Transform = {
size match {
case Size.FitToPicture(border) =>
val w = bb.width + (2 * border)
val h = bb.height + (2 * border)
Transform.screenToLogical(w, h)
case Size.FixedSize(w, h) =>
Transform.screenToLogical(w, h)
}
}
/** Given stroke and fill returns a `String` representing the stroke and
* fill rendered as SVG styles.
*
* If the fill specifies a gradient that gradient, represented in SVG form
* as a Tag, is added to the given Set as a side-effect.
*/
def toStyle(
stroke: Option[Stroke],
fill: Option[Fill],
gradients: mutable.Set[Tag]
): String = {
val f = fill.fold("fill: none;")(f => this.toStyle(f, gradients))
val s = stroke.fold("stroke: none;")(Svg.toStyle(_))
s ++ " " ++ f
}
/** Given a Fill return the string to insert as the style part of the tag
* being rendered
*
* Additionally, if this fill represents a gradient add that gradient to
* the given Set as a side-effect. In SVG gradients cannot be specified
* inline. Hence this construction.
*/
def toStyle(fill: Fill, gradients: mutable.Set[Tag]): String = {
fill match {
case Fill.ColorFill(c) => s"fill: ${Svg.toHSLA(c)};"
case Fill.GradientFill(g) =>
val (id, gradient) = toSvgGradient(g)
gradients += gradient
s"fill: url('#${id}'); "
}
}
def toSvgGradient(gradient: Gradient): (String, Tag) =
gradient match {
case linear: Gradient.Linear => this.toSvgLinearGradient(linear)
case radial: Gradient.Radial => this.toSvgRadialGradient(radial)
}
def toSvgLinearGradient(gradient: Gradient.Linear): (String, Tag) = {
val (x1, y1, x2, y2) =
(gradient.start.x, gradient.start.y, gradient.end.x, gradient.end.y)
val id = Svg.toGradientId(gradient)
val spreadMethod = Svg.toSvgSpreadMethod(gradient.cycleMethod)
val stops = gradient.stops.map(this.toSvgGradientStop)
val domGradient = svg.linearGradient(
svgAttrs.id := id,
svgAttrs.x1 := x1,
svgAttrs.y1 := y1,
svgAttrs.x2 := x2,
svgAttrs.y2 := y2,
svgAttrs.spreadMethod := spreadMethod,
svgAttrs.gradientUnits := "objectBoundingBox"
)(stops: _*)
id -> domGradient
}
def toSvgRadialGradient(gradient: Gradient.Radial): (String, Tag) = {
val (cx, cy, fx, fy, r) = (
gradient.outer.x,
gradient.outer.y,
gradient.inner.x,
gradient.inner.y,
gradient.radius
)
val id = Svg.toGradientId(gradient)
val spreadMethod = Svg.toSvgSpreadMethod(gradient.cycleMethod)
val stops = gradient.stops.map(this.toSvgGradientStop)
val domGradient = svg.radialGradient(
svgAttrs.id := id,
svgAttrs.cx := cx,
svgAttrs.cy := cy,
svgAttrs.fx := fx,
svgAttrs.fy := fy,
svgAttrs.r := r,
svgAttrs.spreadMethod := spreadMethod,
svgAttrs.gradientUnits := "userSpaceOnUse"
)(stops: _*)
id -> domGradient
}
def toSvgGradientStop(tuple: (Color, Double)): Tag = {
val (c, offset) = tuple
val color = Svg.toRGB(c)
val opacity = c.alpha.get
svg.stop(
svgAttrs.offset := offset,
svgAttrs.stopColor := color,
svgAttrs.stopOpacity := opacity
)
}
def toStyle(stroke: Stroke): String = {
val builder = new StringBuilder(64)
val linecap = stroke.cap match {
case Cap.Butt => "butt"
case Cap.Round => "round"
case Cap.Square => "square"
}
val linejoin = stroke.join match {
case Join.Bevel => "bevel"
case Join.Round => "round"
case Join.Miter => "miter"
}
builder ++= s"stroke-width: ${stroke.width}px; "
builder ++= s"stroke: ${toHSLA(stroke.color)}; "
builder ++= s"stroke-linecap: ${linecap}; "
builder ++= s"stroke-linejoin: ${linejoin}; "
builder ++= (stroke.dash match {
case Some(d) => d.map(a => f"stroke-dasharray: $a%.2f; ").mkString(" ")
case None => ""
})
builder.toString
}
def toSvgTransform(tx: Transform): String = {
val elt = tx.elements
val a = elt(0)
val b = elt(3)
val c = elt(1)
val d = elt(4)
val e = elt(2)
val f = elt(5)
s"matrix($a,$b,$c,$d,$e,$f)"
}
sealed trait PathType
case object Open extends PathType
case object Closed extends PathType
private def format(d: Double): String =
d.toString.replaceFirst("\\.0+$", "")
def toSvgPath(elts: List[PathElement], pathType: PathType): String = {
import PathElement._
import scala.collection.mutable.StringBuilder
val builder = new StringBuilder(64, "M 0,0 ")
elts.foreach {
case MoveTo(end) =>
builder ++= s"M ${format(end.x)},${format(end.y)} "
case LineTo(end) =>
builder ++= s"L ${format(end.x)},${format(end.y)} "
case BezierCurveTo(cp1, cp2, end) =>
builder ++= s"C ${format(cp1.x)},${format(cp1.y)} ${format(
cp2.x
)},${format(cp2.y)} ${format(end.x)},${format(end.y)} "
}
pathType match {
case Open => builder.toString
case Closed => (builder += 'Z').toString
}
}
def toSvgPath(points: Array[Point], pathType: PathType): String = {
import scala.collection.mutable.StringBuilder
val builder = new StringBuilder(points.size * 10)
var first = true
points.foreach { pt =>
if (first) {
first = false
builder ++= s"M ${format(pt.x)},${format(pt.y)} "
} else builder ++= s"L ${format(pt.x)},${format(pt.y)} "
}
pathType match {
case Open => builder.toString
case Closed => (builder += 'Z').toString
}
}
def toSvgSpreadMethod(cycleMethod: Gradient.CycleMethod): String =
cycleMethod match {
case Gradient.CycleMethod.NoCycle => "pad"
case Gradient.CycleMethod.Reflect => "reflect"
case Gradient.CycleMethod.Repeat => "repeat"
}
def toHSLA(color: Color): String = {
val (h, s, l, a) =
(color.hue, color.saturation, color.lightness, color.alpha)
s"hsla(${h.toDegrees}, ${s.get * 100}%, ${l.get * 100}%, ${a.get})"
}
def toRGB(color: Color): String = {
val (r, g, b) = (color.red, color.green, color.blue)
s"rgb(${r.get}, ${g.get}, ${b.get})"
}
def toGradientId(gradient: Gradient): String =
s"gradient-${gradient.hashCode.toString}"
}
}