Skip to content

Commit

Permalink
Fontlab node and axis fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Mar 21, 2024
1 parent 76e1703 commit 55d5b29
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/babelfont/convertors/fontlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ def _load(self):
if "defaultMaster" in self.fontlab:
default = self.font.master(self.fontlab["defaultMaster"])
if self.font.axes:
def_loc = self.font.map_backward(default.location)
def_loc = default.location
for axis in self.font.axes:
if axis.tag in default.location:
axis.default = def_loc[axis.tag]
assert self.font.default_master
for g in self.fontlab.get("glyphs", []):
glyph = self._load_thing(g, self.glyph_loader)
# Fontlab allows glyphs to elide layers with no shapes
# so we need to ensure there is a layer for each master
layer_ids = [l.id for l in glyph.layers]
for master in self.font.masters:
if master.id not in layer_ids:
layer = Layer(
name=master.id,
_master=master.id,
)
layer._font = self.font
glyph.layers.append(layer)

self.font.glyphs.append(glyph)
for cls in self.fontlab.get("classes", []):
self._load_class(cls)
Expand Down Expand Up @@ -105,6 +117,12 @@ def _convert_shapes(self, flshapes):
contour = Shape(nodes=[])
for n in flcontour["nodes"]:
contour.nodes.extend(self._load_node(n))
if (
contour.nodes[0].type == "ls"
and contour.nodes[-1].type[0] == "q"
):
contour.nodes[0].type = "cs"
contour.nodes[-1].type = "o"
shapes.append(contour)
return shapes

Expand Down Expand Up @@ -137,14 +155,12 @@ def _load_node(self, input_):
ntyp = "l"
elif len(stuff) == 3 and ix == 2:
ntyp = "c"
if m[3]:
ntyp = ntyp + "s"
elif len(stuff) == 2 and ix == 1:
ntyp = "q"
if m[3]:
ntyp = ntyp + "s"
else:
ntyp = "o"
if m[3]:
ntyp = ntyp + "s"
rv.append(Node(x=float(m[1]), y=float(m[2]), type=ntyp))
return rv

Expand All @@ -160,7 +176,10 @@ def _load_node(self, input_):
"kerning": ("kerning", _load_kerning),
"location": (
"location",
lambda s, x: {s.axis_name_map[k].tag: v for k, v in x.items()},
lambda s, x: {
s.axis_name_map[k].tag: s.axis_name_map[k].map_backward(v)
for k, v in x.items()
},
),
},
)
Expand Down

0 comments on commit 55d5b29

Please sign in to comment.