JFLib

ui/symbol.rs#

Source: lib/ui/symbol.rs

symbol -- multi-colour vector symbols (device pictures for diagrams and palettes): vicon's
layers with palette roles and stroke widths, parsed once, drawn as vectors or rasterised
once per size and palette and blitted

  Symbol::parse(art, vb) -> Symbol     art: SVG path layers separated by '|' on a vb[0] x
                                       vb[1] grid (tools/vsym.py writes them from SVG files)
  sym.paint(g, x, y, scale, &pal)      every layer, the grid's (0, 0) at x, y (the draw
                                       context's coordinates, like Dc::fill), scale = px
                                       per grid unit
  sym.raster(g, w, &pal) -> Surface    the symbol w px wide in a PARGB surface
  sym.height(w) -> px                  the height of the symbol drawn w px wide
  SymPalette { c: [u32; 10] }          the colours of the roles SYM_INK .. SYM_HI
  SymPalette::from_theme(ui, tint)     light, dark or high contrast from the theme's tokens;
                                       tint = the category colour 0xRRGGBB (its light value)
  SymPalette::light / dark(text, bg, tint), contrast(ui)   the three schemes
  pal.powered_off()                    LEDs and screens dark (a device without power)
  SymbolCache::new()                   rasters of up to 256 (symbol, width, palette)
  cache.draw(g, id, &sym, x, y, w, &pal, opacity)   the symbol w px wide at x, y: the
                                       cached raster blitted, wider than 256 px the
                                       vectors; opacity 0..255 (power off: 115 = 45 %)
  cache.get(g, id, &sym, w, &pal) -> Option<Rc<Surface>>   the raster itself

Layer prefixes (vicon.rs's syntax extended, so its art stays valid): F filled (non-zero
rule), E filled (even-odd), neither = stroked with round caps and joins; #n palette role
0..9 (default 0, an 'A' layer 4); S<w> stroke width in grid units (default 1.5; at least
0.8 px when drawn).  Layers paint in order.  Roles: 0 ink (outlines), 1 body, 2 body shade,
3 screen, 4 category tint, 5 ports and dark parts, 6 ok, 7 warn, 8 crit (status LEDs),
9 highlight.  Light and dark themes swap the palette only.

The cache key is (id, width, palette hash) with the palette compared too; `id` is the
caller's number for the symbol.  Rasters are drawn at whole pixels, so x, y of draw are
integers; paint takes fractional positions (a zoomed diagram).
SYM_INK: u8 = 0  // outlines
SYM_BODY: u8 = 1  // the device's body
SYM_SHADE: u8 = 2  // a body part in shadow (sides, bezels, recesses)
SYM_SCREEN: u8 = 3  // displays
SYM_TINT: u8 = 4  // the category colour (one band per symbol)
SYM_PORT: u8 = 5  // ports, grilles, dark parts
SYM_OK: u8 = 6  // status LEDs
SYM_WARN: u8 = 7
SYM_CRIT: u8 = 8
SYM_HI: u8 = 9  // highlight (light details)
SYM_ROLES: usize = 10
/// the widest symbol SymbolCache rasterises (px); wider ones are drawn as vectors
SYM_CACHE_MAX_W: i32 = 256
  • struct Symbol a parsed multi-colour symbol (see the module comment)
    • vb: [f32; 2] -- the grid: width and height in grid units

impl Symbol

  • fn parse(art: &str, vb: [f32; 2]) -> Symbol parse the layers once; layers whose path data does not parse are left out

  • fn layers(&self) -> usize the number of layers that parsed

  • fn height(&self, w: i32) -> i32 the height in px of the symbol drawn w px wide

  • fn paint(&self, g: &mut Gfx, x: f32, y: f32, scale: f32, pal: &SymPalette) draw every layer with the grid's (0, 0) at x, y (draw context coordinates) and scale px per grid unit; the draw colour is kept

  • fn raster(&self, g: &mut Gfx, w: i32, pal: &SymPalette) -> Surface the symbol w px wide in a new PARGB surface (height(w) px high)

  • struct SymPalette the colours of a symbol's roles (straight 0xAARRGGBB, index = SYM_* role)

    • c: [u32; SYM_ROLES]

impl SymPalette

  • fn from_theme(ui: &Ui, tint: u32) -> SymPalette the palette of the current theme: high contrast, dark or light; tint = the category colour 0xRRGGBB as it looks in the light theme

  • fn light(text: u32, bg: u32, tint: u32) -> SymPalette the light scheme from the text and window colours (opaque): slate outlines, near-white bodies, pale steel screens

  • fn dark(text: u32, bg: u32, tint: u32) -> SymPalette the dark scheme: light grey outlines, bodies a step above the window, dark screens, the tint lighter and greyer

  • fn contrast(ui: &Ui) -> SymPalette high contrast: outlines and ports in the text colour, every fill in the window colour, the tint band in the highlight (accent) colour, LEDs in the status colours

  • fn powered_off(&self) -> SymPalette a device without power: status LEDs dark, screens off

  • fn hash(&self) -> u32 FNV-1a of the colours (the cache key)

  • struct SymbolCache rasters of symbols per (id, width, palette), the least recently used one dropped beyond 256

impl SymbolCache

  • fn new() -> SymbolCache
  • fn len(&self) -> usize the number of rasters kept
  • fn is_empty(&self) -> bool
  • fn clear(&mut self) drop every raster
  • fn get(&mut self, g: &mut Gfx, id: u32, sym: &Symbol, w: i32, pal: &SymPalette) -> Option<Rc<Surface>> the raster of symbol id w px wide in this palette, made on the first call; None for widths outside 1..=SYM_CACHE_MAX_W
  • fn draw(&mut self, g: &mut Gfx, id: u32, sym: &Symbol, x: i32, y: i32, w: i32, pal: &SymPalette, opacity: u32) draw symbol id w px wide with its top-left corner at x, y (draw context coordinates) at opacity 0..255: the cached raster, wider than SYM_CACHE_MAX_W the vectors (through a raster when translucent)

Source: docs/API.md in the JFLib repository.