1import subprocess 2import sys 3import tempfile 4from pathlib import Path 5 6from fontTools.ttLib import __main__, TTFont, TTCollection 7 8import pytest 9 10 11TEST_DATA = Path(__file__).parent / "data" 12 13 14@pytest.fixture 15def ttfont_path(): 16 font = TTFont() 17 font.importXML(TEST_DATA / "TestTTF-Regular.ttx") 18 with tempfile.NamedTemporaryFile(suffix=".ttf", delete=False) as fp: 19 font_path = Path(fp.name) 20 font.save(font_path) 21 yield font_path 22 font_path.unlink() 23 24 25@pytest.fixture 26def ttcollection_path(): 27 font1 = TTFont() 28 font1.importXML(TEST_DATA / "TestTTF-Regular.ttx") 29 font2 = TTFont() 30 font2.importXML(TEST_DATA / "TestTTF-Regular.ttx") 31 coll = TTCollection() 32 coll.fonts = [font1, font2] 33 with tempfile.NamedTemporaryFile(suffix=".ttf", delete=False) as fp: 34 collection_path = Path(fp.name) 35 coll.save(collection_path) 36 yield collection_path 37 collection_path.unlink() 38 39 40@pytest.fixture(params=[None, "woff"]) 41def flavor(request): 42 return request.param 43 44 45def test_ttLib_main_as_subprocess(ttfont_path): 46 subprocess.run( 47 [sys.executable, "-m", "fontTools.ttLib", str(ttfont_path)], check=True 48 ) 49 50 51def test_ttLib_open_ttfont(ttfont_path): 52 __main__.main([str(ttfont_path)]) 53 54 55def test_ttLib_open_save_ttfont(tmp_path, ttfont_path, flavor): 56 output_path = tmp_path / "TestTTF-Regular.ttf" 57 args = ["-o", str(output_path), str(ttfont_path)] 58 if flavor is not None: 59 args.extend(["--flavor", flavor]) 60 61 __main__.main(args) 62 63 assert output_path.exists() 64 assert TTFont(output_path).getGlyphOrder() == TTFont(ttfont_path).getGlyphOrder() 65 66 67def test_ttLib_open_ttcollection(ttcollection_path): 68 __main__.main(["-y", "0", str(ttcollection_path)]) 69 70 71def test_ttLib_open_ttcollection_save_single_font(tmp_path, ttcollection_path, flavor): 72 for i in range(2): 73 output_path = tmp_path / f"TestTTF-Regular#{i}.ttf" 74 args = ["-y", str(i), "-o", str(output_path), str(ttcollection_path)] 75 if flavor is not None: 76 args.extend(["--flavor", flavor]) 77 78 __main__.main(args) 79 80 assert output_path.exists() 81 assert ( 82 TTFont(output_path).getGlyphOrder() 83 == TTCollection(ttcollection_path)[i].getGlyphOrder() 84 ) 85 86 87def test_ttLib_open_ttcollection_save_ttcollection(tmp_path, ttcollection_path): 88 output_path = tmp_path / "TestTTF.ttc" 89 90 __main__.main(["-o", str(output_path), str(ttcollection_path)]) 91 92 assert output_path.exists() 93 assert len(TTCollection(output_path)) == len(TTCollection(ttcollection_path)) 94 95 96def test_ttLib_open_multiple_fonts_save_ttcollection(tmp_path, ttfont_path): 97 output_path = tmp_path / "TestTTF.ttc" 98 99 __main__.main(["-o", str(output_path), str(ttfont_path), str(ttfont_path)]) 100 101 assert output_path.exists() 102 103 coll = TTCollection(output_path) 104 assert len(coll) == 2 105 assert coll[0].getGlyphOrder() == coll[1].getGlyphOrder() 106