scoop:scoop.graphics.svg:0.1.0
src/drawing.scoop
Selected production source · src/drawing.scoop
1
package scoop.graphics.svg3
import scoop.graphics.*4
import scoop.graphics.native.withSkiaCanvas6
/** Per-draw root overrides. Explicit child properties retain SVG inheritance semantics. */7
public struct SvgDrawOptions(8
val currentColor: Color? = None,9
val rootStrokeWidth: Double? = None,10
val opacity: Double = 1.011
)13
/** Maps a document's natural viewport to a logical rectangle without adding a clip. */14
public 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.height27
]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.Black34
for (value in [color.red, color.green, color.blue, color.alpha])35
requireSvgUnit(value)36
val stroke = options.rootStrokeWidth ?: 0.037
requireSvgScalar(stroke)38
if (stroke < 0.0)39
throw IllegalArgumentException(Some("negative SVG stroke width"))40
val pointer = document.nativePointer()41
@Unsafe42
{43
withSkiaCanvas {44
canvas -> @Unsafe45
{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.opacity56
)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
}64
}66
private fun requireSvgScalar(value: Double) {67
if (!value.isFinite() || !value.toFloat().isFinite())68
throw IllegalArgumentException(Some("SVG scalar overflow"))69
}71
private 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"))74
}76
@Extern(name = "scoop_graphics_svg_draw")77
private 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: Double86
): Int