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
|
#!/usr/bin/env python3
# Compiles font with FontForge.
import os
import re
import sys
import time
import fontforge
import htic
# Read arguments
SOURCE = sys.argv[1] # *.sfd
COMMON = sys.argv[2] # *.sfd
FEATURES = sys.argv[3] # *.fea
INSTRUCTIONS = sys.argv[4] # *.hti
VERSION = sys.argv[5] # [0-9].[0-9]
TARGET = sys.argv[6] # *.otf|*.ttf
# Set constants
EXT = TARGET.split(".")[-1]
MAJOR = int(VERSION[0])
MINOR = int(VERSION[2])
NOW = time.gmtime(int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
DATE = time.strftime("%Y-%m-%d", NOW)
# Prepare font
font = fontforge.open(SOURCE)
font.mergeFonts(COMMON)
font.mergeFeature(FEATURES)
font.version = VERSION
font.appendSFNTName("English (US)", "UniqueID",
"{} {} {}".format(font.fullname, font.version, DATE))
# Generate font
if EXT == "otf":
# Unlink composite glyphs
for glyph in font.glyphs():
if glyph.references:
glyph.manualHints = True
glyph.unlinkRef()
glyph.removeOverlap()
glyph.round()
glyph.simplify()
glyph.correctDirection()
glyph.canonicalStart()
glyph.canonicalContours()
# Remove glyphs that are only required for TrueType
font['.null'].clear()
font['nonmarkingreturn'].clear()
for glyph in font.glyphs():
if glyph.glyphname.endswith(".tt"):
glyph.clear()
# Generate
font.generate(TARGET, flags=("opentype", "old-kern", "dummy-dsig"))
elif EXT == "ttf":
# Convert to quadratic outlines
font.layers['Fore'].is_quadratic = True
for glyph in font.glyphs():
# If available, use custom quadratic
# outlines from background layer
if not glyph.background.isEmpty():
glyph.foreground = glyph.background
# Remove .tt mark from TrueType exclusive glyphs
if glyph.glyphname.endswith(".tt"):
glyph.glyphname = glyph.glyphname[:-3]
# Add instructions
htic.toFontforge(INSTRUCTIONS, font)
# Generate
font.generate(TARGET, flags=("opentype", "old-kern", "dummy-dsig"))
# Done
font.close()
|