summaryrefslogtreecommitdiff
path: root/misc/fontbuild
blob: 68d26d4a424de13cf2d1442618a16d0b6e7c72ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/usr/bin/env python
from __future__ import print_function, absolute_import
import sys
import os
from os.path import dirname, basename, abspath, relpath, join as pjoin
sys.path.append(abspath(pjoin(dirname(__file__), 'tools')))
from common import BASEDIR, execproc
from collections import OrderedDict

import argparse
import glyphsLib
import logging
import re
import signal
import subprocess
import defcon

from fontTools.designspaceLib import DesignSpaceDocument
from mutatorMath.ufo.document import DesignSpaceDocumentReader
from multiprocessing import Process, Queue
from glyphsLib.interpolation import apply_instance_data

from fontbuildlib import FontBuilder
from fontbuildlib.util import mkdirs, loadTTFont
from fontbuildlib.info import setFontInfo
from fontbuildlib.name import setFamilyName, renameStylesGoogleFonts

log = logging.getLogger(__name__)

# set to true to exclude anchors from .glyphs when generating UFOs
EXCLUDE_ANCHORS = False


def sighandler(signum, frame):
  sys.stdout.write('\n')
  sys.stdout.flush()
  sys.exit(1)


def fatal(msg):
  print(sys.argv[0] + ': ' + msg, file=sys.stderr)
  sys.exit(1)


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)


