Files
rosetta/docs/guide/vector.md
T

5.8 KiB

Vector graphics (SVG)

SVG files load as a Vector: a list of filled and stroked paths, already resolved from the document (shapes turned into path commands, CSS and inherited styles applied, use expanded, gradients resolved). F.ops.vector.rasterize renders any Vector to an rgba8 Image with antialiasing; F.save writes it back as SVG.

local icon = assert(F.vector.load(svgText))           -- a string or buffer of SVG
print(icon.width, icon.height, #icon.paths)           -- 64 64 3
local img = assert(F.ops.vector.rasterize(icon, 128, 128))
local png = assert(F.save(img, nil, { format = "png" }))
assert(img.width == 128 and F.detect(png) == "png")

The type

Vector

Field Type Meaning
kind "vector"
width, height number the document size in CSS pixels (width / height attributes, units converted; else the viewBox size)
viewBox { x, y, w, h } the user-space rectangle that maps onto width x height
paths { VectorPath } in paint order (first is drawn first, at the bottom)
meta table groups (group opacity / clips, by index), clips (clip paths), preserveAspectRatio, unsupported ({ [element]: count } of skipped elements such as text, image, filter), source

VectorPath

Field Type Meaning
d { { op, ... } } absolute commands: { op = "M", x, y }, { op = "L", x, y }, { op = "Q", x1, y1, x, y }, { op = "C", x1, y1, x2, y2, x, y }, { op = "Z" }; arcs and shapes arrive as these
transform { a, b, c, d, e, f }? maps the path's coordinates to viewBox space: x' = a x + c y + e, y' = b x + d y + f
fill, stroke Paint? nil = none
fillRule "nonzero" / "evenodd"
strokeWidth number in the path's user units
strokeLineJoin, strokeLineCap, strokeMiterLimit, strokeDashArray, strokeDashOffset as the SVG properties (defaults "miter", "butt", 4, none, 0)
opacity number 0..1, applied to this path
group, clip, id index into meta.groups / meta.clips; the element id

Paint: { type = "color", color = { r, g, b, a } } with 0..255 components (alpha already includes fill-opacity / stroke-opacity), or a gradient { type = "linear" | "radial", stops = { { offset, r, g, b, a } }, units = "objectBoundingBox" | "userSpaceOnUse", spread = "pad" | "reflect" | "repeat", transform?, opacity } plus x1, y1, x2, y2 (linear) or cx, cy, r, fx, fy, fr (radial), in the units units names.

local v = assert(F.load(svgText))
local bg = v.paths[1]
assert(bg.fill.type == "linear" and #bg.fill.stops == 2)
local first = bg.fill.stops[1]                         -- { offset, r, g, b, a }
assert(first[1] == 0 and first[2] == 0x30 and first[5] == 255)
local sun = v.paths[2]
assert(sun.fill.type == "color" and sun.d[1].op == "M")
local hill = v.paths[3]
assert(hill.stroke ~= nil and hill.strokeWidth == 1)

Load

F.load detects SVG from the text (<svg near the start). Options: maxBytes (default 64 MB).

local v, err = F.vector.load([[<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8">
  <circle cx="4" cy="4" r="3" fill="red"/><text x="0" y="8">skipped</text></svg>]])
assert(v and #v.paths == 1 and v.meta.unsupported.text == 1)
local _ = err

Rasterize

F.ops.vector.rasterize(vector, w?, h?, opts?) returns an rgba8 Image of w x h pixels (default: the vector's size, rounded up). The viewBox maps onto it by preserveAspectRatio (default: centred, uniform scale), so a square icon rendered into a wide image leaves transparent bars.

Option Meaning
background { r, g, b, a } 0..255 under the drawing (default transparent)
tolerance curve flattening in device pixels (default 0.1; larger is faster and coarser)
local v = assert(F.load(svgText))
local wide = assert(F.ops.vector.rasterize(v, 256, 128))
local _, _, _, a = F.ops.image.getPixel(wide, 0, 64)
assert(a == 0)                                           -- the bar left of the centred square
local onWhite = assert(F.ops.vector.rasterize(v, 32, 32, { background = { 255, 255, 255, 255 } }))
assert(select(4, F.ops.image.getPixel(onWhite, 0, 0)) == 255)

Build and edit

local v = F.Types.Vector.new(100, 100)                   -- viewBox 0 0 100 100
table.insert(v.paths, {
	d = { { op = "M", 10, 10 }, { op = "L", 90, 10 }, { op = "L", 50, 90 }, { op = "Z" } },
	fill = { type = "color", color = { 0, 128, 255, 255 } },
	fillRule = "nonzero", strokeWidth = 1, opacity = 1,
})
local svg = assert(F.saveString(v, { format = "svg" }))
assert(string.find(svg, "M 10 10 L 90 10", 1, true))
local img = assert(F.ops.vector.rasterize(v, 50, 50))
assert(select(3, F.ops.image.getPixel(img, 25, 10)) == 255)   -- inside the triangle: blue

Save

F.save(vector) writes SVG (the only vector writer). Gradients become <defs> entries, transforms become transform attributes. The output is a normalised SVG, not the original text.

local v = assert(F.load(svgText))
local out = assert(F.saveString(v))
assert(F.load(out).paths[1].fill.type == "linear")

Formats

Format Read Write Notes
SVG yes yes no text, images, patterns, masks, markers or filters (counted in meta.unsupported)

Tiled maps (.tmx / .tmj) are in the same category but load as a TileMap. See FORMATS.md.

Gotchas

  • F.convert(svgText, "png") does not rasterize: it returns Err.UNKNOWN_FORMAT (PNG is not a vector format). Rasterize first, then save the Image.
  • <text> is not rendered: convert text to paths in your editor before exporting.
  • Colours are 0..255 (not 0..1 as in model materials).
  • Coordinates in d are in the path's own user space; apply transform to get viewBox coordinates.