Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-svg

Source of release 0.9.2

0.9.2 / tools / suite.lucb

## Render a list of SVG files at the resvg test suite's size, 300 pixels wide, for the
## comparison in tools/suite.py. Each file `n` of the list (one path a line) becomes
## `<outdir>/<n>.rgba`: its width and height as little-endian u32s, then straight-alpha
## sRGB RGBA bytes. A file that fails to parse or draw writes nothing. `--drawing` draws
## with the flattened Drawing renderer instead of the full one. Text is set in the system's
## fonts, or with `--fonts DIR` in the fonts of DIR with resvg's generic families (serif Noto
## Serif, sans-serif Noto Sans, cursive Yellowtail, fantasy Sedgwick Ave Display, monospace
## Noto Mono), as resvg's suite sets them.
##   suite <list.txt> <outdir> [--drawing] [--fonts DIR]
import c
from luce_std import files, math
import memory
import strings
import svg

pub func main(arguments: str[]) -> i32!:
    if arguments.length < 3:
        print("usage: suite <list.txt> <outdir> [--drawing] [--fonts DIR]")
        return 2
    var drawing = false
    var fonts = svg.FontFiles()
    var have_fonts = false
    defer fonts.close()
    var at: usize = 3
    while at < arguments.length:
        if arguments[at] == "--drawing":
            drawing = true
        elif arguments[at] == "--fonts" and at + 1 < arguments.length:
            at += 1
            let directories: str[1] = [arguments[at]]
            fonts.close()
            fonts = try svg.FontFiles.open(directories)
            try fonts.set_generic("serif", "Noto Serif")
            try fonts.set_generic("sans-serif", "Noto Sans")
            try fonts.set_generic("cursive", "Yellowtail")
            try fonts.set_generic("fantasy", "Sedgwick Ave Display")
            try fonts.set_generic("monospace", "Noto Mono")
            have_fonts = true
        else:
            print("usage: suite <list.txt> <outdir> [--drawing] [--fonts DIR]")
            return 2
        at += 1
    let list_path = try strings.copy(arguments[1])
    defer strings.release(list_path)
    let list_file = try files.read((c.str)list_path)
    defer list_file.release()
    let list = list_file.value
    var index: usize = 0
    var start: usize = 0
    for position in 0..<list.length + 1:
        if position < list.length and list[position] != '\n':
            continue
        let line = (str)list[start..<position]
        start = position + 1
        if line.length == 0:
            continue
        var source: svg.FontFiles*? = none
        if have_fonts:
            source = &fonts
        render_one(line, arguments[2], index, drawing, source) catch failure:
            print(f"{index}: {failure.message}")
            recover ()
        index += 1
    return 0

## Draw `path` 300 pixels wide into `<outdir>/<index>.rgba`.
func render_one(path: str, outdir: str, index: usize, drawing: bool, fonts: svg.FontFiles*?) -> !:
    let input = try strings.copy(path)
    defer strings.release(input)
    let data_file = try files.read((c.str)input)
    defer data_file.release()
    let data = data_file.value
    # Image files resolve against the test's own directory, as resvg's suite has them.
    let slash = strings.find_last(path, "/") else 0
    let drawn = (try draw_flattened((str)data)) if drawing else (try draw_full((str)data, (str)path.bytes[..<slash], fonts))
    let size = drawn.size
    let bytes = drawn.bytes
    defer free(bytes) in memory.heap
    let output = try new u8[8 + bytes.length] --- in memory.heap
    defer free(output) in memory.heap
    put_u32(output, 0, size.0)
    put_u32(output, 4, size.1)
    memory.copy(output[8..], bytes, bytes.length)
    var name: u8[1024]
    let target = try strings.format(name, f"{outdir}/{index}.rgba")
    try files.write((c.str)target, output)

## The bitmap's size for a picture `width` by `height`: 300 wide, the height in proportion.
func fitted(width: f64, height: f64) -> (u32, u32):
    return (300, (u32)math.max(1.0, math.round(300.0 * height / width)))

## The document drawn by the full renderer.
## A drawn bitmap: its size and straight-alpha sRGB bytes (in the heap).
struct Drawn:
    var size: (u32, u32)
    var bytes: u8[]

func draw_full(source: str, directory: str, fonts: svg.FontFiles*?) -> Drawn!:
    var options = svg.Options(resources = directory)
    if let glyphs = fonts:
        options.glyphs = glyphs
    let picture = try svg.load(source, options)
    defer picture.release()
    let size = fitted(picture.value().width_px(), picture.value().height_px())
    let bytes = try new u8[(usize)size.0 * (usize)size.1 * 4] --- in memory.heap
    errdefer free(bytes) in memory.heap
    try svg.render(picture.value(), svg.fit_transform(picture.value(), size.0, size.1), size.0, size.1, bytes)
    return Drawn(size = size, bytes = bytes)

## The document drawn by the Drawing renderer, in linear light, encoded for output.
func draw_flattened(source: str) -> Drawn!:
    let drawing = try svg.parse(source)
    defer drawing.release()
    let size = fitted(drawing.value().view_width(), drawing.value().view_height())
    let pixels = try new f32[(usize)size.0 * (usize)size.1 * 4] --- in memory.heap
    defer free(pixels) in memory.heap
    try svg.rasterize_rgba(drawing.value(), size.0, size.1, pixels, svg.Color())
    let bytes = try new u8[pixels.length] --- in memory.heap
    for at in 0..<pixels.length:
        var value = math.clamp((f64)pixels[at], 0.0, 1.0)
        if at % 4 != 3:
            value = encode(value)
        bytes[at] = (u8)(value * 255.0 + 0.5)
    return Drawn(size = size, bytes = bytes)

func put_u32(bytes: u8[], at: usize, value: u32):
    bytes[at] = (u8)(value & 0xFF)
    bytes[at + 1] = (u8)((value >> 8) & 0xFF)
    bytes[at + 2] = (u8)((value >> 16) & 0xFF)
    bytes[at + 3] = (u8)((value >> 24) & 0xFF)

## Linear light to the sRGB encoding.
func encode(value: f64) -> f64:
    if value <= 0.0031308:
        return value * 12.92
    return 1.055 * math.pow(value, 1.0 / 2.4) - 0.055