Created
February 7, 2017 17:01
-
-
Save m4rc1e/2085d909c8cd08dc71ee54bbb1d3ccce to your computer and use it in GitHub Desktop.
fonts/google: Search for incorrect macStyle and fsType font calues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Recrusively traverse a collection of fonts and find incorrect fsType and | |
macStyle values | |
fsType should always be 0x000 | |
macStyle values should map to the MACSTYLE constant | |
""" | |
import sys | |
import os | |
from fontTools.ttLib import TTFont | |
from os.path import basename, normpath | |
FS_TYPE = 0 | |
MACSTYLE = { | |
'Black': 0, | |
'BlackItalic': 2, | |
'Bold': 1, | |
'BoldItalic': 3, | |
'ExtraBold': 0, | |
'ExtraBoldItalic': 2, | |
'ExtraLight': 0, | |
'ExtraLightItalic': 2, | |
'Italic': 2, | |
'Light': 0, | |
'LightItalic': 2, | |
'Medium': 0, | |
'MediumItalic': 2, | |
'Regular': 0, | |
'SemiBold': 0, | |
'SemiBoldItalic': 2, | |
'Thin': 0, | |
'ThinItalic': 2, | |
} | |
def increment_font_version(font): | |
pass | |
def get_bad_fstype(fonts_folder, fix=False): | |
"""return font families which have fsTypes which are not set to 0""" | |
bad_fstype = [] | |
for path, r, files in os.walk(fonts_folder): | |
for file in files: | |
if file.endswith('.ttf'): | |
font_path = os.path.join(path, file) | |
font = TTFont(font_path) | |
if int(font['OS/2'].fsType) != FS_TYPE: | |
bad_fstype.append(basename(normpath(path))) | |
if fix: | |
font['OS/2'].fsType = 0 | |
font.save() | |
return set(bad_fstype) | |
def get_bad_mac_style(fonts_folder, fix=False): | |
"""Check if mac style bit matches font style""" | |
bad_mac_style = [] | |
for path, r, files in os.walk(fonts_folder): | |
for file in files: | |
if file.endswith('.ttf'): | |
font_path = os.path.join(path, file) | |
font = TTFont(font_path) | |
style = file.split('-')[-1][:-4] | |
try: | |
if MACSTYLE[style] != int(font['head'].macStyle): | |
bad_mac_style.append(basename(normpath(path))) | |
if fix: | |
font['head'].macStyle = MACSTYLE[style] | |
fonts.save() | |
except: | |
all | |
return set(bad_mac_style) | |
def main(fonts_folder): | |
bad_fstype = get_bad_fstype(fonts_folder) | |
bad_mac_style = get_bad_mac_style(fonts_folder) | |
print 'Bad FsType families: \n%s' % '\n'.join(bad_fstype) | |
print 'Bad Mac Style families: \n%s' % '\n'.join(bad_mac_style) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
print 'Please provide a directory containing a collection of fonts' | |
else: | |
main(sys.argv[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment