2021-11-18 13:06:02 +00:00
|
|
|
"""fontTools.ttLib -- a package for dealing with TrueType fonts."""
|
1999-12-16 21:34:53 +00:00
|
|
|
|
2018-01-23 16:03:45 -08:00
|
|
|
from fontTools.misc.loggingTools import deprecateFunction
|
2016-01-24 14:25:50 +00:00
|
|
|
import logging
|
2022-10-28 16:31:47 -06:00
|
|
|
import sys
|
2003-08-28 18:23:43 +00:00
|
|
|
|
2002-05-02 15:23:25 +00:00
|
|
|
|
2016-01-24 14:25:50 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
class TTLibError(Exception):
|
|
|
|
pass
|
2022-12-13 11:26:36 +00:00
|
|
|
|
|
|
|
|
2022-10-28 16:31:47 -06:00
|
|
|
class TTLibFileIsCollectionError(TTLibError):
|
|
|
|
pass
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
|
2016-01-24 14:25:50 +00:00
|
|
|
@deprecateFunction("use logging instead", category=DeprecationWarning)
|
1999-12-16 21:34:53 +00:00
|
|
|
def debugmsg(msg):
|
|
|
|
import time
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2013-11-27 04:57:33 -05:00
|
|
|
print(msg + time.strftime(" (%H:%M:%S)", time.localtime(time.time())))
|
2022-12-13 11:26:36 +00:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
|
2018-01-23 16:03:45 -08:00
|
|
|
from fontTools.ttLib.ttFont import *
|
2018-01-23 15:39:38 -08:00
|
|
|
from fontTools.ttLib.ttCollection import TTCollection
|
2022-10-28 16:31:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
def main(args=None):
|
2022-10-29 10:57:50 -06:00
|
|
|
"""Open/save fonts with TTFont() or TTCollection()
|
2022-10-28 16:47:45 -06:00
|
|
|
|
|
|
|
./fonttools ttLib [-oFILE] [-yNUMBER] files...
|
|
|
|
|
|
|
|
If multiple files are given on the command-line,
|
|
|
|
they are each opened (as a font or collection),
|
|
|
|
and added to the font list.
|
|
|
|
|
|
|
|
If -o (output-file) argument is given, the font
|
|
|
|
list is then saved to the output file, either as
|
|
|
|
a single font, if there is only one font, or as
|
|
|
|
a collection otherwise.
|
|
|
|
|
|
|
|
If -y (font-number) argument is given, only the
|
|
|
|
specified font from collections is opened.
|
|
|
|
|
|
|
|
The above allow extracting a single font from a
|
|
|
|
collection, or combining multiple fonts into a
|
|
|
|
collection.
|
|
|
|
|
|
|
|
If --lazy or --no-lazy are give, those are passed
|
|
|
|
to the TTFont() or TTCollection() constructors.
|
|
|
|
"""
|
2022-10-28 16:31:47 -06:00
|
|
|
from fontTools import configLogger
|
|
|
|
|
|
|
|
if args is None:
|
|
|
|
args = sys.argv[1:]
|
|
|
|
|
2022-10-29 10:57:50 -06:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
"fonttools ttLib",
|
|
|
|
description="Open/save fonts with TTFont() or TTCollection()",
|
|
|
|
epilog="""
|
|
|
|
If multiple files are given on the command-line,
|
|
|
|
they are each opened (as a font or collection),
|
|
|
|
and added to the font list.
|
|
|
|
|
|
|
|
The above, when combined with -o / --output,
|
|
|
|
allows for extracting a single font from a
|
|
|
|
collection, or combining multiple fonts into a
|
|
|
|
collection.
|
|
|
|
""",
|
|
|
|
)
|
|
|
|
parser.add_argument("font", metavar="font", nargs="*", help="Font file.")
|
|
|
|
parser.add_argument(
|
|
|
|
"-o", "--output", metavar="FILE", default=None, help="Output file."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-y", metavar="NUMBER", default=-1, help="Font number to load from collections."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--lazy", action="store_true", default=None, help="Load fonts lazily."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-lazy", dest="lazy", action="store_false", help="Load fonts immediately."
|
|
|
|
)
|
2023-01-18 16:50:50 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"--flavor",
|
|
|
|
dest="flavor",
|
|
|
|
default=None,
|
|
|
|
help="Flavor of output font. 'woff' or 'woff2'.",
|
|
|
|
)
|
2022-10-29 10:57:50 -06:00
|
|
|
options = parser.parse_args(args)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-10-29 10:57:50 -06:00
|
|
|
fontNumber = int(options.y) if options.y is not None else None
|
|
|
|
outFile = options.output
|
|
|
|
lazy = options.lazy
|
2023-01-18 16:50:50 -07:00
|
|
|
flavor = options.flavor
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-10-28 16:47:45 -06:00
|
|
|
fonts = []
|
2022-10-29 10:57:50 -06:00
|
|
|
for f in options.font:
|
2022-10-28 16:31:47 -06:00
|
|
|
try:
|
2022-10-28 16:47:45 -06:00
|
|
|
font = TTFont(f, fontNumber=fontNumber, lazy=lazy)
|
|
|
|
fonts.append(font)
|
2022-10-28 16:31:47 -06:00
|
|
|
except TTLibFileIsCollectionError:
|
2022-10-28 16:47:45 -06:00
|
|
|
collection = TTCollection(f, lazy=lazy)
|
|
|
|
fonts.extend(collection.fonts)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-10-29 11:08:00 -06:00
|
|
|
if outFile is not None:
|
|
|
|
if len(fonts) == 1:
|
2023-01-18 16:50:50 -07:00
|
|
|
fonts[0].flavor = flavor
|
2022-10-29 11:08:00 -06:00
|
|
|
fonts[0].save(outFile)
|
|
|
|
else:
|
2023-01-18 16:50:50 -07:00
|
|
|
if flavor is not None:
|
|
|
|
raise TTLibError("Cannot set flavor for collections.")
|
2022-10-29 11:08:00 -06:00
|
|
|
collection = TTCollection()
|
|
|
|
collection.fonts = fonts
|
|
|
|
collection.save(outFile)
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-10-28 16:31:47 -06:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|