24. Multi-colour vector symbols#
ui::Symbol (lib/ui/symbol.rs) draws pictures with several colours
-- JFNetworker's device symbols, diagram nodes, palette tiles -- from SVG path layers. The
colours are palette roles, so light, dark and high contrast only swap the SymPalette.
// once: parse the art (tools/vsym.py writes it from SVG files)
let sym = Symbol::parse(art, [64.0, 48.0]);
// per frame: the theme's palette for the symbol's category colour (0xRRGGBB, light value)
let pal = SymPalette::from_theme(ui, 0x4F7396);
// at render time (a canvas Draw): the raster from the cache, blitted ...
self.cache.borrow_mut().draw(g, id, &sym, x, y, 64, &pal, 255); // 64 px wide, opaque
// ... or the vectors at any scale and fractional position (a zoomed diagram)
sym.paint(g, x as f32, y as f32, 2.0, &pal); // 2 px per grid unit
- Roles: 0 ink (outlines), 1 body, 2 body shade, 3 screen, 4 category tint, 5 ports / dark
parts, 6 ok, 7 warn, 8 crit (status LEDs), 9 highlight (
SYM_INK..SYM_HI). An app shows a device's state by changing a role, e.g.pal.c[SYM_OK as usize] = ...;pal.powered_off()turns LEDs and screens off (draw it at opacity 115 = 45 %). SymbolCachekeeps up to 256 rasters per (id, width, palette); wider than 256 px it draws the vectors. Keep it in aRefCellnext to the parsed symbols (aDrawgets&self).- The layer syntax extends that of
ui::paint_svg_icon(one-colour icons,tools/vicon.py):F/Efill,#nrole,S<w>stroke width in grid units (default 1.5), so icon art is valid symbol art (role 0, anAlayer role 4).
Drawing symbols in SVG. python tools/vsym.py apps/NAME reads apps/NAME/icons/*.svg
(viewBox 0 0 64 48, 2-unit margin) and writes src/workspace/icons/symbols_data.rs with
SYMBOLS: &[(&str, &str)] (name, art). Each element's class names its roles ("body",
"tint", or "body port" = fill body, stroke port); it is filled unless fill="none"
and stroked when it has a stroke; the colour values are ignored (they only make the file
look right in a viewer). path, rect, circle, ellipse, line, polyline,
polygon and g with any transform are supported, other elements are errors. Layers
paint in document order: fill a body, then its details, then the outline.
apps/symboltest shows every symbol at 32 / 64 / 128 DIP and without power, light and dark
(tests/sheet.uis).