Skip to content

fontlabapp.components.writer

writer

Shared plan, eject and write kernel of the component writer scripts.

What: the Components writer scripts (Align By Anchor, Build From Recipes, Copy Offsets Between Masters, Decompose, Nested Finder, Replace Reference, Unlock Lock) share one model. A script planner turns one layer into Change rows. A row can hold one edit, which the script's Editor writes: EjectEditor (Decompose, Nested Finder) replaces a component with the parts of its base glyph, one level per press; MoveEditor (Align By Anchor, Copy Offsets) sets the offset of a component that plain_move accepts; other scripts subclass Editor.

  • collect runs the planner on every editable layer and returns a Plan. Auto layers are listed and skipped, never edited.
  • copy_problem gives the reason why the parts of a component cannot be copied safely, or "". It walks every base level as FontLab's builder does: a plain (not auto) base layer, affine transforms only, no composite shape that is not a component, no loop, not deeper than MAX_DEPTH.
  • require_safe refuses the whole batch before any history exists: a glyph that owns an auto layer or is a base of one (also through other composites).
  • apply_work writes all edits through ONE history.commit with operation="geometry". EjectEditor builds the copies first, gives each removed component with its data and builder as retain= (history.component_history), and reads the layers back.

How: eject refreshes the component cache, then copies the base parts with ejectTo() BEFORE history opens. keep_raw_state copies back what a native copy drops: the raw node connection bits (FontLab issue 8516), the element note (FontLab issue 8538) and the element name. Inside history, the write only removes the component and inserts the copies at its place. history.commit puts the retained objects first in the notification and asks for deep history, which FontLab's Undo needs for a removed component (FontLab issue 7641).

Why: the logic was a private copy in each script. One App module keeps the refusals and the Undo protocol the same in every script that uses it. The generic kernel (Change, Plan, Editor, apply_work, fmt, plural, layer_of, shown, settle) lives in fontlabapp.masters.layers.writer; this module re-exports it, so the Components scripts keep one import.

This module imports only the standard library and stdlib-only App modules (history, edit_guards, layer_writer) at load time. The fontlab module loads inside the functions, so FontLab's 5-second cold import stays fast. Offline tests do not prove native Undo/Redo.

How to use (a script)::

import fly.ap.components.writer as component_writer

def planner(glyph, master, layer):
    rows = []
    for shape in component_writer.components(layer):
        why = component_writer.copy_problem(
            package, (glyph.name, master), shape, master
        )
        edit = None if why else shape.id
        name = component_writer.base_of(shape)
        rows.append(component_writer.Change(name, "", "parts", edit, why))
    return rows

plan = component_writer.collect(glyphs, masters, planner)
facts = history.scan_auto_facts(package)
component_writer.require_safe(plan.work, facts)
component_writer.apply_work(
    plan.work, "Decompose", facts, component_writer.EjectEditor(unlock=True)
)

Classes

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
MAX_DEPTH attribute (shared captured text)
base_of function (shape: Any) -> str Name of the glyph that a component shape refers to.
blocked_rows function (work: Work, facts: history.AutoFacts) -> list[tuple[str, Change]] One blocking row per edited glyph that the geometry policy refuses.
collect function (glyphs: Iterable[Any], masters: Iterable[str], planner: Planner) -> Plan Run planner on every editable layer.
components function (layer: Any) -> list[Any] Component shapes of the layer, in layer order.
copy_problem function (package: Any, owner: tuple[str, str], shape: Any, master: str) -> str Why the parts of shape cannot be copied safely, or "".
eject function (shape: Any) -> list[Any] Detached copies of the shapes inside a component, placed by its transform.
half_up function (value: float) -> int Round half up: 14.5 -> 15 and -14.5 -> -14, on every platform.
keep_raw_state function (source: Any, copy: Any) -> None Restore what a native copy drops.
offset function (shape: Any) -> tuple[float, float] Translation part of the shape transform, unrounded.
offset_text function (point: tuple[float, float]) -> str '40, -12' for a preview cell.
part_counts function (package: Any, shape: Any, master: str) -> tuple[int, int] (contours, components) that one eject of shape gives, read from its base.
parts_text function (contours: int, inner: int) -> str '3 contours', '2 contours + 1 component'.
plain_move function (shape: Any) -> bool True when the transform is a pure translation: no scale, slant or rotation.
refreshed function (shape: Any) -> None Rebuild the component cache first: its includes can describe an older base.
require_safe function (work: Work, facts: history.AutoFacts) -> None Refuse the whole batch before any history exists, when Undo would fail.
shape_index function (layer: Any, shape_id: Any) -> int Position of the shape with shape_id in the layer.