Skip to content

Commit

Permalink
bracket layers
Browse files Browse the repository at this point in the history
  • Loading branch information
schriftgestalt committed Dec 21, 2023
1 parent 3cbf81e commit eabc45a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
53 changes: 26 additions & 27 deletions Lib/glyphsLib/builder/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,33 +568,32 @@ def font(self):
# TODO: Georg to implement
# Use self.designspace.rules

# base_glyph, location = m.groups()
# layer_name = bracket_glyph.lib.get(
# GLYPHLIB_PREFIX + "_originalLayerName"
# )
# if layer_name is None:
# # Determine layer name from location
# raise NotImplementedError
# # _originalLayerName is an empty string for 'implicit' bracket layers;
# # we don't import these since they were copies of master layers.
# if layer_name:
# if layer_name not in source.font.layers:
# ufo_layer = source.font.newLayer(layer_name)
# else:
# ufo_layer = source.font.layers[layer_name]
# bracket_glyph_new = ufo_layer.newGlyph(base_glyph)
# bracket_glyph_new.copyDataFromGlyph(bracket_glyph)

# # strip '*.BRACKET.123' suffix from the components' glyph names
# for comp in bracket_glyph_new.components:
# m = BRACKET_GLYPH_RE.match(comp.baseGlyph)
# if m:
# comp.baseGlyph = m.group("glyph_name")

# # Remove all freestanding bracket layer glyphs from all layers.
# for layer in source.font.layers:
# if glyph_name in layer:
# del layer[glyph_name]
base_glyph, location = m.groups()
layer_name = bracket_glyph.lib.get(
GLYPHLIB_PREFIX + "_originalLayerName"
)
if layer_name is None:
# Determine layer name from location
raise NotImplementedError
# _originalLayerName is an empty string for 'implicit' bracket layers;
# we don't import these since they were copies of master layers.
if layer_name not in source.font.layers:
ufo_layer = source.font.newLayer(layer_name)
else:
ufo_layer = source.font.layers[layer_name]
bracket_glyph_new = ufo_layer.newGlyph(base_glyph)
bracket_glyph_new.copyDataFromGlyph(bracket_glyph)

# strip '*.BRACKET.123' suffix from the components' glyph names
for comp in bracket_glyph_new.components:
m = BRACKET_GLYPH_RE.match(comp.baseGlyph)
if m:
comp.baseGlyph = m.group("glyph_name")

# Remove all freestanding bracket layer glyphs from all layers.
for layer in source.font.layers:
if glyph_name in layer:
del layer[glyph_name]

for layer in _sorted_backgrounds_last(source.font.layers):
self.to_glyphs_layer_lib(layer, master)
Expand Down
23 changes: 16 additions & 7 deletions Lib/glyphsLib/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,17 @@ def __repr__(self):
def __eq__(self, other):
return self.name == other.name and self.axisTag == other.axisTag

@property
def shortAxisTag(self):
shortAxisTagMapping = {
"ital": "it",
"opsz": "oz",
"slnt": "sl",
"wdth": "wd",
"wght": "wg",
}
return shortAxisTagMapping.get(self.axisTag, self.axisTag)


GSAxis._add_parsers([
{"plist_name": "tag", "object_name": "axisTag"},
Expand Down Expand Up @@ -4886,6 +4897,10 @@ def master(self):
master = self.parent.parent.masterForId(self.associatedMasterId)
return master

@property
def font(self):
return self.parent.parent

def _name_from_attributes(self):
# For Glyphs 3's speciall layers (brace, bracket, color) we must generate the
# name from the attributes (as it's shown in Glyphs.app UI) and discard
Expand Down Expand Up @@ -4920,8 +4935,6 @@ def _brace_layer_name(self):
return f"{{{', '.join(floatToString5(v) for v in coordinates.values())}}}"

def _bracket_layer_name(self):
if not self.isBraceLayer:
return None

axisRules = self.attributes[LAYER_ATTRIBUTE_AXIS_RULES]
if not axisRules or not isinstance(axisRules, dict):
Expand All @@ -4945,11 +4958,7 @@ def _bracket_layer_name(self):

@property
def name(self):
if (
self.associatedMasterId
and self.associatedMasterId == self.layerId
and self.parent
):
if self.isMasterLayer:
master = self.parent.parent.masterForId(self.associatedMasterId)
if master:
return master.name
Expand Down
21 changes: 4 additions & 17 deletions tests/builder/designspace_gen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,24 +307,11 @@ def test_designspace_generation_bracket_roundtrip(datadir, ufo_module):
font_rt = to_glyphs(designspace)
assert "x" in font_rt.glyphs
g1 = font_rt.glyphs["x"]
assert len(g1.layers) == 12 and {l.name for l in g1.layers} == {
"[300]",
"[600]",
"Bold",
"Bold Condensed",
"Light Condensed",
"Light",
"Other [600]",
"Something [300]",
}
assert len(g1.layers) == 12
assert {l.name for l in g1.layers} == {'Light', 'Bold', 'Light Condensed', 'Bold Condensed', '[300‹wg]', '[600‹wg]'}
g2 = font_rt.glyphs["a"]
assert len(g2.layers) == 8 and {l.name for l in g2.layers} == {
"[300]",
"Bold",
"Bold Condensed",
"Light Condensed",
"Light",
}
assert len(g2.layers) == 8
assert {l.name for l in g2.layers} == {'Light', 'Bold', 'Light Condensed', 'Bold Condensed', '[300‹wg]'}
assert "a.BRACKET.300" not in font_rt.glyphs
assert "x.BRACKET.300" not in font_rt.glyphs
assert "x.BRACKET.600" not in font_rt.glyphs
Expand Down
16 changes: 15 additions & 1 deletion tests/classes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@
OFFCURVE,
)
from glyphsLib.types import Point, Transform, Rect

from glyphsLib.classes import LAYER_ATTRIBUTE_AXIS_RULES

TESTFILE_PATHV2 = os.path.join(
os.path.dirname(__file__), os.path.join("data", "GlyphsUnitTestSans2.glyphs")
)
TESTFILE_PATHV3 = os.path.join(
os.path.dirname(__file__), os.path.join("data", "GlyphsUnitTestSans3.glyphs")
)
TESTFILE_BRACKETV2 = os.path.join(
os.path.dirname(__file__), os.path.join("data", "BracketTestFont.glyphs")
)


def generate_minimal_font(formatVersion=2):
Expand Down Expand Up @@ -161,6 +164,17 @@ def test_append_layer_same_id(self):
assert len({m.id for m in font.masters}) == 2


class GlyphsBracketLayerTest(unittest.TestCase):
def test_read_glyphs_2(self):
font = GSFont(TESTFILE_BRACKETV2)
glyph = font.glyphs["a"]
for layer in glyph.layers:
if layer.isMasterLayer:
continue
self.assertEqual(layer.attributes[LAYER_ATTRIBUTE_AXIS_RULES], {"a01": {"min": 300}})
self.assertEqual(layer.name, "[300‹wg]")


class GSFontTest(unittest.TestCase):
def test_init(self):
font = GSFont()
Expand Down

0 comments on commit eabc45a

Please sign in to comment.