Interactive vector graphics environment

ctx consists of a terminal, rasterizer and protocol for 2D vector graphics, aiming for compatibility with HTML5 Canvas' 2D context. It leverages portable C code and auto-vectorization capabilities of modern compilers to target everything from microcontrollers to threaded, color managed RGB and CMYK compositing with 32bit floating point.

ctx-0.1.8.tar.bz2 - release jul.1 2025.

The motivation behind ctx was having a batteries included portable vector graphics library with a serialization protocol for use in a terminal emulator/UI shell, while also covering the needs of GIMP and GEGL.

Resource usage minimization has benefits in reduced cache trashing, also some microcontroller platforms. Though raster caches might be introduced in some places, it is likely on a higher level than something like a text glyph cache. Through refactorings ctx is now implementing the SDL and KMS/fbdev backends through the same callback interface used for SPI displays on microcontrollers, sharing bugs and features like multi-core rendering, all while being frugal with RAM usage. Ctx is being used for UI rendering in these computing festival/hacker-camp badges: card10, flow3r and tildagon.

Serial full duplex is the foundation of ctx, and future improvements like shared memory should be on top of a serial fallback. ctx employs compression using the previous drawlist as a dictionary, which makes content with groups using transforms efficiently recompress. When used in the ctx terminal special escape codes turn switch from DEC like terminal to ctx terminal for rendering and event handling. The serialization of the rendering model is a superset of SVG path d attribute syntax.

Pixel encodings from 1bit to 32bit per pixel, in grayscale, RGB and CMY are supported.

automatic partial redraws The interactive backends of ctx are tailored towards low latency rendering of UIs where only a part of the screen changes, though the full scene is expected to be redrawn each time. Hashes are computed for tiles in a grid and only changed ones are scheduled as jobs for the rendering thread and the main thread. Users of ctx are still encouraged to minimize redraws and the api calls ctx_queue_draw (ctx); and ctx_needs_redraw (ctx) are provided for keeping things down.

event handling is a place where ctx differs from other vetor engines, during drawing you can listen for pointer interactions with implicit drag grabs, this allows abstracting over multiple input devices/buttons with a mouse+keyboard abstraction. In the event method callback both absolute and local (At time of callback registration) coordinates are provided. Interactive ctx backends drive event delivery, if you are implementing the callback backend for microcontrollers and external displays you are expected to turn hardware events into interaction events. The terminal contains an on-screen keyboard that uses the same API for delivering synthetic events.

auto-vectorization for platform support, the code in ctx is written to touch a minimal amount of pixels while also being friendly to autovectorization on platforms supporting it.

arm32
tested on rp2040 and rpi2
aarch64
tested on a pi, mediatek based chromebook and more
x86-64
Automatic runtime choice of 3 different levels of SIMD support.
tensilica
tested on esp32 and esp32s3
risc-v
tested on esp32c3
wasm
built with emscripten
?
if it is a 32bit or 64bit platform; with a C compiler it probably works.

Desired features specified for HTML5 canvas that are missing:

miter-limit
The stroker is currently always beveling strokes, fixing miter limit would also remove some currently hidden interesecting geometry on other parts of strokes.
focal point of radial gradient
correct result for shadow-blur
the SDF approximation is servicable in some cases but not correct.
filters
most importantly blur, but also brightness(),contrast(),grayscale(),hue-rotate(),invert(),opacity(),saturate(),sepia()
fontKerning
kerning is implemented in some of the font backends - but not ability to toggle on/off
direction
fontStretch
fontVariantCaps
letterSpacing
wordSpacing
textRendering
strokeText
Non HTML5-Canvas features desired, some for SVG capabilities:
stroke to path
for implementing strokeText
text along path baseline
text to path

license and funding

ctx is available under the ISC license, though not required it is appreciated if the ctx logo is shown during boot/in relevant place of settings or information screens. can encourage continued development of ctx and dissimilar technologies by financially supporting me, Øyvind Kolås who is doing independent pro-bono R&D through patreon and similar.