Skip to content

Instantly share code, notes, and snippets.

@lindwurm lindwurm/genwebfonts.py forked from taskie/genwebfonts.py
Last active Sep 15, 2018

Embed
What would you like to do?
RobotoのU+EE01が欲しいだけなんです
#!/usr/bin/env fontforge -lang=py -script
# -*- coding: utf-8 -*-
import fontforge
import sys
import os
def _select_codepoint(codepoint, selection, more=True):
more_str = 'more' if more else 'less'
if isinstance(codepoint, tuple) and len(codepoint) == 2:
first, last = codepoint
if isinstance(first, int) and isinstance(last, int):
selection.select((more_str, 'ranges', 'unicode'), first, last)
elif isinstance(first, unicode) and isinstance(last, unicode):
selection.select((more_str, 'ranges', 'unicode'), ord(first), ord(last))
else:
message = 'invalid codepoint type: ' + str(codepoint)
raise Exception(message)
elif isinstance(codepoint, int):
selection.select((more_str, 'unicode'), codepoint)
elif isinstance(codepoint, unicode):
for c in codepoint:
selection.select((more_str, 'unicode'), ord(c))
else:
message = 'invalid codepoint type: ' + str(codepoint)
raise Exception(message)
def _clear_unneeded_glyphs(font, codepoints):
font.selection.all()
for codepoint in codepoints:
_select_codepoint(codepoint, font.selection, more=False)
font.clear()
def generate_webfonts(input, outdir, codepoints):
if not os.path.exists(outdir):
os.mkdir(outdir)
if not os.path.isdir(outdir):
message = 'last argument is not directory: ' + outdir
raise Exception(message)
font = fontforge.open(input)
_clear_unneeded_glyphs(font, codepoints)
filename = os.path.split(input)[-1]
basename, ext = os.path.splitext(filename)
for newext in [ext, '.woff', '.eot']:
output = os.path.join(outdir, basename + newext)
font.generate(output)
font.close()
if __name__ == '__main__':
codepoints = [(0xEE01)]
if len(sys.argv) > 2:
for input in sys.argv[1:-1]:
generate_webfonts(input, sys.argv[-1], codepoints)
else:
print('usage: ' + sys.argv[0] + ' inputs... outdir')
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.