Maximilian Paju
2015-07-16 09:49:14 UTC
Hi,
I recently began working on a project at work that aims towards creating a
service for converting fonts from as many formats as possible to .woff and
subsetting the font glyphs to reduce the file size. I've looked through
some tools that may help with this and I've found FontForge to be quite
versatile. I'm now trying to do some scripting with the python extension.
However, the lack of documentation in some of the code have left me
helpless.
The problem I'm facing at the moment is applying the generate method in the
font-module to actually produce my .woff file. I've successfully subsetted
the font and been able to save it as a .sfd but when I try to generate the
woff the file won't open in FontForge afterwards.
[code]
import fontforge
import sys
import getopt
import os
import struct
def select_with_refs(font, unicode):
font.selection.select(('more', 'unicode'), unicode)
try:
for ref in font[unicode].references:
font.selection.select(('more',), ref[0])
except:
print('Resolving references on u+%04x failed' % unicode)
def main(argv):
optlist, args = getopt.gnu_getopt(argv, '', ['string=', 'strip_names',
'opentype-features', 'simplify', 'new', 'script', 'nmr', 'roundtrip',
'subset=', 'namelist', 'null'])
font_in, font_out = args
unicodes = range(0x20, 0x7f) # Basic latin
flags = ()
oldfont = fontforge.open(argv[0])
for i in unicodes:
select_with_refs(oldfont, i)
oldfont.copy()
newfont = fontforge.font()
newfont.sfnt_names = oldfont.sfnt_names
newfont.encoding = oldfont.encoding
newfont.em = oldfont.em
newfont.layers['Fore'].is_quadratic = oldfont.layers['Fore'].is_quadratic
for i in unicodes:
select_with_refs(newfont, i)
newfont.paste()
newfont.fontname="NewFont"
newfont.save(argv[1] + ".sfd")
newfont.generate(argv[1] + ".woff", flags = flags)
if __name__ == '__main__':
main(sys.argv[1:])
[/code]
Any help would be most appreciated!
I recently began working on a project at work that aims towards creating a
service for converting fonts from as many formats as possible to .woff and
subsetting the font glyphs to reduce the file size. I've looked through
some tools that may help with this and I've found FontForge to be quite
versatile. I'm now trying to do some scripting with the python extension.
However, the lack of documentation in some of the code have left me
helpless.
The problem I'm facing at the moment is applying the generate method in the
font-module to actually produce my .woff file. I've successfully subsetted
the font and been able to save it as a .sfd but when I try to generate the
woff the file won't open in FontForge afterwards.
[code]
import fontforge
import sys
import getopt
import os
import struct
def select_with_refs(font, unicode):
font.selection.select(('more', 'unicode'), unicode)
try:
for ref in font[unicode].references:
font.selection.select(('more',), ref[0])
except:
print('Resolving references on u+%04x failed' % unicode)
def main(argv):
optlist, args = getopt.gnu_getopt(argv, '', ['string=', 'strip_names',
'opentype-features', 'simplify', 'new', 'script', 'nmr', 'roundtrip',
'subset=', 'namelist', 'null'])
font_in, font_out = args
unicodes = range(0x20, 0x7f) # Basic latin
flags = ()
oldfont = fontforge.open(argv[0])
for i in unicodes:
select_with_refs(oldfont, i)
oldfont.copy()
newfont = fontforge.font()
newfont.sfnt_names = oldfont.sfnt_names
newfont.encoding = oldfont.encoding
newfont.em = oldfont.em
newfont.layers['Fore'].is_quadratic = oldfont.layers['Fore'].is_quadratic
for i in unicodes:
select_with_refs(newfont, i)
newfont.paste()
newfont.fontname="NewFont"
newfont.save(argv[1] + ".sfd")
newfont.generate(argv[1] + ".woff", flags = flags)
if __name__ == '__main__':
main(sys.argv[1:])
[/code]
Any help would be most appreciated!