Skip to content

fontlabapp.objects.json_data

json_data

FontLab 9.1 object JSON: read, edit, copy, save and paste glyphs, layers, elements and contours.

What: FontLab 9.1 (build 9812 and later) can write glyphs, layers, elements and contours as JSON and read that JSON back into new, detached objects: fontlab.flModelProducer.getClipboardDataJson(package_id, upm, objects) returns a QByteArray and getObjectsFromJsonData(data) returns a list, or None when it cannot read the data. This module gives that pair a safe Python surface.

The pure layer needs no FontLab. Envelope.parse reads the JSON envelope ("dataType": "com.fontlab.objects", "upm", "packageId" and one payload: a singleton "glyph", "layer", "element" or "contour" key for one object, "objects": [{"glyph": {...}}, ...] for two or more, and no payload key for an empty list). Envelope.to_bytes writes the same shapes back. describe lists what the data holds, and rename_glyphs, unique_glyph_names, filter_layers, set_advance_width and drop_unicodes return edited copies.

The native layer runs inside FontLab: available, export, decode, copy_to_clipboard, read_clipboard, save_file, load_file, paste_glyphs and paste_shapes.

How: decode always hands FontLab a PythonQt.QtCore.QByteArray. Passing Python bytes, bytearray or str to getObjectsFromJsonData crashes FontLab 9.1.9812 (SIGBUS), so no other type reaches it. None from FontLab becomes ValueError. Decoded objects are detached: a decoded glyph has a new id and no font, and flGlyph has no unicode attribute, so Unicode values come from the JSON. upm is ignored on decode; nothing is scaled.

paste_glyphs never calls flPackage.addGlyph (a raw insert without Undo that also produced duplicate names live). It creates glyphs through fontlabapp.glyphs.creation.create_glyphs and copies the decoded outlines in its fill step. paste_shapes adds decoded shapes to one live layer inside one fontlabapp.core.history.glyphs.commit.

Undo: the JSON calls record nothing. paste_glyphs: each new glyph is its own Undo step (FontLab's Add Glyph) and the outlines are one more named step. paste_shapes: one named Undo step. Everything else changes no font.

Clipboard: FontLab 9.1 puts this JSON on the clipboard as application/json (seen live on 9.1.9812: Edit > Copy of a glyph in the Font window, byte for byte what export returns), next to its own vic/* formats. copy_to_clipboard writes application/json plus the JSON as plain text; read_clipboard reads application/json, then the older application/x-fontlab-objects+json, then plain text.

Limits: whether FontLab's Edit > Paste accepts our clipboard without its vic/* formats is not verified live. Components in pasted glyphs keep their base names; a base missing from the target font is not created. Unicode values are dropped from renamed glyphs; other Unicode clashes are not checked.

How to use::

from fontlabapp.objects import json_data
raw = json_data.export(package, [glyph])        # bytes, UTF-8 JSON
env = json_data.Envelope.parse(raw)
print("\n".join(json_data.describe(env)))
env = json_data.rename_glyphs(env, {"o": "o.alt"})
json_data.paste_glyphs(package, env, facts=scan_auto_facts(package))

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
DATA_TYPE attribute
GLYPHS_UNDO_TEXT attribute
Item attribute
KINDS attribute
MIME_TYPE attribute
NEEDS_91 attribute
PARTIAL_TEXT attribute
SHAPES_UNDO_TEXT attribute
as_qbytearray function (data: Any, qt: Any=None) -> Any Return a <code>QByteArray</code><code> for </code><code>data</code><code>; a </code><code>QByteArray</code> passes unchanged.
available function (fl: Any=None) -> bool True when this FontLab has the object JSON API (9.1 or later).
copy_objects function (package: Any, objects: Sequence[Any], *, fl: Any=None, qt: Any=None) -> bytes Copy <code>objects</code> to the clipboard so FontLab's Edit > Paste accepts them.
copy_to_clipboard function (data: Any, *, qt: Any=None) -> None Put object JSON on the clipboard as <code>application/json</code> and as plain text.
current_layer function (fl: Any=None) -> tuple[Any, Any] Return <code>(flGlyph, flLayer)</code><code> of the current glyph, or </code><code>(None, None)</code>.
decode function (data: Any, *, fl: Any=None, qt: Any=None) -> list[Any] Return new detached objects read from object JSON.
describe function (envelope: Envelope) -> list[str] Return one line per item: kind, name, layers, widths and counts.
drop_unicodes function (envelope: Envelope, names: Iterable[str] | None=None) -> Envelope Remove the <code>unicode</code> value from all glyph items, or from the named ones.
export function (package: Any, objects: Sequence[Any], *, fl: Any=None) -> bytes Return FontLab's JSON for <code>objects</code> (glyphs, layers, shapes or contours).
filter_layers function (envelope: Envelope, names: Iterable[str]) -> Envelope Keep only the layers named in <code>names</code>: inside glyphs and as layer items.
load_file function (path: str | Path) -> bytes Read object JSON from <code>path</code><code>; raise </code><code>ValueError</code> when it is not object JSON.
only_glyphs function (envelope: Envelope) -> Envelope Return a copy holding only the glyph items.
paste_glyphs function (package: Any, data: Any, *, facts: Any, on_conflict: str='rename', masters: Sequence[str] | None=None, allow… Create new glyphs from the glyph items of object JSON, with Undo.
paste_shapes function (layer: Any, glyph: Any, data: Any, *, facts: Any, title: str='Paste JSON Shapes', fl: Any=None, qt: Any=None… Add the shapes of object JSON to one live <code>layer</code><code> of </code><code>glyph</code> as one Undo step.
read_clipboard function (*, qt: Any=None) -> bytes | None Return object JSON from the clipboard, or <code>None</code> when it holds none.
rename_glyphs function (envelope: Envelope, rename: Mapping[str, str] | Callable[[str], str]) -> Envelope Return a copy whose glyph items carry new names from a mapping or a function.
require_available function (fl: Any=None) -> None Raise <code>RuntimeError(NEEDS&#95;91)</code> on FontLab 9.0 or outside FontLab.
save_file function (path: str | Path, data: Any) -> Path Write object JSON to <code>path</code> as pretty-printed UTF-8; return the path.
set_advance_width function (envelope: Envelope, width: float, layers: Iterable[str] | None=None) -> Envelope Set <code>advanceWidth</code> on glyph layers and layer items (all, or the named ones).
unicodes function (payload: Mapping[str, Any]) -> tuple[int, ...] Return the code points of a glyph payload's <code>unicode</code><code> hex text (</code><code>&quot;006f&quot;</code>).
unique_glyph_names function (envelope: Envelope, existing: Container[str]) -> tuple[Envelope, dict[str, str]] Rename glyph items so no name is in <code>existing</code> or repeated in the data.
unique_name function (name: str, existing: Container[str]) -> str Return <code>name</code><code>, or </code><code>name.1</code><code>, </code><code>name.2</code><code>, ... the first not in </code><code>existing</code>.