tituli.layout

The layout model: placed runs of text, and the engines that place them.

A Layout is the single intermediate every use case produces and the renderer consumes — a title card, a credits crawl and a calligram are all just different ways of filling one. A Run is the unit: a string, a baseline origin in frame pixels, an angle, a resolved face and a colour. A block of prose is a few runs (one per line); a calligram is one run per glyph. That is the whole unification — the humble cases are the degenerate calligram, exactly as hoped.

Real font metrics throughout (Face.length), never an average-character estimate.

>>> from tituli.frame import Frame
>>> from tituli.style import TextStyle
>>> lay = block("Hello\nWorld", TextStyle(size=0.05), Frame.blank((1920, 1080)))
>>> len(lay.runs)                      # one run per line (a tracked style would give one per glyph)
2
>>> lay.bbox().width > 0
True

Module Attributes

FACE_ENGINE

the face measures, as Pillow does.

Functions

along_path(text, path, style, frame, *[, ...])

Set text glyph by glyph along path (the path is the baseline).

block(text, style, frame, *[, max_width, x, ...])

Lay out prose as lines from the top-left corner (x, y).

check_measured_by(run, engine)

Raise if run was measured by a different engine than the one in hand.

engine_name(engine, face)

Who measures face with engine (None is the face, as Pillow does).

fill_shape(text, mask, style, frame, *, box)

Pour prose into a silhouette, row by row — the concrete-poem 'shape' case.

fit_size(text, style, frame_height, *, max_width)

Shrink style.size until text wraps within the given bounds.

glyph_columns(lines, style, frame, *, box, ...)

Upright glyphs stepping down slanted streaks — Apollinaire's Il pleut.

measure(text, style, frame_height, *[, engine])

Width in pixels of text set in style on a frame of that height.

merge(layouts)

Concatenate layouts.

text_advance(engine, face, text, *[, per_glyph])

