Repository: googlefonts/glyphsLib
Pull request #659 (2021-01-27 10:31:09)

check that vert{Width,Origin} are not None, can be 0
Fixes https://github.com/googlefonts/glyphsLib/issues/557#issuecomment-768020307


Patch:
diff --git a/Lib/glyphsLib/builder/font.py b/Lib/glyphsLib/builder/font.py
index 70183ea6..4d36663f 100644
--- a/Lib/glyphsLib/builder/font.py
+++ b/Lib/glyphsLib/builder/font.py
@@ -122,7 +122,8 @@ def _set_glyphs_font_attributes(self, source):
         # FIXME: (jany) should wrap in glyphs_datetime? or maybe the GSFont
         #     should wrap in glyphs_datetime if needed?
         font.date = from_ufo_time(info.openTypeHeadCreated)
-    font.upm = info.unitsPerEm
+    if info.unitsPerEm is not None:
+        font.upm = info.unitsPerEm
     if info.versionMajor is not None:
         font.versionMajor = info.versionMajor
     if info.versionMinor is not None:
diff --git a/Lib/glyphsLib/builder/glyph.py b/Lib/glyphsLib/builder/glyph.py
index 7e3da876..44a9db9a 100644
--- a/Lib/glyphsLib/builder/glyph.py
+++ b/Lib/glyphsLib/builder/glyph.py
@@ -277,12 +277,12 @@ def to_ufo_glyph_height_and_vertical_origin(self, ufo_glyph, layer):
 
     ascender, descender = _get_typo_ascender_descender(layer.master)
 
-    if layer.vertWidth:
+    if layer.vertWidth is not None:
         ufo_glyph.height = layer.vertWidth
     else:
         ufo_glyph.height = ascender - descender
 
-    if layer.vertOrigin:
+    if layer.vertOrigin is not None:
         ufo_glyph.verticalOrigin = ascender - layer.vertOrigin
     else:
         ufo_glyph.verticalOrigin = ascender
@@ -339,9 +339,9 @@ def _to_glyphs_color(color):
 
 
 def to_glyphs_glyph_height_and_vertical_origin(self, ufo_glyph, master, layer):
-    if ufo_glyph.height:
+    ascender, descender = _get_typo_ascender_descender(master)
+    if ufo_glyph.height != (ascender - descender):
         layer.vertWidth = ufo_glyph.height
 
-    if ufo_glyph.verticalOrigin:
-        ascender, _ = _get_typo_ascender_descender(master)
+    if ufo_glyph.verticalOrigin is not None and ufo_glyph.verticalOrigin != ascender:
         layer.vertOrigin = ascender - ufo_glyph.verticalOrigin
diff --git a/Lib/glyphsLib/builder/masters.py b/Lib/glyphsLib/builder/masters.py
index 24008d63..f0014ae6 100644
--- a/Lib/glyphsLib/builder/masters.py
+++ b/Lib/glyphsLib/builder/masters.py
@@ -118,10 +118,14 @@ def to_glyphs_master_attributes(self, source, master):
         # relative to anything.
         master.customParameters[UFO_FILENAME_CUSTOM_PARAM] = os.path.basename(ufo.path)
 
-    master.ascender = ufo.info.ascender
-    master.capHeight = ufo.info.capHeight
-    master.descender = ufo.info.descender
-    master.xHeight = ufo.info.xHeight
+    if ufo.info.ascender is not None:
+        master.ascender = ufo.info.ascender
+    if ufo.info.capHeight is not None:
+        master.capHeight = ufo.info.capHeight
+    if ufo.info.descender is not None:
+        master.descender = ufo.info.descender
+    if ufo.info.xHeight is not None:
+        master.xHeight = ufo.info.xHeight
 
     horizontal_stems = ufo.info.postscriptStemSnapH
     vertical_stems = ufo.info.postscriptStemSnapV


Test patch:
diff --git a/tests/builder/to_glyphs_test.py b/tests/builder/to_glyphs_test.py
index 0fc5b946..529599be 100644
--- a/tests/builder/to_glyphs_test.py
+++ b/tests/builder/to_glyphs_test.py
@@ -261,7 +261,10 @@ def test_glyph_height_and_vertical_origin(ufo_module):
     a = ufo.newGlyph("a")
     a.height = 1024
     a.verticalOrigin = 100
-    ufo.newGlyph("b")
+    b = ufo.newGlyph("b")
+    b.height = ufo.info.ascender - ufo.info.descender  # Glyphs default vertWidth
+    c = ufo.newGlyph("c")
+    assert c.height == 0  # defcon default glyph.height == 0
 
     font = to_glyphs([ufo])
 
@@ -269,13 +272,17 @@ def test_glyph_height_and_vertical_origin(ufo_module):
     assert font.glyphs["a"].layers[0].vertOrigin == 900
     assert font.glyphs["b"].layers[0].vertWidth is None
     assert font.glyphs["b"].layers[0].vertOrigin is None
+    assert font.glyphs["c"].layers[0].vertWidth == 0
+    assert font.glyphs["c"].layers[0].vertOrigin is None
 
     (ufo,) = to_ufos(font)
 
     assert ufo["a"].height == font.glyphs["a"].layers[0].vertWidth
     assert ufo["a"].verticalOrigin == 100
     assert ufo["b"].height == (ufo.info.ascender - ufo.info.descender)
-    assert ufo["b"].verticalOrigin == 1000
+    assert ufo["b"].verticalOrigin == ufo.info.ascender
+    assert ufo["c"].height == 0
+    assert ufo["c"].verticalOrigin == ufo.info.ascender
 
 
 def test_glyph_height_and_vertical_origin_trigger_vhea_metrics(ufo_module):