File: test_fonts.py

package info (click to toggle)
manimpango 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,804 kB
  • sloc: python: 1,252; sh: 164; makefile: 18
file content (158 lines) | stat: -rw-r--r-- 4,950 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
import os
import subprocess
import sys
import textwrap
from contextlib import contextmanager
from pathlib import Path
from shutil import copyfile

import pytest

import manimpango

from . import FONT_DIR
from ._manim import MarkupText


@contextmanager
def register_font(font_file: Path):
    font_file = os.fspath(font_file)
    init = manimpango.list_fonts()
    assert manimpango.register_font(font_file), "Invalid Font possibly."
    final = manimpango.list_fonts()
    yield list(set(final) - set(init))[0]
    assert manimpango.unregister_font(font_file), "Can't unregister Font"


def font_list():
    _t = [
        (FONT_DIR / "AdobeVFPrototype.ttf").absolute(),
        (FONT_DIR / "BungeeColor-Regular_colr_Windows.ttf").absolute(),
        (FONT_DIR / "MaShanZheng-Regular.ttf").absolute(),
    ]
    _d = {}
    for i in _t:
        with register_font(i) as f:
            _d[i] = f
    return _d


font_lists_dict = font_list()


def test_unicode_font_name(tmpdir):
    final_font = str(Path(tmpdir, "庞门正.ttf").absolute())
    copyfile(FONT_DIR / "AdobeVFPrototype.ttf", final_font)
    assert manimpango.register_font(final_font)
    assert manimpango.unregister_font(final_font)


@pytest.mark.parametrize("font_name", font_lists_dict)
def test_register_and_unregister_font(font_name):
    intial = manimpango.list_fonts()
    assert manimpango.register_font(str(font_name)), "Invalid Font possibly."
    final = manimpango.list_fonts()
    assert intial != final
    assert manimpango.unregister_font(os.fspath(font_name)), "Can't unregister font."
    assert intial == manimpango.list_fonts()


@pytest.mark.skipif(
    sys.platform.startswith("linux"), reason="no warning are raised for linux"
)
@pytest.mark.parametrize("font_file,font_name", font_lists_dict.items())
def test_warning(font_file, font_name):
    # this tests need to be run in a separate subprocess because
    # of fontmap cache in Pango.
    command = textwrap.dedent(
        f"""\
            from tests import set_dll_search_path
            set_dll_search_path() # this is needed on Windows to run
            import manimpango
            import os
            from tests._manim import Text
            font_file = r'{os.fspath(font_file)}'
            font_name = r'{font_name}'
            intial = manimpango.list_fonts()
            manimpango.register_font(os.fspath(font_file))
            final = manimpango.list_fonts()
            assert intial != final
            Text("Testing", font=font_name)
            manimpango.unregister_font(os.fspath(font_file))
        """
    )
    a = subprocess.run(
        [sys.executable, "-c", command],
        check=True,
        stderr=subprocess.PIPE,
        cwd=Path(__file__).parent.parent,
    )
    captured = a.stderr.decode()
    assert "Pango-WARNING **" not in captured, "Looks like Pango raised a warning?"


@pytest.mark.skipif(
    sys.platform.startswith("linux"), reason="unsupported api for linux"
)
@pytest.mark.parametrize("font_name", font_lists_dict)
@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="always returns true")
def test_fail_just_unregister(font_name):
    assert not manimpango.unregister_font(
        str(font_name)
    ), "Failed to unregister the font"


@pytest.mark.skipif(
    sys.platform.startswith("win32"), reason="unsupported api for win32"
)
@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="unsupported api for mac")
def test_unregister_not_fail_linux():
    assert manimpango.unregister_font("random")


@pytest.mark.skipif(
    sys.platform.startswith("linux"), reason="unsupported api for linux"
)
def test_adding_dummy_font(tmpdir):
    dummy = tmpdir / "font.ttf"
    with open(dummy, "wb") as f:
        f.write(b"dummy")

    assert not manimpango.register_font(str(dummy)), "Registered a dummy font?"


def test_simple_fonts_render(tmpdir):
    filename = str(Path(tmpdir) / "hello.svg")
    MarkupText("Hello World", filename=filename)
    assert Path(filename).exists()


@pytest.mark.skipif(
    not sys.platform.startswith("linux"), reason="unsupported api other than linux"
)
def test_both_fc_and_register_font_are_same():
    assert (
        manimpango._register_font._fc_register_font
        == manimpango._register_font._register_font
    )
    assert (
        manimpango._register_font._fc_unregister_font
        == manimpango._register_font._unregister_font
    )


@pytest.mark.parametrize("font_file", font_lists_dict)
def test_fc_font_register(setup_fontconfig, font_file):
    intial = manimpango.list_fonts()
    assert manimpango.fc_register_font(str(font_file)), "Invalid Font possibly."
    final = manimpango.list_fonts()
    assert intial != final


def test_fc_font_unregister(setup_fontconfig):
    # it will remove everything
    intial = manimpango.list_fonts()
    manimpango.fc_unregister_font("clear")
    final = manimpango.list_fonts()
    assert intial != final