1 xtk_vector
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_vector.h

A small 2D vector rasterizer that draws paths into an RGBA8 buffer.

This module fills and strokes paths made of lines and Bezier curves, with solid colors or linear and radial gradients, into an in-memory image. It needs no X connection, so it can render off screen at any time. xtk_svg.h uses it to draw SVG documents, and a program can use it directly to draw shapes that it then puts on screen.

Use.

  1. Make a canvas with xtk_vector_canvas_create(). It starts fully transparent.
  2. Make a path with xtk_vector_path_create() and add to it with xtk_vector_path_move_to(), xtk_vector_path_line_to(), xtk_vector_path_curve_to(), xtk_vector_path_quad_to(), xtk_vector_path_close(), xtk_vector_path_add_rect() and xtk_vector_path_add_ellipse().
  3. Make a paint with xtk_vector_paint_solid(), xtk_vector_paint_linear() or xtk_vector_paint_radial().
  4. Draw with xtk_vector_fill() or xtk_vector_stroke(). Each draw call takes a matrix that maps path coordinates to canvas pixels, and an optional clip rectangle.
  5. Read the result from the canvas's pixels, then free the path with xtk_vector_path_destroy(), the paint with xtk_vector_paint_destroy() and the canvas with xtk_vector_canvas_destroy().

Pixel layout. The canvas holds 4 bytes for each pixel in the order R, G, B, A, row by row from the top, with straight (not premultiplied) alpha. This is the same layout that xtk_image_load_rgba8() gives, so the same code can put either on screen. Each draw call blends its paint over what is already there ("source over").

Coordinates. The canvas has its origin at the top left corner, with y going down. A pixel's center is at (x + 0.5, y + 0.5).

Example.

XtkVectorCanvas *canvas = xtk_vector_canvas_create(64, 64);
XtkVectorPath *path = xtk_vector_path_create();
xtk_vector_path_add_ellipse(path, 32, 32, 24, 24);
XtkVectorPaint fill = xtk_vector_paint_solid(40, 120, 220, 255);
XtkVectorPaint line = xtk_vector_paint_solid(255, 255, 255, 255);
XtkVectorMatrix matrix = xtk_vector_matrix_identity();
xtk_vector_fill(canvas, path, matrix, &fill, XTK_VECTOR_FILL_NONZERO, NULL);
xtk_vector_stroke(canvas, path, matrix, &line, 3.0, XTK_VECTOR_CAP_BUTT,
                  XTK_VECTOR_JOIN_ROUND, 4.0, NULL, 0, 0.0, NULL);
// canvas->pixels now holds a blue disc with a white ring
xtk_vector_paint_destroy(&fill);
xtk_vector_paint_destroy(&line);
xtk_vector_path_destroy(path);
xtk_vector_canvas_destroy(canvas);
 * 

Limitations.

  • Clipping is one axis-aligned rectangle for each draw call; there is no clipping to a path.
  • Anti-aliasing uses 4 sub-scanlines for each pixel row with exact horizontal coverage. It is good for icons and widget art, but it is not identical to other rasterizers.
  • Curves are cut into straight lines with a tolerance of 0.2 units. For a fill this is measured in canvas pixels; for a stroke it is measured in path units before the matrix is applied, so a stroke drawn with a large scale shows the straight pieces.
  • A gradient is placed in path coordinates, as the shape is: its gradient_transform maps gradient coordinates to path coordinates, and the draw call's matrix then maps them to the canvas.
  • A dash pattern with a negative entry, or whose entries are all zero, is not a pattern: the line is drawn solid.
  • A dashed closed subpath is dashed as if it were open at its first point.
#include "xtk_vector.h"

Types: XtkVectorPoint, XtkVectorMatrix, XtkVectorFillRule, XtkVectorLineCap, XtkVectorLineJoin, XtkVectorColor, XtkVectorPath, XtkVectorSegmentType, XtkVectorGradientStop, XtkVectorPaintType, XtkVectorSpreadMethod, XtkVectorPaint, XtkVectorCanvas, XtkVectorClipRect

