Debian Codegraph @

tests/test_fonts.py

raw symbols SCIP
1 import subprocess
2
3 import pytest
4 from reportlab.pdfbase.ttfonts import TTFOpenFile, TTFError
5 from svglib.fonts import (
6 DEFAULT_FONT_NAME, STANDARD_FONT_NAMES, FontMap, get_global_font_map
7 )
8 from svglib.svglib import (
9 Svg2RlgAttributeConverter, SvgRenderer, find_font, register_font,
10 )
11 from tests.utils import drawing_from_svg
12
13 try:
14 TTFOpenFile('times.ttf')
15 HAS_TIMES_FONT = True
16 except TTFError:
17 HAS_TIMES_FONT = False
18
19
20 @pytest.mark.parametrize("family,weight,style,expected", [
21 ("Times New Roman", "normal", "normal", "Times New Roman"),
22 ("Times New Roman", "bold", "normal", "Times New Roman-Bold"),
23 ("Times New Roman", "bold", "italic", "Times New Roman-BoldItalic"),
24 ("Times New Roman", "BOLD", "italic", "Times New Roman-BoldItalic"),
25 ("Times New Roman", "BOld", "ITalic", "Times New Roman-BoldItalic"),
26 ("Times New Roman", "DemiBold", "SomeStyle", "Times New Roman-DemiboldSomestyle"),
27 ("Times New Roman", "600", "SomeStyle", "Times New Roman-600Somestyle"),
28 ("Times New Roman", 600, "SomeStyle", "Times New Roman-600Somestyle"),
29 ("Courier New", "bold", "normal", "Courier New-Bold"),
30 ])
31 def test_internal_names(family, weight, style, expected):
32 assert FontMap.build_internal_name(family, weight, style) == expected
33
34
35 @pytest.mark.parametrize("family, path, weight, style, rlgName, expected", [
36 # no path, no reportlab name -> None result
37 ("Times New Roman", None, "normal", "normal", None, None),
38 # mapping to standard font
39 ("Times New Roman", None, "bold", "normal", "Times-Bold", ("Times New Roman-Bold", True)),
40 # mapping to standard font
41 ("Times New Roman", None, "bold", "italic", 'Times-BoldItalic',
42 ("Times New Roman-BoldItalic", True)),
43 # may fail on systems without times.ttf
44 ("Times New Roman", 'times.ttf', "normal", "normal", None, ("Times New Roman", True)),
45 ("Unknown Font", 'unknown_font.ttf', "normal", "normal", None, (None, False))])
46 def test_register_return(family, path, weight, style, rlgName, expected):
47 """
48 Check if the result of the register_font function matches the expected results
49 """
50 if path == 'times.ttf' and not HAS_TIMES_FONT:
51 pytest.skip('times.ttf is not installed on this system')
52 converter = SvgRenderer('../')
53 converter.font_map = FontMap()
54 assert converter.font_map.register_font(family, path, weight, style, rlgName) == expected
55
56
57 @pytest.mark.parametrize("svgname, fontname", [
58 ("sans-serif", "Helvetica"),
59 ("serif", "Times-Roman"),
60 ("times", "Times-Roman"),
61 ("monospace", "Courier"),
62 ])
63 def test_convertFontFamily_defaults(svgname, fontname):
64 attrib_converter = Svg2RlgAttributeConverter()
65 name = attrib_converter.convertFontFamily(svgname)
66 assert name == fontname
67
68
69 @pytest.mark.parametrize("fontname", list(STANDARD_FONT_NAMES))
70 def test_find_font_defaults(fontname):
71 name, exact = find_font(fontname)
72 assert name == fontname
73 assert exact is True
74
75
76 def test_plain_text():
77 drawing = drawing_from_svg('''
78 <?xml version="1.0"?>
79 <svg width="777" height="267" xml:space="preserve">
80 <text style="fill:#000000; stroke:none; font-size:28;">
81 <tspan>TITLE 1</tspan>
82 <tspan x="-10.761" y="33.487">Subtitle</tspan>
83 </text>
84 </svg>
85 ''')
86 main_group = drawing.contents[0]
87 assert main_group.contents[0].contents[1].fontName == DEFAULT_FONT_NAME
88
89
90 def test_fontfamily_text():
91 drawing = drawing_from_svg('''
92 <?xml version="1.0"?>
93 <svg width="777" height="267" xml:space="preserve">
94 <text style="fill:#000000; stroke:none; font-size:28;font-family:'Times-Roman';">
95 <tspan>TITLE 1</tspan>
96 <tspan x="-10.761" y="33.487">Subtitle</tspan>
97 </text>
98 </svg>
99 ''')
100 main_group = drawing.contents[0]
101 assert main_group.contents[0].contents[1].fontName == 'Times-Roman'
102 drawing = drawing_from_svg('''
103 <?xml version="1.0"?>
104 <svg width="777" height="267" xml:space="preserve">
105 <text style="fill:#000000; stroke:none; font-size:28;font-family:'Courier';">
106 <tspan>TITLE 1</tspan>
107 <tspan x="-10.761" y="33.487">Subtitle</tspan>
108 </text>
109 </svg>
110 ''')
111 main_group = drawing.contents[0]
112 assert main_group.contents[0].contents[1].fontName == 'Courier'
113
114
115 @pytest.mark.skipif(not HAS_TIMES_FONT, reason="times.ttf is not available")
116 def test_fontfamily_reg_text():
117 name, exact = register_font('MyFont', 'times.ttf')
118 assert name == 'MyFont'
119 assert exact is True
120 drawing = drawing_from_svg('''
121 <?xml version="1.0"?>
122 <svg width="777" height="267" xml:space="preserve">
123 <text style="fill:#000000; stroke:none; font-size:28;font-family:'MyFont';">
124 <tspan>TITLE 1</tspan>
125 <tspan x="-10.761" y="33.487">Subtitle</tspan>
126 </text>
127 </svg>
128 ''')
129 main_group = drawing.contents[0]
130 assert main_group.contents[0].contents[1].fontName == 'MyFont'
131
132
133 def test_fontfamily_weight_text():
134 drawing = drawing_from_svg('''
135 <?xml version="1.0"?>
136 <svg width="777" height="267" xml:space="preserve">
137 <text style="fill:#000000; stroke:none; font-size:28;
138 font-family:'Times New Roman';font-weight:bold;">
139 <tspan>TITLE 1</tspan>
140 <tspan x="-10.761" y="33.487">Subtitle</tspan>
141 </text>
142 </svg>
143 ''')
144 main_group = drawing.contents[0]
145 assert main_group.contents[0].contents[1].fontName == 'Times-Bold'
146 drawing = drawing_from_svg('''
147 <?xml version="1.0"?>
148 <svg width="777" height="267" xml:space="preserve">
149 <text style="fill:#000000; stroke:none; font-size:28;
150 font-family:'Courier New';font-weight:bold;">
151 <tspan>TITLE 1</tspan>
152 <tspan x="-10.761" y="33.487">Subtitle</tspan>
153 </text>
154 </svg>
155 ''')
156 main_group = drawing.contents[0]
157 assert main_group.contents[0].contents[1].fontName == 'Courier-Bold'
158 drawing = drawing_from_svg('''
159 <?xml version="1.0"?>
160 <svg width="777" height="267" xml:space="preserve">
161 <text style="fill:#000000; stroke:none; font-size:28;">
162 <tspan style="font-family:'Courier New';font-weight:bold;">TITLE 1</tspan>
163 <tspan x="-10.761" y="33.487">Subtitle</tspan>
164 </text>
165 </svg>
166 ''')
167 main_group = drawing.contents[0]
168 assert main_group.contents[0].contents[1].fontName == 'Courier-Bold'
169 drawing = drawing_from_svg('''
170 <?xml version="1.0"?>
171 <svg width="777" height="267" xml:space="preserve">
172 <text style="fill:#000000; stroke:none; font-size:28;">
173 <tspan style="font-family:'Courier';font-weight:bold;">TITLE 1</tspan>
174 <tspan x="-10.761" y="33.487">Subtitle</tspan>
175 </text>
176 </svg>
177 ''')
178 main_group = drawing.contents[0]
179 assert main_group.contents[0].contents[1].fontName == 'Courier-Bold'
180
181
182 def test_fontfamily_style_text():
183 drawing = drawing_from_svg('''
184 <?xml version="1.0"?>
185 <svg width="777" height="267" xml:space="preserve">
186 <text style="fill:#000000; stroke:none; font-size:28;font-family:'Times New Roman';
187 font-style:italic;">
188 <tspan>TITLE 1</tspan>
189 <tspan x="-10.761" y="33.487">Subtitle</tspan>
190 </text>
191 </svg>
192 ''')
193 main_group = drawing.contents[0]
194 assert main_group.contents[0].contents[1].fontName == 'Times-Italic'
195 drawing = drawing_from_svg('''
196 <?xml version="1.0"?>
197 <svg width="777" height="267" xml:space="preserve">
198 <text style="fill:#000000; stroke:none; font-size:28;font-family:'Courier New';
199 font-style:Italic;">
200 <tspan>TITLE 1</tspan>
201 <tspan x="-10.761" y="33.487">Subtitle</tspan>
202 </text>
203 </svg>
204 ''')
205 main_group = drawing.contents[0]
206 assert main_group.contents[0].contents[1].fontName == 'Courier-Oblique'
207 drawing = drawing_from_svg('''
208 <?xml version="1.0"?>
209 <svg width="777" height="267" xml:space="preserve">
210 <text style="fill:#000000; stroke:none; font-size:28;">
211 <tspan style="font-family:'Courier New';font-style:italic;">TITLE 1</tspan>
212 <tspan x="-10.761" y="33.487">Subtitle</tspan>
213 </text>
214 </svg>
215 ''')
216 main_group = drawing.contents[0]
217 assert main_group.contents[0].contents[1].fontName == 'Courier-Oblique'
218
219
220 def test_fontfamily_weight_style_text():
221 drawing = drawing_from_svg('''
222 <?xml version="1.0"?>
223 <svg width="777" height="267" xml:space="preserve">
224 <text style="fill:#000000; stroke:none; font-size:28;font-family:'Times New Roman';
225 font-style:italic;font-weight:bold;">
226 <tspan>TITLE 1</tspan>
227 <tspan x="-10.761" y="33.487">Subtitle</tspan>
228 </text>
229 </svg>
230 ''')
231 main_group = drawing.contents[0]
232 assert main_group.contents[0].contents[1].fontName == 'Times-BoldItalic'
233 drawing = drawing_from_svg('''
234 <?xml version="1.0"?>
235 <svg width="777" height="267" xml:space="preserve">
236 <text style="fill:#000000; stroke:none; font-size:28;font-family:'Courier New';
237 font-style:Italic;font-weight:BOLD;">
238 <tspan>TITLE 1</tspan>
239 <tspan x="-10.761" y="33.487">Subtitle</tspan>
240 </text>
241 </svg>
242 ''')
243 main_group = drawing.contents[0]
244 assert main_group.contents[0].contents[1].fontName == 'Courier-BoldOblique'
245 drawing = drawing_from_svg('''
246 <?xml version="1.0"?>
247 <svg width="777" height="267" xml:space="preserve">
248 <text style="fill:#000000; stroke:none; font-size:28;">
249 <tspan style="font-family:'Courier New';font-style:italic;
250 font-weight:bold;">TITLE 1</tspan>
251 <tspan x="-10.761" y="33.487">Subtitle</tspan>
252 </text>
253 </svg>
254 ''')
255 main_group = drawing.contents[0]
256 assert main_group.contents[0].contents[1].fontName == 'Courier-BoldOblique'
257 drawing = drawing_from_svg('''
258 <?xml version="1.0"?>
259 <svg width="777" height="267" xml:space="preserve">
260 <text style="fill:#000000; stroke:none; font-size:28;">
261 <tspan style="font-family:'Courier';font-style:italic;
262 font-weight:bold;">TITLE 1</tspan>
263 <tspan x="-10.761" y="33.487">Subtitle</tspan>
264 </text>
265 </svg>
266 ''')
267 main_group = drawing.contents[0]
268 assert main_group.contents[0].contents[1].fontName == 'Courier-BoldOblique'
269
270
271 def test_failed_registration():
272 fontname, exact = register_font(font_name="unknown path", font_path="/home/unknown_font.tff")
273 assert fontname is None
274 assert exact is False
275
276
277 def test_font_family():
278 def font_config_available():
279 try:
280 subprocess.call(["fc-match"])
281 except OSError:
282 return False
283 return True
284
285 converter = Svg2RlgAttributeConverter()
286 # Check PDF standard names are untouched
287 assert converter.convertFontFamily('ZapfDingbats') == 'ZapfDingbats'
288 assert converter.convertFontFamily('bilbo ZapfDingbats') == 'ZapfDingbats'
289 assert converter.convertFontFamily(' bilbo ZapfDingbats ') == 'ZapfDingbats'
290 assert converter.convertFontFamily(' bilbo, ZapfDingbats ') == 'ZapfDingbats'
291 if font_config_available():
292 # Fontconfig will always provide at least a default font and register
293 # that font under the provided font name.
294 assert converter.convertFontFamily('SomeFont') == 'SomeFont'
295 # Should be cached anyway
296 assert 'SomeFont' in get_global_font_map()._map
297 else:
298 # Unknown fonts are converted to Helvetica by default.
299 assert converter.convertFontFamily('SomeFont') == 'Helvetica'
300 # Check font names with spaces
301 assert converter.split_attr_list("'Open Sans', Arial, 'New Times Roman'") == [
302 'Open Sans', 'Arial', 'New Times Roman'
303 ]