Skip to main content

Vector outlines

An outline is a shape described by a path: move to a point, draw lines and curves, close. It has no size of its own, so the same outline can be drawn at any scale, rotated or sheared, filled, stroked or both. Letters are outlines too, which is how glyphs are drawn.

import math
import os

import libstb as stb

os.makedirs("out", exist_ok=True)

# A cream background of 360 x 240 pixels with an alpha channel.
img = stb.Image(memoryview(bytearray([250, 248, 240, 255] * (360 * 240))).cast("B", (240, 360, 4)))

# A heart from two cubic curves, filled, then outlined with a dashed line.
heart = stb.Outline().move_to(60, 190).cubic_to(-20, 120, 30, 40, 60, 90).cubic_to(90, 40, 140, 120, 60, 190).close()
img = img.fill_outline(heart, "#e63946")
img = img.stroke_outline(heart, "#1d3557", 3, cap="round", join="round", dashes=[8, 6])

# A star filled with the even-odd rule (a hole in the middle) and moved by a matrix.
star = stb.Outline()
for i in range(5):
a = 2 * math.pi * (i * 2) / 5
x, y = 50 * math.sin(a), -50 * math.cos(a)
if i == 0:
star.move_to(x, y)
else:
star.line_to(x, y)
star.close()
placed = stb.Matrix().translate(200, 90).rotate(0.2)
img = img.fill_outline(star, "#f4a261", rule="even_odd", matrix=placed)
img = img.stroke_outline(star, "#264653", 2, matrix=placed)

# Caps and joins side by side, and a rounded rectangle, through a canvas.
zigzag = stb.Outline().move_to(0, 40).line_to(25, 0).line_to(50, 40).line_to(75, 0)
canvas = stb.Canvas(img)
for i, (cap, join) in enumerate([("butt", "miter"), ("round", "round"), ("square", "bevel")]):
canvas.add_outline(zigzag).no_fill().stroke(10, "#2a9d8f").cap(cap).join(join).transform(stb.Matrix.translation(255, 20 + 62 * i)).opacity(0.9)
canvas.add_outline(stb.Outline().add_rounded_rect(15, 15, 120, 40, 12)).fill((0, 0, 0, 40)).stroke(1.5, "#555555")
canvas.finish().write("out/outlines.png")
print("bounds of the heart:", [round(v) for v in heart.bounds])
Output
bounds of the heart: [-20, 40, 140, 190]
out/outlines.png
out/outlines.png

Building a path​

An outline is built by calls that each return it, so they chain: move_to, line_to, quad_to and cubic_to for a quadratic and a cubic curve, arc_to, and close. Shortcuts add a whole shape: add_rect, add_rounded_rect, add_ellipse and add_circle. The coordinates are floats, with y pointing down the picture.

Fill​

Filling needs a rule that says what is inside a path that crosses itself. With non_zero, the default, the whole star is inside. With even_odd a point is inside only if a line from it out of the shape crosses the path an odd number of times, so the middle of the star is a hole, as in the example.

Stroke​

A stroke is a line along the path. Its settings are:

  • width, in the outline's units;
  • cap, how an open end looks: butt (flat, ends at the point), round or square (both stick out by half the width);
  • join, how two segments meet: miter (a sharp corner), round or bevel (cut off);
  • dashes, alternating "on" and "off" lengths: [8, 6] is 8 on, 6 off, repeated.
  • miter limit, the length at which a sharp miter corner is cut to a bevel: a miter longer than limit * width / 2 is cut, and the default limit is 4. A very sharp angle would otherwise make a spike many widths long.

The three zigzags on the right show the cap and join pairs side by side.

A matrix​

A matrix moves the outline when it is drawn: translate, rotate (radians), scale and shear, chained. Each call on the chain applies to a point first, so read it from the bottom up. The star is rotated and then moved to (200, 90) by Matrix().translate(200, 90).rotate(0.2).

Ways to draw​

Image.fill_outline and Image.stroke_outline draw on an image and return a new one. Canvas.add_outline collects an outline like the other canvas items, with fill, no_fill, stroke, no_stroke, cap, join, miter_limit, dash, rule, transform, opacity, blend and composite on the item.

API at a glance​

Buildstb.Outline().move_to(x, y).line_to(x, y).close()
A shape in one calloutline.add_rounded_rect(x, y, w, h, r)
Fillimg.fill_outline(outline, color, rule=, matrix=)
Strokeimg.stroke_outline(outline, color, width, cap=, join=, dashes=, matrix=)
A matrixstb.Matrix().translate(x, y).rotate(a)
On a canvascanvas.add_outline(outline).fill(c).stroke(w, c)