std1.0.0-beta-7 SafeC docs

Module gui::gui_canvas

#include <std/gui/gui_canvas.h> source

GUI: the anti-aliased 2D vector canvas.

A software rasterizer with the drawing model of the HTML canvas / Skia / Cairo family, drawing into a GuiWindow's pixel buffer or a GuiImage:

paths      move/line/quadratic/cubic/arc/close (gui_path.h), filled by
           the nonzero or even-odd rule, stroked with any width, miter/
           round/bevel joins, butt/round/square caps and dash patterns
paint      solid colors, linear / radial (two-circle) / sweep
           gradients with up to 16 stops and pad/repeat/reflect spread,
           and image patterns (any transform, clamp/repeat/none,
           nearest or bilinear sampling)
state      an affine transform, global alpha, blend mode, rectangular
           and path clipping, all saved/restored with save()/restore()
compositing premultiplied alpha; 12 Porter-Duff operators and 12
           separable blend modes (multiply, screen, overlay, ...)
images     drawn scaled/rotated/cropped with filtering
text       TrueType glyphs from gui_text.h, anti-aliased and cached

Anti-aliasing is exact in x and 16x supersampled in y: each pixel row is cut by 16 sample lines and every edge crossing contributes its exact horizontal coverage, accumulated in a difference buffer and integrated once per row — so a pixel's coverage is the true area inside the shape to within 1/16 of a pixel vertically, for either fill rule, with overlapping and self-intersecting paths. Compositing runs four channels at a time on SIMD vectors (vec<float, 4>, std/simd's f32x4); solid opaque spans are filled with plain 32-bit stores. Everything is deterministic: the same calls produce the same bytes on every platform.

struct GuiCanvas c = GuiCanvas::for_window(&win);
c.clear(GuiColor::hex(0xFFFFFFU));
c.set_fill_color(GuiColor::hex(0x3366CCU));
c.fill_rounded_rect(20.0f, 20.0f, 120.0f, 40.0f, 8.0f);
c.set_stroke_color(GuiColor::hex(0x000000U));
c.set_line_width(2.0f);
c.begin_path(); c.move_to(20.0f, 80.0f); c.line_to(140.0f, 120.0f); c.stroke();
c.set_font(GuiTypeface::fallback(), 16.0f);
c.fill_text("Hello", 20.0f, 150.0f);
c.free();
win.present();

A canvas holds no copy of the pixels, only a pointer: after the window is resized (its buffer reallocated), point the canvas at it again with retarget_window().

Structs

GuiColorStop
GuiPaintWhat to fill or stroke with. A value type: build one, add stops, hand it to set_fill_paint()/set_stroke_paint() (which copy it).
GuiCanvasStateEverything save()/restore() saves.
GuiEdgeA line segment of a flattened path, in device space (internal).
GuiCanvas

Macros

GUI_PAINT_SOLIDGuiPaint.kind
GUI_PAINT_LINEAR
GUI_PAINT_RADIAL
GUI_PAINT_SWEEP
GUI_PAINT_IMAGE
GUI_SPREAD_PADGuiPaint.spread: what gradients do past their ends, and image patterns outside the image.
GUI_SPREAD_REPEAT
GUI_SPREAD_REFLECT
GUI_SPREAD_NONEtransparent (image patterns)
GUI_GRADIENT_MAX_STOPS
GUI_DASH_MAX
GUI_FILL_NONZEROFill rules
GUI_FILL_EVENODD
GUI_CAP_BUTTLine caps / joins
GUI_CAP_ROUND
GUI_CAP_SQUARE
GUI_JOIN_MITER
GUI_JOIN_ROUND
GUI_JOIN_BEVEL
GUI_FILTER_NEARESTImage filtering
GUI_FILTER_BILINEAR
GUI_BASELINE_ALPHABETICText baseline (GuiCanvas::set_text_baseline): what fill_text's y means.
GUI_BASELINE_TOP
GUI_BASELINE_MIDDLE
GUI_BASELINE_BOTTOM
GUI_BLEND_SRC_OVERBlend modes (GuiCanvas::set_blend_mode). Porter-Duff operators:
GUI_BLEND_SRCreplace
GUI_BLEND_DST
GUI_BLEND_DST_OVER
GUI_BLEND_SRC_IN
GUI_BLEND_DST_IN
GUI_BLEND_SRC_OUT
GUI_BLEND_DST_OUT
GUI_BLEND_SRC_ATOP
GUI_BLEND_DST_ATOP
GUI_BLEND_XOR
GUI_BLEND_CLEAR
GUI_BLEND_PLUSadditive, clamped
GUI_BLEND_MULTIPLYSeparable blend modes (W3C Compositing and Blending), composited source-over:
GUI_BLEND_SCREEN
GUI_BLEND_OVERLAY
GUI_BLEND_DARKEN
GUI_BLEND_LIGHTEN
GUI_BLEND_COLOR_DODGE
GUI_BLEND_COLOR_BURN
GUI_BLEND_HARD_LIGHT
GUI_BLEND_SOFT_LIGHT
GUI_BLEND_DIFFERENCE
GUI_BLEND_EXCLUSION
GUI_BLEND_COUNT