handanim.stylings & utils Package

handanim.stylings.color

handanim.stylings.fonts

handanim.stylings.fonts.list_fonts()[source]

List all available font names (built-in and user-registered).

Return type:

list[str]

handanim.stylings.fonts.get_font_info(font_name)[source]

Return the metadata dict for font_name, or None if not found.

Parameters:

font_name (str)

Return type:

dict[str, str] | None

handanim.stylings.fonts.register_font(name, file_path, font_type=None, persist=False)[source]

Register a TTF, SVG 1.1 single-line, or custom JSON font for use in Text and Math.

By default (persist=False), registers the font in memory for the current Python process without modifying ~/.handanim/config.json.

Parameters:
  • name (str) – Identifier to use as font_name (e.g. “casual_hand”).

  • file_path (str | Path) – Path to the .ttf, .svg, or .json font file.

  • font_type (str | None) – One of “ttf”, “svg”, or “custom”. Inferred from suffix if omitted.

  • persist (bool) – If True, also saves the registration in ~/.handanim/config.json.

Returns:

The resolved absolute path to the registered font file.

Return type:

str

handanim.stylings.fonts.get_font_path(font_name)[source]

Return the filesystem path to a TTF or custom JSON font.

User-registered fonts may supply an absolute path in their ‘file’ field; built-in fonts are resolved via importlib.resources from handanim.fonts.

Parameters:

font_name (str)

Return type:

str

handanim.stylings.fillpatterns

class handanim.stylings.fillpatterns.SolidFillPattern(bound_box_list, fill_style=Ellipsis, sketch_style=Ellipsis)[source]

Bases: DrawableFill

A fill pattern implementation for solid color fills.

This class extends DrawableFill and provides a method to create a solid color fill for a given set of bounding boxes. It sets the pen color, opacity, and fills the specified geometric shapes by drawing lines connecting the box vertices.

bound_box_list

A list of bounding boxes to be filled

Type:

list

fill_style

Style parameters for the fill

Type:

FillStyle

sketch_style

Sketch style parameters

Type:

SketchStyle, optional

Returns:

A set of drawing operations to render the solid fill

Return type:

OpsSet

fill()[source]
Return type:

OpsSet

class handanim.stylings.fillpatterns.HachureFillPattern(bound_box_list, fill_style=<handanim.core.styles.FillStyle object>, sketch_style=<handanim.core.styles.SketchStyle object>)[source]

Bases: DrawableFill

A base class for hachure fill patterns that renders fill lines for polygons.

This class provides methods to render hachure lines with a specific stroke style and sketch style. The render_fill_lines method converts a list of line points into drawable Line objects with the specified styling.

Parameters:
fill_style

Style parameters for the fill

Type:

FillStyle

sketch_style

Sketch style parameters for line rendering

Type:

SketchStyle, optional

render_fill_lines()[source]

Converts line points to drawable Line objects

Parameters:

lines (list[list[tuple[float, float]]])

Return type:

OpsSet

fill()[source]

Generates hachure lines for a set of polygons and renders them

render_fill_lines(lines)[source]
Parameters:

lines (list[list[tuple[float, float]]])

Return type:

OpsSet

fill()[source]
class handanim.stylings.fillpatterns.HatchFillPattern(bound_box_list, fill_style=<handanim.core.styles.FillStyle object>, sketch_style=<handanim.core.styles.SketchStyle object>)[source]

Bases: HachureFillPattern

Generates a hatch fill pattern by rendering hachure lines at two perpendicular angles.

Creates an OpsSet with two sets of hachure lines rotated 90 degrees from each other, creating a criss-cross fill pattern. Preserves the original fill style after rendering.

Returns:

A set of drawing operations representing the hatch fill pattern.

Return type:

OpsSet

Parameters:
fill()[source]
class handanim.stylings.fillpatterns.ZigZagLineFillPattern(bound_box_list, fill_style=<handanim.core.styles.FillStyle object>, sketch_style=<handanim.core.styles.SketchStyle object>)[source]

Bases: HachureFillPattern

Fills polygons with a zigzag line pattern — like back-and-forth colored pencil shading.

Takes the same hachure lines as HachureFillPattern, then subdivides each line into segments and offsets midpoints perpendicular to the line direction, alternating sides. The zigzag_offset field in FillStyle controls the amplitude; when negative, defaults to the hachure gap.

Parameters:
fill()[source]
handanim.stylings.fillpatterns.get_filler(bound_box_list, fill_style=<handanim.core.styles.FillStyle object>, sketch_style=<handanim.core.styles.SketchStyle object>)[source]
Parameters:
Return type:

DrawableFill

handanim.stylings.strokes

handanim.stylings.strokes.apply_stroke_pressure(opsset, stroke_pressure)[source]

Apply variable pen width and opacity to simulate ink pressure.

Interleaves SET_PEN updates at random intervals along the stroke so that width and opacity vary with a smooth sinusoidal profile over the full path.

