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).
- handanim.stylings.fonts.get_font_info(font_name)[source]¶
Return the metadata dict for font_name, or None if not found.
- 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:
handanim.stylings.fillpatterns¶
- class handanim.stylings.fillpatterns.SolidFillPattern(bound_box_list, fill_style=Ellipsis, sketch_style=Ellipsis)[source]¶
Bases:
DrawableFillA 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.
- sketch_style¶
Sketch style parameters
- Type:
SketchStyle, optional
- Returns:
A set of drawing operations to render the solid fill
- Return type:
- class handanim.stylings.fillpatterns.HachureFillPattern(bound_box_list, fill_style=<handanim.core.styles.FillStyle object>, sketch_style=<handanim.core.styles.SketchStyle object>)[source]¶
Bases:
DrawableFillA 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:
- sketch_style¶
Sketch style parameters for line rendering
- Type:
SketchStyle, optional
- class handanim.stylings.fillpatterns.HatchFillPattern(bound_box_list, fill_style=<handanim.core.styles.FillStyle object>, sketch_style=<handanim.core.styles.SketchStyle object>)[source]¶
Bases:
HachureFillPatternGenerates 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:
- Parameters:
- class handanim.stylings.fillpatterns.ZigZagLineFillPattern(bound_box_list, fill_style=<handanim.core.styles.FillStyle object>, sketch_style=<handanim.core.styles.SketchStyle object>)[source]¶
Bases:
HachureFillPatternFills 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:
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:
opsset (OpsSet)
stroke_pressure (StrokePressure)
- Return type:
handanim.stylings.utils¶
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:
- 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:
- Returns:
Intermediate control points for the cubic Bezier curve
- Return type:
- 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:
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.
- handanim.utils.humanize.jitter_point(pt, amplitude)[source]¶
Displace a point by uniform random noise in both axes.
- 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.
- 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:
opsset (OpsSet)
stroke_pressure (StrokePressure)
- Return type: