Scoop API

scoop:scoop.graphics.svg:0.1.0

src/document.scoop

Selected production source · src/document.scoop

1package scoop.graphics.svg
3import scoop.graphics.*
4import scoop.platform.concurrent.currentThreadId
6/** Immutable SVG source; creating an asset does not parse or allocate native resources. */
7public struct SvgAsset(val source: String)
9/** A static Skia SVG document, owned and used by its creating thread. */
10public class SvgDocument private constructor(pointer: Ptr<Unit>): Closeable {
11 private val thread = currentThreadId()
12 private var handle: Ptr<Unit>? = Some(pointer)
13 private val info = readInfo(pointer)
14 public val intrinsicSize: Size? =
15 if ((info.flags and 1) != 0)
16 Some(Size(info.width, info.height))
17 else
18 None
19 public val viewBox: Rect? =
20 if ((info.flags and 2) != 0)
21 Some(info.viewBox)
22 else
23 None
24 public val isClosed: Boolean get() = handle == None
26 internal fun nativePointer(): Ptr<Unit> {
27 checkThread()
28 return handle ?: throw IllegalStateException(Some("SVG document is closed"))
29 }
31 private fun checkThread() {
32 if (thread != currentThreadId())
33 throw IllegalStateException(Some("SVG document requires its owner thread"))
34 }
36 public override fun close() {
37 checkThread()
38 when (handle) {
39 case Some(val value) -> {
40 handle = None
41 @Unsafe
42 {
43 svgDrop(value)
44 }
45 }
46 case None -> {}
47 }
48 }
50 public companion object {
51 /** Parses a copy of UTF-8 source; unsupported Skia elements may be skipped. */
52 public fun parse(source: String): SvgDocument {
53 @Unsafe
54 {
55 return source.withUtf8Bytes {
56 bytes,
57 count -> @Unsafe
58 {
59 return@withUtf8Bytes SvgDocument(
60 svgDecode(bytes.cast<Byte>(), count) ?:
61 throw GraphicsException("parse SVG document")
62 )
63 }
64 }
65 }
66 }
68 /** Parses a copy of an XML byte stream, including its encoding declaration. */
69 public fun decode(bytes: Array<Byte>): SvgDocument {
70 @Unsafe
71 {
72 return bytes.withDataPointer {
73 data,
74 count -> @Unsafe
75 {
76 return@withDataPointer SvgDocument(
77 svgDecode(data, count) ?: throw GraphicsException("decode SVG document")
78 )
79 }
80 }
81 }
82 }
83 }
86@CLayout
87private struct SvgInfo(val width: Double, val height: Double, val viewBox: Rect, val flags: Int)
89private fun readInfo(pointer: Ptr<Unit>): SvgInfo {
90 @Unsafe
91 {
92 return svgInfo(pointer)
93 }
96@Extern(name = "scoop_graphics_svg_decode")
97private fun svgDecode(bytes: Ptr<Byte>, length: Long): Ptr<Unit>?
99@Extern(name = "scoop_graphics_svg_info")
100private fun svgInfo(document: Ptr<Unit>): SvgInfo
102@Extern(name = "scoop_graphics_svg_drop")
103private fun svgDrop(document: Ptr<Unit>)