|
1 |
| -from manim import PangoText, START_X, START_Y, SVGMobject |
| 1 | +"""Tests :class:`PangoText` by comparing SVG files created. |
| 2 | +""" |
2 | 3 | import os
|
| 4 | +import re |
| 5 | + |
3 | 6 | import cairocffi
|
4 | 7 | import pangocairocffi
|
5 | 8 | import pangocffi
|
| 9 | +from manim import START_X, START_Y, PangoText, SVGMobject |
6 | 10 |
|
7 |
| -rtl_text = """صباح الخير |
| 11 | +RTL_TEXT: str = """صباح الخير |
8 | 12 | مرحبا جميعا"""
|
9 | 13 |
|
10 |
| -width = 600 |
11 |
| -height = 400 |
| 14 | +WIDTH: int = 600 |
| 15 | +HEIGTH: int = 400 |
| 16 | +folder: str = os.path.abspath(os.path.join("media", "texts")) |
| 17 | +filename: str = os.path.join(folder, "hello.svg") |
| 18 | + |
| 19 | + |
| 20 | +def remove_last_M(file_path: str) -> None: # pylint: disable=invalid-name |
| 21 | + """Format SVG file so that it can be compared""" |
| 22 | + with open(file_path, "r") as fpr: |
| 23 | + content = fpr.read() |
| 24 | + content = re.sub(r'Z M [^A-Za-z]*? "\/>', 'Z "/>', content) |
| 25 | + with open(file_path, "w") as fpw: |
| 26 | + fpw.write(content) |
| 27 | + |
| 28 | + |
| 29 | +def compare_SVGObject_with_PangoText( # pylint: disable=invalid-name |
| 30 | + text: PangoText, svg_path: str |
| 31 | +) -> bool: |
| 32 | + """Checks for the path_string formed by PangoText and Formed SVG file. |
| 33 | + Uses SVGMobject as it parses the SVG and returns the path_string |
| 34 | + """ |
| 35 | + remove_last_M(svg_path) # to prevent issue displaying |
| 36 | + svg = SVGMobject(svg_path) |
| 37 | + assert len(text.submobjects) == len(svg.submobjects) |
| 38 | + assert (text.points == svg.points).all() |
| 39 | + for i in range(len(text.submobjects)): |
| 40 | + assert text.submobjects[i].path_string == svg.submobjects[i].path_string |
| 41 | + assert ( |
| 42 | + text.submobjects[i].sheen_direction == svg.submobjects[i].sheen_direction |
| 43 | + ).all() |
| 44 | + assert ( |
| 45 | + text.submobjects[i].stroke_rgbas == svg.submobjects[i].stroke_rgbas |
| 46 | + ).all() |
| 47 | + return True |
12 | 48 |
|
13 | 49 |
|
14 |
| -def test_general_text_svgobject(): |
15 |
| - """Checks number of submobjects generated when directly called using ``SVGMobject``""" |
| 50 | +def test_general_text_svgobject() -> None: |
| 51 | + """Checks number of submobjects generated when directly |
| 52 | + called using ``SVGMobject`` |
| 53 | + """ |
16 | 54 | text = "hello"
|
17 | 55 | size = 1
|
18 |
| - folder = os.path.abspath(os.path.join("media", "texts")) |
19 |
| - if not os.path.exists(folder): |
20 |
| - os.makedirs(folder) |
21 |
| - filename = os.path.join(folder, "hello.svg") |
22 |
| - a = PangoText(text, size=10) |
23 |
| - pangoManim = os.path.join(folder, a.text2hash() + ".svg") |
24 |
| - surface = cairocffi.SVGSurface(filename, width, height) |
| 56 | + temp_pango_text = PangoText(text, size=size) |
| 57 | + surface = cairocffi.SVGSurface(filename, WIDTH, HEIGTH) |
25 | 58 | context = cairocffi.Context(surface)
|
26 | 59 | context.move_to(START_X, START_Y)
|
27 | 60 | layout = pangocairocffi.create_layout(context)
|
28 |
| - layout.set_width(pangocffi.units_from_double(width)) |
| 61 | + layout.set_width(pangocffi.units_from_double(WIDTH)) |
29 | 62 | fontdesc = pangocffi.FontDescription()
|
30 | 63 | fontdesc.set_size(pangocffi.units_from_double(size * 10))
|
31 | 64 | layout.set_font_description(fontdesc)
|
32 | 65 | layout.set_text(text)
|
33 | 66 | pangocairocffi.show_layout(context, layout)
|
34 | 67 | surface.finish()
|
35 |
| - b = SVGMobject(filename) |
36 |
| - assert len(a.submobjects) == len(b.submobjects) |
| 68 | + assert compare_SVGObject_with_PangoText(temp_pango_text, filename) |
37 | 69 |
|
38 | 70 |
|
39 |
| -def test_rtl_text_text_svgobject(): |
40 |
| - """Checks number of submobjects generated when directly called using ``SVGMobject``""" |
| 71 | +def test_rtl_text_to_svgobject() -> None: |
| 72 | + """Checks number of submobjects generated when directly |
| 73 | + called using ``SVGMobject``""" |
41 | 74 | size = 1
|
42 |
| - text = rtl_text |
43 |
| - folder = os.path.abspath(os.path.join("media", "texts")) |
44 |
| - if not os.path.exists(folder): |
45 |
| - os.makedirs(folder) |
46 |
| - filename = os.path.join(folder, "hello.svg") |
47 |
| - a = PangoText(text, size=1) |
48 |
| - pangoManim = os.path.join(folder, a.text2hash() + ".svg") |
49 |
| - surface = cairocffi.SVGSurface(filename, width, height) |
| 75 | + text = RTL_TEXT.replace("\n", "") |
| 76 | + temp_pango_text = PangoText(text, size=1) |
| 77 | + surface = cairocffi.SVGSurface(filename, WIDTH, HEIGTH) |
50 | 78 | context = cairocffi.Context(surface)
|
51 | 79 | context.move_to(START_X, START_Y)
|
52 | 80 | layout = pangocairocffi.create_layout(context)
|
53 |
| - layout.set_width(pangocffi.units_from_double(width)) |
| 81 | + layout.set_width(pangocffi.units_from_double(WIDTH)) |
54 | 82 | fontdesc = pangocffi.FontDescription()
|
55 | 83 | fontdesc.set_size(pangocffi.units_from_double(size * 10))
|
56 | 84 | layout.set_font_description(fontdesc)
|
57 | 85 | layout.set_text(text)
|
58 | 86 | pangocairocffi.show_layout(context, layout)
|
59 | 87 | surface.finish()
|
60 |
| - b = SVGMobject(filename) |
61 |
| - assert len(a.submobjects) == len(b.submobjects) |
| 88 | + assert compare_SVGObject_with_PangoText(temp_pango_text, filename) |
62 | 89 |
|
63 | 90 |
|
64 |
| -def test_font_face(): |
| 91 | +def test_font_face() -> None: |
65 | 92 | """Checks font face using submobject len"""
|
66 | 93 | size = 1
|
67 |
| - text = rtl_text |
| 94 | + text = RTL_TEXT.replace("\n", "") |
68 | 95 | font_face = "sans"
|
69 |
| - folder = os.path.abspath(os.path.join("media", "texts")) |
70 |
| - if not os.path.exists(folder): |
71 |
| - os.makedirs(folder) |
72 |
| - filename = os.path.join(folder, "hello.svg") |
73 |
| - a = PangoText(text, size=1, font=font_face) |
74 |
| - pangoManim = os.path.join(folder, a.text2hash() + ".svg") |
75 |
| - surface = cairocffi.SVGSurface(filename, width, height) |
| 96 | + temp_pango_text = PangoText(text, size=1, font=font_face) |
| 97 | + surface = cairocffi.SVGSurface(filename, WIDTH, HEIGTH) |
76 | 98 | context = cairocffi.Context(surface)
|
77 | 99 | context.move_to(START_X, START_Y)
|
78 | 100 | layout = pangocairocffi.create_layout(context)
|
79 |
| - layout.set_width(pangocffi.units_from_double(width)) |
| 101 | + layout.set_width(pangocffi.units_from_double(WIDTH)) |
80 | 102 | fontdesc = pangocffi.FontDescription()
|
81 | 103 | fontdesc.set_family(font_face)
|
82 | 104 | fontdesc.set_size(pangocffi.units_from_double(size * 10))
|
83 | 105 | layout.set_font_description(fontdesc)
|
84 | 106 | layout.set_text(text)
|
85 | 107 | pangocairocffi.show_layout(context, layout)
|
86 | 108 | surface.finish()
|
87 |
| - b = SVGMobject(filename) |
88 |
| - assert len(a.submobjects) == len(b.submobjects) |
| 109 | + assert compare_SVGObject_with_PangoText(temp_pango_text, filename) |
89 | 110 |
|
90 | 111 |
|
91 |
| -def test_whether_svg_file_created(): |
| 112 | +def test_whether_svg_file_created() -> None: |
92 | 113 | """Checks Whether SVG file is created in desired location"""
|
93 |
| - a = PangoText("hello", size=1) |
94 |
| - folder = os.path.abspath(os.path.join("media", "texts")) |
95 |
| - if not os.path.exists(folder): |
96 |
| - os.makedirs(folder) |
97 |
| - theoPath = os.path.abspath(os.path.join(folder, a.text2hash() + ".svg")) |
98 |
| - actualPath = os.path.abspath(a.text2svg()) |
99 |
| - assert theoPath == actualPath |
| 114 | + temp_pango_text = PangoText("hello", size=1) |
| 115 | + theo_path = os.path.abspath( |
| 116 | + os.path.join(folder, temp_pango_text.text2hash() + ".svg") |
| 117 | + ) |
| 118 | + actual_path = os.path.abspath(temp_pango_text.text2svg()) |
| 119 | + assert theo_path == actual_path |
100 | 120 |
|
101 | 121 |
|
102 |
| -def test_tabs_replace(): |
103 |
| - """Checks whether are there in end svg image. Pango should handle tabs and line breaks.""" |
| 122 | +def test_tabs_replace() -> None: |
| 123 | + """Checks whether are there in end svg image. |
| 124 | + Pango should handle tabs and line breaks.""" |
104 | 125 | size = 1
|
105 |
| - folder = os.path.abspath(os.path.join("media", "texts")) |
106 |
| - if not os.path.exists(folder): |
107 |
| - os.makedirs(folder) |
108 |
| - a = PangoText("hello\thi\nf") |
109 |
| - assert a.text == "hellohif" |
110 |
| - pangoManim = os.path.join(folder, a.text2hash() + ".svg") |
111 |
| - filename = os.path.join(folder, "hello.svg") |
112 |
| - surface = cairocffi.SVGSurface(filename, width, height) |
| 126 | + temp_pango_text = PangoText("hello\thi\nf") |
| 127 | + assert temp_pango_text.text == "hellohif" |
| 128 | + surface = cairocffi.SVGSurface(filename, WIDTH, HEIGTH) |
113 | 129 | context = cairocffi.Context(surface)
|
114 | 130 | context.move_to(START_X, START_Y)
|
115 | 131 | layout = pangocairocffi.create_layout(context)
|
116 |
| - layout.set_width(pangocffi.units_from_double(width)) |
| 132 | + layout.set_width(pangocffi.units_from_double(WIDTH)) |
117 | 133 | fontdesc = pangocffi.FontDescription()
|
118 | 134 | fontdesc.set_size(pangocffi.units_from_double(size * 10))
|
119 | 135 | layout.set_font_description(fontdesc)
|
120 |
| - layout.set_text("hello\thi\nf") |
| 136 | + layout.set_text("hellohif") |
121 | 137 | pangocairocffi.show_layout(context, layout)
|
122 | 138 | surface.finish()
|
123 |
| - b = SVGMobject(filename) |
124 |
| - assert len(a.submobjects) == len(b.submobjects) |
| 139 | + assert compare_SVGObject_with_PangoText(temp_pango_text, filename) |
0 commit comments