Functions: xtk_vector_matrix_identity, xtk_vector_matrix_translate, xtk_vector_matrix_scale, xtk_vector_matrix_rotate, xtk_vector_matrix_multiply, xtk_vector_matrix_apply, xtk_vector_matrix_invert, xtk_vector_path_create, xtk_vector_path_destroy, xtk_vector_path_reset, xtk_vector_path_move_to, xtk_vector_path_line_to, xtk_vector_path_curve_to, xtk_vector_path_quad_to, xtk_vector_path_close, xtk_vector_path_get_bounds, xtk_vector_path_segment_count, xtk_vector_path_get_segment, xtk_vector_path_add_rect, xtk_vector_path_add_ellipse, xtk_vector_paint_solid, xtk_vector_paint_linear, xtk_vector_paint_radial, xtk_vector_paint_destroy, xtk_vector_canvas_create, xtk_vector_canvas_destroy, xtk_vector_canvas_clear, xtk_vector_fill, xtk_vector_stroke

Types

XtkVectorPoint

A point in path or canvas coordinates.

Field Type Description
x double The X coordinate.
y double The Y coordinate (it grows downward on the canvas).

XtkVectorMatrix

A 2D affine transform.

A point (x, y) becomes (a*x + c*y + e, b*x + d*y + f). This is the same order of values as SVG's matrix(a b c d e f).

Field Type Description
a double The X scale (and part of a rotation).
b double The Y shear (and part of a rotation).
c double The X shear (and part of a rotation).
d double The Y scale (and part of a rotation).
e double The X translation.
f double The Y translation.

XtkVectorFillRule

The rule that decides which areas of a self-overlapping path are filled.

Name Value Description
XTK_VECTOR_FILL_NONZERO A point is inside when the path winds around it a nonzero number of times (SVG "nonzero", the default).
XTK_VECTOR_FILL_EVENODD A point is inside when a ray from it crosses the path an odd number of times (SVG "evenodd"): holes stay empty.

XtkVectorLineCap

The shape at the open ends of a stroke.

Name Value Description
XTK_VECTOR_CAP_BUTT A flat end exactly at the end point.
XTK_VECTOR_CAP_ROUND A half circle past the end point, with a radius of half the line width.
XTK_VECTOR_CAP_SQUARE A flat end, half the line width past the end point.

XtkVectorLineJoin

The shape of a stroke's corner where two segments meet.

Name Value Description
XTK_VECTOR_JOIN_MITER A sharp point; it becomes a bevel when the point would be longer than the miter limit.
XTK_VECTOR_JOIN_ROUND A rounded corner.
XTK_VECTOR_JOIN_BEVEL A corner cut off straight.

XtkVectorColor

An RGBA color with straight (not premultiplied) alpha.

Field Type Description
r unsigned char Red, 0 to 255.
g unsigned char Green, 0 to 255.
b unsigned char Blue, 0 to 255.
a unsigned char Alpha, 0 (transparent) to 255 (opaque).

XtkVectorPath

typedef struct XtkVectorPath XtkVectorPath;

A path: one or more subpaths of lines and cubic Bezier curves.

The structure is opaque. A path keeps a current point: each line or curve starts there, and a line or curve added before the first xtk_vector_path_move_to() is ignored.

XtkVectorSegmentType

The kind of one segment of a path (xtk_vector_path_get_segment()).

Name Value Description
XTK_VECTOR_SEGMENT_MOVE Starts a subpath at points[0].
XTK_VECTOR_SEGMENT_LINE A straight line to points[0].
XTK_VECTOR_SEGMENT_CUBIC A cubic Bezier curve through the control points points[0] and points[1] to points[2].
XTK_VECTOR_SEGMENT_CLOSE Closes the subpath with a line back to its start; no points.

XtkVectorGradientStop

One color stop of a gradient.

Field Type Description
offset double The place along the gradient, from 0 (start) to 1 (end).
color XtkVectorColor The color at that place; colors between stops are interpolated linearly.

