- Clean up

- Prepare .glyphs file to add back bracket glyphs since fontmake now supports another feature swapping a feature variation
This commit is contained in:
weiweihuanghuang 2020-02-24 16:59:18 +11:00
parent dcd044c29b
commit cb7fa7c60a
5 changed files with 79 additions and 5553 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3
""" """
Fontmake can only generate a single variable font. It cannot generate a Fontmake can only generate a single variable font. It cannot generate a
family of variable fonts, that are related to one another. family of variable fonts, that are related to one another.
@ -21,11 +22,13 @@ from argparse import ArgumentParser
from fontTools.ttLib import TTFont, newTable from fontTools.ttLib import TTFont, newTable
from fontTools.ttLib.tables import otTables from fontTools.ttLib.tables import otTables
import os import os
import sys
if sys.version_info.major == 3:
unicode = str
OS_2_WEIGHT_CLASS = { OS_2_WEIGHT_CLASS = {
'Thin': 250, 'Thin': 100,
'ExtraLight': 275, 'ExtraLight': 200,
'Light': 300, 'Light': 300,
'Regular': 400, 'Regular': 400,
'': 400, '': 400,
@ -94,43 +97,6 @@ def fonts_are_same_family(ttfonts):
return True return True
def fix_nametable(ttfont):
"""Cleanup nametable issues caused by fontmake"""
table = ttfont['name']
family_name = table.getName(1, 3, 1, 1033).toUnicode()
# Remove style from family name. Often happens for Italic fonts
# Family Name Light --> Family Name
for style in (' Regular', ' Light', ' Bold'):
if style in family_name:
table.setName(family_name.replace(style, ''), 1, 3, 1, 1033)
# Remove preferred family name and prefered style
idx = 0
while idx < len(table.names) - 1:
if table.names[idx].nameID in [16, 17]:
table.names.pop(idx)
idx = 0
else:
idx += 1
# Update existing nameids based on the varfont's default style
default_style = _get_vf_default_style(ttfont)
family_name = table.getName(1, 3, 1, 1033).toUnicode()
table.setName(default_style, 2, 3, 1, 1033)
fullname = '{} {}'.format(family_name, default_style)
table.setName(unicode(fullname), 4, 3, 1, 1033)
psname = '{}-{}'.format(family_name.replace(' ', ''), default_style.replace(' ', ''))
table.setName(unicode(psname), 6, 3, 1, 1033)
# uniqueid basedon fontmake output version;vendorid;psname
font_version = format(ttfont['head'].fontRevision, '.3f')
vendor = ttfont['OS/2'].achVendID
uniqueid = '{};{};{}'.format(font_version, vendor, psname)
table.setName(unicode(uniqueid), 3, 3, 1, 1033)
def fix_bits(ttfont): def fix_bits(ttfont):
"""Set fsSelection, macStyle and usWeightClass to correct values. """Set fsSelection, macStyle and usWeightClass to correct values.
@ -173,7 +139,7 @@ def create_stat_table(ttfont):
stat.table.AxisValueArray.AxisValue = [] stat.table.AxisValueArray.AxisValue = []
for idx, instance in enumerate(ttfont['fvar'].instances): for idx, instance in enumerate(ttfont['fvar'].instances):
append_stat_record(stat, 0, instance.coordinates.values()[0], instance.subfamilyNameID) append_stat_record(stat, 0, list(instance.coordinates.values())[0], instance.subfamilyNameID)
# Set ElidedFallbackNameID # Set ElidedFallbackNameID
stat.table.ElidedFallbackNameID = 2 stat.table.ElidedFallbackNameID = 2
@ -200,7 +166,7 @@ def _get_vf_default_style(ttfont):
for inst in ttfont['fvar'].instances: for inst in ttfont['fvar'].instances:
if inst.coordinates['wght'] == default_fvar_val: if inst.coordinates['wght'] == default_fvar_val:
name_id = inst.subfamilyNameID name_id = inst.subfamilyNameID
return ttfont['name'].getName(name_id, 1, 0, 0).toUnicode() return ttfont['name'].getName(name_id, 3, 1, 1033).toUnicode()
def add_other_vf_styles_to_nametable(ttfont, text_records): def add_other_vf_styles_to_nametable(ttfont, text_records):
@ -316,12 +282,12 @@ def main():
ttfonts = [TTFont(p) for p in font_paths] ttfonts = [TTFont(p) for p in font_paths]
if not fonts_are_same_family(ttfonts): if not fonts_are_same_family(ttfonts):
raise Exception('Fonts have different family_names: [{}]'.format( raise Exception('Fonts have different family_names: [{}]'.format(
', '.join(map(os.path.basename, ttfonts)) ', '.join(map(os.path.basename, font_paths))
)) ))
map(fix_nametable, ttfonts) for ttfont in ttfonts:
map(fix_bits, ttfonts) fix_bits(ttfont)
map(create_stat_table, ttfonts) create_stat_table(ttfont)
harmonize_vf_families(ttfonts) harmonize_vf_families(ttfonts)
for path, ttfont in zip(font_paths, ttfonts): for path, ttfont in zip(font_paths, ttfonts):

View File

@ -9,7 +9,7 @@ from glyphsLib import GSGlyph
file = sys.argv[1] file = sys.argv[1]
font = GSFont(file) font = GSFont(file)
print "\tPreparing %s" % file print("\tPreparing %s" % file)
# List of Glyphs which should have export enabled - in the Glyphs App static fonts export these are not active but get swapped with a custom parameter, for the VFs though they needs to be exported # List of Glyphs which should have export enabled - in the Glyphs App static fonts export these are not active but get swapped with a custom parameter, for the VFs though they needs to be exported
italicBracketGlyphs = [ italicBracketGlyphs = [

View File

@ -8,7 +8,7 @@ from glyphsLib import GSLayer
file = sys.argv[1] file = sys.argv[1]
font = GSFont(file) font = GSFont(file)
print "\tPreparing %s" % file print("\tPreparing %s" % file)
# Append Italic to font family naame if Italics # Append Italic to font family naame if Italics
style = sys.argv[2] style = sys.argv[2]
@ -33,7 +33,7 @@ for eachGlyph in font.glyphs:
if re.match('.*\d\}$', eachLayer.name): if re.match('.*\d\}$', eachLayer.name):
listOfBraceGlyphs += [eachGlyph.name] listOfBraceGlyphs += [eachGlyph.name]
print "\tBrace glyphs found: ", listOfBraceGlyphs print("\tBrace glyphs found: ", listOfBraceGlyphs)
listOfBraceGlyphs += ["space", ".notdef"] listOfBraceGlyphs += ["space", ".notdef"]
listOfGlyphsNotToExport = [g.name for g in font.glyphs if g.name not in listOfBraceGlyphs] listOfGlyphsNotToExport = [g.name for g in font.glyphs if g.name not in listOfBraceGlyphs]