| 1 |
|
| 2 |
|
| 3 |
"""Testsuite for svglib. |
| 4 |
|
| 5 |
This tests basic functionality. Run with one of these lines from |
| 6 |
inside the test directory: |
| 7 |
|
| 8 |
py.test -v -s test_basic.py |
| 9 |
""" |
| 10 |
|
| 11 |
import io |
| 12 |
import os |
| 13 |
import pathlib |
| 14 |
import textwrap |
| 15 |
from tempfile import NamedTemporaryFile |
| 16 |
|
| 17 |
from reportlab.graphics.shapes import ( |
| 18 |
_CLOSEPATH, _CURVETO, _LINETO, _MOVETO, Group, Path, Polygon, PolyLine, Rect, Line, |
| 19 |
) |
| 20 |
from reportlab.lib import colors |
| 21 |
from reportlab.lib.units import cm, inch |
| 22 |
from reportlab.pdfgen.canvas import FILL_EVEN_ODD |
| 23 |
|
| 24 |
from svglib import svglib, utils |
| 25 |
from tests.utils import drawing_from_svg, minimal_svg_node |
| 26 |
|
| 27 |
import pytest |
| 28 |
|
| 29 |
|
| 30 |
def _testit(func, mapping): |
| 31 |
"Call `func` on input in mapping and return list of failed tests." |
| 32 |
|
| 33 |
failed = [] |
| 34 |
for input, expected in mapping: |
| 35 |
if isinstance(input, dict): |
| 36 |
result = func(**input) |
| 37 |
else: |
| 38 |
result = func(input) |
| 39 |
if not result == expected: |
| 40 |
failed.append((input, result, expected)) |
| 41 |
|
| 42 |
if failed: |
| 43 |
print("failed tests (input, result, expected):") |
| 44 |
for input, result, expected in failed: |
| 45 |
print(f" {input!r} : {result} != {expected}") |
| 46 |
|
| 47 |
return failed |
| 48 |
|
| 49 |
|
| 50 |
class TestSvg2rlgInput: |
| 51 |
test_content = textwrap.dedent('''\ |
| 52 |
<?xml version="1.0"?> |
| 53 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 54 |
width="1200" height="800" |
| 55 |
viewBox="0 0 36 24"> |
| 56 |
<rect y="10" width="36" height="4"/> |
| 57 |
</svg> |
| 58 |
''') |
| 59 |
|
| 60 |
def test_path_as_string_input(self): |
| 61 |
try: |
| 62 |
with NamedTemporaryFile(mode='w', suffix='.svg', delete=False) as fp: |
| 63 |
fp.write(self.test_content) |
| 64 |
file_path = fp.name |
| 65 |
drawing = svglib.svg2rlg(file_path) |
| 66 |
assert drawing is not None |
| 67 |
finally: |
| 68 |
os.unlink(file_path) |
| 69 |
|
| 70 |
def test_path_as_pathlib_input(self): |
| 71 |
try: |
| 72 |
with NamedTemporaryFile(mode='w', suffix='.svg', delete=False) as fp: |
| 73 |
fp.write(self.test_content) |
| 74 |
file_path = pathlib.Path(fp.name) |
| 75 |
drawing = svglib.svg2rlg(file_path) |
| 76 |
assert drawing is not None |
| 77 |
finally: |
| 78 |
os.unlink(file_path) |
| 79 |
|
| 80 |
def test_filelike_input(self): |
| 81 |
drawing = svglib.svg2rlg(io.StringIO(self.test_content)) |
| 82 |
assert drawing is not None |
| 83 |
|
| 84 |
def test_filehandle_input(self): |
| 85 |
try: |
| 86 |
with NamedTemporaryFile(mode='w', suffix='.svg', delete=False) as fp: |
| 87 |
fp.write(self.test_content) |
| 88 |
file_path = fp.name |
| 89 |
with open(file_path, 'rb') as fp: |
| 90 |
drawing = svglib.svg2rlg(fp) |
| 91 |
assert drawing is not None |
| 92 |
finally: |
| 93 |
os.unlink(file_path) |
| 94 |
|
| 95 |
|
| 96 |
class TestPaths: |
| 97 |
"""Testing path-related code.""" |
| 98 |
|
| 99 |
def test_path_normalisation(self): |
| 100 |
"Test path normalisation." |
| 101 |
|
| 102 |
mapping = ( |
| 103 |
("", |
| 104 |
[]), |
| 105 |
|
| 106 |
("M10 20, L 30 40 ", |
| 107 |
["M", [10, 20], "L", [30, 40]]), |
| 108 |
|
| 109 |
("M10 20, L 40 40Z", |
| 110 |
["M", [10, 20], "L", [40, 40], "Z", []]), |
| 111 |
|
| 112 |
("M10 20, L 30 40 40 40Z", |
| 113 |
["M", [10, 20], "L", [30, 40], "L", [40, 40], "Z", []]), |
| 114 |
|
| 115 |
(" M10 20,L30 40,40 40Z ", |
| 116 |
["M", [10, 20], "L", [30, 40], "L", [40, 40], "Z", []]), |
| 117 |
|
| 118 |
(" M 10 20, M 30 40, L 40 40, Z M 40 50, l 50 60, Z", |
| 119 |
["M", [10, 20], "L", [30, 40], "L", [40, 40], "Z", [], |
| 120 |
"M", [40, 50], "l", [50, 60], "Z", []]), |
| 121 |
|
| 122 |
(" m 10 20, m 30 40, l 40 40, z", |
| 123 |
["m", [10, 20], "l", [30, 40], "l", [40, 40], "z", []]), |
| 124 |
|
| 125 |
(" m 10,20 30,40, l 40 40, z ", |
| 126 |
["m", [10, 20], "l", [30, 40], "l", [40, 40], "z", []]), |
| 127 |
|
| 128 |
("M 10,20 30,40, l 40 40, z", |
| 129 |
["M", [10, 20], "L", [30, 40], "l", [40, 40], "z", []]), |
| 130 |
|
| 131 |
("M0,0 500,300M500,0 0,300", |
| 132 |
["M", [0, 0], "L", [500, 300], "M", [500, 0], "L", [0, 300]]), |
| 133 |
|
| 134 |
("M10 20, l 5e-5,0", |
| 135 |
["M", [10, 20], "l", [5e-5, 0]]), |
| 136 |
|
| 137 |
("a1.518 1.518 0 011.195-.547 2.401,2.401,0,00-.602,-.297 ", |
| 138 |
["a", [1.518, 1.518, 0, 0, 1, 1.195, -0.547], |
| 139 |
"a", [2.401, 2.401, 0, 0, 0, -0.602, -0.297]]), |
| 140 |
|
| 141 |
("m246.026 120.178c-.558-.295-1.186-.768-1.395-1.054-.314-.438-.132-.456 1.163-.104 " |
| 142 |
"2.318.629 3.814.383 5.298-.873l1.308-1.103 1.54.784c.848.428 1.748.725 " |
| 143 |
"2.008.656.667-.176 2.05-1.95 2.005-2.564-.054-.759.587-.568.896.264.615 1.631-.281 " |
| 144 |
"3.502-1.865 3.918-.773.201-1.488.127-2.659-.281-1.438-.502-1.684-.494-2.405.058" |
| 145 |
"-1.618 1.239-3.869 1.355-5.894.299z", |
| 146 |
['m', [246.026, 120.178], 'c', [-0.558, -0.295, -1.186, -0.768, -1.395, -1.054], |
| 147 |
'c', [-0.314, -0.438, -0.132, -0.456, 1.163, -0.104], |
| 148 |
'c', [2.318, 0.629, 3.814, 0.383, 5.298, -0.873], |
| 149 |
'l', [1.308, -1.103], 'l', [1.54, 0.784], |
| 150 |
'c', [0.848, 0.428, 1.748, 0.725, 2.008, 0.656], |
| 151 |
'c', [0.667, -0.176, 2.05, -1.95, 2.005, -2.564], |
| 152 |
'c', [-0.054, -0.759, 0.587, -0.568, 0.896, 0.264], |
| 153 |
'c', [0.615, 1.631, -0.281, 3.502, -1.865, 3.918], |
| 154 |
'c', [-0.773, 0.201, -1.488, 0.127, -2.659, -0.281], |
| 155 |
'c', [-1.438, -0.502, -1.684, -0.494, -2.405, 0.058], |
| 156 |
'c', [-1.618, 1.239, -3.869, 1.355, -5.894, 0.299], |
| 157 |
'z', []]), |
| 158 |
|
| 159 |
|
| 160 |
("M 114.2,1.919e2 1e-4,22.5E-1", |
| 161 |
["M", [114.2, 191.9], 'L', [0.0001, 2.25]]), |
| 162 |
("a1518e-3 .1518E2 0 011.195E+2-.547", |
| 163 |
["a", [1.518, 15.18, 0, 0, 1, 119.5, -0.547]]), |
| 164 |
) |
| 165 |
failed = _testit(utils.normalise_svg_path, mapping) |
| 166 |
assert len(failed) == 0 |
| 167 |
|
| 168 |
def test_relative_move_after_closepath(self): |
| 169 |
""" |
| 170 |
A relative subpath is relative to the point *after* the previous |
| 171 |
closePath op (which is not recorded in path.points). |
| 172 |
""" |
| 173 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 174 |
node = minimal_svg_node( |
| 175 |
'<path d="M0 0,0 1,1 1z m-1-1 0 1 1 0z"/>' |
| 176 |
) |
| 177 |
|
| 178 |
path = converter.convertPath(node).contents[0] |
| 179 |
assert path.points[-2:] == [0, 0] |
| 180 |
|
| 181 |
def test_cubic_bezier_shorthand(self): |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 186 |
node = minimal_svg_node( |
| 187 |
'<path d="M3,4c-0.5-0.25-1.3-0.77-1.3-1.8h2.5s0.04,0.46,0.04,1.48z"/>' |
| 188 |
) |
| 189 |
path = converter.convertPath(node).contents[0] |
| 190 |
assert path.operators == [_MOVETO, _CURVETO, _LINETO, _CURVETO, _CLOSEPATH] |
| 191 |
assert path.points == [ |
| 192 |
3.0, 4.0, 2.5, 3.75, 1.7, 3.23, 1.7, 2.2, 4.2, 2.2, 4.2, 2.2, 4.24, 2.66, |
| 193 |
4.24, 3.68, |
| 194 |
] |
| 195 |
|
| 196 |
def test_elliptical_arc(self): |
| 197 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 198 |
node = minimal_svg_node( |
| 199 |
'<path d="M334.500000 0.000000 A185.000000 185.000000 0 0 1 334.500000 0.000000 ' |
| 200 |
'L334.500000 185.000000 A0.000000 0.000000 0 0 0 334.500000 185.000000 z"/>' |
| 201 |
) |
| 202 |
|
| 203 |
path = converter.convertPath(node).contents[0] |
| 204 |
assert path.points == [334.5, 0.0, 334.5, 185.0, 334.5, 185.0] |
| 205 |
|
| 206 |
def test_unclosed_paths(self): |
| 207 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 208 |
node = minimal_svg_node('<path d="M0,0 4.5,3 0,6M4.5,3H9" id="W"/>') |
| 209 |
group = converter.convertPath(node) |
| 210 |
assert len(group.contents) == 2 |
| 211 |
closed_path = group.contents[0] |
| 212 |
unclosed_path = group.contents[1] |
| 213 |
assert len(closed_path.points) == len(unclosed_path.points) |
| 214 |
assert closed_path.operators == [ |
| 215 |
_MOVETO, _LINETO, _LINETO, _CLOSEPATH, _MOVETO, _LINETO, _CLOSEPATH] |
| 216 |
assert unclosed_path.operators == [ |
| 217 |
_MOVETO, _LINETO, _LINETO, _MOVETO, _LINETO] |
| 218 |
|
| 219 |
def test_empty_path(self): |
| 220 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 221 |
node = minimal_svg_node('<path id="W"/>') |
| 222 |
group = converter.convertPath(node) |
| 223 |
assert group is None |
| 224 |
|
| 225 |
def test_clipped_path(self): |
| 226 |
drawing = drawing_from_svg(''' |
| 227 |
<?xml version="1.0"?> |
| 228 |
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" |
| 229 |
xmlns:xlink="http://www.w3.org/1999/xlink" width="660" height="480"> |
| 230 |
<defs> |
| 231 |
<path id="my-clipping-path" d="M99,176 L 110 170 112 172Z"/> |
| 232 |
<rect id="my-clipping-rect" x="88.155" y="163" width="419.69" height="20.68" |
| 233 |
fill="red" stroke="blue"/> |
| 234 |
</defs> |
| 235 |
<clipPath id="my-clip-path"> |
| 236 |
<use xlink:href="#my-clipping-path"/> |
| 237 |
</clipPath> |
| 238 |
<clipPath id="my-clip-rect"> |
| 239 |
<use xlink:href="#my-clipping-rect"/> |
| 240 |
</clipPath> |
| 241 |
<path clip-path="url(#my-clip-path)" d="M99,176 L 110 170 112 172Z"/> |
| 242 |
<path clip-path="url(#my-clip-rect)" d="M99,176 L 110 170 112 172Z"/> |
| 243 |
</svg> |
| 244 |
''') |
| 245 |
assert isinstance(drawing.contents[0].contents[0].contents[0], svglib.ClippingPath) |
| 246 |
|
| 247 |
rect_clip = drawing.contents[0].contents[1].contents[0] |
| 248 |
assert isinstance(rect_clip, svglib.ClippingPath) |
| 249 |
|
| 250 |
assert rect_clip.getProperties()['fillColor'] is None |
| 251 |
assert rect_clip.getProperties()['strokeColor'] is None |
| 252 |
|
| 253 |
|
| 254 |
def force_cmyk(rgb): |
| 255 |
c, m, y, k = colors.rgb2cmyk(rgb.red, rgb.green, rgb.blue) |
| 256 |
return colors.CMYKColor(c, m, y, k, alpha=rgb.alpha) |
| 257 |
|
| 258 |
|
| 259 |
class TestColorAttrConverter: |
| 260 |
"Testing color attribute conversion." |
| 261 |
|
| 262 |
def test_0(self): |
| 263 |
"Test color attribute conversion." |
| 264 |
|
| 265 |
mapping = ( |
| 266 |
("red", colors.red), |
| 267 |
("#ff0000", colors.red), |
| 268 |
("#ff000055", colors.Color(1, 0, 0, 1 / 3)), |
| 269 |
("#f00", colors.red), |
| 270 |
("#f00f", colors.red), |
| 271 |
("rgb(100%,50%,10%)", colors.Color(1, 128 / 255, 26 / 255, 1)), |
| 272 |
("rgb(255, 0, 0)", colors.red), |
| 273 |
("rgba(255, 255, 128, .5)", colors.Color(1, 1, 128 / 255, .5)), |
| 274 |
("fuchsia", colors.Color(1, 0, 1, 1)), |
| 275 |
("slategrey", colors.HexColor(0x708090)), |
| 276 |
("transparent", colors.Color(0, 0, 0, 0)), |
| 277 |
("whatever", None), |
| 278 |
) |
| 279 |
ac = svglib.Svg2RlgAttributeConverter() |
| 280 |
failed = _testit(ac.convertColor, mapping) |
| 281 |
assert len(failed) == 0 |
| 282 |
|
| 283 |
def test_1(self): |
| 284 |
"Test color attribute conversion to CMYK" |
| 285 |
|
| 286 |
mapping = ( |
| 287 |
("red", force_cmyk(colors.red)), |
| 288 |
("#ff0000", force_cmyk(colors.red)), |
| 289 |
("#f00", force_cmyk(colors.red)), |
| 290 |
("rgb(100%,0%,0%)", force_cmyk(colors.red)), |
| 291 |
("rgb(255, 0, 0)", force_cmyk(colors.red)), |
| 292 |
("rgb(0,255, 0)", force_cmyk(colors.Color(0, 1, 0))), |
| 293 |
("rgb(0, 0, 255)", force_cmyk(colors.Color(0, 0, 1))), |
| 294 |
) |
| 295 |
ac = svglib.Svg2RlgAttributeConverter(color_converter=force_cmyk) |
| 296 |
failed = _testit(ac.convertColor, mapping) |
| 297 |
assert len(failed) == 0 |
| 298 |
|
| 299 |
|
| 300 |
class TestLengthAttrConverter: |
| 301 |
"Testing length attribute conversion." |
| 302 |
|
| 303 |
def test_length_conversion(self): |
| 304 |
"Test length attribute conversion." |
| 305 |
|
| 306 |
mapping = ( |
| 307 |
("0", 0), |
| 308 |
("316", 316), |
| 309 |
("-316", -316), |
| 310 |
("-3.16", -3.16), |
| 311 |
("-1e-2", -0.01), |
| 312 |
("1e-5", 1e-5), |
| 313 |
("1e1cm", 10*cm), |
| 314 |
("1e1in", 10*inch), |
| 315 |
("-8e-2cm", (-8e-2)*cm), |
| 316 |
("20px", 20), |
| 317 |
("20pt", 20 * 1.25), |
| 318 |
("1.5em", 12 * 1.5), |
| 319 |
("10.5mm", 10.5*(cm*0.1)), |
| 320 |
("3, 5 -7", [3, 5, -7]), |
| 321 |
("2pt 12pt", [2 * 1.25, 12 * 1.25]), |
| 322 |
) |
| 323 |
ac = svglib.Svg2RlgAttributeConverter() |
| 324 |
failed = _testit(ac.convertLength, mapping) |
| 325 |
assert len(failed) == 0 |
| 326 |
assert ac.convertLength("1.5em", em_base=16.5) == 24.75 |
| 327 |
|
| 328 |
def test_percentage_conversion(self): |
| 329 |
"Test percentage length attribute conversion." |
| 330 |
|
| 331 |
ac = svglib.Svg2RlgAttributeConverter() |
| 332 |
ac.set_box(svglib.Box(0, 0, 50, 150)) |
| 333 |
|
| 334 |
length = ac.convertLength("1e1%", attr_name='width') |
| 335 |
assert length == 5 |
| 336 |
length = ac.convertLength("1e1%", attr_name='height') |
| 337 |
assert length == 15 |
| 338 |
|
| 339 |
def test_length_list_conversion(self): |
| 340 |
"Test length list attribute conversion." |
| 341 |
|
| 342 |
mapping = ( |
| 343 |
(" 5cm 5in", [5*cm, 5*inch]), |
| 344 |
(" 5, 5", [5, 5]), |
| 345 |
) |
| 346 |
ac = svglib.Svg2RlgAttributeConverter() |
| 347 |
failed = _testit(ac.convertLengthList, mapping) |
| 348 |
assert len(failed) == 0 |
| 349 |
|
| 350 |
|
| 351 |
class TestTransformAttrConverter: |
| 352 |
"Testing transform attribute conversion." |
| 353 |
|
| 354 |
def test_0(self): |
| 355 |
"Test transform attribute conversion." |
| 356 |
|
| 357 |
mapping = ( |
| 358 |
("", |
| 359 |
[]), |
| 360 |
("scale(2) translate(10,-20.5)", |
| 361 |
[("scale", 2.0), ("translate", (10.0, -20.5))]), |
| 362 |
("scale(0.9), translate(27,40)", |
| 363 |
[("scale", 0.9), ("translate", (27.0, 40.0))]), |
| 364 |
|
| 365 |
("scale(0.9), translate", |
| 366 |
[]), |
| 367 |
("ref(svg)", |
| 368 |
[]), |
| 369 |
) |
| 370 |
ac = svglib.Svg2RlgAttributeConverter() |
| 371 |
failed = _testit(ac.convertTransform, mapping) |
| 372 |
assert len(failed) == 0 |
| 373 |
|
| 374 |
|
| 375 |
class TestAttrConverter: |
| 376 |
"Testing multi-attribute conversion." |
| 377 |
|
| 378 |
def test_0(self): |
| 379 |
"Test multi-attribute conversion." |
| 380 |
|
| 381 |
mapping = ( |
| 382 |
("fill: black; stroke: yellow", |
| 383 |
{"fill": "black", "stroke": "yellow"}), |
| 384 |
) |
| 385 |
ac = svglib.Svg2RlgAttributeConverter() |
| 386 |
failed = _testit(ac.parseMultiAttributes, mapping) |
| 387 |
assert len(failed) == 0 |
| 388 |
|
| 389 |
def test_findAttr(self): |
| 390 |
""" |
| 391 |
Test various attribute peculiarities. |
| 392 |
""" |
| 393 |
ac = svglib.Svg2RlgAttributeConverter() |
| 394 |
|
| 395 |
node = minimal_svg_node('<rect fill=" #00A1DE\n"/>') |
| 396 |
assert ac.findAttr(node, 'fill') == "#00A1DE" |
| 397 |
|
| 398 |
|
| 399 |
node = minimal_svg_node('<text style="-inkscape-font-specification:Arial"/>') |
| 400 |
assert ac.findAttr(node, '-inkscape-font-specification') == "" |
| 401 |
|
| 402 |
def test_findAttr_parents(self): |
| 403 |
ac = svglib.Svg2RlgAttributeConverter() |
| 404 |
rect_node = next(minimal_svg_node( |
| 405 |
'<g style="fill:#008000;stroke:#008000;"><rect style="fill:#ff0;"/></g>' |
| 406 |
).iter_children()) |
| 407 |
assert ac.findAttr(rect_node, 'fill') == "#ff0" |
| 408 |
assert ac.findAttr(rect_node, 'stroke') == "#008000" |
| 409 |
|
| 410 |
def test_no_fill_on_shape(self): |
| 411 |
""" |
| 412 |
Any shape with no fill property should set black color in rlg syntax. |
| 413 |
""" |
| 414 |
drawing = drawing_from_svg(''' |
| 415 |
<?xml version="1.0"?> |
| 416 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 417 |
width="1200" height="800" |
| 418 |
viewBox="0 0 36 24"> |
| 419 |
<rect y="10" width="36" height="4"/> |
| 420 |
</svg> |
| 421 |
''') |
| 422 |
assert drawing.contents[0].contents[0].fillColor == colors.black |
| 423 |
|
| 424 |
def test_fillopacity(self): |
| 425 |
""" |
| 426 |
The fill-opacity property set the alpha of the color. |
| 427 |
""" |
| 428 |
drawing = drawing_from_svg(''' |
| 429 |
<?xml version="1.0"?> |
| 430 |
<svg version="1.1" |
| 431 |
xmlns="http://www.w3.org/2000/svg" |
| 432 |
width="660" height="480"> |
| 433 |
<polygon id="triangle" points="0,-29.14 -25.23, 14.57 25.23, 14.57" |
| 434 |
stroke="#0038b8" stroke-width="5.5" fill-opacity="0"/> |
| 435 |
</svg> |
| 436 |
''') |
| 437 |
assert drawing.contents[0].contents[0].fillColor == colors.Color(0, 0, 0, 0) |
| 438 |
|
| 439 |
def test_fillcolor_alpha_set_fillopacity(self): |
| 440 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 441 |
node = minimal_svg_node('<polygon fill="#ff000080"/>') |
| 442 |
poly = Polygon() |
| 443 |
converter.applyStyleOnShape(poly, node) |
| 444 |
assert poly.fillOpacity == 128 / 255 |
| 445 |
assert poly.strokeOpacity == 1 |
| 446 |
|
| 447 |
def test_strokecolor_alpha_set_strokeopacity(self): |
| 448 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 449 |
node = minimal_svg_node('<polygon stroke="#cccccca0"/>') |
| 450 |
poly = Polygon() |
| 451 |
converter.applyStyleOnShape(poly, node) |
| 452 |
assert poly.fillOpacity == 1 |
| 453 |
assert poly.strokeOpacity == 160 / 255 |
| 454 |
|
| 455 |
def test_fillrule(self): |
| 456 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 457 |
node = minimal_svg_node('<polygon fill-rule="evenodd"/>') |
| 458 |
poly = Polygon() |
| 459 |
converter.applyStyleOnShape(poly, node) |
| 460 |
assert poly._fillRule == FILL_EVEN_ODD |
| 461 |
|
| 462 |
def test_stroke(self): |
| 463 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 464 |
node = minimal_svg_node('<path d="m0,6.5h27m0,5H0" stroke="#FFF" stroke-opacity="0.5"/>') |
| 465 |
path = Path() |
| 466 |
converter.applyStyleOnShape(path, node) |
| 467 |
assert path.strokeColor == colors.white |
| 468 |
assert path.strokeOpacity == 0.5 |
| 469 |
assert path.strokeWidth == 1 |
| 470 |
|
| 471 |
|
| 472 |
class TestApplyTransformOnGroup: |
| 473 |
def test_translate_only_x(self): |
| 474 |
""" |
| 475 |
When the second translate value is missing, 0 is assumed. |
| 476 |
""" |
| 477 |
group = Group() |
| 478 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 479 |
transform = "translate(10)" |
| 480 |
converter.applyTransformOnGroup(transform, group) |
| 481 |
assert group.transform == (1, 0, 0, 1, 10, 0) |
| 482 |
|
| 483 |
def test_transform_matrix(self): |
| 484 |
NEUTRAL_TRANSFORM = (1, 0, 0, 1, 0, 0) |
| 485 |
group = Group() |
| 486 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 487 |
|
| 488 |
transform = "matrix(1 2 3 4 5)" |
| 489 |
converter.applyTransformOnGroup(transform, group) |
| 490 |
assert group.transform == NEUTRAL_TRANSFORM |
| 491 |
|
| 492 |
|
| 493 |
group = Group() |
| 494 |
transform = "matrix(3 1 -1 3 30 40)" |
| 495 |
converter.applyTransformOnGroup(transform, group) |
| 496 |
assert group.transform == (3, 1, -1, 3, 30, 40) |
| 497 |
|
| 498 |
|
| 499 |
group = Group() |
| 500 |
transform = "translate(40 40) matrix(3 1 -1 3 30 40)" |
| 501 |
converter.applyTransformOnGroup(transform, group) |
| 502 |
assert group.transform == (3, 1, -1, 3, 70, 80) |
| 503 |
|
| 504 |
|
| 505 |
class TestStyleSheets: |
| 506 |
def test_css_stylesheet(self): |
| 507 |
drawing = drawing_from_svg(''' |
| 508 |
<?xml version="1.0"?> |
| 509 |
<svg width="777" height="267" xml:space="preserve"> |
| 510 |
<defs> |
| 511 |
<style type="text/css"> |
| 512 |
path { fill:none; } |
| 513 |
#p1 { fill:rgb(255,0,0) !important; } |
| 514 |
#p2 { fill:rgb(255,0,0); } |
| 515 |
.paths { stroke-width:1.5; } |
| 516 |
</style> |
| 517 |
</defs> |
| 518 |
<g id="g1"> |
| 519 |
<path id="p1" class="paths" d="M 0,-100 V 0 H 50"/> |
| 520 |
<path id="p2" class="paths other" style="fill: #000000" d="M 0,100 V 0 H 50"/> |
| 521 |
</g> |
| 522 |
</svg> |
| 523 |
''') |
| 524 |
main_group = drawing.contents[0] |
| 525 |
assert main_group.contents[0].contents[0].contents[0].fillColor == colors.red |
| 526 |
|
| 527 |
assert main_group.contents[0].contents[1].contents[0].fillColor == colors.black |
| 528 |
assert main_group.contents[0].contents[1].contents[0].strokeWidth == 1.5 |
| 529 |
|
| 530 |
def test_css_stylesheet_multiple_style_tags(self): |
| 531 |
drawing = drawing_from_svg(''' |
| 532 |
<?xml version="1.0"?> |
| 533 |
<svg width="777" height="267" xml:space="preserve"> |
| 534 |
<defs> |
| 535 |
<style type="text/css"> |
| 536 |
#p1 { fill:rgb(255,0,0) } |
| 537 |
</style> |
| 538 |
<style type="text/css"> |
| 539 |
#p2 { fill:rgb(0,0,0); } |
| 540 |
</style> |
| 541 |
</defs> |
| 542 |
<g> |
| 543 |
<path id="p1" d="M 0,-100 V 0 H 50"/> |
| 544 |
<path id="p2" d="M 0,100 V 0 H 50"/> |
| 545 |
</g> |
| 546 |
</svg> |
| 547 |
''') |
| 548 |
main_group = drawing.contents[0] |
| 549 |
assert main_group.contents[0].contents[0].contents[0].fillColor == colors.red |
| 550 |
assert main_group.contents[0].contents[1].contents[0].fillColor == colors.black |
| 551 |
|
| 552 |
def test_css_nth_of_type(self): |
| 553 |
drawing = drawing_from_svg(''' |
| 554 |
<?xml version="1.0"?> |
| 555 |
<svg viewBox="0 0 649 487"> |
| 556 |
<style type="text/css">.bold1:nth-of-type(2n) { font-weight: bold; font-size: 1.1em; }</style> |
| 557 |
<g> |
| 558 |
<text class="bold1" x="324" y="304">A</text> |
| 559 |
<text class="bold1" x="324" y="384">B</text> |
| 560 |
<text class="bold1" x="324" y="464">C</text> |
| 561 |
</g> |
| 562 |
</svg> |
| 563 |
''') |
| 564 |
main_group = drawing.contents[0] |
| 565 |
assert main_group.contents[0].contents[0].contents[0].fontName == 'Helvetica' |
| 566 |
assert main_group.contents[0].contents[1].contents[0].fontName == 'Helvetica-Bold' |
| 567 |
assert main_group.contents[0].contents[2].contents[0].fontName == 'Helvetica' |
| 568 |
|
| 569 |
def test_import_rule_no_crash(self): |
| 570 |
|
| 571 |
drawing = drawing_from_svg(''' |
| 572 |
<svg> |
| 573 |
<defs> |
| 574 |
<style type="text/css"> |
| 575 |
@import url('https://fonts.example.org/css2?family=Ubuntu+Condensed'); |
| 576 |
</style> |
| 577 |
</defs> |
| 578 |
</svg> |
| 579 |
''') |
| 580 |
|
| 581 |
|
| 582 |
class TestGroupNode: |
| 583 |
def test_svg_groups_have_svgid(self): |
| 584 |
drawing = drawing_from_svg(''' |
| 585 |
<?xml version="1.0"?> |
| 586 |
<svg width="777" height="267"> |
| 587 |
<g id="g856"> |
| 588 |
<rect |
| 589 |
y="107.07929" |
| 590 |
x="64.942139" |
| 591 |
height="19.506001" |
| 592 |
width="34.690311" |
| 593 |
id="rect850" |
| 594 |
</g> |
| 595 |
</svg> |
| 596 |
''') |
| 597 |
main_group = drawing.contents[0] |
| 598 |
|
| 599 |
gr, = main_group.contents |
| 600 |
assert gr.svgid == 'g856' |
| 601 |
assert isinstance(gr, Group) |
| 602 |
|
| 603 |
def test_created_groups_have_svgid_of_their_content(self): |
| 604 |
drawing = drawing_from_svg(''' |
| 605 |
<?xml version="1.0"?> |
| 606 |
<svg width="777" height="267"> |
| 607 |
<g id="g856"> |
| 608 |
<rect |
| 609 |
y="107.07929" |
| 610 |
x="64.942139" |
| 611 |
height="19.506001" |
| 612 |
width="34.690311" |
| 613 |
id="rect850" |
| 614 |
<rect |
| 615 |
y="136.5135" |
| 616 |
x="108.62624" |
| 617 |
height="17.637161" |
| 618 |
width="29.083796" |
| 619 |
id="rect852" |
| 620 |
<path |
| 621 |
d="M 601.06712,388.63166 H 954.67961" |
| 622 |
id="path819" /> |
| 623 |
</g> |
| 624 |
</svg> |
| 625 |
''') |
| 626 |
main_group = drawing.contents[0] |
| 627 |
gr, = main_group.contents |
| 628 |
|
| 629 |
r1_gr, r2_gr, pth_gr = gr.contents |
| 630 |
assert r1_gr.svgid == 'rect850' |
| 631 |
assert r2_gr.svgid == 'rect852' |
| 632 |
assert pth_gr.svgid == 'path819' |
| 633 |
assert isinstance(pth_gr, Group) |
| 634 |
|
| 635 |
def test_svg_layers_have_label(self): |
| 636 |
drawing = drawing_from_svg(''' |
| 637 |
<?xml version="1.0"?> |
| 638 |
<svg width="777" height="267"> |
| 639 |
<g inkscape:groupmode="layer" |
| 640 |
id="layer2" |
| 641 |
inkscape:label="x_axis" |
| 642 |
style="display:inline" |
| 643 |
transform="translate(-476.20282,35.510971)"> |
| 644 |
<path |
| 645 |
d="M 601.06712,388.63166 H 954.67961" |
| 646 |
id="path819" /> |
| 647 |
</g> |
| 648 |
</svg> |
| 649 |
''') |
| 650 |
main_group = drawing.contents[0] |
| 651 |
gr, = main_group.contents |
| 652 |
assert gr.label == "x_axis" |
| 653 |
|
| 654 |
|
| 655 |
class TestTextNode: |
| 656 |
def test_space_preservation(self): |
| 657 |
drawing = drawing_from_svg(''' |
| 658 |
<?xml version="1.0"?> |
| 659 |
<svg width="777" height="267"> |
| 660 |
<text style="fill:#000000; stroke:none; font-size:28;"> |
| 661 |
<tspan>TITLE 1</tspan> |
| 662 |
<tspan x="-10.761" y="33.487">Subtitle</tspan> |
| 663 |
</text> |
| 664 |
</svg> |
| 665 |
''') |
| 666 |
main_group = drawing.contents[0] |
| 667 |
|
| 668 |
|
| 669 |
assert len(main_group.contents[0].contents) == 3 |
| 670 |
assert main_group.contents[0].contents[0].text == "TITLE 1" |
| 671 |
|
| 672 |
drawing = drawing_from_svg(''' |
| 673 |
<?xml version="1.0"?> |
| 674 |
<svg width="777" height="267" xml:space="preserve"> |
| 675 |
<text style="fill:#000000; stroke:none; font-size:28;"> |
| 676 |
<tspan>TITLE 1</tspan> |
| 677 |
<tspan x="-10.761" y="33.487">Subtitle</tspan> |
| 678 |
</text> |
| 679 |
</svg> |
| 680 |
''') |
| 681 |
main_group = drawing.contents[0] |
| 682 |
assert main_group.contents[0].contents[0].text == ' ' |
| 683 |
assert main_group.contents[0].contents[1].text == "TITLE 1" |
| 684 |
|
| 685 |
drawing = drawing_from_svg(''' |
| 686 |
<?xml version="1.0"?> |
| 687 |
<svg width="777" height="267"> |
| 688 |
<text style="fill:#000000; stroke:none; font-size:28;"> |
| 689 |
<tspan>TITLE 1</tspan> |
| 690 |
<tspan x="-10.761" y="33.487">Subtitle</tspan> |
| 691 |
</text> |
| 692 |
<text xml:space="preserve"> with spaces </text> |
| 693 |
</svg> |
| 694 |
''') |
| 695 |
main_group = drawing.contents[0] |
| 696 |
|
| 697 |
assert main_group.contents[0].contents[0].text == "TITLE 1" |
| 698 |
assert main_group.contents[1].contents[0].text == ' with spaces ' |
| 699 |
|
| 700 |
def test_tspan_position(self): |
| 701 |
""" |
| 702 |
The x/y positions of a tspan are either relative to the current text |
| 703 |
position, or can be absoluted by specifying the x/y attributes. |
| 704 |
""" |
| 705 |
drawing = drawing_from_svg(''' |
| 706 |
<?xml version="1.0"?> |
| 707 |
<svg width="777" height="267"> |
| 708 |
<text x="10" y="20" style="fill:#000000; stroke:none; font-size:28;"> |
| 709 |
The start |
| 710 |
<tspan>TITLE 1</tspan> |
| 711 |
<!-- x position relative to current text position |
| 712 |
y position offset in em --> |
| 713 |
<tspan dy="1.3em">(after title)</tspan> |
| 714 |
<!-- absolute position --> |
| 715 |
<tspan x="16.75" y="33.487">Subtitle</tspan> |
| 716 |
<!-- absolute position + shifting --> |
| 717 |
<tspan x="10" y="20" dx="3em" dy="1.5em">Complete</tspan> |
| 718 |
The end |
| 719 |
</text> |
| 720 |
</svg> |
| 721 |
''') |
| 722 |
main_group = drawing.contents[0] |
| 723 |
assert main_group.contents[0].contents[0].x == 10 |
| 724 |
assert main_group.contents[0].contents[0].y == -20 |
| 725 |
assert main_group.contents[0].contents[0].text == "The start " |
| 726 |
assert main_group.contents[0].contents[1].x > 10 |
| 727 |
assert main_group.contents[0].contents[1].text == "TITLE 1" |
| 728 |
assert main_group.contents[0].contents[2].text == " " |
| 729 |
assert main_group.contents[0].contents[3].y == -20 - (1.3 * 28) |
| 730 |
assert main_group.contents[0].contents[3].text == "(after title)" |
| 731 |
assert main_group.contents[0].contents[4].text == " " |
| 732 |
assert main_group.contents[0].contents[5].x == 16.75 |
| 733 |
assert main_group.contents[0].contents[5].y == -33.487 |
| 734 |
assert main_group.contents[0].contents[5].text == "Subtitle" |
| 735 |
assert main_group.contents[0].contents[6].text == " " |
| 736 |
assert main_group.contents[0].contents[7].x == 10 + (3 * 28) |
| 737 |
assert main_group.contents[0].contents[7].y == -20 - (1.5 * 28) |
| 738 |
assert main_group.contents[0].contents[7].text == 'Complete' |
| 739 |
assert main_group.contents[0].contents[8].text == ' The end' |
| 740 |
|
| 741 |
|
| 742 |
class TestRectNode: |
| 743 |
def test_rx_ry(self): |
| 744 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 745 |
node = minimal_svg_node( |
| 746 |
'<rect rx="10" ry="4" width="10" height="3" x="0" y="0"/>' |
| 747 |
) |
| 748 |
rect = converter.convertRect(node) |
| 749 |
|
| 750 |
assert rect.rx == 5 |
| 751 |
assert rect.ry == 1.5 |
| 752 |
|
| 753 |
def test_rx_ry_lonely(self): |
| 754 |
"""If only rx or ry is present, the other default to the same value.""" |
| 755 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 756 |
node = minimal_svg_node( |
| 757 |
'<rect rx="2.5" width="10" height="3" x="0" y="0"/>' |
| 758 |
) |
| 759 |
rect = converter.convertRect(node) |
| 760 |
assert rect.ry == 2.5 |
| 761 |
node = minimal_svg_node( |
| 762 |
'<rect ry="1" width="10" height="3" x="0" y="0"/>' |
| 763 |
) |
| 764 |
rect = converter.convertRect(node) |
| 765 |
assert rect.rx == 1 |
| 766 |
|
| 767 |
def test_strokewidth_0(self): |
| 768 |
""" |
| 769 |
When strokeWidth = 0, we force strokeColor to None to avoid the stroke |
| 770 |
to be displayed. |
| 771 |
""" |
| 772 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 773 |
node = minimal_svg_node( |
| 774 |
'<rect style="stroke: rgb(100, 84, 0); fill: rgb(255, 255, 255); ' |
| 775 |
'fill-opacity: 1; stroke-width: 0;" width="135" height="86" x="10" y="10"/>' |
| 776 |
) |
| 777 |
rect = converter.convertShape('rect', node) |
| 778 |
assert rect.strokeColor is None |
| 779 |
|
| 780 |
|
| 781 |
class TestLineNode: |
| 782 |
def test_length_zero(self): |
| 783 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 784 |
node = minimal_svg_node( |
| 785 |
'<line stroke="#000000" x1="10" y1="20" x2="10" y2="20" />' |
| 786 |
) |
| 787 |
line = converter.convertLine(node) |
| 788 |
assert isinstance(line, Line) |
| 789 |
|
| 790 |
|
| 791 |
assert line.x1 != 10 |
| 792 |
|
| 793 |
def test_length_not_zero(self): |
| 794 |
""" No change needed when length is not zero. """ |
| 795 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 796 |
node = minimal_svg_node( |
| 797 |
'<line stroke="#000000" x1="10" y1="20" x2="10" y2="30" />' |
| 798 |
) |
| 799 |
line = converter.convertLine(node) |
| 800 |
assert isinstance(line, Line) |
| 801 |
|
| 802 |
assert line.x1 == 10 |
| 803 |
|
| 804 |
|
| 805 |
class TestPolylineNode: |
| 806 |
def test_filling(self): |
| 807 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 808 |
node = minimal_svg_node( |
| 809 |
'<polyline fill="none" stroke="#000000" ' |
| 810 |
'points="10,50,35,150,60,50,85,150,110,50,135,150" />' |
| 811 |
) |
| 812 |
polyline = converter.convertPolyline(node) |
| 813 |
assert isinstance(polyline, PolyLine) |
| 814 |
|
| 815 |
|
| 816 |
node = minimal_svg_node( |
| 817 |
'<polyline fill="#fff" stroke="#000000" ' |
| 818 |
'points="10,50,35,150,60,50,85,150,110,50,135,150" />' |
| 819 |
) |
| 820 |
group = converter.convertPolyline(node) |
| 821 |
assert isinstance(group.contents[0], Polygon) |
| 822 |
assert group.contents[0].fillColor == colors.white |
| 823 |
assert isinstance(group.contents[1], PolyLine) |
| 824 |
|
| 825 |
def test_length_zero(self): |
| 826 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 827 |
node = minimal_svg_node( |
| 828 |
'<polyline fill="none" stroke="#000000" ' |
| 829 |
'points="10,50,10,50" />' |
| 830 |
) |
| 831 |
polyline = converter.convertPolyline(node) |
| 832 |
assert isinstance(polyline, PolyLine) |
| 833 |
|
| 834 |
|
| 835 |
assert polyline.points[0] != 10 |
| 836 |
|
| 837 |
def test_odd_length(self): |
| 838 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 839 |
node = minimal_svg_node( |
| 840 |
'<polyline fill="none" stroke="#000000" ' |
| 841 |
'points="10,50,10,50,10" />' |
| 842 |
) |
| 843 |
polyline = converter.convertPolyline(node) |
| 844 |
assert polyline is None |
| 845 |
|
| 846 |
|
| 847 |
class TestPolygonNode: |
| 848 |
def test_length_zero(self): |
| 849 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 850 |
node = minimal_svg_node( |
| 851 |
'<polygon fill="none" stroke="#000000" ' |
| 852 |
'points="10,50,10,50" />' |
| 853 |
) |
| 854 |
polygon = converter.convertPolygon(node) |
| 855 |
assert isinstance(polygon, Polygon) |
| 856 |
|
| 857 |
|
| 858 |
assert polygon.points[0] != 10 |
| 859 |
|
| 860 |
def test_odd_length(self): |
| 861 |
converter = svglib.Svg2RlgShapeConverter(None) |
| 862 |
node = minimal_svg_node( |
| 863 |
'<polygon fill="none" stroke="#000000" ' |
| 864 |
'points="10,50,10,50,10" />' |
| 865 |
) |
| 866 |
polygon = converter.convertPolygon(node) |
| 867 |
assert polygon is None |
| 868 |
|
| 869 |
|
| 870 |
class TestUseNode: |
| 871 |
def test_use(self, drawing_source=None): |
| 872 |
drawing = drawing_from_svg(drawing_source or ''' |
| 873 |
<?xml version="1.0"?> |
| 874 |
<svg version="1.1" |
| 875 |
xmlns="http://www.w3.org/2000/svg" |
| 876 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 877 |
width="10cm" height="3cm" viewBox="0 0 100 30"> |
| 878 |
<defs> |
| 879 |
<rect id="MyRect" width="60" height="10"/> |
| 880 |
</defs> |
| 881 |
<rect x=".1" y=".1" width="99.8" height="29.8" |
| 882 |
fill="none" stroke="blue" stroke-width=".2" /> |
| 883 |
<use x="20" y="10" xlink:href="#MyRect" /> |
| 884 |
<use x="30" y="20" xlink:href="#MyRect" fill="#f00" /> |
| 885 |
</svg> |
| 886 |
''') |
| 887 |
main_group = drawing.contents[0] |
| 888 |
|
| 889 |
assert isinstance(main_group.contents[0], Rect) |
| 890 |
|
| 891 |
assert isinstance(main_group.contents[1].contents[0], Rect) |
| 892 |
assert main_group.contents[1].contents[0].fillColor == colors.black |
| 893 |
|
| 894 |
assert isinstance(main_group.contents[2].contents[0], Rect) |
| 895 |
assert main_group.contents[2].contents[0].fillColor == colors.red |
| 896 |
|
| 897 |
def test_use_href_svg2(self): |
| 898 |
"""In SVG 2, xlink:href="" can be simply href="".""" |
| 899 |
drawing = drawing_from_svg(''' |
| 900 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 901 |
width="10cm" height="3cm" viewBox="0 0 100 30"> |
| 902 |
<defs> |
| 903 |
<rect id="MyRect" x=".1" y=".1" width="99.8" height="29.8" |
| 904 |
fill="none" stroke="blue" /> |
| 905 |
</defs> |
| 906 |
<use x="20" y="10" href="#MyRect" /> |
| 907 |
</svg> |
| 908 |
''') |
| 909 |
assert isinstance(drawing.contents[0].contents[0].contents[0], Rect) |
| 910 |
|
| 911 |
def test_use_with_defs_at_end(self): |
| 912 |
self.test_use(drawing_source=''' |
| 913 |
<?xml version="1.0"?> |
| 914 |
<svg version="1.1" |
| 915 |
xmlns="http://www.w3.org/2000/svg" |
| 916 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 917 |
width="10cm" height="3cm" viewBox="0 0 100 30"> |
| 918 |
<rect x=".1" y=".1" width="99.8" height="29.8" |
| 919 |
fill="none" stroke="blue" stroke-width=".2" /> |
| 920 |
<use x="20" y="10" xlink:href="#MyRect" /> |
| 921 |
<use x="30" y="20" xlink:href="#MyRect" fill="#f00" /> |
| 922 |
<defs> |
| 923 |
<rect id="MyRect" width="60" height="10"/> |
| 924 |
</defs> |
| 925 |
</svg> |
| 926 |
''') |
| 927 |
|
| 928 |
def test_transform_inherited_by_use(self): |
| 929 |
drawing = drawing_from_svg(''' |
| 930 |
<?xml version="1.0"?> |
| 931 |
<svg version="1.1" |
| 932 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 933 |
width="900" height="600"> |
| 934 |
<g id="c"> |
| 935 |
<path id="t" d="M 0,-100 V 0 H 50" |
| 936 |
transform="rotate(18 0,-100)"/> |
| 937 |
<use xlink:href="#t" transform="scale(-1,1)"/> |
| 938 |
</g> |
| 939 |
</svg> |
| 940 |
''') |
| 941 |
cgroup_node = drawing.contents[0].contents[0] |
| 942 |
assert ( |
| 943 |
cgroup_node.contents[0].transform == cgroup_node.contents[1].contents[0].transform |
| 944 |
), "The transform of the original path is different from the transform of the reused path." |
| 945 |
|
| 946 |
def test_use_forward_reference(self): |
| 947 |
""" |
| 948 |
Sometimes, a node definition pointed to by xlink:href can appear after |
| 949 |
it has been referenced. But the order should remain. |
| 950 |
""" |
| 951 |
drawing = drawing_from_svg(''' |
| 952 |
<?xml version="1.0"?> |
| 953 |
<svg version="1.1" |
| 954 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 955 |
width="900" height="600"> |
| 956 |
<use xlink:href="#back" x="-100"/> |
| 957 |
<rect id="back" x="42" y="42" width="416" height="216" fill="#007a5e"/> |
| 958 |
</svg> |
| 959 |
''') |
| 960 |
assert len(drawing.contents[0].contents) == 2 |
| 961 |
assert isinstance(drawing.contents[0].contents[0], Group) |
| 962 |
assert isinstance(drawing.contents[0].contents[1], Rect) |
| 963 |
|
| 964 |
def test_use_node_properties(self): |
| 965 |
""" |
| 966 |
Properties on the use node apply to the referenced item. |
| 967 |
""" |
| 968 |
drawing = drawing_from_svg(''' |
| 969 |
<?xml version="1.0"?> |
| 970 |
<svg version="1.1" |
| 971 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 972 |
width="900" height="600"> |
| 973 |
<path id="a" fill="#FF0000" d="M-15 37.57h60L-15 0v80h60l-60-60z"/> |
| 974 |
<use stroke="#003893" stroke-width="5" xlink:href="#a"/> |
| 975 |
<use stroke="#003893" stroke-width="2" xlink:href="#a"/> |
| 976 |
</svg> |
| 977 |
''') |
| 978 |
use_path1 = drawing.contents[0].contents[1].contents[0].contents[0] |
| 979 |
use_path2 = drawing.contents[0].contents[2].contents[0].contents[0] |
| 980 |
|
| 981 |
assert use_path1.fillColor == colors.Color(1, 0, 0, 1) |
| 982 |
|
| 983 |
assert use_path1.strokeWidth == 5 |
| 984 |
assert use_path2.strokeWidth == 2 |
| 985 |
|
| 986 |
def test_use_node_with_unclosed_path(self): |
| 987 |
""" |
| 988 |
When a <use> node references an unclosed path (which is a group with two |
| 989 |
different paths for filling and stroking), the use properties shouldn't |
| 990 |
affect the no-stroke property of the fake stroke-only path. |
| 991 |
""" |
| 992 |
drawing = drawing_from_svg(''' |
| 993 |
<?xml version="1.0"?> |
| 994 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 995 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 996 |
width="900" height="600" viewBox="0 0 9 6"> |
| 997 |
<defs> |
| 998 |
<path d="M0,0 4.5,3 0,6" id="X"/> |
| 999 |
</defs> |
| 1000 |
<use xlink:href="#X" fill="#f00" stroke="#ffb612" stroke-width="2"/> |
| 1001 |
</svg> |
| 1002 |
''') |
| 1003 |
use_group = drawing.contents[0].contents[0].contents[0] |
| 1004 |
assert use_group.contents[0].getProperties()['strokeWidth'] == 0 |
| 1005 |
assert use_group.contents[0].getProperties()['strokeColor'] is None |
| 1006 |
|
| 1007 |
|
| 1008 |
class TestSymbolNode: |
| 1009 |
def test_symbol_unused(self): |
| 1010 |
"""<symbol> by itself should not be rendered.""" |
| 1011 |
drawing = drawing_from_svg(''' |
| 1012 |
<?xml version="1.0"?> |
| 1013 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 1014 |
width="900" height="600" viewBox="0 0 100 100"> |
| 1015 |
<symbol id="X"> |
| 1016 |
<path d="M 0,0100 V 0 H 50"/> |
| 1017 |
</symbol> |
| 1018 |
</svg> |
| 1019 |
''') |
| 1020 |
assert drawing.contents[0].contents == [] |
| 1021 |
|
| 1022 |
def test_symbol_node(self): |
| 1023 |
drawing = drawing_from_svg(''' |
| 1024 |
<?xml version="1.0"?> |
| 1025 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 1026 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 1027 |
width="900" height="600" viewBox="0 0 100 100"> |
| 1028 |
<defs> |
| 1029 |
<symbol id="X"> |
| 1030 |
<path d="M 0,-100 V 0 H 50"/> |
| 1031 |
</symbol> |
| 1032 |
</defs> |
| 1033 |
<use xlink:href="#X" fill="#f00"/> |
| 1034 |
</svg> |
| 1035 |
''') |
| 1036 |
use_group = drawing.contents[0].contents[0].contents[0] |
| 1037 |
assert isinstance(use_group.contents[0].contents[0], svglib.NoStrokePath) |
| 1038 |
assert isinstance(use_group.contents[0].contents[1], Path) |
| 1039 |
|
| 1040 |
|
| 1041 |
class TestViewBox: |
| 1042 |
def test_nonzero_origin(self): |
| 1043 |
drawing = drawing_from_svg(''' |
| 1044 |
<?xml version="1.0"?> |
| 1045 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 1046 |
width="1200" height="800" |
| 1047 |
viewBox="-60 -40 120 80"> |
| 1048 |
<g fill="#E70013"> |
| 1049 |
<rect x="-60" y="-40" width="120" height="80"/> |
| 1050 |
</g> |
| 1051 |
</svg> |
| 1052 |
''') |
| 1053 |
|
| 1054 |
assert drawing.contents[0].transform == (10, 0, 0, -10, 600.0, 400.0) |
| 1055 |
|
| 1056 |
def test_percent_width_height(self): |
| 1057 |
drawing = drawing_from_svg(''' |
| 1058 |
<?xml version="1.0"?> |
| 1059 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 1060 |
width="100%" height="100%" |
| 1061 |
viewBox="0 0 480 360"> |
| 1062 |
<g fill="#E70013"> |
| 1063 |
<rect x="60" y="40" width="120" height="80"/> |
| 1064 |
</g> |
| 1065 |
</svg> |
| 1066 |
''') |
| 1067 |
assert (drawing.width, drawing.height) == (480, 360) |
| 1068 |
|
| 1069 |
def test_no_width_height(self): |
| 1070 |
drawing = drawing_from_svg(''' |
| 1071 |
<?xml version="1.0"?> |
| 1072 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 1073 |
viewBox="0 0 480 360"> |
| 1074 |
<g fill="#E70013"> |
| 1075 |
<rect x="60" y="40" width="120" height="80"/> |
| 1076 |
</g> |
| 1077 |
</svg> |
| 1078 |
''') |
| 1079 |
assert (drawing.width, drawing.height) == (480, 360) |
| 1080 |
|
| 1081 |
|
| 1082 |
class TestEmbedded: |
| 1083 |
def test_svg_in_svg(self): |
| 1084 |
drawing = drawing_from_svg(''' |
| 1085 |
<?xml version="1.0"?> |
| 1086 |
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" |
| 1087 |
viewBox="0 0 210 297" height="297mm" width="210mm"> |
| 1088 |
<g> |
| 1089 |
<rect ry="5" y="15" x="11" height="85" width="90" |
| 1090 |
style="fill:#b8393d;stroke:#dfd8c3;stroke-width:0.75;" /> |
| 1091 |
<text id="text8866" y="32" x="25" |
| 1092 |
style="font-size:10px;line-height:1.25;font-family:sans-serif;"> |
| 1093 |
<tspan x="25" y="31" style="stroke-width:0.2">Test 1,2,3</tspan></text> |
| 1094 |
</g> |
| 1095 |
<g> |
| 1096 |
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" |
| 1097 |
viewBox="0 0 210 50" x="0" y="100" height="25mm" width="105mm"> |
| 1098 |
<g> |
| 1099 |
<text y="30" x="5" |
| 1100 |
style="line-height:1.25;font-family:sans-serif;"> |
| 1101 |
<tspan x="5" y="30" |
| 1102 |
style="font-size:25px;stroke-width:0.2">TEST</tspan></text> |
| 1103 |
</g> |
| 1104 |
</svg> |
| 1105 |
</g> |
| 1106 |
</svg> |
| 1107 |
''') |
| 1108 |
embedded_svg_group = drawing.contents[0].contents[1].contents[0] |
| 1109 |
|
| 1110 |
assert embedded_svg_group.getProperties()['transform'][-2:] == (0, 100) |
| 1111 |
|
| 1112 |
assert pytest.approx(1.417, 0.001) == embedded_svg_group.getProperties()['transform'][0] |
| 1113 |
assert pytest.approx(1.417, 0.001) == embedded_svg_group.getProperties()['transform'][3] |
| 1114 |
|
| 1115 |
def test_embedded_svg_with_percentages(self): |
| 1116 |
drawing = drawing_from_svg(''' |
| 1117 |
<?xml version="1.0"?> |
| 1118 |
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100" width="100"> |
| 1119 |
<rect height="100%" width="100%" x="0" y="0" /> |
| 1120 |
<svg height="15" width="15" x="45" y="45"> |
| 1121 |
<rect fill="black" height="100%" width="100%" x="0" y="0" /> |
| 1122 |
</svg> |
| 1123 |
</svg> |
| 1124 |
''') |
| 1125 |
inner_rect = drawing.contents[0].contents[1].contents[0] |
| 1126 |
assert inner_rect.width == 15 |
| 1127 |
|
| 1128 |
def test_png_in_svg_file_like(self): |
| 1129 |
drawing = drawing_from_svg(''' |
| 1130 |
<?xml version="1.0"?> |
| 1131 |
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" |
| 1132 |
xmlns:xlink="http://www.w3.org/1999/xlink" |
| 1133 |
viewBox="0 0 210 297" height="297mm" width="210mm"> |
| 1134 |
<image id="refImage" xlink:href="../png/jpeg-required-201-t.png" |
| 1135 |
height="36" width="48" y="77" x="50" /> |
| 1136 |
</svg> |
| 1137 |
''') |
| 1138 |
|
| 1139 |
|
| 1140 |
assert drawing.contents[0].contents == [] |