XtkVectorPaintType

The kind of a paint.

Name Value Description
XTK_VECTOR_PAINT_SOLID One color.
XTK_VECTOR_PAINT_LINEAR A linear gradient along a line.
XTK_VECTOR_PAINT_RADIAL A radial gradient from a focal point out to a circle.

XtkVectorSpreadMethod

What a gradient shows past its start and end.

Name Value Description
XTK_VECTOR_SPREAD_PAD The color of the first or last stop continues.
XTK_VECTOR_SPREAD_REPEAT The gradient starts again from its first stop.
XTK_VECTOR_SPREAD_REFLECT The gradient runs back and forth, as in a mirror.

XtkVectorPaint

A solid color or a gradient to fill or stroke with.

Make one with xtk_vector_paint_solid(), xtk_vector_paint_linear() or xtk_vector_paint_radial(), change spread or gradient_transform when needed, and free it with xtk_vector_paint_destroy(). A paint can be used for any number of draw calls.

Field Type Description
type XtkVectorPaintType Solid, linear or radial.
solid XtkVectorColor For a solid paint: the color.
x0 double Linear: the X coordinate of the start of the gradient line. Radial: the X coordinate of the circle's center.
y0 double Linear: the Y coordinate of the start of the gradient line. Radial: the Y coordinate of the circle's center.
x1 double Linear: the X coordinate of the end of the gradient line. Not used for radial.
y1 double Linear: the Y coordinate of the end of the gradient line. Not used for radial.
r double Radial: the circle's radius, where offset 1 is.
focal_x double Radial: the X coordinate of the focal point, where offset 0 is. Set it to x0 for a centered gradient.
focal_y double Radial: the Y coordinate of the focal point. Set it to y0 for a centered gradient.
spread XtkVectorSpreadMethod What the gradient shows past its ends; XTK_VECTOR_SPREAD_PAD by default.
gradient_transform XtkVectorMatrix Maps gradient coordinates to path coordinates; the draw call's matrix is applied after it, as to the path. The constructors set it to the identity. It is not used for a solid paint.
stops XtkVectorGradientStop * The paint's own copy of the stops, sorted by offset. xtk_vector_paint_destroy() frees it.
stop_count int The number of stops. With 0 stops the paint draws nothing; with 1 stop it draws that color everywhere.

XtkVectorCanvas

An RGBA8 image to draw into, made by xtk_vector_canvas_create().

Field Type Description
pixels unsigned char * The pixels: R, G, B, A bytes, row by row from the top, straight alpha. A caller may take them and set this to NULL before it destroys the canvas.
width int The width in pixels.
height int The height in pixels.
stride int The number of bytes in one row; always width * 4.

XtkVectorClipRect

A clip rectangle in canvas pixels.

A draw call changes only the pixels with x0 <= x < x1 and y0 <= y < y1.

Field Type Description
x0 int The left edge; this column is inside.
y0 int The top edge; this row is inside.
x1 int The right edge; this column is outside.
y1 int The bottom edge; this row is outside.

Functions

xtk_vector_matrix_identity

XtkVectorMatrix xtk_vector_matrix_identity(void);

Gives the identity transform, which leaves every point where it is.

Returns: The identity matrix.

xtk_vector_matrix_translate

XtkVectorMatrix xtk_vector_matrix_translate(double tx, double ty);

Gives a transform that moves every point by a fixed distance.

Parameter Description
tx The distance along X.
ty The distance along Y.

Returns: The translation matrix.

xtk_vector_matrix_scale

XtkVectorMatrix xtk_vector_matrix_scale(double sx, double sy);

Gives a transform that scales about the origin.

Parameter Description
sx The factor along X.
sy The factor along Y.

Returns: The scale matrix.

xtk_vector_matrix_rotate

XtkVectorMatrix xtk_vector_matrix_rotate(double radians);

Gives a transform that rotates about the origin.

A positive angle turns the X axis toward the Y axis, which is clockwise on the canvas because Y grows downward.

