Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "Link Metrics..." custom parameters #641

Merged
merged 11 commits into from
Apr 1, 2021
48 changes: 47 additions & 1 deletion Lib/glyphsLib/builder/glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,53 @@ def to_ufo_glyph(self, ufo_glyph, layer, glyph): # noqa: C901
ufo_glyph.lib[GLYPHLIB_PREFIX + "subCategory"] = subCategory

# load width before background, which is loaded with lib data
width = layer.width

# The width may be taken from another master via the customParameters
# 'Link Metrics With Master' or 'Link Metrics With First Master'.
master = self.font.masters[layer.associatedMasterId or layer.layerId]
source_master_id = master.customParameters[
"Link Metrics With Master"
]
if source_master_id is None:
user_first_master = master.customParameters[
"Link Metrics With First Master"
]
if user_first_master == 1:
width = self.font.glyphs[glyph.name].layers[0].width
if layer.width != width:
logger.info(
f"{layer.parent.name}: Applying width from first master: "
f"{layer.width} -> {width}"
)
else:
width = layer.width
else:
source_layer = self.font.glyphs[glyph.name].layers[source_master_id]
if source_layer is None:
# Try by name instead of master ID
found = False
for master in self.font.masters:
if master.name == source_master_id:
found = True
source_layer = self.font.glyphs[glyph.name].layers[master.id]
break
else:
found = True
if found:
simoncozens marked this conversation as resolved.
Show resolved Hide resolved
width = source_layer.width
else:
logger.debug(
simoncozens marked this conversation as resolved.
Show resolved Hide resolved
f"{layer.parent.name}: Trying to apply width from master "
f"'{source_master_id}': master not found."
)
# Use the current layer width
width = layer.width
if layer.width != width:
logger.info(
simoncozens marked this conversation as resolved.
Show resolved Hide resolved
f"{layer.parent.name}: Applying width from master "
f"'{source_master_id}': {layer.width} -> {width}"
)

if width is None:
pass
elif category == "Mark" and subCategory == "Nonspacing" and width > 0:
Expand Down
42 changes: 40 additions & 2 deletions Lib/glyphsLib/builder/kerning.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,50 @@

import re

from copy import deepcopy

UFO_KERN_GROUP_PATTERN = re.compile("^public\\.kern([12])\\.(.*)$")


def to_ufo_kerning(self):
for master_id, kerning in self.font.kerning.items():
_to_ufo_kerning(self, self._sources[master_id].font, kerning)
# Check if the kerning should be copied from another master
kerning_source_masters = {}

for master in self.font.masters:
master_id = master.id
master = self.font.masters[master_id]
source_master_id = master.customParameters[
"Link Metrics With Master"
]
if source_master_id is None:
source_master_id = master.customParameters[
"Link Metrics With First Master"
]
if source_master_id == 1:
kerning_source_masters[master_id] = self.font.masters[0].id
else:
source_master = self.font.masters[source_master_id]
if source_master is None:
# Try by name instead of master ID
found = False
for source_master in self.font.masters:
if source_master.name == source_master_id:
found = True
source_master_id = source_master.id
break
else:
found = True
if found:
kerning_source_masters[master_id] = source_master_id
else:
print(f"Source master for kerning not found: '{source_master_id}'")
simoncozens marked this conversation as resolved.
Show resolved Hide resolved

for master in self.font.masters:
master_id = master.id
source_master_id = kerning_source_masters.get(master_id, master_id)
if source_master_id in self.font.kerning:
kerning = deepcopy(self.font.kerning[source_master_id])
_to_ufo_kerning(self, self._sources[master_id].font, kerning)


def _to_ufo_kerning(self, ufo, kerning_data):
Expand Down
Loading