dymokomi/luce-svg
Source of release 0.9.2
0.9.2 / src / svg / module.lucb
#==============================================================================================
#
# svg - SVG documents: a full renderer, and a flattened Drawing for icons
#
# DESCRIPTION:
# Two ways to draw SVG. The full renderer (`load`, `render`) reads a document as a
# browser would — XML, CSS, the cascade, `use`, viewports, paint servers, clip
# paths and masks — into a render tree and draws it with anti-aliased coverage and
# sRGB compositing (picture.lucb has the overview). The Drawing (`parse`) is the
# older, smaller model: an icon's shapes flattened to contours with their paints,
# drawn into an 8-bit coverage mask the caller tints or into linear-light RGBA;
# `<text>` is read into runs for a caller with fonts. Fragments, in ORDER:
#
# module.lucb the Drawing, its contours and their paints
# parse.lucb the Drawing's document grammar: tags, attributes, groups
# paths.lucb the Drawing's path data and shapes, flattened to points
# style.lucb the Drawing's styles, their inheritance and paints
# gradient.lucb the Drawing's gradients and color ramps
# text.lucb text runs and font properties
# text_runs.lucb reading a `<text>` element's content into runs
# coverage.lucb the Drawing's exact-area coverage rasteriser and 8-bit mask
# stroke.lucb the Drawing's stroke outlines
# rgba.lucb compositing the Drawing into linear RGBA
# buffers.lucb growable lists and a text arena
# names.lucb the element and attribute names the tree knows
# xml.lucb the XML reader
# css.lucb style sheets, selectors and declarations
# tree.lucb the SVG document tree and its queries
# tree_build.lucb building the tree, with the cascade and `use` copies
# tree_text.lucb text content in the tree, and the `font` shorthand
# values.lucb the public value parsers: numbers, lengths, transforms
# colors.lucb CSS colors
# path.lucb paths, path data, arcs and bounds
# raster.lucb flattening paths into polylines
# scanline.lucb coverage by sub-scanlines with true winding rules
# stroker.lucb stroking flattened paths: joins, caps, dashes
# scene.lucb the render tree a document converts into
# convert*.lucb converting the tree: structure, paints, servers, use, clips
# canvas.lucb layers and blend modes
# shade.lucb painting coverage with colors, gradients and patterns
# text_*.lucb text: glyph sources, layout, shaping, text paths and bidi;
# text_font_files.lucb is FontFiles, the default glyph source
# over font files (the catalog in text_font_catalog.lucb)
# render_scene.lucb drawing the render tree, clip paths and masks
# render_more.lucb images, filters and patterns (to come)
# picture.lucb the full renderer's public entry points
# *tests.lucb the tests
#
#==============================================================================================
import memory
import interop
import os
from luce_std import math
import strings
import sync
from luce_color import transfer
from luce_fonts import opentype, shaping
## An SVG the parser could not read.
pub let invalid: ErrorCode = ErrorCode.package(320)
## A point in viewBox (or, inside the rasterisers, pixel) coordinates.
pub struct Point:
pub var x: f64 = 0.0
pub var y: f64 = 0.0
## An sRGB color, each channel encoded (not linear) in 0..1.
pub struct Color:
pub var r: f64 = 0.0
pub var g: f64 = 0.0
pub var b: f64 = 0.0
## How a fill or stroke is painted: not at all, with `color`, with the caller's
## currentColor, or with a gradient.
struct Paint:
var painted: bool = false
var current: bool = false
var color: Color = Color()
## One more than the index of the drawing's gradient this paints with; 0 for none.
var gradient: usize = 0
## For a gradient on a contour: maps viewBox coordinates into the gradient's space.
var space: Matrix = Matrix()
## An affine transform mapping (x, y) to (a·x + c·y + e, b·x + d·y + f).
pub struct Matrix:
pub var a: f64 = 1.0
pub var b: f64 = 0.0
pub var c: f64 = 0.0
pub var d: f64 = 1.0
pub var e: f64 = 0.0
pub var f: f64 = 0.0
## Where the transform takes (`x`, `y`).
pub func map(x: f64, y: f64) -> Point:
return apply(self, x, y)
func identity() -> Matrix:
return Matrix()
func apply(transform: Matrix, x: f64, y: f64) -> Point:
return Point(transform.a * x + transform.c * y + transform.e, transform.b * x + transform.d * y + transform.f)
## `outer` composed with `inner`: a point is transformed by `inner`, then `outer`.
func multiply(outer: Matrix, inner: Matrix) -> Matrix:
return Matrix(a = outer.a * inner.a + outer.c * inner.b,
b = outer.b * inner.a + outer.d * inner.b,
c = outer.a * inner.c + outer.c * inner.d,
d = outer.b * inner.c + outer.d * inner.d,
e = outer.a * inner.e + outer.c * inner.f + outer.e,
f = outer.b * inner.e + outer.d * inner.f + outer.f)
## One flattened outline: a run of points plus how it should be drawn. The contours of one
## element share `element` and its paints, and are filled together by its fill rule.
struct Contour:
var start: usize = 0
var length: usize = 0
var closed: bool = false
var element: usize = 0
var fill: Paint = Paint()
var stroke: Paint = Paint()
var pen: Pen = Pen()
## The paints' opacities, each already multiplied by the element's and its groups' `opacity`.
var fill_opacity: f64 = 1.0
var stroke_opacity: f64 = 1.0
## The element's fill rule: even-odd, else nonzero.
var evenodd: bool = false
# mark: The drawing ===========================================================================
## A parsed drawing: the viewBox size and its contours over a shared point buffer. Every point
## is in viewBox space, measured from the viewBox's top-left corner.
pub struct Drawing:
var view_w: f64 = 0.0
var view_h: f64 = 0.0
var points: Point[]
var point_used: usize = 0
var contours: Contour[]
var contour_used: usize = 0
## Gradients by index (a Paint's `gradient` less one), their stops in one buffer.
var gradients: Gradient[]
var gradient_used: usize = 0
var stops: Stop[]
var stop_used: usize = 0
## Text runs, their families and characters in one byte buffer.
var runs: Run[]
var run_used: usize = 0
var text_bytes: u8[]
var text_used: usize = 0
## The style and element number the next contours are added with.
var style: Style = Style()
var element: usize = 0
static func own(native: Drawing*) -> interop.Reference[Drawing]!:
return try interop.Reference[Drawing].adopt(native, drawing_type)
func add_point(value: Point) -> !:
if self.point_used >= self.points.length:
self.points = try grown(self.points, self.point_used, 64)
self.points[self.point_used] = value
self.point_used += 1
func add_contour(value: Contour) -> !:
if self.contour_used >= self.contours.length:
self.contours = try grown(self.contours, self.contour_used, 16)
self.contours[self.contour_used] = value
self.contour_used += 1
func add_gradient(value: Gradient) -> !:
if self.gradient_used >= self.gradients.length:
self.gradients = try grown(self.gradients, self.gradient_used, 4)
self.gradients[self.gradient_used] = value
self.gradient_used += 1
func add_stop(value: Stop) -> !:
if self.stop_used >= self.stops.length:
self.stops = try grown(self.stops, self.stop_used, 8)
self.stops[self.stop_used] = value
self.stop_used += 1
func add_run(value: Run) -> !:
if self.run_used >= self.runs.length:
self.runs = try grown(self.runs, self.run_used, 8)
self.runs[self.run_used] = value
self.run_used += 1
func add_text_byte(value: u8) -> !:
if self.text_used >= self.text_bytes.length:
self.text_bytes = try grown(self.text_bytes, self.text_used, 256)
self.text_bytes[self.text_used] = value
self.text_used += 1
func add_text_bytes(values: const u8[]) -> !:
for value in values:
try self.add_text_byte(value)
## Close an outline that has at least one point, painted in the current style; a
## `line` passes `fillable` false, since only its stroke is ever drawn.
func flush(start: usize, fillable: bool, closed: bool) -> !:
let length = self.point_used - start
if length == 0:
return
let style = self.style
try self.add_contour(Contour(start = start, length = length, closed = closed, element = self.element,
fill = style.fill if fillable else Paint(), stroke = style.stroke,
pen = style.pen, fill_opacity = style.fill_opacity * style.opacity,
stroke_opacity = style.stroke_opacity * style.opacity, evenodd = style.evenodd))
## The viewBox width (or the `width` attribute, else 24).
pub func view_width() -> f64:
return self.view_w
## The viewBox height (or the `height` attribute, else 24).
pub func view_height() -> f64:
return self.view_h
## How many contours the drawing holds, in document order.
pub func contour_count() -> usize:
return self.contour_used
## The index of a contour's first point.
pub func contour_start(index: usize) -> usize:
return self.contours[index].start
## How many points a contour has.
pub func contour_size(index: usize) -> usize:
return self.contours[index].length
## Which element, counted from 0 in document order, a contour belongs to.
pub func contour_element(index: usize) -> usize:
return self.contours[index].element
## Whether a contour is filled.
pub func contour_filled(index: usize) -> bool:
return self.contours[index].fill.painted
## Whether a contour is stroked.
pub func contour_stroked(index: usize) -> bool:
return self.contours[index].stroke.painted
## Whether a contour was closed (Z, rect, circle, ellipse, polygon).
pub func contour_closed(index: usize) -> bool:
return self.contours[index].closed
## The stroke width of a contour, in viewBox units.
pub func contour_width(index: usize) -> f64:
return self.contours[index].pen.width
## The fill's opacity, `fill-opacity` times every enclosing `opacity`.
pub func contour_fill_opacity(index: usize) -> f64:
return self.contours[index].fill_opacity
## The stroke's opacity, `stroke-opacity` times every enclosing `opacity`.
pub func contour_stroke_opacity(index: usize) -> f64:
return self.contours[index].stroke_opacity
## The fill's sRGB color; meaningless when it is unfilled, currentColor or a gradient.
pub func contour_fill_color(index: usize) -> Color:
return self.contours[index].fill.color
## Whether the fill is currentColor, the color the caller supplies.
pub func contour_fill_current(index: usize) -> bool:
return self.contours[index].fill.current
## The stroke's sRGB color; meaningless when it is unstroked, currentColor or a gradient.
pub func contour_stroke_color(index: usize) -> Color:
return self.contours[index].stroke.color
## Whether the stroke is currentColor, the color the caller supplies.
pub func contour_stroke_current(index: usize) -> bool:
return self.contours[index].stroke.current
## How many shape elements the drawing holds; contours' `element` counts up to it.
pub func element_count() -> usize:
return self.element
## How many text runs the drawing holds, in document order.
pub func text_count() -> usize:
return self.run_used
## Text run `index`; its strings are views into the drawing, valid while it is open.
pub func text_run(index: usize) -> TextRun:
let run = self.runs[index]
var shown = run.shown
shown.families = (str)self.text_bytes[run.family_start..<run.family_start + run.family_length]
shown.text = (str)self.text_bytes[run.text_start..<run.text_start + run.text_length]
return shown
## A point of the shared buffer, in viewBox units.
pub func point(index: usize) -> Point:
return self.points[index]
## Free the drawing's buffers; it is empty afterwards.
pub func close():
if self.points.length > 0:
free(self.points) in memory.heap
if self.contours.length > 0:
free(self.contours) in memory.heap
if self.gradients.length > 0:
free(self.gradients) in memory.heap
if self.stops.length > 0:
free(self.stops) in memory.heap
if self.runs.length > 0:
free(self.runs) in memory.heap
if self.text_bytes.length > 0:
free(self.text_bytes) in memory.heap
var empty_points: Point[]
var empty_contours: Contour[]
var empty_gradients: Gradient[]
var empty_stops: Stop[]
var empty_runs: Run[]
var empty_bytes: u8[]
self.points = empty_points
self.contours = empty_contours
self.gradients = empty_gradients
self.stops = empty_stops
self.runs = empty_runs
self.text_bytes = empty_bytes
self.run_used = 0
self.text_used = 0
self.point_used = 0
self.contour_used = 0
self.gradient_used = 0
self.stop_used = 0
## `items`, of which `used` are in use, moved into a heap array twice as long (or `first`
## long when empty); the old array is freed.
func grown[T](items: T[], used: usize, first: usize) -> T[]!:
let larger = try new T[first if items.length == 0 else items.length * 2] --- in memory.heap
memory.copy(larger, items, used)
if items.length > 0:
free(items) in memory.heap
return larger
## The interop type a parsed Drawing is owned under.
pub let drawing_type: interop.Type[Drawing] = interop.Type[Drawing]("SVG drawing", Drawing.close, closeable = true)