Skip to content

Commit

Permalink
Run featureWriters from CLI
Browse files Browse the repository at this point in the history
similar to #470 but for featureWriters. Also useful for testing/debugging.
  • Loading branch information
anthrotype committed Mar 1, 2021
1 parent 357f920 commit 564b0b6
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Lib/ufo2ft/featureWriters/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import argparse
from io import StringIO
import logging

from fontTools.misc.cliTools import makeOutputFileName

from ufo2ft.constants import FEATURE_WRITERS_KEY
from ufo2ft.featureCompiler import FeatureCompiler
from ufo2ft.featureWriters import loadFeatureWriterFromString, logger

try:
import ufoLib2

loader = ufoLib2.Font
except ImportError:
import defcon

loader = defcon.Font

logging.basicConfig(level=logging.INFO)

parser = argparse.ArgumentParser(description="Apply feature writers to a UFO file")
parser.add_argument("--output", "-o", metavar="OUTPUT", help="output file name")
parser.add_argument("ufo", metavar="UFO", help="UFO file")
parser.add_argument(
"writers",
metavar="WRITER",
nargs="*",
help="list of feature writers to enable",
)

args = parser.parse_args()
if not args.output:
args.output = makeOutputFileName(args.ufo)

ufo = loader(args.ufo)
writers = [loadFeatureWriterFromString(w) for w in args.writers]
compiler = FeatureCompiler(ufo, featureWriters=writers or None)
compiler.setupFeatures()
buf = StringIO()
compiler.writeFeatures(buf)
ufo.features.text = buf.getvalue()

if FEATURE_WRITERS_KEY in ufo.lib:
del ufo.lib[FEATURE_WRITERS_KEY]

logger.info("Written on %s" % args.output)
ufo.save(args.output)

0 comments on commit 564b0b6

Please sign in to comment.