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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
import subprocess
import pytest
from reportlab.pdfbase.ttfonts import TTFOpenFile, TTFError
from svglib.fonts import (
DEFAULT_FONT_NAME, STANDARD_FONT_NAMES, FontMap, get_global_font_map
)
from svglib.svglib import (
Svg2RlgAttributeConverter, SvgRenderer, find_font, register_font,
)
from tests.utils import drawing_from_svg
try:
TTFOpenFile('times.ttf')
HAS_TIMES_FONT = True
except TTFError:
HAS_TIMES_FONT = False
@pytest.mark.parametrize("family,weight,style,expected", [
("Times New Roman", "normal", "normal", "Times New Roman"),
("Times New Roman", "bold", "normal", "Times New Roman-Bold"),
("Times New Roman", "bold", "italic", "Times New Roman-BoldItalic"),
("Times New Roman", "BOLD", "italic", "Times New Roman-BoldItalic"),
("Times New Roman", "BOld", "ITalic", "Times New Roman-BoldItalic"),
("Times New Roman", "DemiBold", "SomeStyle", "Times New Roman-DemiboldSomestyle"),
("Times New Roman", "600", "SomeStyle", "Times New Roman-600Somestyle"),
("Times New Roman", 600, "SomeStyle", "Times New Roman-600Somestyle"),
("Courier New", "bold", "normal", "Courier New-Bold"),
])
def test_internal_names(family, weight, style, expected):
assert FontMap.build_internal_name(family, weight, style) == expected
@pytest.mark.parametrize("family, path, weight, style, rlgName, expected", [
# no path, no reportlab name -> None result
("Times New Roman", None, "normal", "normal", None, None),
# mapping to standard font
("Times New Roman", None, "bold", "normal", "Times-Bold", ("Times New Roman-Bold", True)),
# mapping to standard font
("Times New Roman", None, "bold", "italic", 'Times-BoldItalic',
("Times New Roman-BoldItalic", True)),
# may fail on systems without times.ttf
("Times New Roman", 'times.ttf', "normal", "normal", None, ("Times New Roman", True)),
("Unknown Font", 'unknown_font.ttf', "normal", "normal", None, (None, False))])
def test_register_return(family, path, weight, style, rlgName, expected):
"""
Check if the result of the register_font function matches the expected results
"""
if path == 'times.ttf' and not HAS_TIMES_FONT:
pytest.skip('times.ttf is not installed on this system')
converter = SvgRenderer('../')
converter.font_map = FontMap()
assert converter.font_map.register_font(family, path, weight, style, rlgName) == expected
@pytest.mark.parametrize("svgname, fontname", [
("sans-serif", "Helvetica"),
("serif", "Times-Roman"),
("times", "Times-Roman"),
("monospace", "Courier"),
])
def test_convertFontFamily_defaults(svgname, fontname):
attrib_converter = Svg2RlgAttributeConverter()
name = attrib_converter.convertFontFamily(svgname)
assert name == fontname
@pytest.mark.parametrize("fontname", list(STANDARD_FONT_NAMES))
def test_find_font_defaults(fontname):
name, exact = find_font(fontname)
assert name == fontname
assert exact is True
def test_plain_text():
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == DEFAULT_FONT_NAME
def test_fontfamily_text():
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;font-family:'Times-Roman';">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Times-Roman'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;font-family:'Courier';">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier'
@pytest.mark.skipif(not HAS_TIMES_FONT, reason="times.ttf is not available")
def test_fontfamily_reg_text():
name, exact = register_font('MyFont', 'times.ttf')
assert name == 'MyFont'
assert exact is True
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;font-family:'MyFont';">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'MyFont'
def test_fontfamily_weight_text():
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;
font-family:'Times New Roman';font-weight:bold;">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Times-Bold'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;
font-family:'Courier New';font-weight:bold;">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier-Bold'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;">
<tspan style="font-family:'Courier New';font-weight:bold;">TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier-Bold'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;">
<tspan style="font-family:'Courier';font-weight:bold;">TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier-Bold'
def test_fontfamily_style_text():
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;font-family:'Times New Roman';
font-style:italic;">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Times-Italic'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;font-family:'Courier New';
font-style:Italic;">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier-Oblique'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;">
<tspan style="font-family:'Courier New';font-style:italic;">TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier-Oblique'
def test_fontfamily_weight_style_text():
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;font-family:'Times New Roman';
font-style:italic;font-weight:bold;">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Times-BoldItalic'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;font-family:'Courier New';
font-style:Italic;font-weight:BOLD;">
<tspan>TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier-BoldOblique'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;">
<tspan style="font-family:'Courier New';font-style:italic;
font-weight:bold;">TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier-BoldOblique'
drawing = drawing_from_svg('''
<?xml version="1.0"?>
<svg width="777" height="267" xml:space="preserve">
<text style="fill:#000000; stroke:none; font-size:28;">
<tspan style="font-family:'Courier';font-style:italic;
font-weight:bold;">TITLE 1</tspan>
<tspan x="-10.761" y="33.487">Subtitle</tspan>
</text>
</svg>
''')
main_group = drawing.contents[0]
assert main_group.contents[0].contents[1].fontName == 'Courier-BoldOblique'
def test_failed_registration():
fontname, exact = register_font(font_name="unknown path", font_path="/home/unknown_font.tff")
assert fontname is None
assert exact is False
def test_font_family():
def font_config_available():
try:
subprocess.call(["fc-match"])
except OSError:
return False
return True
converter = Svg2RlgAttributeConverter()
# Check PDF standard names are untouched
assert converter.convertFontFamily('ZapfDingbats') == 'ZapfDingbats'
assert converter.convertFontFamily('bilbo ZapfDingbats') == 'ZapfDingbats'
assert converter.convertFontFamily(' bilbo ZapfDingbats ') == 'ZapfDingbats'
assert converter.convertFontFamily(' bilbo, ZapfDingbats ') == 'ZapfDingbats'
if font_config_available():
# Fontconfig will always provide at least a default font and register
# that font under the provided font name.
assert converter.convertFontFamily('SomeFont') == 'SomeFont'
# Should be cached anyway
assert 'SomeFont' in get_global_font_map()._map
else:
# Unknown fonts are converted to Helvetica by default.
assert converter.convertFontFamily('SomeFont') == 'Helvetica'
# Check font names with spaces
assert converter.split_attr_list("'Open Sans', Arial, 'New Times Roman'") == [
'Open Sans', 'Arial', 'New Times Roman'
]
|