Scoop API

scoop:scoop.graphics.svg:0.1.0

src/drawing.scoop

Selected production source · src/drawing.scoop

1package scoop.graphics.svg
3import scoop.graphics.*
4import scoop.graphics.native.withSkiaCanvas
6/** Per-draw root overrides. Explicit child properties retain SVG inheritance semantics. */
7public struct SvgDrawOptions(
8 val currentColor: Color? = None,
9 val rootStrokeWidth: Double? = None,
10 val opacity: Double = 1.0
13/** Maps a document's natural viewport to a logical rectangle without adding a clip. */
14public fun Canvas.drawSvg(
15 document: SvgDocument,
16 viewport: Rect,
17 options: SvgDrawOptions = SvgDrawOptions()
18) {
19 for (
20 value in [
21 viewport.x,
22 viewport.y,
23 viewport.width,
24 viewport.height,
25 viewport.x + viewport.width,
26 viewport.y + viewport.height
27 ]
28 )
29 requireSvgScalar(value)
30 if (viewport.width < 0.0 || viewport.height < 0.0)
31 throw IllegalArgumentException(Some("negative SVG viewport size"))
32 requireSvgUnit(options.opacity)
33 val color = options.currentColor ?: Color.Black
34 for (value in [color.red, color.green, color.blue, color.alpha])
35 requireSvgUnit(value)
36 val stroke = options.rootStrokeWidth ?: 0.0
37 requireSvgScalar(stroke)
38 if (stroke < 0.0)
39 throw IllegalArgumentException(Some("negative SVG stroke width"))
40 val pointer = document.nativePointer()
41 @Unsafe
42 {
43 withSkiaCanvas {
44 canvas -> @Unsafe
45 {
46 val result =
47 svgDraw(
48 pointer,
49 canvas,
50 viewport,
51 color,
52 options.currentColor != None,
53 stroke,
54 options.rootStrokeWidth != None,
55 options.opacity
56 )
57 if (result < 0)
58 throw IllegalArgumentException(Some("SVG viewport scale overflow"))
59 if (result == 0)
60 throw GraphicsException("draw SVG document")
61 }
62 }
63 }
66private fun requireSvgScalar(value: Double) {
67 if (!value.isFinite() || !value.toFloat().isFinite())
68 throw IllegalArgumentException(Some("SVG scalar overflow"))
71private fun requireSvgUnit(value: Double) {
72 if (!value.isFinite() || value < 0.0 || value > 1.0)
73 throw IllegalArgumentException(Some("SVG color and opacity require 0..1"))
76@Extern(name = "scoop_graphics_svg_draw")
77private fun svgDraw(
78 document: Ptr<Unit>,
79 canvas: Ptr<Unit>,
80 viewport: Rect,
81 color: Color,
82 hasColor: Boolean,
83 stroke: Double,
84 hasStroke: Boolean,
85 opacity: Double
86): Int