| 1 |
from datetime import datetime |
| 2 |
from babelfont import Glyph, Layer, Node, Shape, Master, Guide, Anchor, Axis, Instance |
| 3 |
from babelfont.convertors import BaseConvertor |
| 4 |
from pathlib import Path |
| 5 |
import orjson |
| 6 |
import os |
| 7 |
|
| 8 |
|
| 9 |
# One would hope this would be easy. |
| 10 |
|
| 11 |
|
| 12 |
class Babelfont(BaseConvertor): |
| 13 |
suffix = ".babelfont" |
| 14 |
|
| 15 |
def _load_file(self, filename): |
| 16 |
contents = open(os.path.join(self.filename, filename), "r").read() |
| 17 |
return orjson.loads(contents) |
| 18 |
|
| 19 |
def _load(self): |
| 20 |
names = self._load_file("names.json") |
| 21 |
info = self._load_file("info.json") |
| 22 |
glyphs = self._load_file("glyphs.json") |
| 23 |
self.font._formatspecific = info["_"] |
| 24 |
for k, v in names.items(): |
| 25 |
if k in self.font.names.__dataclass_fields__: |
| 26 |
getattr(self.font.names, k).copy_in(v) |
| 27 |
elif k == "_": |
| 28 |
self.font.names._formatspecific = v |
| 29 |
|
| 30 |
self.font.axes = [Axis(**j) for j in info.get("axes", [])] |
| 31 |
self.font.instances = [Instance(**j) for j in info.get("instances", [])] |
| 32 |
|
| 33 |
self._load_masters(info.get("masters", [])) |
| 34 |
|
| 35 |
for g in glyphs: |
| 36 |
glyph = Glyph(**g) |
| 37 |
self.font.glyphs.append(glyph) |
| 38 |
for json_layer in self._load_file(glyph.babelfont_filename): |
| 39 |
layer = self._inflate_layer(json_layer) |
| 40 |
glyph.layers.append(layer) |
| 41 |
|
| 42 |
self._load_metadata(info) |
| 43 |
self._load_features() |
| 44 |
return self.font |
| 45 |
|
| 46 |
def _load_masters(self, masters): |
| 47 |
for json_master in masters: |
| 48 |
if "kerning" in json_master: |
| 49 |
json_master["kerning"] = { |
| 50 |
tuple(k.split("//")): v for k, v in json_master["kerning"].items() |
| 51 |
} |
| 52 |
master = Master(**json_master) |
| 53 |
master.font = self.font |
| 54 |
master.guides = [Guide(**m) for m in master.guides] |
| 55 |
self.font.masters.append(master) |
| 56 |
|
| 57 |
def _inflate_layer(self, json_layer): |
| 58 |
layer = Layer(**json_layer) |
| 59 |
layer.guides = [Guide(**m) for m in layer.guides] |
| 60 |
layer.anchors = [Anchor(**m) for m in layer.anchors] |
| 61 |
layer._font = self.font |
| 62 |
layer.shapes = [self._inflate_shape(layer, s) for s in layer.shapes] |
| 63 |
return layer |
| 64 |
|
| 65 |
def _inflate_shape(self, layer, s): |
| 66 |
shape = Shape(**s) |
| 67 |
if shape.nodes: |
| 68 |
shape.nodes = [self._inflate_node(n) for n in shape.nodes] |
| 69 |
return shape |
| 70 |
|
| 71 |
def _inflate_node(self, n): |
| 72 |
return Node(*n) |
| 73 |
|
| 74 |
def _load_metadata(self, info): |
| 75 |
for k in ["note", "upm", "version", "date", "customOpenTypeValues"]: |
| 76 |
if k in info: |
| 77 |
setattr(self.font, k, info[k]) |
| 78 |
self.font.date = datetime.strptime(self.font.date, "%Y-%m-%d %H:%M:%S") |
| 79 |
|
| 80 |
def _load_features(self): |
| 81 |
path = os.path.join(self.filename, "features.fea") |
| 82 |
if os.path.isfile(path): |
| 83 |
f = open(path, "r") |
| 84 |
self.font.features = f.read() |
| 85 |
|
| 86 |
def _save(self): |
| 87 |
path = Path(self.filename) |
| 88 |
path.mkdir(parents=True, exist_ok=True) |
| 89 |
|
| 90 |
with open(path / "info.json", "wb") as f: |
| 91 |
self.font.write(stream=f) |
| 92 |
|
| 93 |
with open(path / "names.json", "wb") as f: |
| 94 |
self.font._write_value(f, "glyphs", self.font.names) |
| 95 |
|
| 96 |
with open(path / "features.fea", "w") as f: |
| 97 |
if self.font.features: |
| 98 |
f.write(self.font.features.to_fea()) |
| 99 |
|
| 100 |
with open(path / "glyphs.json", "wb") as f: |
| 101 |
for g in self.font.glyphs: |
| 102 |
glyphpath = path / "glyphs" |
| 103 |
glyphpath.mkdir(parents=True, exist_ok=True) |
| 104 |
with open(path / g.babelfont_filename, "wb") as f2: |
| 105 |
g._write_value(f2, "layers", g.layers) |
| 106 |
self.font._write_value(f, "glyphs", self.font.glyphs) |