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
the face measures, as Pillow does. |
Functions
|
Set |
|
Lay out prose as lines from the top-left corner |
|
Raise if |
|
Who measures |
|
Pour prose into a silhouette, row by row — the concrete-poem 'shape' case. |
|
Shrink |
|
Upright glyphs stepping down slanted streaks — Apollinaire's Il pleut. |
|
Width in pixels of |
|
Concatenate layouts. |
|
Advance of |
|
Greedy word wrap on measured widths. |
Classes
|
Everything the renderer needs: runs on top of plates, in frame pixels. |
|
A backdrop drawn under the runs: a scrim, a box, a rule. |
|
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:
objectEverything the renderer needs: runs on top of plates, in frame pixels.
- bbox(*, include_halo=False)[source]¶
Bounds of the runs (plates excluded).
With
include_haloeach 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:
- place_in(frame, *, anchor='auto', halo=None)[source]¶
Where
frameputs this layout:(anchor name, box of the glyphs).The glow is placed, not only the glyphs: the block
frame.placeis asked for includes each haloed run’s reach (bbox()withinclude_halo), so a halo stays clear of an edge or a reserved zone.halois 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 formoved_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'
- staggered(*, start=0.0, step, ramp)[source]¶
Reveal runs one after another: run
istarts atstart + i*step.- Return type:
- class tituli.layout.Plate(box, color, kind='box', radius=0.0, solid=(0.5, 0.5))[source]¶
Bases:
objectA backdrop drawn under the runs: a scrim, a box, a rule.
- 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:
objectA string drawn from a baseline origin, possibly rotated.
- advance: float | None = None¶
Width the drawing engine measured for this run (tituli#7);
Nonemeans measure with the face (Pillow), which is what a hand-built run does. Valid only for thetextandfaceit was measured with: build a run by hand, or drop it (replace(run, advance=None)) after changing either.
- measured_by: str | None = None¶
unrecorded);
check_measured_by()refuses a run used with another engine. Cleared wheneveradvanceisNone.- Type:
Name of the engine that measured
advance(None
- 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
textglyph by glyph alongpath(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).offsetshifts the baseline perpendicular to the path (negative = above, in screen terms).alignpositions the whole string on the path fromstart.Text longer than the path keeps its spacing and runs off the end — that is reported in
meta["overflow"]rather than silently squeezed.engineis the one that will draw it; it measures the glyph advances.- Return type:
- 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).textis a string (wrapped tomax_widthwhen given) or pre-broken lines. Alignment followsstyle.alignwithinmax_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 — seewrap().engineis 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:
- tituli.layout.check_measured_by(run, engine)[source]¶
Raise if
runwas 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:
- tituli.layout.engine_name(engine, face)[source]¶
Who measures
facewithengine(Noneis 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 itsname(an instance’s own wins, so two configurations of one engine can differ) — unless a subclass re-measures (overrideslength) without restatingname/measurer, when it is told apart by class name.- Return type:
- 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.
maskis 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 intobox; 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. Withrepeatthe text cycles until the shape is full; otherwise leftover spans stay empty and leftover words are reported inmeta["unplaced"].engineis the one that will draw it; it measures the words.- Return type:
- 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.sizeuntiltextwraps within the given bounds.Shrinks geometrically by
stepand never belowmin_size(a fraction of frame height, likesizeitself).- Return type:
- 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
kbecomes a streak: glyphisits at(u, v) = (head_offsets[k] + i * slants[k], i)on an abstract slot grid, fitted toboxin one step, so the shape is aspect-independent. Letters stay upright. This is the solver muvid’scalligramarchetype introduced, generalised over real glyph metrics and any frame — the streaks arePathlines, which is how it stays one preset ofalong_path()rather than a second family.slantsandhead_offsetsare cycled if shorter thanlines.engineis the one that will draw it; it centres each glyph in its slot.- Return type:
- tituli.layout.measure(text, style, frame_height, *, engine=None)[source]¶
Width in pixels of
textset instyleon a frame of that height.engineis 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:
- 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 (whatPillowEnginedoes).per_glyphsums 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:
- 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_atmakes 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