scoop:scoop.graphics.svg:0.1.0
src/document.scoop
Selected production source · src/document.scoop
1
package scoop.graphics.svg3
import scoop.graphics.*4
import scoop.platform.concurrent.currentThreadId6
/** Immutable SVG source; creating an asset does not parse or allocate native resources. */7
public struct SvgAsset(val source: String)9
/** A static Skia SVG document, owned and used by its creating thread. */10
public 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
else18
None19
public val viewBox: Rect? =20
if ((info.flags and 2) != 0)21
Some(info.viewBox)22
else23
None24
public val isClosed: Boolean get() = handle == None26
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 = None41
@Unsafe42
{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
@Unsafe54
{55
return source.withUtf8Bytes {56
bytes,57
count -> @Unsafe58
{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
@Unsafe71
{72
return bytes.withDataPointer {73
data,74
count -> @Unsafe75
{76
return@withDataPointer SvgDocument(77
svgDecode(data, count) ?: throw GraphicsException("decode SVG document")78
)79
}80
}81
}82
}83
}84
}86
@CLayout87
private struct SvgInfo(val width: Double, val height: Double, val viewBox: Rect, val flags: Int)89
private fun readInfo(pointer: Ptr<Unit>): SvgInfo {90
@Unsafe91
{92
return svgInfo(pointer)93
}94
}96
@Extern(name = "scoop_graphics_svg_decode")97
private fun svgDecode(bytes: Ptr<Byte>, length: Long): Ptr<Unit>?99
@Extern(name = "scoop_graphics_svg_info")100
private fun svgInfo(document: Ptr<Unit>): SvgInfo102
@Extern(name = "scoop_graphics_svg_drop")103
private fun svgDrop(document: Ptr<Unit>)