Browse Source

Merge pull request #131 from m4rc1e/bits

build_name_table: set bits as well
Marc Foley 1 year ago
parent
commit
6e33403b7f
2 changed files with 40 additions and 2 deletions
  1. 20 2
      Lib/axisregistry/__init__.py
  2. 20 0
      tests/test_names.py

+ 20 - 2
Lib/axisregistry/__init__.py

@@ -250,13 +250,31 @@ def build_stat(ttFont, sibling_ttFonts=[]):
 
 
 def build_name_table(ttFont, family_name=None, style_name=None, siblings=[]):
+    from fontTools.varLib.instancer import setRibbiBits
+
     log.info("Building name table")
     name_table = ttFont["name"]
     family_name = family_name if family_name else name_table.getBestFamilyName()
     style_name = style_name if style_name else name_table.getBestSubFamilyName()
     if is_variable(ttFont):
-        return build_vf_name_table(ttFont, family_name, siblings=siblings)
-    return build_static_name_table_v1(ttFont, family_name, style_name)
+        build_vf_name_table(ttFont, family_name, siblings=siblings)
+    else:
+        build_static_name_table_v1(ttFont, family_name, style_name)
+
+    # Set bits
+    style_name = name_table.getBestSubFamilyName()
+    # usWeightClass
+    weight_seen = False
+    for weight in sorted(GF_STATIC_STYLES, key=lambda k: len(k), reverse=True):
+        if weight in style_name:
+            weight_seen = True
+            ttFont["OS/2"].usWeightClass = GF_STATIC_STYLES[weight]
+            break
+    if not weight_seen:
+        log.warning(
+            f"No known weight found for stylename {style_name}. Cannot set OS2.usWeightClass"
+        )
+    setRibbiBits(ttFont)
 
 
 def _fvar_instance_collisions(ttFont, siblings=[]):

+ 20 - 0
tests/test_names.py

@@ -525,3 +525,23 @@ def test_build_variations_ps_name(fp, result):
     build_variations_ps_name(ttFont)
     variation_ps_name = ttFont["name"].getName(25, 3, 1, 0x409).toUnicode()
     assert variation_ps_name == result
+
+
+@pytest.mark.parametrize(
+    "fp, style_name, result",
+    [
+        (mavenpro_fp, "Regular", 400),
+        (mavenpro_fp, "Italic", 400),
+        (mavenpro_fp, "Black Italic", 900),
+        (mavenpro_fp, "ExtraBold Italic", 800),
+        (mavenpro_fp, "ExtraBold", 800),
+        (mavenpro_fp, "Bold", 700),
+        (mavenpro_fp, "Bold Italic", 700),
+        (mavenpro_fp, "Thin Italic", 100),
+        (mavenpro_fp, "Thin", 100),
+    ],
+)
+def test_us_weight_class(fp, style_name, result):
+    ttFont = TTFont(fp)
+    build_name_table(ttFont, style_name=style_name)
+    assert ttFont["OS/2"].usWeightClass == result