| 1 | import sys |
| 2 | from fontTools.ttLib import TTFont |
| 3 | from fontTools.varLib import instancer |
| 4 | SOURCE = sys.argv[1] |
| 5 | STYLES = [("Regular", 400, 0, False), ("SemiBold", 600, 0, False), ("Bold", 700, 0, False), ("Italic", 400, -10, True)] |
| 6 | for style, weight, slant, italic in STYLES: |
| 7 | font = TTFont(SOURCE) |
| 8 | pinned = instancer.instantiateVariableFont(font, {"wght": weight, "wdth": 100, "opsz": 14, "GRAD": 0, "ROND": 100, "slnt": slant}) |
| 9 | name = pinned["name"] |
| 10 | family = "Google Sans Flex" |
| 11 | if weight == 600: |
| 12 | family_legacy, sub_legacy = "Google Sans Flex SemiBold", "Regular" |
| 13 | elif weight == 700: |
| 14 | family_legacy, sub_legacy = family, "Bold" |
| 15 | elif italic: |
| 16 | family_legacy, sub_legacy = family, "Italic" |
| 17 | else: |
| 18 | family_legacy, sub_legacy = family, "Regular" |
| 19 | full = f"{family} {style}" if style != "Regular" else family |
| 20 | for record in list(name.names): |
| 21 | if record.nameID in (1, 2, 3, 4, 6, 16, 17, 25): |
| 22 | name.removeNames(nameID=record.nameID) |
| 23 | for platform, encoding, language in ((3, 1, 0x409), (1, 0, 0)): |
| 24 | name.setName(family_legacy, 1, platform, encoding, language) |
| 25 | name.setName(sub_legacy, 2, platform, encoding, language) |
| 26 | name.setName(f"{family}-{style}", 3, platform, encoding, language) |
| 27 | name.setName(full, 4, platform, encoding, language) |
| 28 | name.setName(f"GoogleSansFlex-{style}", 6, platform, encoding, language) |
| 29 | name.setName(family, 16, platform, encoding, language) |
| 30 | name.setName(style, 17, platform, encoding, language) |
| 31 | pinned["OS/2"].usWeightClass = weight |
| 32 | pinned["OS/2"].fsSelection = (pinned["OS/2"].fsSelection & ~0b1100001) | (1 if italic else 0) << 0 | (0x20 if weight == 700 else 0x40 if not italic and weight == 400 else 0) |
| 33 | pinned["head"].macStyle = (1 if weight == 700 else 0) | (2 if italic else 0) |
| 34 | for table in ("STAT", "MVAR", "HVAR", "avar", "fvar", "gvar"): |
| 35 | if table in pinned: |
| 36 | del pinned[table] |
| 37 | pinned.save(f"{sys.argv[2]}/GoogleSansFlex-{style}.ttf") |
| 38 | print(style, "ok") |