Merge pull request #3088 from fonttools/ttLib-main
add missing __main__.py file to ttLib package, plus some tests
This commit is contained in:
commit
2e1eda2695
@ -2,7 +2,6 @@
|
||||
|
||||
from fontTools.misc.loggingTools import deprecateFunction
|
||||
import logging
|
||||
import sys
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@ -25,99 +24,3 @@ def debugmsg(msg):
|
||||
|
||||
from fontTools.ttLib.ttFont import *
|
||||
from fontTools.ttLib.ttCollection import TTCollection
|
||||
|
||||
|
||||
def main(args=None):
|
||||
"""Open/save fonts with TTFont() or TTCollection()
|
||||
|
||||
./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.
|
||||
"""
|
||||
from fontTools import configLogger
|
||||
|
||||
if args is None:
|
||||
args = sys.argv[1:]
|
||||
|
||||
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."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--flavor",
|
||||
dest="flavor",
|
||||
default=None,
|
||||
help="Flavor of output font. 'woff' or 'woff2'.",
|
||||
)
|
||||
options = parser.parse_args(args)
|
||||
|
||||
fontNumber = int(options.y) if options.y is not None else None
|
||||
outFile = options.output
|
||||
lazy = options.lazy
|
||||
flavor = options.flavor
|
||||
|
||||
fonts = []
|
||||
for f in options.font:
|
||||
try:
|
||||
font = TTFont(f, fontNumber=fontNumber, lazy=lazy)
|
||||
fonts.append(font)
|
||||
except TTLibFileIsCollectionError:
|
||||
collection = TTCollection(f, lazy=lazy)
|
||||
fonts.extend(collection.fonts)
|
||||
|
||||
if outFile is not None:
|
||||
if len(fonts) == 1:
|
||||
fonts[0].flavor = flavor
|
||||
fonts[0].save(outFile)
|
||||
else:
|
||||
if flavor is not None:
|
||||
raise TTLibError("Cannot set flavor for collections.")
|
||||
collection = TTCollection()
|
||||
collection.fonts = fonts
|
||||
collection.save(outFile)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
|
100
Lib/fontTools/ttLib/__main__.py
Normal file
100
Lib/fontTools/ttLib/__main__.py
Normal file
@ -0,0 +1,100 @@
|
||||
import sys
|
||||
from fontTools.ttLib import TTLibError, TTLibFileIsCollectionError
|
||||
from fontTools.ttLib.ttFont import *
|
||||
from fontTools.ttLib.ttCollection import TTCollection
|
||||
|
||||
|
||||
def main(args=None):
|
||||
"""Open/save fonts with TTFont() or TTCollection()
|
||||
|
||||
./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.
|
||||
"""
|
||||
from fontTools import configLogger
|
||||
|
||||
if args is None:
|
||||
args = sys.argv[1:]
|
||||
|
||||
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."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--flavor",
|
||||
dest="flavor",
|
||||
default=None,
|
||||
help="Flavor of output font. 'woff' or 'woff2'.",
|
||||
)
|
||||
options = parser.parse_args(args)
|
||||
|
||||
fontNumber = int(options.y) if options.y is not None else None
|
||||
outFile = options.output
|
||||
lazy = options.lazy
|
||||
flavor = options.flavor
|
||||
|
||||
fonts = []
|
||||
for f in options.font:
|
||||
try:
|
||||
font = TTFont(f, fontNumber=fontNumber, lazy=lazy)
|
||||
fonts.append(font)
|
||||
except TTLibFileIsCollectionError:
|
||||
collection = TTCollection(f, lazy=lazy)
|
||||
fonts.extend(collection.fonts)
|
||||
|
||||
if outFile is not None:
|
||||
if len(fonts) == 1:
|
||||
fonts[0].flavor = flavor
|
||||
fonts[0].save(outFile)
|
||||
else:
|
||||
if flavor is not None:
|
||||
raise TTLibError("Cannot set flavor for collections.")
|
||||
collection = TTCollection()
|
||||
collection.fonts = fonts
|
||||
collection.save(outFile)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
105
Tests/ttLib/main_test.py
Normal file
105
Tests/ttLib/main_test.py
Normal file
@ -0,0 +1,105 @@
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from fontTools.ttLib import __main__, TTFont, TTCollection
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
TEST_DATA = Path(__file__).parent / "data"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ttfont_path():
|
||||
font = TTFont()
|
||||
font.importXML(TEST_DATA / "TestTTF-Regular.ttx")
|
||||
with tempfile.NamedTemporaryFile(suffix=".ttf", delete=False) as fp:
|
||||
font_path = Path(fp.name)
|
||||
font.save(font_path)
|
||||
yield font_path
|
||||
font_path.unlink()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ttcollection_path():
|
||||
font1 = TTFont()
|
||||
font1.importXML(TEST_DATA / "TestTTF-Regular.ttx")
|
||||
font2 = TTFont()
|
||||
font2.importXML(TEST_DATA / "TestTTF-Regular.ttx")
|
||||
coll = TTCollection()
|
||||
coll.fonts = [font1, font2]
|
||||
with tempfile.NamedTemporaryFile(suffix=".ttf", delete=False) as fp:
|
||||
collection_path = Path(fp.name)
|
||||
coll.save(collection_path)
|
||||
yield collection_path
|
||||
collection_path.unlink()
|
||||
|
||||
|
||||
@pytest.fixture(params=[None, "woff"])
|
||||
def flavor(request):
|
||||
return request.param
|
||||
|
||||
|
||||
def test_ttLib_main_as_subprocess(ttfont_path):
|
||||
subprocess.run(
|
||||
[sys.executable, "-m", "fontTools.ttLib", str(ttfont_path)], check=True
|
||||
)
|
||||
|
||||
|
||||
def test_ttLib_open_ttfont(ttfont_path):
|
||||
__main__.main([str(ttfont_path)])
|
||||
|
||||
|
||||
def test_ttLib_open_save_ttfont(tmp_path, ttfont_path, flavor):
|
||||
output_path = tmp_path / "TestTTF-Regular.ttf"
|
||||
args = ["-o", str(output_path), str(ttfont_path)]
|
||||
if flavor is not None:
|
||||
args.extend(["--flavor", flavor])
|
||||
|
||||
__main__.main(args)
|
||||
|
||||
assert output_path.exists()
|
||||
assert TTFont(output_path).getGlyphOrder() == TTFont(ttfont_path).getGlyphOrder()
|
||||
|
||||
|
||||
def test_ttLib_open_ttcollection(ttcollection_path):
|
||||
__main__.main(["-y", "0", str(ttcollection_path)])
|
||||
|
||||
|
||||
def test_ttLib_open_ttcollection_save_single_font(tmp_path, ttcollection_path, flavor):
|
||||
for i in range(2):
|
||||
output_path = tmp_path / f"TestTTF-Regular#{i}.ttf"
|
||||
args = ["-y", str(i), "-o", str(output_path), str(ttcollection_path)]
|
||||
if flavor is not None:
|
||||
args.extend(["--flavor", flavor])
|
||||
|
||||
__main__.main(args)
|
||||
|
||||
assert output_path.exists()
|
||||
assert (
|
||||
TTFont(output_path).getGlyphOrder()
|
||||
== TTCollection(ttcollection_path)[i].getGlyphOrder()
|
||||
)
|
||||
|
||||
|
||||
def test_ttLib_open_ttcollection_save_ttcollection(tmp_path, ttcollection_path):
|
||||
output_path = tmp_path / "TestTTF.ttc"
|
||||
|
||||
__main__.main(["-o", str(output_path), str(ttcollection_path)])
|
||||
|
||||
assert output_path.exists()
|
||||
assert len(TTCollection(output_path)) == len(TTCollection(ttcollection_path))
|
||||
|
||||
|
||||
def test_ttLib_open_multiple_fonts_save_ttcollection(tmp_path, ttfont_path):
|
||||
output_path = tmp_path / "TestTTF.ttc"
|
||||
|
||||
__main__.main(["-o", str(output_path), str(ttfont_path), str(ttfont_path)])
|
||||
|
||||
assert output_path.exists()
|
||||
|
||||
coll = TTCollection(output_path)
|
||||
assert len(coll) == 2
|
||||
assert coll[0].getGlyphOrder() == coll[1].getGlyphOrder()
|
Loading…
x
Reference in New Issue
Block a user