Parameters:
Return type:

OpsSet

handanim.stylings.strokes.apply_strokes_gradient(opsset, start_color, end_color, n_steps=10)[source]

Interpolate SET_PEN colour linearly from start_color to end_color.

Parameters:
Return type:

OpsSet

handanim.stylings.utils

handanim.stylings.utils.rotate_points(points, center, degrees)[source]
Parameters:
Return type:

ndarray

handanim.stylings.utils.straight_hachure_lines(polygon_list, gap, step_offset=1)[source]
Parameters:
handanim.stylings.utils.hachure_lines(polygon_list, gap, angle, offset=1)[source]
Parameters:
handanim.stylings.utils.polygon_hachure_lines(polygon_list, fill_style=<handanim.core.styles.FillStyle object>, sketch_style=<handanim.core.styles.SketchStyle object>)[source]
Parameters:

polygon_list (list[list[tuple[float, float]]])

handanim.utils.geometry

handanim.utils.geometry.slice_bezier(p0, p1, p2, p3, t)[source]

Slice a Bezier curve at a given parameter t.

Computes intermediate points along a cubic Bezier curve at a specified interpolation point.

Parameters:
  • p0 (array-like) – Starting point of the Bezier curve

  • p1 (array-like) – First control point

  • p2 (array-like) – Second control point

  • p3 (array-like) – Ending point of the Bezier curve

  • t (float) – Interpolation parameter between 0 and 1

Returns:

A list of three points representing the sliced Bezier curve segment

Return type:

list

handanim.utils.geometry.get_bezier_points_from_quadcurve(p0, q1, q2)[source]

Convert a quadratic Bezier curve to a cubic Bezier curve representation.

Transforms control points from a quadratic curve (with two control points) to an equivalent cubic curve (with three control points) using linear interpolation.

Parameters:
  • p0 (Tuple[float, float]) – Starting point of the quadratic curve

  • q1 (Tuple[float, float]) – Control point of the quadratic curve

  • q2 (Tuple[float, float]) – Ending point of the quadratic curve

Returns:

Intermediate control points for the cubic Bezier curve

Return type:

Tuple[list, list, list]

handanim.utils.geometry.cairo_surface_to_numpy(surface)[source]

Convert a Cairo surface to a numpy array with RGBA color channel ordering.

Transforms the image surface data from Cairo’s ARGB format to a numpy array with RGBA color channels, suitable for further image processing or saving.

Parameters:

surface (cairo.ImageSurface) – The source Cairo image surface to convert

Returns:

A numpy array representing the image with shape (height, width, 4)

and dtype np.uint8, with color channels reordered from BGR to RGB

Return type:

numpy.ndarray

handanim.utils.geometry.solve_quad_eqn(a, b, c, ignore_error=False)[source]
Parameters:
Return type:

tuple[float | None, float | None]

handanim.utils.geometry.get_bezier_extreme_points(p0, p1, p2, p3)[source]
Parameters:
Return type:

tuple[float, float, float, float]

handanim.utils.geometry.get_line_slope_angle(p0, p1)[source]

Calculate the angle of the line segment defined by two points.

Parameters:
  • p0 (Tuple[float, float]) – The first point (x0, y0)

  • p1 (Tuple[float, float]) – The second point (x1, y1)

Returns:

The angle in radians of the line segment from p0 to p1

Return type:

float

handanim.utils.humanize

handanim.utils.humanize.bow_segment(start, end, amplitude)[source]

Return (c1, c2) cubic Bézier control points that bow a segment naturally.

Both control points share the same perpendicular displacement (no S-curve inflection) and sit at a shared random diverge fraction in [0.2, 0.4], matching the technique used in Line.draw_single_line.

amplitude: maximum perpendicular displacement in world units.

Parameters:
Return type:

tuple[ndarray, ndarray]

handanim.utils.humanize.jitter_point(pt, amplitude)[source]

Displace a point by uniform random noise in both axes.

Parameters:
Return type:

ndarray

handanim.utils.humanize.apply_roughness(ops, roughness, font_size)[source]

Replace LINE_TO segments with naturally bowed CURVE_TO segments.

Amplitude = roughness * sqrt(length * font_size / 24), which keeps roughness dimensionless (≈ % of font_size for a full-height stroke) while giving shorter segments proportionally less wobble. Uses bow_segment so both control points bow consistently to one side.

Parameters:
Return type:

OpsSet

handanim.utils.humanize.apply_stroke_pressure(opsset, stroke_pressure)[source]

Apply variable pen width and opacity to simulate ink pressure.

Interleaves SET_PEN updates at random intervals along the stroke so that width and opacity vary with a smooth sinusoidal profile over the full path.

Parameters:
Return type:

OpsSet

handanim.utils.humanize.apply_strokes_gradient(opsset, start_color, end_color, n_steps=10)[source]

Interpolate SET_PEN colour linearly from start_color to end_color.

Parameters:
Return type:

OpsSet