Skip to content

Commit

Permalink
Respect absolute coordinates for tspan
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep committed Apr 10, 2017
1 parent bcec342 commit e31497c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions svglib/svglib.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def convertText(self, node):
fs = attrConv.findAttr(node, "font-size") or "12"
fs = attrConv.convertLength(fs)
for c in itertools.chain([node], node.getchildren()):
has_x = False
has_x, has_y = False, False
dx, dy = 0, 0
baseLineShift = 0
if node_name(c) == 'text':
Expand All @@ -703,7 +703,7 @@ def convertText(self, node):
if not text:
continue
x1, y1, dx, dy = [c.attrib.get(name, '') for name in ("x", "y", "dx", "dy")]
has_x = x1 != ''
has_x, has_y = (x1 != '', y1 != '')
x1, y1, dx, dy = map(attrConv.convertLength, (x1, y1, dx, dy))
dx0 = dx0 + dx
dy0 = dy0 + dy
Expand All @@ -716,16 +716,16 @@ def convertText(self, node):
continue

frag_lengths.append(stringWidth(text, ff, fs))
new_x = x1 if has_x else sum(frag_lengths[:-1])
shape = String(x + new_x, y - y1 - dy0 + baseLineShift, text)
new_x = x1 if has_x else (x + sum(frag_lengths[:-1]))
new_y = y1 if has_y else y
shape = String(new_x, -(new_y + dy0 - baseLineShift), text)
self.applyStyleOnShape(shape, node)
if node_name(c) == 'tspan':
self.applyStyleOnShape(shape, c)

gr.add(shape)

gr.scale(1, -1)
gr.translate(0, -2*y)

return gr

Expand Down
12 changes: 6 additions & 6 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,21 +391,21 @@ def test_tspan_position(self):
drawing = svglib.svg2rlg(io.StringIO(textwrap.dedent(u'''\
<?xml version="1.0"?>
<svg width="777" height="267">
<text x="10" style="fill:#000000; stroke:none; font-size:28;">
<text x="10" y="20" style="fill:#000000; stroke:none; font-size:28;">
<tspan>TITLE 1</tspan>
<!-- position relative to current text position-->
<tspan>(after title)</tspan>
<!-- absolute position from parent start -->
<tspan x="-10.75" y="33.487">Subtitle</tspan>
<!-- absolute position -->
<tspan x="16.75" y="33.487">Subtitle</tspan>
</text>
</svg>
''')))
main_group = drawing.contents[0]
assert main_group.contents[0].contents[0].x == 10
assert main_group.contents[0].contents[0].y == 0
assert main_group.contents[0].contents[0].y == -20
assert main_group.contents[0].contents[1].x > 10
assert main_group.contents[0].contents[1].y == 0
assert main_group.contents[0].contents[2].x == -0.75 # 10 - 10.75
assert main_group.contents[0].contents[1].y == -20
assert main_group.contents[0].contents[2].x == 16.75
assert main_group.contents[0].contents[2].y == -33.487


Expand Down

0 comments on commit e31497c

Please sign in to comment.