1from fontTools.misc.py23 import tobytes 2from fontTools.pens.recordingPen import RecordingPen 3from fontTools.svgLib import SVGPath 4 5import os 6from tempfile import NamedTemporaryFile 7 8 9SVG_DATA = """\ 10<?xml version="1.0" standalone="no"?> 11<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" 12 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> 13<svg version="1.0" xmlns="http://www.w3.org/2000/svg" 14 width="1000.0" height="1000.0"> 15<path d="M 100 100 L 300 100 L 200 300 z"/> 16<path d="M100,200 C100,100 250,100 250,200 S400,300 400,200"/> 17</svg> 18""" 19 20EXPECTED_PEN_COMMANDS = [ 21 ("moveTo", ((100.0, 100.0),)), 22 ("lineTo", ((300.0, 100.0),)), 23 ("lineTo", ((200.0, 300.0),)), 24 ("lineTo", ((100.0, 100.0),)), 25 ("closePath", ()), 26 ("moveTo", ((100.0, 200.0),)), 27 ("curveTo", ((100.0, 100.0), 28 (250.0, 100.0), 29 (250.0, 200.0))), 30 ("curveTo", ((250.0, 300.0), 31 (400.0, 300.0), 32 (400.0, 200.0))), 33 ("endPath", ()) 34] 35 36 37class SVGPathTest(object): 38 39 def test_from_svg_file(self): 40 pen = RecordingPen() 41 with NamedTemporaryFile(delete=False) as tmp: 42 tmp.write(tobytes(SVG_DATA)) 43 try: 44 svg = SVGPath(tmp.name) 45 svg.draw(pen) 46 finally: 47 os.remove(tmp.name) 48 49 assert pen.value == EXPECTED_PEN_COMMANDS 50 51 def test_fromstring(self): 52 pen = RecordingPen() 53 svg = SVGPath.fromstring(SVG_DATA) 54 svg.draw(pen) 55 56 assert pen.value == EXPECTED_PEN_COMMANDS 57 58 def test_transform(self): 59 pen = RecordingPen() 60 svg = SVGPath.fromstring(SVG_DATA, 61 transform=(1.0, 0, 0, -1.0, 0, 1000)) 62 svg.draw(pen) 63 64 assert pen.value == [ 65 ("moveTo", ((100.0, 900.0),)), 66 ("lineTo", ((300.0, 900.0),)), 67 ("lineTo", ((200.0, 700.0),)), 68 ("lineTo", ((100.0, 900.0),)), 69 ("closePath", ()), 70 ("moveTo", ((100.0, 800.0),)), 71 ("curveTo", ((100.0, 900.0), 72 (250.0, 900.0), 73 (250.0, 800.0))), 74 ("curveTo", ((250.0, 700.0), 75 (400.0, 700.0), 76 (400.0, 800.0))), 77 ("endPath", ()) 78 ] 79