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