2018-09-03 22:55:49 +03:00
|
|
|
#!/usr/bin/env python
|
2018-09-04 00:19:38 +03:00
|
|
|
from __future__ import print_function, absolute_import
|
2019-10-23 03:00:58 +03:00
|
|
|
import sys
|
|
|
|
import os
|
2018-09-04 00:19:38 +03:00
|
|
|
from os.path import dirname, basename, abspath, relpath, join as pjoin
|
|
|
|
sys.path.append(abspath(pjoin(dirname(__file__), 'tools')))
|
2019-10-23 03:00:58 +03:00
|
|
|
from common import BASEDIR, execproc
|
2019-05-27 21:32:39 +03:00
|
|
|
from collections import OrderedDict
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import glyphsLib
|
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import signal
|
|
|
|
import subprocess
|
2019-10-23 03:00:58 +03:00
|
|
|
|
|
|
|
from fontTools.designspaceLib import DesignSpaceDocument
|
2018-09-03 22:55:49 +03:00
|
|
|
from mutatorMath.ufo.document import DesignSpaceDocumentReader
|
2018-10-01 00:41:45 +03:00
|
|
|
from multiprocessing import Process, Queue
|
2019-10-23 03:00:58 +03:00
|
|
|
from glyphsLib.interpolation import apply_instance_data
|
2018-09-04 05:08:46 +03:00
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
from fontbuildlib import FontBuilder
|
|
|
|
from fontbuildlib.util import mkdirs, loadTTFont
|
|
|
|
from fontbuildlib.info import setFontInfo, updateFontVersion
|
2019-10-24 04:44:33 +03:00
|
|
|
from fontbuildlib.name import setFamilyName, renameStylesGoogleFonts
|
2018-09-04 05:08:46 +03:00
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
log = logging.getLogger(__name__)
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
def sighandler(signum, frame):
|
|
|
|
sys.stdout.write('\n')
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2018-09-09 22:29:33 +03:00
|
|
|
def fatal(msg):
|
|
|
|
print(sys.argv[0] + ': ' + msg, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2019-09-04 06:36:58 +03:00
|
|
|
def collapseFontStyleName(font):
|
|
|
|
# collapse whitespace in style name. i.e. "Semi Bold Italic" -> "SemiBoldItalic"
|
|
|
|
font.info.styleName = re.sub(r'\s', '', font.info.styleName)
|
|
|
|
# update info to have style changes "trickle down" to other metadata
|
|
|
|
setFontInfo(font, font.info.openTypeOS2WeightClass)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
class Main(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-09-04 00:19:38 +03:00
|
|
|
self.tmpdir = pjoin(BASEDIR,'build','tmp')
|
2018-09-09 22:29:33 +03:00
|
|
|
self.quiet = False
|
2018-09-12 19:43:19 +03:00
|
|
|
self.logLevelName = 'WARNING'
|
2018-09-09 22:29:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
def log(self, msg):
|
|
|
|
if not self.quiet:
|
|
|
|
print(msg)
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
def main(self, argv):
|
|
|
|
# make ^C instantly exit program
|
|
|
|
signal.signal(signal.SIGINT, sighandler)
|
|
|
|
|
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
description='',
|
|
|
|
usage='''
|
|
|
|
%(prog)s [options] <command> [<args>]
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
compile Build font files
|
2018-09-09 22:29:33 +03:00
|
|
|
compile-var Build variable font files
|
2018-09-03 22:55:49 +03:00
|
|
|
glyphsync Generate designspace and UFOs from Glyphs file
|
|
|
|
instancegen Generate instance UFOs for designspace
|
2019-10-22 22:27:06 +03:00
|
|
|
checkfont Verify integrity of font files
|
|
|
|
rename Rename fonts
|
2018-09-03 22:55:49 +03:00
|
|
|
'''.strip().replace('\n ', '\n'))
|
|
|
|
|
|
|
|
argparser.add_argument('-v', '--verbose', action='store_true',
|
|
|
|
help='Print more details')
|
|
|
|
|
2018-09-09 22:29:33 +03:00
|
|
|
argparser.add_argument('--debug', action='store_true',
|
|
|
|
help='Print lots of details')
|
|
|
|
|
|
|
|
argparser.add_argument('-q', '--quiet', action='store_true',
|
|
|
|
help='Only print errors')
|
|
|
|
|
|
|
|
argparser.add_argument('-C', metavar='<dir>', dest='chdir',
|
|
|
|
help='Run as if %(prog)s started in <dir> instead of the '+\
|
|
|
|
'current working directory.')
|
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
argparser.add_argument('command', metavar='<command>')
|
|
|
|
|
2018-09-09 22:29:33 +03:00
|
|
|
# search past base arguments
|
|
|
|
i = 1
|
|
|
|
while i < len(argv) and argv[i][0] == '-':
|
|
|
|
i = i + 1
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
# parse CLI arguments
|
|
|
|
args = argparser.parse_args(argv[1:i])
|
2019-10-23 03:00:58 +03:00
|
|
|
|
|
|
|
# log config
|
2018-09-09 22:29:33 +03:00
|
|
|
if args.quiet:
|
|
|
|
self.quiet = True
|
|
|
|
if args.debug:
|
|
|
|
fatal("--quiet and --debug are mutually exclusive arguments")
|
|
|
|
if args.verbose:
|
|
|
|
fatal("--quiet and --verbose are mutually exclusive arguments")
|
|
|
|
elif args.debug:
|
2019-10-23 03:00:58 +03:00
|
|
|
logging.basicConfig(level=logging.DEBUG, format='%(funcName)s: %(message)s')
|
2018-09-10 20:20:35 +03:00
|
|
|
self.logLevelName = 'DEBUG'
|
2018-09-09 22:29:33 +03:00
|
|
|
elif args.verbose:
|
2019-10-23 03:00:58 +03:00
|
|
|
logging.basicConfig(level=logging.INFO, format='%(funcName)s: %(message)s')
|
2018-09-10 20:20:35 +03:00
|
|
|
self.logLevelName = 'INFO'
|
2018-09-03 22:55:49 +03:00
|
|
|
else:
|
2019-10-23 03:00:58 +03:00
|
|
|
logging.basicConfig(level=logging.WARNING, format='%(message)s')
|
2018-09-12 19:43:19 +03:00
|
|
|
self.logLevelName = 'WARNING'
|
2018-09-09 22:29:33 +03:00
|
|
|
|
|
|
|
if args.chdir:
|
|
|
|
os.chdir(args.chdir)
|
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
cmd = 'cmd_' + args.command.replace('-', '_')
|
|
|
|
if not hasattr(self, cmd):
|
2018-09-09 22:29:33 +03:00
|
|
|
fatal('Unrecognized command %s. Try --help' % args.command)
|
|
|
|
getattr(self, cmd)(argv[i:])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cmd_compile_var(self, argv):
|
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
usage='%(prog)s compile-var [-h] [-o <file>] <designspace>',
|
|
|
|
description='Compile variable font file')
|
|
|
|
|
|
|
|
argparser.add_argument('srcfile', metavar='<designspace>',
|
|
|
|
help='Source file (.designspace file)')
|
|
|
|
|
|
|
|
argparser.add_argument('-o', '--output', metavar='<fontfile>',
|
|
|
|
help='Output font file')
|
|
|
|
|
|
|
|
args = argparser.parse_args(argv)
|
|
|
|
|
|
|
|
# decide output filename (or check user-provided name)
|
|
|
|
outfilename = args.output
|
|
|
|
if outfilename is None or outfilename == '':
|
2019-10-23 03:00:58 +03:00
|
|
|
outfilename = pjoin(
|
|
|
|
dirname(args.srcfile),
|
|
|
|
os.path.splitext(basename(args.srcfile))[0] + '.otf'
|
|
|
|
)
|
|
|
|
log.debug('setting --output %r' % outfilename)
|
2018-09-09 22:29:33 +03:00
|
|
|
|
|
|
|
mkdirs(dirname(outfilename))
|
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
FontBuilder().buildVariable(args.srcfile, outfilename)
|
2019-10-22 22:27:06 +03:00
|
|
|
|
2018-09-09 22:29:33 +03:00
|
|
|
self.log("write %s" % outfilename)
|
|
|
|
# Note: we can't run ots-sanitize on the generated file as OTS
|
|
|
|
# currently doesn't support variable fonts.
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cmd_compile(self, argv):
|
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
usage='%(prog)s compile [-h] [-o <file>] <ufo>',
|
|
|
|
description='Compile font files')
|
2018-09-09 22:29:33 +03:00
|
|
|
|
|
|
|
argparser.add_argument('srcfile', metavar='<ufo>',
|
|
|
|
help='Source file (.ufo file)')
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
argparser.add_argument('-o', '--output', metavar='<fontfile>',
|
2019-10-23 03:00:58 +03:00
|
|
|
help='Output font file (.otf or .ttf)')
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
argparser.add_argument('--validate', action='store_true',
|
|
|
|
help='Enable ufoLib validation on reading/writing UFO files')
|
|
|
|
|
|
|
|
args = argparser.parse_args(argv)
|
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
# write an OTF/CFF (or a TTF if false)
|
|
|
|
cff = True
|
2018-09-03 22:55:49 +03:00
|
|
|
|
2018-09-09 22:29:33 +03:00
|
|
|
# decide output filename
|
|
|
|
outfilename = args.output
|
|
|
|
if outfilename is None or outfilename == '':
|
2019-10-23 03:00:58 +03:00
|
|
|
outfilename = pjoin(
|
|
|
|
dirname(args.srcfile),
|
|
|
|
os.path.splitext(basename(args.srcfile))[0] + '.otf'
|
|
|
|
)
|
|
|
|
log.debug('setting --output %r' % outfilename)
|
2018-09-03 22:55:49 +03:00
|
|
|
else:
|
2019-10-23 03:00:58 +03:00
|
|
|
fext = os.path.splitext(outfilename)[1].lower()
|
|
|
|
if fext == ".ttf":
|
|
|
|
cff = False
|
|
|
|
elif fext != ".otf":
|
|
|
|
raise Exception('invalid file format %r (expected ".otf" or ".ttf")' % fext)
|
2018-09-03 22:55:49 +03:00
|
|
|
|
2018-09-09 22:29:33 +03:00
|
|
|
# temp file to write to
|
|
|
|
tmpfilename = pjoin(self.tmpdir, basename(outfilename))
|
2018-09-03 22:55:49 +03:00
|
|
|
mkdirs(self.tmpdir)
|
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
# build OTF or TTF file from UFO
|
|
|
|
FontBuilder().buildStatic(args.srcfile, tmpfilename, cff)
|
2018-09-03 22:55:49 +03:00
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
# Run ots-sanitize on produced OTF/TTF file and write sanitized version to outfilename
|
|
|
|
self._ots_sanitize(tmpfilename, outfilename)
|
2018-09-09 22:29:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
def _ots_sanitize(self, tmpfilename, outfilename):
|
2018-09-03 22:55:49 +03:00
|
|
|
# run through ots-sanitize
|
|
|
|
# for filename in args.output:
|
2018-09-09 22:29:33 +03:00
|
|
|
tmpfile = pjoin(self.tmpdir, tmpfilename)
|
|
|
|
mkdirs(dirname(outfilename))
|
2018-09-03 22:55:49 +03:00
|
|
|
success = True
|
|
|
|
try:
|
2018-09-09 22:29:33 +03:00
|
|
|
self.log("write %s" % outfilename)
|
2018-09-03 22:55:49 +03:00
|
|
|
otssan_res = subprocess.check_output(
|
2018-09-09 22:29:33 +03:00
|
|
|
['ots-sanitize', tmpfile, outfilename],
|
|
|
|
# ['cp', tmpfile, outfilename],
|
2018-09-03 22:55:49 +03:00
|
|
|
shell=False
|
|
|
|
).strip()
|
|
|
|
# Note: ots-sanitize does not exit with an error in many cases where
|
|
|
|
# it fails to sanitize the font.
|
2018-12-28 04:44:26 +03:00
|
|
|
success = str(otssan_res).find('Failed') == -1
|
2018-09-03 22:55:49 +03:00
|
|
|
except:
|
|
|
|
success = False
|
2018-12-28 04:44:26 +03:00
|
|
|
if len(otssan_res) == 0:
|
|
|
|
otssan_res = 'error'
|
2019-07-19 00:04:29 +03:00
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
if success:
|
|
|
|
os.unlink(tmpfile)
|
|
|
|
else:
|
2018-09-09 22:29:33 +03:00
|
|
|
fatal('ots-sanitize failed for %s: %s' % (tmpfile, otssan_res))
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-01 00:41:45 +03:00
|
|
|
def _glyphsyncWriteUFO(self, font, weight, ufo_path):
|
|
|
|
# fixup font info
|
2018-10-07 00:50:21 +03:00
|
|
|
setFontInfo(font, weight)
|
2018-10-01 00:41:45 +03:00
|
|
|
|
|
|
|
# cleanup lib
|
|
|
|
lib = dict()
|
2018-11-28 21:56:35 +03:00
|
|
|
for key, value in font.lib.items():
|
2018-10-01 00:41:45 +03:00
|
|
|
if key.startswith('com.schriftgestaltung'):
|
|
|
|
continue
|
|
|
|
if key == 'public.postscriptNames':
|
|
|
|
continue
|
|
|
|
lib[key] = value
|
|
|
|
font.lib.clear()
|
|
|
|
font.lib.update(lib)
|
|
|
|
|
|
|
|
# remove all but the primary (default) layer
|
|
|
|
layers = font.layers
|
|
|
|
defaultLayer = layers.defaultLayer
|
|
|
|
delLayerNames = set()
|
|
|
|
for layer in layers:
|
|
|
|
if layer != defaultLayer:
|
|
|
|
delLayerNames.add(layer.name)
|
|
|
|
for layerName in delLayerNames:
|
|
|
|
del layers[layerName]
|
|
|
|
|
2019-05-27 21:32:39 +03:00
|
|
|
# process glyphs
|
|
|
|
stripGlyphs = []
|
|
|
|
glyphOrder = OrderedDict([(k,None) for k in font.lib['public.glyphOrder']])
|
|
|
|
componentRefs = font.componentReferences
|
2018-10-01 00:41:45 +03:00
|
|
|
for g in font:
|
2019-05-27 21:32:39 +03:00
|
|
|
if not g.lib.get('com.schriftgestaltung.Glyphs.Export', True):
|
|
|
|
del glyphOrder[g.name]
|
|
|
|
g.unicodes = []
|
|
|
|
if len(componentRefs.get(g.name, [])) == 0:
|
|
|
|
# unused
|
|
|
|
stripGlyphs.append(g.name)
|
2018-10-01 00:41:45 +03:00
|
|
|
g.clearAnchors()
|
|
|
|
del g.lib['com.schriftgestaltung.Glyphs.lastChange']
|
|
|
|
|
2019-05-27 21:32:39 +03:00
|
|
|
# update possibly modified glyphorder
|
|
|
|
font.lib['public.glyphOrder'] = list(glyphOrder)
|
|
|
|
|
|
|
|
# strip unused glyphs
|
|
|
|
for gname in stripGlyphs:
|
|
|
|
log.info(
|
|
|
|
'Strip unused and unexported glyph "%s" from %s',
|
|
|
|
gname, ufo_path
|
|
|
|
)
|
|
|
|
del font[gname]
|
|
|
|
|
2018-10-01 00:41:45 +03:00
|
|
|
# write UFO file
|
|
|
|
self.log("write %s" % relpath(ufo_path, os.getcwd()))
|
|
|
|
font.save(ufo_path)
|
|
|
|
|
|
|
|
|
2018-10-08 04:27:12 +03:00
|
|
|
def _genSubsetDesignSpace(self, designspace, tag, filename):
|
|
|
|
ds = designspace
|
|
|
|
italic = False
|
|
|
|
if tag == 'italic':
|
|
|
|
italic = True
|
2019-10-22 22:27:06 +03:00
|
|
|
elif tag != 'roman':
|
2018-10-08 04:27:12 +03:00
|
|
|
raise Exception('unexpected tag ' + tag)
|
|
|
|
|
|
|
|
for a in ds.axes:
|
|
|
|
if a.tag == "slnt":
|
|
|
|
ds.axes.remove(a)
|
|
|
|
break
|
|
|
|
|
|
|
|
rmlist = []
|
|
|
|
hasDefault = not italic
|
|
|
|
for source in designspace.sources:
|
|
|
|
isitalic = source.name.find('italic') != -1
|
|
|
|
if italic != isitalic:
|
|
|
|
rmlist.append(source)
|
|
|
|
elif italic and not hasDefault:
|
|
|
|
source.copyLib = True
|
|
|
|
source.copyInfo = True
|
|
|
|
source.copyGroups = True
|
|
|
|
source.copyFeatures = True
|
|
|
|
hasDefault = True
|
|
|
|
for source in rmlist:
|
|
|
|
designspace.sources.remove(source)
|
|
|
|
|
|
|
|
rmlist = []
|
|
|
|
for instance in designspace.instances:
|
|
|
|
isitalic = instance.name.find('italic') != -1
|
|
|
|
if italic != isitalic:
|
|
|
|
rmlist.append(instance)
|
|
|
|
for instance in rmlist:
|
|
|
|
designspace.instances.remove(instance)
|
|
|
|
|
|
|
|
self.log("write %s" % relpath(filename, os.getcwd()))
|
|
|
|
ds.write(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
def cmd_glyphsync(self, argv):
|
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
usage='%(prog)s glyphsync <glyphsfile> [options]',
|
|
|
|
description='Generates designspace and UFOs from Glyphs file')
|
|
|
|
|
|
|
|
argparser.add_argument('glyphsfile', metavar='<glyphsfile>',
|
|
|
|
help='Glyphs source file')
|
|
|
|
|
|
|
|
argparser.add_argument('-o', '--outdir', metavar='<dir>',
|
|
|
|
help='''Write output to <dir>. If omitted, designspace and UFOs are
|
|
|
|
written to the directory of the glyphs file.
|
|
|
|
'''.strip().replace('\n ', ''))
|
|
|
|
|
|
|
|
args = argparser.parse_args(argv)
|
|
|
|
|
|
|
|
outdir = args.outdir
|
|
|
|
if outdir is None:
|
2018-09-04 00:19:38 +03:00
|
|
|
outdir = dirname(args.glyphsfile)
|
2018-09-03 22:55:49 +03:00
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
# TODO: Move into fontbuildlib
|
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
# files
|
|
|
|
master_dir = outdir
|
|
|
|
glyphsfile = args.glyphsfile
|
2020-03-23 05:46:59 +03:00
|
|
|
designspace_file = pjoin(outdir, 'Inter.designspace')
|
2018-09-04 00:19:38 +03:00
|
|
|
instance_dir = pjoin(BASEDIR, 'build', 'ufo')
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
# load glyphs project file
|
2018-09-09 22:29:33 +03:00
|
|
|
self.log("generating %s from %s" % (
|
2018-09-04 00:19:38 +03:00
|
|
|
relpath(designspace_file, os.getcwd()),
|
|
|
|
relpath(glyphsfile, os.getcwd())
|
2018-09-03 22:55:49 +03:00
|
|
|
))
|
|
|
|
font = glyphsLib.GSFont(glyphsfile)
|
|
|
|
|
|
|
|
# generate designspace from glyphs project
|
|
|
|
designspace = glyphsLib.to_designspace(
|
|
|
|
font,
|
|
|
|
propagate_anchors=False,
|
2019-02-03 23:17:56 +03:00
|
|
|
instance_dir=relpath(instance_dir, master_dir),
|
|
|
|
|
|
|
|
store_editor_state=False,
|
|
|
|
# do not store glyphs editor UI state in UFOs
|
|
|
|
|
|
|
|
minimize_glyphs_diffs=True,
|
|
|
|
# do not store glyphs metadata in UFOs
|
2018-09-03 22:55:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# strip lib data
|
|
|
|
designspace.lib.clear()
|
|
|
|
|
|
|
|
# patch and write UFO files
|
|
|
|
# TODO: Only write out-of-date UFOs
|
2018-10-01 00:41:45 +03:00
|
|
|
procs = []
|
2018-09-03 22:55:49 +03:00
|
|
|
for source in designspace.sources:
|
|
|
|
# source : fontTools.designspaceLib.SourceDescriptor
|
|
|
|
# source.font : defcon.objects.font.Font
|
2019-02-03 01:29:56 +03:00
|
|
|
ufo_path = pjoin(master_dir, source.filename)
|
2018-09-03 22:55:49 +03:00
|
|
|
# no need to also set the relative 'filename' attribute as that
|
|
|
|
# will be auto-updated on writing the designspace document
|
2018-09-12 09:41:01 +03:00
|
|
|
if source.styleName == "Italic Italic":
|
|
|
|
# Workaround for Glyphs limitation
|
|
|
|
# (Base italic master can't be called just Italic, so it's called
|
|
|
|
# "Italic Italic" which is converted here to just "Italic")
|
2020-03-23 05:46:59 +03:00
|
|
|
ufo_path = pjoin(master_dir, 'Inter-Italic.ufo')
|
2018-09-12 09:41:01 +03:00
|
|
|
source.styleName = "Italic"
|
|
|
|
source.name = "italic"
|
2018-09-14 18:50:06 +03:00
|
|
|
source.font.info.styleName = source.styleName
|
|
|
|
elif source.styleName == "Black Italic Italic":
|
2020-03-23 05:46:59 +03:00
|
|
|
ufo_path = pjoin(master_dir, 'Inter-BlackItalic.ufo')
|
2018-09-14 18:50:06 +03:00
|
|
|
source.styleName = "Black Italic"
|
|
|
|
source.name = "blackitalic"
|
2018-09-27 20:08:44 +03:00
|
|
|
source.font.info.styleName = source.styleName
|
2018-11-26 19:57:00 +03:00
|
|
|
elif source.styleName == "Thin Italic Italic":
|
2020-03-23 05:46:59 +03:00
|
|
|
ufo_path = pjoin(master_dir, 'Inter-ThinItalic.ufo')
|
2018-11-26 19:57:00 +03:00
|
|
|
source.styleName = "Thin Italic"
|
|
|
|
source.name = "thinitalic"
|
|
|
|
source.font.info.styleName = source.styleName
|
2018-09-12 09:41:01 +03:00
|
|
|
else:
|
2019-02-03 01:29:56 +03:00
|
|
|
# name "Black" => "black"
|
2018-09-12 09:41:01 +03:00
|
|
|
source.name = source.styleName.lower().replace(' ', '')
|
2019-07-19 00:04:29 +03:00
|
|
|
|
2018-10-01 00:41:45 +03:00
|
|
|
source.path = ufo_path
|
2018-09-03 22:55:49 +03:00
|
|
|
weight = int(source.location['Weight'])
|
|
|
|
|
2018-10-01 00:41:45 +03:00
|
|
|
p = Process(
|
|
|
|
target=self._glyphsyncWriteUFO,
|
|
|
|
args=(source.font, weight, ufo_path)
|
|
|
|
)
|
|
|
|
p.start()
|
|
|
|
procs.append(p)
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
# patch instance names
|
|
|
|
for instance in designspace.instances:
|
2019-02-03 01:29:56 +03:00
|
|
|
# name "Black Italic" => "blackitalic"
|
2018-09-03 22:55:49 +03:00
|
|
|
instance.name = instance.styleName.lower().replace(' ', '')
|
|
|
|
|
2018-09-09 22:29:33 +03:00
|
|
|
self.log("write %s" % relpath(designspace_file, os.getcwd()))
|
2018-09-03 22:55:49 +03:00
|
|
|
designspace.write(designspace_file)
|
|
|
|
|
2019-10-22 22:27:06 +03:00
|
|
|
# roman designspace
|
2020-03-23 05:46:59 +03:00
|
|
|
roman_designspace_file = pjoin(outdir, 'Inter-roman.designspace')
|
2018-10-08 04:27:12 +03:00
|
|
|
p = Process(
|
|
|
|
target=self._genSubsetDesignSpace,
|
2019-10-22 22:27:06 +03:00
|
|
|
args=(designspace, 'roman', roman_designspace_file)
|
2018-10-08 04:27:12 +03:00
|
|
|
)
|
|
|
|
p.start()
|
|
|
|
procs.append(p)
|
|
|
|
|
|
|
|
# italic designspace
|
2020-03-23 05:46:59 +03:00
|
|
|
italic_designspace_file = pjoin(outdir, 'Inter-italic.designspace')
|
2018-10-08 04:27:12 +03:00
|
|
|
p = Process(
|
|
|
|
target=self._genSubsetDesignSpace,
|
|
|
|
args=(designspace, 'italic', italic_designspace_file)
|
|
|
|
)
|
|
|
|
p.start()
|
|
|
|
procs.append(p)
|
|
|
|
|
2018-10-01 00:41:45 +03:00
|
|
|
for p in procs:
|
|
|
|
p.join()
|
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
def cmd_instancegen(self, argv):
|
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
description='Generate UFO instances from designspace')
|
|
|
|
|
|
|
|
argparser.add_argument('designspacefile', metavar='<designspace>',
|
|
|
|
help='Designspace file')
|
|
|
|
|
|
|
|
argparser.add_argument('instances', metavar='<instance-name>', nargs='*',
|
|
|
|
help='Style instances to generate. Omit to generate all.')
|
|
|
|
|
|
|
|
args = argparser.parse_args(argv)
|
|
|
|
|
|
|
|
instances = set([s.lower() for s in args.instances])
|
|
|
|
all_instances = len(instances) == 0
|
|
|
|
|
|
|
|
# files
|
|
|
|
designspace_file = args.designspacefile
|
2018-09-04 00:19:38 +03:00
|
|
|
instance_dir = pjoin(BASEDIR, 'build', 'ufo')
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
# DesignSpaceDocumentReader generates UFOs
|
|
|
|
gen = DesignSpaceDocumentReader(
|
|
|
|
designspace_file,
|
|
|
|
ufoVersion=3,
|
|
|
|
roundGeometry=True,
|
|
|
|
verbose=True
|
|
|
|
)
|
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
designspace = DesignSpaceDocument()
|
2018-09-03 22:55:49 +03:00
|
|
|
designspace.read(designspace_file)
|
|
|
|
|
|
|
|
# Generate UFOs for instances
|
|
|
|
instance_weight = dict()
|
|
|
|
instance_files = set()
|
|
|
|
for instance in designspace.instances:
|
|
|
|
if all_instances or instance.name in instances:
|
2018-09-04 00:19:38 +03:00
|
|
|
filebase = basename(instance.filename)
|
|
|
|
relname = relpath(
|
|
|
|
pjoin(dirname(designspace_file), instance.filename),
|
2018-09-03 22:55:49 +03:00
|
|
|
os.getcwd()
|
|
|
|
)
|
2018-09-09 22:29:33 +03:00
|
|
|
self.log('generating %s' % relname)
|
2018-09-03 22:55:49 +03:00
|
|
|
gen.readInstance(("name", instance.name))
|
|
|
|
instance_files.add(instance.filename)
|
|
|
|
instance_weight[filebase] = int(instance.location['Weight'])
|
|
|
|
if not all_instances:
|
|
|
|
instances.remove(instance.name)
|
|
|
|
|
|
|
|
if len(instances) > 0:
|
2018-09-09 22:29:33 +03:00
|
|
|
fatal('unknown style(s): %s' % ', '.join(list(instances)))
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
ufos = apply_instance_data(designspace_file, instance_files)
|
|
|
|
|
|
|
|
# patch ufos (list of defcon.Font instances)
|
|
|
|
italicAngleKey = 'com.schriftgestaltung.customParameter.' +\
|
|
|
|
'InstanceDescriptorAsGSInstance.italicAngle'
|
|
|
|
for font in ufos:
|
|
|
|
# move italicAngle from lib to info
|
|
|
|
italicAngle = font.lib.get(italicAngleKey)
|
|
|
|
if italicAngle != None:
|
|
|
|
italicAngle = float(italicAngle)
|
|
|
|
del font.lib[italicAngleKey]
|
|
|
|
font.info.italicAngle = italicAngle
|
2018-09-12 09:41:01 +03:00
|
|
|
|
|
|
|
# clear anchors
|
|
|
|
for g in font:
|
|
|
|
g.clearAnchors()
|
2019-07-19 00:04:29 +03:00
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
# update font info
|
2018-09-04 00:19:38 +03:00
|
|
|
weight = instance_weight[basename(font.path)]
|
2018-09-03 22:55:49 +03:00
|
|
|
setFontInfo(font, weight)
|
2019-07-19 00:04:29 +03:00
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
font.save()
|
|
|
|
|
|
|
|
|
2019-01-04 02:10:08 +03:00
|
|
|
def checkfont(self, fontfile, q):
|
|
|
|
res, ok = execproc('ots-idempotent', fontfile)
|
|
|
|
# Note: ots-idempotent does not exit with an error in many cases where
|
|
|
|
# it fails to sanitize the font, so we look at its output to determine
|
|
|
|
# success or failure.
|
|
|
|
if not ok or res.find('Failed') != -1:
|
|
|
|
log.error('%s: checkfont ots-idempotent: %s' % (fontfile, res))
|
|
|
|
q.put(False)
|
|
|
|
return
|
2018-09-03 22:55:49 +03:00
|
|
|
|
2019-01-04 02:10:08 +03:00
|
|
|
res, ok = execproc('ots-validator-checker', fontfile)
|
|
|
|
if not ok or res.find("didn't crash") == -1:
|
|
|
|
log.error('%s: checkfont ots-validator-checker: %s' % (fontfile, res))
|
|
|
|
q.put(False)
|
|
|
|
return
|
|
|
|
|
|
|
|
# ok
|
|
|
|
q.put(True)
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
def cmd_checkfont(self, argv):
|
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
usage='%(prog)s checkfont <file> ...',
|
|
|
|
description='Verify integrity of font files')
|
|
|
|
|
|
|
|
argparser.add_argument('files', metavar='<file>', nargs='+',
|
|
|
|
help='Font files')
|
2019-07-19 00:04:29 +03:00
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
args = argparser.parse_args(argv)
|
2019-01-04 02:10:08 +03:00
|
|
|
q = Queue()
|
|
|
|
|
|
|
|
if len(args.files) < 2:
|
|
|
|
self.checkfont(args.files[0], q)
|
|
|
|
else:
|
|
|
|
procs = []
|
|
|
|
for fontfile in args.files:
|
|
|
|
p = Process(target=self.checkfont, args=(fontfile, q))
|
|
|
|
p.start()
|
|
|
|
procs.append(p)
|
|
|
|
for p in procs:
|
|
|
|
p.join()
|
2018-09-03 22:55:49 +03:00
|
|
|
|
|
|
|
for fontfile in args.files:
|
2019-01-04 02:10:08 +03:00
|
|
|
if not q.get():
|
2018-09-03 22:55:49 +03:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2019-10-22 22:27:06 +03:00
|
|
|
def cmd_rename(self, argv):
|
|
|
|
argparser = argparse.ArgumentParser(
|
|
|
|
usage='%(prog)s rename <options> <file>',
|
|
|
|
description='Rename family and/or styles of font'
|
|
|
|
)
|
|
|
|
a = lambda *args, **kwargs: argparser.add_argument(*args, **kwargs)
|
|
|
|
|
|
|
|
a('-o', '--output', metavar='<file>',
|
|
|
|
help='Output font file. Defaults to input file (overwrite.)')
|
2019-10-24 04:44:33 +03:00
|
|
|
|
2019-10-22 22:27:06 +03:00
|
|
|
a('--family', metavar='<name>',
|
|
|
|
help='Rename family to <name>')
|
2019-10-24 04:44:33 +03:00
|
|
|
|
|
|
|
a('--google-style', action='store_true',
|
|
|
|
help='Rename style names to Google Fonts standards.')
|
|
|
|
|
2019-10-22 22:27:06 +03:00
|
|
|
a('input', metavar='<file>',
|
|
|
|
help='Input font file')
|
|
|
|
|
|
|
|
args = argparser.parse_args(argv)
|
|
|
|
|
|
|
|
infile = args.input
|
|
|
|
outfile = args.output or infile
|
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
font = loadTTFont(infile)
|
2019-10-22 22:27:06 +03:00
|
|
|
editCount = 0
|
|
|
|
try:
|
|
|
|
if args.family:
|
|
|
|
editCount += 1
|
2019-10-23 03:00:58 +03:00
|
|
|
setFamilyName(font, args.family)
|
2019-10-22 22:27:06 +03:00
|
|
|
|
2019-10-24 04:44:33 +03:00
|
|
|
if args.google_style:
|
2019-10-22 22:27:06 +03:00
|
|
|
editCount += 1
|
2019-10-24 04:44:33 +03:00
|
|
|
renameStylesGoogleFonts(font)
|
2019-10-22 22:27:06 +03:00
|
|
|
|
2019-10-23 03:00:58 +03:00
|
|
|
if editCount == 0:
|
2019-10-22 22:27:06 +03:00
|
|
|
print("no rename options provided", file=sys.stderr)
|
2019-10-23 03:00:58 +03:00
|
|
|
argparser.print_help(sys.stderr)
|
2019-10-22 22:27:06 +03:00
|
|
|
sys.exit(1)
|
2019-10-23 03:00:58 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
font.save(outfile)
|
2019-10-22 22:27:06 +03:00
|
|
|
finally:
|
|
|
|
font.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-09-03 22:55:49 +03:00
|
|
|
if __name__ == '__main__':
|
2020-03-23 05:46:59 +03:00
|
|
|
Main().main(sys.argv)
|