Parameter Description
radians The angle, in radians.

Returns: The rotation matrix.

xtk_vector_matrix_multiply

XtkVectorMatrix xtk_vector_matrix_multiply(XtkVectorMatrix lhs, XtkVectorMatrix rhs);

Combines two transforms into one.

The result applies rhs first and then lhs. This is the convention of SVG's transform="A B", which applies B to a point before A.

Parameter Description
lhs The transform that is applied second.
rhs The transform that is applied first.

Returns: The product lhs * rhs.

xtk_vector_matrix_apply

XtkVectorPoint xtk_vector_matrix_apply(XtkVectorMatrix m, XtkVectorPoint p);

Applies a transform to a point.

Parameter Description
m The transform.
p The point.

Returns: The transformed point.

xtk_vector_matrix_invert

int xtk_vector_matrix_invert(XtkVectorMatrix m, XtkVectorMatrix *out);

Computes the inverse of a transform.

Parameter Description
m The transform.
out Receives the inverse. It is not changed when m has no inverse.

Returns: 1 when the inverse was written; 0 when m is singular (its determinant is nearly 0), for example a scale by 0.

xtk_vector_path_create

XtkVectorPath *xtk_vector_path_create(void);

Creates an empty path.

Returns: The new path, which the caller frees with xtk_vector_path_destroy(); NULL when memory runs out.

xtk_vector_path_destroy

void xtk_vector_path_destroy(XtkVectorPath *path);

Frees a path and all its segments.

Parameter Description
path The path. NULL is allowed and does nothing.

xtk_vector_path_reset

void xtk_vector_path_reset(XtkVectorPath *path);

Removes all segments from a path but keeps its memory for reuse.

After this the path has no current point, as a new path does.

Parameter Description
path The path. NULL is allowed and does nothing.

xtk_vector_path_move_to

void xtk_vector_path_move_to(XtkVectorPath *path, double x, double y);

Starts a new subpath at a point, which becomes the current point.

Parameter Description
path The path.
x The X coordinate.
y The Y coordinate.

xtk_vector_path_line_to

void xtk_vector_path_line_to(XtkVectorPath *path, double x, double y);

Adds a straight line from the current point to a new point.

Nothing is added when the path has no current point.

Parameter Description
path The path.
x The X coordinate of the end point.
y The Y coordinate of the end point.

xtk_vector_path_curve_to

void xtk_vector_path_curve_to(
    XtkVectorPath *path,
    double x1,
    double y1,
    double x2,
    double y2,
    double x,
    double y);

Adds a cubic Bezier curve from the current point to a new point.

Nothing is added when the path has no current point.

Parameter Description
path The path.
x1 The X coordinate of the first control point.
y1 The Y coordinate of the first control point.
x2 The X coordinate of the second control point.
y2 The Y coordinate of the second control point.
x The X coordinate of the end point.
y The Y coordinate of the end point.

xtk_vector_path_quad_to

void xtk_vector_path_quad_to(
    XtkVectorPath *path,
    double x1,
    double y1,
    double x,
    double y);

Adds a quadratic Bezier curve from the current point to a new point.

The curve is stored as the equal cubic curve. Nothing is added when the path has no current point.

Parameter Description
path The path.
x1 The X coordinate of the control point.
y1 The Y coordinate of the control point.
x The X coordinate of the end point.
y The Y coordinate of the end point.

xtk_vector_path_close

void xtk_vector_path_close(XtkVectorPath *path);

Closes the current subpath with a line back to its start point.

The start point becomes the current point. A stroke of a closed subpath has joins at every corner and no caps. Nothing is added when the path has no current point.

Parameter Description
path The path.

xtk_vector_path_get_bounds

int xtk_vector_path_get_bounds(
    const XtkVectorPath *path,
    double *min_x,
    double *min_y,
    double *max_x,
    double *max_y);

Gives the box around all points and control points of a path.