Advance of text: the engine that will draw it measures it (tituli#7); with no engine, the face does (what PillowEngine does).

wrap(text, style, frame_height, *, max_width)

Greedy word wrap on measured widths.

Classes

Layout([runs, plates, meta])

Everything the renderer needs: runs on top of plates, in frame pixels.

Plate(box, color[, kind, radius, solid])

A backdrop drawn under the runs: a scrim, a box, a rule.

Run(text, x, y, face, color[, angle, ...])

A string drawn from a baseline origin, possibly rotated.

tituli.layout.FACE_ENGINE = 'pillow'

the face measures, as Pillow does.

Type:

What engine_name() answers for “no engine”

class tituli.layout.Layout(runs=(), plates=(), meta=dict())[source]

Bases: object

Everything the renderer needs: runs on top of plates, in frame pixels.

bbox(*, include_halo=False)[source]

Bounds of the runs (plates excluded).

With include_halo each haloed run’s box grows by its halo’s reach (reach), so placement can keep the glow, and not only the glyphs, clear of an edge or a reserved zone.

Return type:

Box

property duration_hint: float

Latest t_full among the runs (0 when untimed).

moved_to(box)[source]

Translate so the runs’ bbox top-left lands on box’s top-left.

Return type:

Layout

place_in(frame, *, anchor='auto', halo=None)[source]

Where frame puts this layout: (anchor name, box of the glyphs).

The glow is placed, not only the glyphs: the block frame.place is asked for includes each haloed run’s reach (bbox() with include_halo), so a halo stays clear of an edge or a reserved zone. halo is the one the layout is about to get (with_halo); runs that already carry one count too. The returned box is the glyphs’ own, ready for moved_to().

>>> from tituli.layout import block
>>> from tituli.style import TextStyle
>>> lay = block("Hi", TextStyle(), Frame.blank((1000, 500)), max_width=900)
>>> lay.place_in(Frame.blank((1000, 500)), anchor="bottom")[0]
'bottom'
Return type:

tuple[str, Box]

recolored(color)[source]

A copy with every run drawn in color.

Return type:

Layout

staggered(*, start=0.0, step, ramp)[source]

Reveal runs one after another: run i starts at start + i*step.

Return type:

Layout

translated(dx, dy)[source]

A copy moved by (dx, dy).

Return type:

Layout

with_halo(halo=Halo(color=None, spread=0.03, blur=0.1, gain=2.0, opacity=0.65))[source]

Every run with halo under it (None removes it).

Return type:

Layout

with_plates(*plates)[source]

A copy of the layout with plates added.

Return type:

Layout

with_timing(t_in, t_full)[source]

Give every run the same reveal envelope.

Return type:

Layout

class tituli.layout.Plate(box, color, kind='box', radius=0.0, solid=(0.5, 0.5))[source]

Bases: object

A backdrop drawn under the runs: a scrim, a box, a rule.

translated(dx, dy)[source]

A copy moved by (dx, dy).

Return type:

Plate

class tituli.layout.Run(text, x, y, face, color, angle=0.0, opacity=1.0, tracking=0.0, unit='line', index=0, t_in=None, t_full=None, tags=(), advance=None, measured_by=None, halo=None)[source]

Bases: object

A string drawn from a baseline origin, possibly rotated.

advance: float | None = None

Width the drawing engine measured for this run (tituli#7); None means measure with the face (Pillow), which is what a hand-built run does. Valid only for the text and face it was measured with: build a run by hand, or drop it (replace(run, advance=None)) after changing either.

bbox()[source]

Axis-aligned bounds (ignores rotation — fine for placement).

Return type:

Box

halo: Halo | None = None

none.

Type:

A soft glow drawn under the glyphs (Halo); None

measured_by: str | None = None

unrecorded); check_measured_by() refuses a run used with another engine. Cleared whenever advance is None.

Type:

Name of the engine that measured advance (None

translated(dx, dy)[source]

A copy moved by (dx, dy).

Return type:

Run

property width: float

the measured advance when known, else the text’s length plus tracking.

Type:

The width

tituli.layout.along_path(text, path, style, frame, *, start=0.0, align='start', offset=0.0, upright=False, color=None, tags=(), engine=None)[source]

Set text glyph by glyph along path (the path is the baseline).

Each glyph’s origin is the point at its arc length and it is rotated to the tangent there — unless upright (a calligram convention: letters stay vertical while their positions follow the shape). offset shifts the baseline perpendicular to the path (negative = above, in screen terms). align positions the whole string on the path from start.

Text longer than the path keeps its spacing and runs off the end — that is reported in meta["overflow"] rather than silently squeezed. engine is the one that will draw it; it measures the glyph advances.

Return type:

Layout

tituli.layout.block(text, style, frame, *, max_width=None, x=0.0, y=0.0, color=None, unit='line', tags=(), break_at=None, engine=None)[source]

Lay out prose as lines from the top-left corner (x, y).

text is a string (wrapped to max_width when given) or pre-broken lines. Alignment follows style.align within max_width (or the widest line when no width is given). unit="glyph" emits one run per character (needed for tracking and for per-glyph reveals). break_at (e.g. " · ") wraps a list only between its items — see wrap(). engine is the one that will draw the layout: it measures the lines, and the runs keep that width, so a page composed from this block (stack, grid) is placed by the engine’s measure too.

Return type:

Layout

tituli.layout.check_measured_by(run, engine)[source]

Raise if run was measured by a different engine than the one in hand.

Runs that record no measured_by (hand-built, or laid out without an engine) are not checked: they carry no claim. A recorded one is refused both ways, by the face (engine=None) and by another engine, since each would place glyphs where the measuring engine did not draw them (tituli#15).

Return type:

None

tituli.layout.engine_name(engine, face)[source]

Who measures face with engine (None is the face, as Pillow does).

An engine that hands some faces to another (measurer(face), as HarfBuzz does with the embedded font) is named by the one it hands them to. Otherwise its name (an instance’s own wins, so two configurations of one engine can differ) — unless a subclass re-measures (overrides length) without restating name/measurer, when it is told apart by class name.

Return type:

str

tituli.layout.fill_shape(text, mask, style, frame, *, box, threshold=128, min_span=0.5, color=None, tags=(), repeat=True, engine=None)[source]

Pour prose into a silhouette, row by row — the concrete-poem ‘shape’ case.

mask is any Pillow image (or path): dark pixels are outside, light pixels are the shape (invert your mask if it is the other way round). It is fitted into box; for each line of text height the horizontal spans of the shape are found and filled with as many words as fit, in reading order. With repeat the text cycles until the shape is full; otherwise leftover spans stay empty and leftover words are reported in meta["unplaced"]. engine is the one that will draw it; it measures the words.

Return type:

Layout

tituli.layout.fit_size(text, style, frame_height, *, max_width, max_height=None, min_size=0.012, step=0.9, break_at=None, engine=None)[source]

Shrink style.size until text wraps within the given bounds.

Shrinks geometrically by step and never below min_size (a fraction of frame height, like size itself).

Return type:

TextStyle

tituli.layout.glyph_columns(lines, style, frame, *, box, slants, head_offsets, word_gap_slots=1, size_ratio=0.86, color=None, tags=(), engine=None)[source]

Upright glyphs stepping down slanted streaks — Apollinaire’s Il pleut.

Line k becomes a streak: glyph i sits at (u, v) = (head_offsets[k] + i * slants[k], i) on an abstract slot grid, fitted to box in one step, so the shape is aspect-independent. Letters stay upright. This is the solver muvid’s calligram archetype introduced, generalised over real glyph metrics and any frame — the streaks are Path lines, which is how it stays one preset of along_path() rather than a second family.

slants and head_offsets are cycled if shorter than lines. engine is the one that will draw it; it centres each glyph in its slot.

Return type:

Layout

tituli.layout.measure(text, style, frame_height, *, engine=None)[source]

Width in pixels of text set in style on a frame of that height.

engine is the one that will draw it (default: measure with the face, as Pillow draws); a page measured by one engine and drawn by another can have wrong wrap points and column pitch.

Return type:

float

tituli.layout.merge(layouts)[source]

Concatenate layouts.

Return type:

Layout

tituli.layout.text_advance(engine, face, text, *, per_glyph=False)[source]

Advance of text: the engine that will draw it measures it (tituli#7); with no engine, the face does (what PillowEngine does).

per_glyph sums the characters one by one — how a glyph-by-glyph layout (unit="glyph", a tracked style) places them, so the line measures what its glyphs occupy even for an engine that kerns across characters.

Return type:

float

tituli.layout.wrap(text, style, frame_height, *, max_width, break_at=None, engine=None)[source]

Greedy word wrap on measured widths. Explicit newlines are honoured.

break_at makes a list wrap only between its items: with " · ", "バス bus · スープ soup" never ends a line on バス. An item wider than the whole line falls back to word wrapping inside that item (it is never cut). A no-break space (U+00A0) binds two words in any mode.

>>> from tituli.style import CAPTION
>>> lines = wrap("one two three four five six", CAPTION, 1080, max_width=300)
>>> len(lines) >= 2 and all(measure(l, CAPTION, 1080) <= 300 for l in lines)
True
Return type:

list[str]