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
|
import io
import time
import unittest
from svgelements import *
class TestElementText(unittest.TestCase):
def test_issue_157(self):
q = io.StringIO(
"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<g id="layer1">
<text
style="font-size:18px;line-height:1.25;font-family:sans-serif;stroke-width:0.264583"
id="textobject"><tspan
id="tspanobject"
x="0"
y="0">Test</tspan></text>
</g>
</svg>
"""
)
m = SVG.parse(q)
q = list(m.elements())
self.assertIsNotNone(q[1].id) # Group
self.assertIsNotNone(q[2].id) # Text
self.assertIsNotNone(q[3].id) # TSpan
def test_shorthand_fontproperty_1(self):
font = "12pt/14pt sans-serif"
q = io.StringIO(
f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<text
font="{font}"
id="textobject">Shorthand</text>
</svg>
"""
)
m = SVG.parse(q)
text_object = list(m.elements())[1]
self.assertEqual(text_object.font_style, "normal")
self.assertEqual(text_object.font_variant, 'normal')
self.assertEqual(text_object.font_weight, "normal") # Normal
self.assertEqual(text_object.font_stretch, "normal")
self.assertEqual(text_object.font_size, Length("12pt").value())
self.assertEqual(text_object.line_height, Length("14pt").value())
self.assertEqual(text_object.font_family, "sans-serif")
self.assertEqual(text_object.font_list, ["sans-serif"])
def test_shorthand_fontproperty_2(self):
font = "80% sans-serif"
q = io.StringIO(
f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<text
font="{font}"
id="textobject">Shorthand</text>
</svg>
"""
)
m = SVG.parse(q)
text_object = list(m.elements())[1]
self.assertEqual(text_object.font_style, 'normal')
self.assertEqual(text_object.font_variant, 'normal')
self.assertEqual(text_object.font_weight, "normal") # Normal
self.assertEqual(text_object.font_stretch, "normal")
self.assertEqual(text_object.font_size, "80%")
self.assertEqual(text_object.line_height, 16.0)
self.assertEqual(text_object.font_family, "sans-serif")
def test_shorthand_fontproperty_3(self):
font = 'x-large/110% "new century schoolbook", serif'
q = io.StringIO(
f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<text
font="{font.replace('"', """)}"
id="textobject">Shorthand</text>
</svg>
"""
)
m = SVG.parse(q)
text_object = list(m.elements())[1]
self.assertEqual(text_object.font_style, "normal")
self.assertEqual(text_object.font_variant, 'normal')
self.assertEqual(text_object.font_weight, "normal") # Normal
self.assertEqual(text_object.font_stretch, "normal")
self.assertEqual(text_object.font_size, "x-large")
self.assertEqual(text_object.line_height, "110%")
self.assertEqual(text_object.font_family, '"new century schoolbook", serif')
self.assertEqual(text_object.font_list, ["new century schoolbook", "serif"])
def test_shorthand_fontproperty_4(self):
font = "bold italic large Palatino, serif"
q = io.StringIO(
f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<text
font="{font}"
id="textobject">Shorthand</text>
</svg>
"""
)
m = SVG.parse(q)
text_object = list(m.elements())[1]
self.assertEqual(text_object.font_style, "italic")
self.assertEqual(text_object.font_variant, 'normal')
self.assertEqual(text_object.font_weight, "bold") # Normal
self.assertEqual(text_object.font_stretch, "normal")
self.assertEqual(text_object.font_size, "large")
self.assertEqual(text_object.line_height, 16.0)
self.assertEqual(text_object.font_family, 'Palatino, serif')
self.assertEqual(text_object.font_list, ["Palatino", "serif"])
def test_shorthand_fontproperty_5(self):
font = "normal small-caps 120%/120% fantasy"
q = io.StringIO(
f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<text
font="{font}"
id="textobject">Shorthand</text>
</svg>
"""
)
m = SVG.parse(q)
text_object = list(m.elements())[1]
self.assertEqual(text_object.font_style, "normal")
self.assertEqual(text_object.font_variant, 'small-caps')
self.assertEqual(text_object.font_weight, "normal") # Normal
self.assertEqual(text_object.font_stretch, "normal")
self.assertEqual(text_object.font_size, "120%")
self.assertEqual(text_object.line_height, "120%")
self.assertEqual(text_object.font_family, 'fantasy')
def test_shorthand_fontproperty_6(self):
font = 'condensed oblique 12pt "Helvetica Neue", serif;'
q = io.StringIO(
f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<text
font="{font.replace('"', """)}"
id="textobject">Shorthand</text>
</svg>
"""
)
m = SVG.parse(q)
text_object = list(m.elements())[1]
self.assertEqual(text_object.font_style, 'oblique')
self.assertEqual(text_object.font_variant, 'normal')
self.assertEqual(text_object.font_weight, "normal")
self.assertEqual(text_object.font_stretch, "condensed")
self.assertEqual(text_object.font_size, Length("12pt").value())
self.assertEqual(text_object.line_height, Length("12pt").value())
self.assertEqual(text_object.font_family, '"Helvetica Neue", serif')
self.assertEqual(text_object.font_list, ["Helvetica Neue", "serif"])
def test_shorthand_fontproperty_7(self):
font = """condensed oblique 12pt "Helvetica", 'Veranda', serif;"""
q = io.StringIO(
f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<text
font="{font.replace('"', """)}"
id="textobject">Shorthand</text>
</svg>
"""
)
m = SVG.parse(q)
text_object = list(m.elements())[1]
self.assertEqual(text_object.font_style, 'oblique')
self.assertEqual(text_object.font_variant, 'normal')
self.assertEqual(text_object.font_weight, "normal")
self.assertEqual(text_object.font_stretch, "condensed")
self.assertEqual(text_object.font_size, Length("12pt").value())
self.assertEqual(text_object.line_height, Length("12pt").value())
self.assertEqual(text_object.font_family, '''"Helvetica", 'Veranda', serif''')
self.assertEqual(text_object.font_list, ["Helvetica", "Veranda", "serif"])
def test_issue_154(self):
"""
reDoS check. If suffering from Issue 154 this takes about 20 seconds. Normally 0.01s.
"""
font = "normal " * 12
q = io.StringIO(
f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg>
<text
font="{font}"
id="textobject">reDoS</text>
</svg>
"""
)
t = time.time()
m = SVG.parse(q)
t2 = time.time()
self.assertTrue((time.time() - t) < 1000)
|