Skip to content

fontlabapp.anchors.writer

writer

Shared plan and write kernel of the anchor writer scripts.

What: the Anchors scripts (Rename Bulk, Center Horizontally, Place From Bounds, Snap To Metrics, Round To Grid, Delete By Name, Mirror Across Masters) share one model. A script planner turns one layer into Change rows. Each row can hold one Edit:

  • ("set", name, (x, y)): move the anchor; add it when it is missing;
  • ("remove", name, None): delete every anchor with this name;
  • ("rename", name, new): rename every anchor with this name.

collect runs the planner on every editable layer and returns a Plan. require_safe refuses the whole batch before any history exists. apply_work writes all edits as ONE native Undo step and reads every value back.

How: apply_work builds every native pin and point first (prepare), then calls fontlabapp.core.history.glyphs.commit with operation="geometry". The commit discloses queued native updates and refuses auto layers in the edit list, glyphs that own an auto layer and bases of auto composites (also through other composites). Then it inits history, runs write, stamps the glyphs, sends one notification and stamps again. unstored is the read-back: a value that FontLab did not keep raises RuntimeError. A failure after history started raises history.PartialWriteError ("... use Edit > Undo").

Why: FontLab cannot Redo an existing anchor moved to 0, 0 (FontLab issue 8527), so require_safe refuses that move. A new pin gets its name-derived appearance now (anchor_copy.APPEARANCE), because FontLab infers it only on Redo or reopen. An existing pin keeps its own style and expressions: write edits the existing pin object in place.

This module imports only the standard library and stdlib-only App modules at load time. PythonQt, FontLab and anchor_copy load inside the functions, so FontLab's 5-second cold import stays fast. The panel base is in fontlabapp.anchors.panel.model. Offline tests do not prove native Undo/Redo.

How to use (a script)::

import fly.ap.anchors.panel.model as anchor_panel
import fly.ap.anchors.writer as anchor_writer

def planner(glyph, master, layer):
    return [anchor_writer.Change("top", "1, 2", "0, 2", ("set", "top", (0, 2)))]

class Panel(anchor_panel.panel_type()):
    TITLE, KEY = "Example", "anchors_example"

    def planner(self):
        return planner

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
EPSILON attribute
Edit attribute
ORIGIN_ERROR attribute
Planner attribute
SHOWN_NAMES attribute
Work attribute
anchor_points function (layer: Any) -> dict[str, tuple[float, float]] Return <code>{name: (x, y)}</code> of the layer anchors, not rounded.
apply_work function (work: Work, title: str, facts: history.AutoFacts) -> dict[str, Any] Write all edits as ONE native Undo step named <code>title</code>.
blocked_rows function (work: Work, facts: history.AutoFacts) -> list[tuple[str, Change]] Return one blocking row per edited glyph that the geometry policy refuses.
collect function (glyphs: Iterable[Any], masters: Iterable[str], planner: Planner) -> Plan Run <code>planner</code><code> on every existing layer and return the </code><code>Plan</code>.
duplicated function (layer: Any, names: Iterable[str]) -> list[str] Return the sorted names in <code>names</code> that several layer anchors carry.
fmt function (value: float) -> str Format a unit value without trailing decimals: 700.0 gives "700".
half_up function (value: float) -> int Round half up on every platform: 14.5 gives 15, -14.5 gives -14.
layer_of function (glyph: Any, master: str) -> Any | None Return the existing layer of <code>glyph</code><code> in </code><code>master</code>, or None.
on_origin function (point: Iterable[float], near: float=0.0) -> bool True when <code>point</code> counts as 0, 0 for the Redo rule (FontLab issue 8527).
parse_names function (text: str) -> list[str] Split a name list at commas and semicolons: "top, bottom;ogonek" gives 3 names.
plural function (count: int, word: str) -> str Return "1 layer" or "2 layers": add "s" when <code>count</code> is not 1.
prepare function (layer: Any, edits: list[Edit]) -> list[tuple[str, Any, Any]] Build every native pin and point BEFORE history opens.
raw_point function (pin: Any) -> tuple[float, float] Return the stored position of an anchor, not rounded.
refusal function (names: Iterable[str]) -> list[Change] Return one blocking row per name: an edit of a repeated name is ambiguous.
require_safe function (work: Work, facts: history.AutoFacts) -> None Refuse the whole batch before any history exists.
shown function (names: list[str]) -> str Join the first <code>SHOWN&#95;NAMES</code> names with commas; add " …" for more.
unstored function (work: Work, before: list[dict[str, tuple[float, float]]]) -> list[str] Return "glyph/anchor" of each edit that FontLab did not keep.
write function (layer: Any, steps: list[tuple[str, Any, Any]]) -> None Run the prepared steps on the layer.