class Main(object):

  def __init__(self):
    self.tmpdir = pjoin(BASEDIR,'build','tmp')
    self.quiet = False
    self.logLevelName = 'WARNING'


  def log(self, msg):
    if not self.quiet:
      print(msg)


  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
        compile-var  Build variable font files
        glyphsync    Generate designspace and UFOs from Glyphs file
        instancegen  Generate instance UFOs for designspace
        checkfont    Verify integrity of font files
        rename       Rename fonts
      '''.strip().replace('\n      ', '\n'))

    argparser.add_argument('-v', '--verbose', action='store_true',
      help='Print more details')

    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('--profile', metavar='<file>',
      help='Run in profiler for debugging, writing pstats data to <file>')

    argparser.add_argument('-C', metavar='<dir>', dest='chdir',
      help='Run as if %(prog)s started in <dir> instead of the '+\
           'current working directory.')

    argparser.add_argument('command', metavar='<command>')

    # 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])

    # log config
    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:
      logging.basicConfig(level=logging.DEBUG, format='%(funcName)s: %(message)s')
      self.logLevelName = 'DEBUG'
    elif args.verbose:
      logging.basicConfig(level=logging.INFO, format='%(funcName)s: %(message)s')
      self.logLevelName = 'INFO'
    else:
      logging.basicConfig(level=logging.WARNING, format='%(message)s')
      self.logLevelName = 'WARNING'

    if args.chdir:
      os.chdir(args.chdir)

    cmd = 'cmd_' + args.command.replace('-', '_')
    if not hasattr(self, cmd):
      fatal('Unrecognized command %s. Try --help' % args.command)
    cmdfn = getattr(self, cmd)
    if args.profile:
      try:
        import cProfile as profile
      except:
        import profile
      import __main__
      __main__.__dict__["cmdfn"] = cmdfn
      __main__.__dict__["argv"] = argv[i:]
      profile.run('cmdfn(argv)', args.profile)
      print("")
      print("profile saved to %r. You can now inspect it with for example:" %
            args.profile)
      print("misc/tools/fmtprofile.py -n 20 %r" % args.profile)
      print("python3 -m pstats %r" % args.profile)
      print("")
    else:
      cmdfn(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 == '':
      outfilename = pjoin(
        dirname(args.srcfile),
        os.path.splitext(basename(args.srcfile))[0] + '.otf'
      )
      log.debug('setting --output %r' % outfilename)

    mkdirs(dirname(outfilename))

    FontBuilder().buildVariable(args.srcfile, outfilename)

    self.log("write %s" % outfilename)
    # Note: we can't run ots-sanitize on the generated file as OTS
    # currently doesn't support variable fonts.



  def cmd_compile(self, argv):
    argparser = argparse.ArgumentParser(
      usage='%(prog)s compile [-h] [-o <file>] <ufo>',
      description='Compile font files')

    argparser.add_argument('srcfile', metavar='<ufo>',
      help='Source file (.ufo file)')

    argparser.add_argument('-o', '--output', metavar='<fontfile>',
      help='Output font file (.otf or .ttf)')

    argparser.add_argument('--validate', action='store_true',
      help='Enable ufoLib validation on reading/writing UFO files')

    args = argparser.parse_args(argv)

    # write an OTF/CFF (or a TTF if false)
    cff = True

    # decide output filename
    outfilename = args.output
    if outfilename is None or outfilename == '':
      outfilename = pjoin(
        dirname(args.srcfile),
        os.path.splitext(basename(args.srcfile))[0] + '.otf'
      )
      log.debug('setting --output %r' % outfilename)
    else:
      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)

    # build OTF or TTF file from UFO
    FontBuilder().buildStatic(args.srcfile, outfilename, cff)

    # code to pipe font through ots-sanitize:
    # # temp file to write to
    # tmpfilename = pjoin(self.tmpdir, basename(outfilename))
    # mkdirs(self.tmpdir)
    #
    # # build OTF or TTF file from UFO
    # FontBuilder().buildStatic(args.srcfile, tmpfilename, cff)
    #
    # # Run ots-sanitize on produced OTF/TTF file and write sanitized version to outfilename
    # self._ots_sanitize(tmpfilename, outfilename)


  #def _ots_sanitize(self, tmpfilename, outfilename):
  #  # run through ots-sanitize
  #  # for filename in args.output:
  #  tmpfile = pjoin(self.tmpdir, tmpfilename)
  #  mkdirs(dirname(outfilename))
  #  success = True
  #  try:
  #    self.log("write %s" % outfilename)
  #    otssan_res = subprocess.check_output(
  #      ['ots-sanitize', tmpfile, outfilename],
  #      # ['cp', tmpfile, outfilename],
  #      shell=False
  #    ).strip()
  #    # Note: ots-sanitize does not exit with an error in many cases where
  #    # it fails to sanitize the font.
  #    success = str(otssan_res).find('Failed') == -1
  #  except:
  #    success = False
  #    if len(otssan_res) == 0:
  #      otssan_res = 'error'
  #
  #  if success:
  #    os.unlink(tmpfile)
  #  else:
  #    fatal('ots-sanitize failed for %s: %s' % (tmpfile, otssan_res))



  def _glyphsyncWriteUFO(self, font, weight, ufo_path):
    # fixup font info
    setFontInfo(font, weight)

    # cleanup lib
    lib = dict()
    for key, value in font.lib.items():
      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])

    # process glyphs
    glyphOrder = OrderedDict([(k,None) for k in font.lib['public.glyphOrder']])
    rmglyphs = []
    for g in font:
      if not g.lib.get('com.schriftgestaltung.Glyphs.Export', True):
        if g.name in glyphOrder:
          del(glyphOrder[g.name])
        rmglyphs.append(g.name)
        g.unicodes = []
        continue
      if EXCLUDE_ANCHORS:
        g.clearAnchors()
      if 'com.schriftgestaltung.Glyphs.lastChange' in g.lib:
        del(g.lib['com.schriftgestaltung.Glyphs.lastChange'])

    for gname in rmglyphs:
      del(font[gname])

    # update possibly modified glyphorder
    font.lib['public.glyphOrder'] = list(glyphOrder)

    # write UFO file
    self.log("write %s" % relpath(ufo_path, os.getcwd()))
    font.save(ufo_path, overwrite=True, validate=False)


  def _genSubsetDesignSpace(self, designspace, tag, filename):
    ds = designspace
    italic = False
    if tag == 'italic':
      italic = True
    elif tag != 'roman':
      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)



  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:
      outdir = dirname(args.glyphsfile)

    # TODO: Move into fontbuildlib

    # files
    master_dir = outdir
    glyphsfile = args.glyphsfile
    name = os.path.splitext(basename(glyphsfile))[0]  # e.g. "Inter"
    designspace_file = pjoin(outdir, name + '.designspace')
    instance_dir = pjoin(BASEDIR, 'build', 'ufo')

    # load glyphs project file
    self.log("generating %s from %s" % (
      relpath(designspace_file, os.getcwd()),
      relpath(glyphsfile, os.getcwd())
    ))
    font = glyphsLib.GSFont(glyphsfile)

    # remove archive layers and backgrounds
    masterLayerIDs = set()
    for master in font.masters:
      masterLayerIDs.add(master.id)
    for glyph in font.glyphs:
      for layer in list(glyph.layers): # list to accumulate iterator since we del()
        # remove background images from all layers
        layer.backgroundImage = None
        lname = layer.name
        lid = layer.layerId
        if lname[0] != '_' and (lid in masterLayerIDs or lname.find('{') != -1):
          # Keep only layers which are masters or bracket layers.
          # Next, clear background to speed up UFO generation.
          layer.background.paths = []
          layer.background.components = []
          layer.background.anchors = []
          layer.background.hints = []
          layer.background.guides = []
        else:
          del(glyph.layers[lid])

    # generate designspace from glyphs project
    designspace = glyphsLib.to_designspace(
      font,
      propagate_anchors=(not EXCLUDE_ANCHORS),
      instance_dir=relpath(instance_dir, master_dir),
      store_editor_state=False, # do not store glyphs editor UI state in UFOs
    )

    # strip lib data
    designspace.lib.clear()

    # patch and write UFO files
    # TODO: Only write out-of-date UFOs
    procs = []
    for source in designspace.sources:
      # source      : fontTools.designspaceLib.SourceDescriptor
      # source.font : ufoLib2.objects.font.Font
      ufo_path = pjoin(master_dir, source.filename)
      # no need to also set the relative 'filename' attribute as that
      # will be auto-updated on writing the designspace document
      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")
        ufo_path = pjoin(master_dir, name + '-Italic.ufo')
        source.styleName = "Italic"
        source.name = "italic"
        source.font.info.styleName = source.styleName
      elif source.styleName == "Black Italic Italic":
        ufo_path = pjoin(master_dir, name + '-BlackItalic.ufo')
        source.styleName = "Black Italic"
        source.name = "blackitalic"
        source.font.info.styleName = source.styleName
      elif source.styleName == "Thin Italic Italic":
        ufo_path = pjoin(master_dir, name + '-ThinItalic.ufo')
        source.styleName = "Thin Italic"
        source.name = "thinitalic"
        source.font.info.styleName = source.styleName
      elif source.styleName:
        # name "Black" => "black"
        source.name = source.styleName.lower().replace(' ', '')

      source.path = ufo_path
      weight = int(source.location['Weight'])

      # serial
      # self._glyphsyncWriteUFO(source.font, weight, ufo_path)

      # parallel
      p = Process(
        target=self._glyphsyncWriteUFO,
        args=(source.font, weight, ufo_path)
      )
      p.start()
      procs.append(p)

    # patch instance names
    for instance in designspace.instances:
      # name "Black Italic" => "blackitalic"
      instance.name = instance.styleName.lower().replace(' ', '')

    self.log("write %s" % relpath(designspace_file, os.getcwd()))
    designspace.write(designspace_file)

    # roman designspace
    roman_designspace_file = pjoin(outdir, name + '-roman.designspace')
    p = Process(
      target=self._genSubsetDesignSpace,
      args=(designspace, 'roman', roman_designspace_file)
    )
    p.start()
    procs.append(p)

    # italic designspace
    italic_designspace_file = pjoin(outdir, name + '-italic.designspace')
    p = Process(
      target=self._genSubsetDesignSpace,
      args=(designspace, 'italic', italic_designspace_file)
    )
    p.start()
    procs.append(p)

    for p in procs:
      p.join()



  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
    instance_dir = pjoin(BASEDIR, 'build', 'ufo')

    # DesignSpaceDocumentReader generates UFOs
    gen = DesignSpaceDocumentReader(
      designspace_file,
      ufoVersion=3,
      roundGeometry=True,
      verbose=True
    )

    designspace = DesignSpaceDocument.fromfile(designspace_file)

    master0_ufo = defcon.Font(designspace.sources[0].path)

    # Generate UFOs for instances
    instance_weight = dict()
    instance_files = set()
    for instance in designspace.instances:
      if all_instances or instance.name in instances:
        filebase = basename(instance.filename)
        relname = relpath(
          pjoin(dirname(designspace_file), instance.filename),
          os.getcwd()
        )
        self.log('generating %s' % relname)
        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:
      fatal('unknown style(s): %s' % ', '.join(list(instances)))

    ufos = apply_instance_data(designspace, instance_files)

    # patch ufos (list of defcon.Font instances)
    italicAngleKey = 'com.schriftgestaltung.customParameter.' +\
                     'InstanceDescriptorAsGSInstance.italicAngle'
    for ufo in ufos:
      # set metrics
      ufo.info.ascender = master0_ufo.info.ascender
      ufo.info.capHeight = master0_ufo.info.capHeight
      ufo.info.descender = master0_ufo.info.descender
      ufo.info.xHeight = master0_ufo.info.xHeight

      # move italicAngle from lib to info
      italicAngle = ufo.lib.get(italicAngleKey)
      if italicAngle != None:
        italicAngle = float(italicAngle)
        del(ufo.lib[italicAngleKey])
        ufo.info.italicAngle = italicAngle

      # clear anchors
      if EXCLUDE_ANCHORS:
        for g in ufo:
          g.clearAnchors()

      # update font info
      weight = instance_weight[basename(ufo.path)]
      setFontInfo(ufo, weight)

      ufo.save()


  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

    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)


  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')

    args = argparser.parse_args(argv)
    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()

    for fontfile in args.files:
      if not q.get():
        sys.exit(1)


  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.)')

    a('--family', metavar='<name>',
      help='Rename family to <name>')

    a('--google-style', action='store_true',
      help='Rename style names to Google Fonts standards.')

    a('input', metavar='<file>',
      help='Input font file')

    args = argparser.parse_args(argv)

    infile = args.input
    outfile = args.output or infile

    font = loadTTFont(infile)
    editCount = 0
    try:
      if args.family:
        editCount += 1
        setFamilyName(font, args.family)

      if args.google_style:
        editCount += 1
        renameStylesGoogleFonts(font)

      if editCount == 0:
        print("no rename options provided", file=sys.stderr)
        argparser.print_help(sys.stderr)
        sys.exit(1)
        return

      font.save(outfile)
    finally:
      font.close()



if __name__ == '__main__':
  Main().main(sys.argv)