The box is in the path's own coordinates, before any matrix. Because it includes the control points of curves, it can be larger than the curve itself (a Bezier curve never leaves the polygon of its control points). It is good for placing an SVG gradient on a shape, not for hit testing.

Parameter Description
path The path.
min_x Receives the smallest X; may be NULL.
min_y Receives the smallest Y; may be NULL.
max_x Receives the largest X; may be NULL.
max_y Receives the largest Y; may be NULL.

Returns: 1 when the box was written; 0 when path is NULL or has no points, and then nothing is written.

xtk_vector_path_segment_count

int xtk_vector_path_segment_count(const XtkVectorPath *path);

Gives how many segments a path has, to read them with xtk_vector_path_get_segment().

A quadratic curve was stored as a cubic one, and an ellipse or a rounded rectangle as cubic curves.

Parameter Description
path The path, or NULL.

Returns: The number of segments; 0 for NULL.

xtk_vector_path_get_segment

XtkVectorSegmentType xtk_vector_path_get_segment(
    const XtkVectorPath *path,
    int index,
    XtkVectorPoint points[3]);

Reads one segment of a path, for example to place something at each of its points.

Parameter Description
path The path.
index The segment's number, from 0 to xtk_vector_path_segment_count() - 1.
points Receives the segment's points, in the path's own coordinates: one for a move or a line, three for a cubic curve (see XtkVectorSegmentType), none for a close.

Returns: The segment's kind; XTK_VECTOR_SEGMENT_CLOSE, with nothing written, when path is NULL or index is out of range.

xtk_vector_path_add_rect

void xtk_vector_path_add_rect(
    XtkVectorPath *path,
    double x,
    double y,
    double w,
    double h,
    double rx,
    double ry);

Adds a rectangle, with square or rounded corners, as a new closed subpath.

Nothing is added when the width or height is 0 or less. The corners are square when rx or ry is 0 or less. A radius larger than half the side is reduced to half the side.

Parameter Description
path The path.
x The left edge.
y The top edge.
w The width.
h The height.
rx The corner radius along X.
ry The corner radius along Y.

xtk_vector_path_add_ellipse

void xtk_vector_path_add_ellipse(
    XtkVectorPath *path,
    double cx,
    double cy,
    double rx,
    double ry);

Adds an axis-aligned ellipse as a new closed subpath of four cubic curves.

Nothing is added when a radius is 0 or less. For a circle, give the same radius twice.

Parameter Description
path The path.
cx The X coordinate of the center.
cy The Y coordinate of the center.
rx The radius along X.
ry The radius along Y.

xtk_vector_paint_solid

XtkVectorPaint xtk_vector_paint_solid(
    unsigned char r,
    unsigned char g,
    unsigned char b,
    unsigned char a);

Makes a paint of one color.

A solid paint holds no memory, but calling xtk_vector_paint_destroy() on it is allowed.

Parameter Description
r Red, 0 to 255.
g Green, 0 to 255.
b Blue, 0 to 255.
a Alpha, 0 (transparent) to 255 (opaque).

Returns: The paint.

xtk_vector_paint_linear

XtkVectorPaint xtk_vector_paint_linear(
    double x0,
    double y0,
    double x1,
    double y1,
    const XtkVectorGradientStop *stops,
    int n_stops);

Makes a linear gradient paint.

The color changes along the line from (x0, y0), where offset 0 is, to (x1, y1), where offset 1 is, and stays the same across it. The paint keeps a sorted copy of the stops, so the caller's array can be freed at once; free the paint with xtk_vector_paint_destroy(). When memory runs out or there are no stops, the paint has 0 stops and draws nothing.

Parameter Description
x0 The X coordinate of the start of the line.
y0 The Y coordinate of the start of the line.
x1 The X coordinate of the end of the line.
y1 The Y coordinate of the end of the line.
stops The color stops, in any order.
n_stops The number of stops.

Returns: The paint.

xtk_vector_paint_radial

XtkVectorPaint xtk_vector_paint_radial(
    double cx,
    double cy,
    double r,
    double fx,
    double fy,
    const XtkVectorGradientStop *stops,
    int n_stops);

Makes a radial gradient paint.

Offset 0 is at the focal point and offset 1 is on the circle. The stops are copied as for xtk_vector_paint_linear(); free the paint with xtk_vector_paint_destroy().

Parameter Description
cx The X coordinate of the circle's center.
cy The Y coordinate of the circle's center.
r The circle's radius.
fx The X coordinate of the focal point (cx for a centered gradient).
fy The Y coordinate of the focal point (cy for a centered gradient).
stops The color stops, in any order.
n_stops The number of stops.

Returns: The paint.

xtk_vector_paint_destroy

void xtk_vector_paint_destroy(XtkVectorPaint *paint);

Frees a paint's copy of its stops and sets its stop count to 0.

The XtkVectorPaint itself belongs to the caller and is not freed.

Parameter Description
paint The paint. NULL is allowed and does nothing.

xtk_vector_canvas_create

XtkVectorCanvas *xtk_vector_canvas_create(int width, int height);

Creates a canvas whose pixels are all fully transparent (all bytes 0).

Parameter Description
width The width in pixels.
height The height in pixels.

Returns: The new canvas, which the caller frees with xtk_vector_canvas_destroy(); NULL when width or height is 0 or less, or when memory runs out.

xtk_vector_canvas_destroy

void xtk_vector_canvas_destroy(XtkVectorCanvas *canvas);

Frees a canvas and its pixels.

Parameter Description
canvas The canvas. NULL is allowed and does nothing.

xtk_vector_canvas_clear

void xtk_vector_canvas_clear(XtkVectorCanvas *canvas, XtkVectorColor color);

Sets every pixel to one color, replacing what was there (no blending).

Parameter Description
canvas The canvas. NULL is allowed and does nothing.
color The color.

xtk_vector_fill

void xtk_vector_fill(
    XtkVectorCanvas *canvas,
    const XtkVectorPath *path,
    XtkVectorMatrix matrix,
    const XtkVectorPaint *paint,
    XtkVectorFillRule rule,
    const XtkVectorClipRect *clip);

Fills the inside of a path with a paint.

Every subpath is treated as closed, whether or not it was closed with xtk_vector_path_close(). The paint is blended over the canvas with anti-aliased edges. Nothing is drawn when canvas, path or paint is NULL.

Parameter Description
canvas The canvas to draw into.
path The path.
matrix The transform from path coordinates to canvas pixels.
paint The paint to fill with.
rule The rule that decides which areas are inside.
clip The clip rectangle, or NULL for the whole canvas.

xtk_vector_stroke

void xtk_vector_stroke(
    XtkVectorCanvas *canvas,
    const XtkVectorPath *path,
    XtkVectorMatrix matrix,
    const XtkVectorPaint *paint,
    double width,
    XtkVectorLineCap cap,
    XtkVectorLineJoin join,
    double miter_limit,
    const double *dash_array,
    int dash_count,
    double dash_offset,
    const XtkVectorClipRect *clip);

Draws a line of a given width along a path.

The outline of the line is built in path coordinates and then transformed by the matrix, so the line width scales with the matrix. Open subpaths get caps at their ends; closed subpaths get joins at every corner. With a dash pattern, each dash is stroked as an open line with caps. Nothing is drawn when canvas, path or paint is NULL or when width is 0 or less.

Parameter Description
canvas The canvas to draw into.
path The path.
matrix The transform from path coordinates to canvas pixels.
paint The paint to stroke with.
width The line width, in path units.
cap The shape of the open ends.
join The shape of the corners.
miter_limit The longest miter join, as a ratio of the miter's length to half the line width; a longer one is drawn as a bevel. SVG's default is 4.
dash_array Dash and gap lengths in path units, starting with a dash, or NULL for a solid line. The lengths should not be negative.
dash_count The number of lengths in dash_array; 0 for a solid line.
dash_offset How far into the dash pattern the line starts.
clip The clip rectangle, or NULL for the whole canvas.