Skip to content

Commit

Permalink
Follow output-width and output-height parameters correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed May 20, 2019
1 parent 0d71521 commit 889ee3f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
10 changes: 6 additions & 4 deletions cairosvg/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ def point_angle(cx, cy, px, py):
return atan2(py - cy, px - cx)


def preserve_ratio(surface, node):
def preserve_ratio(surface, node, width=None, height=None):
"""Manage the ratio preservation."""
if node.tag == 'marker':
width = size(surface, node.get('markerWidth', '3'), 'x')
height = size(surface, node.get('markerHeight', '3'), 'y')
width = width or size(surface, node.get('markerWidth', '3'), 'x')
height = height or size(surface, node.get('markerHeight', '3'), 'y')
_, _, viewbox = node_format(surface, node)
viewbox_width, viewbox_height = viewbox[2:]
elif node.tag in ('svg', 'image', 'g'):
width, height, _ = node_format(surface, node)
node_width, node_height, _ = node_format(surface, node)
width = width or node_width
height = height or node_height
viewbox_width, viewbox_height = node.image_width, node.image_height

translate_x = 0
Expand Down
9 changes: 2 additions & 7 deletions cairosvg/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,6 @@ def __init__(self, tree, output, dpi, parent_surface=None,
self.stroke_and_fill = True
width, height, viewbox = node_format(self, tree)

# If one of output_width or output_height is set, compute the scale
if output_width:
scale = output_width / width
elif output_height:
scale = output_height / height

if output_width and output_height:
width, height = output_width, output_height
else:
Expand Down Expand Up @@ -246,7 +240,8 @@ def set_context_size(self, width, height, viewbox, scale, tree):
tree.image_width = width
tree.image_height = height

scale_x, scale_y, translate_x, translate_y = preserve_ratio(self, tree)
scale_x, scale_y, translate_x, translate_y = preserve_ratio(
self, tree, width, height)
rect_x, rect_y = rect_x * scale_x, rect_y * scale_y
rect_width, rect_height = width, height
self.context.translate(*self.context.get_current_point())
Expand Down

0 comments on commit 889ee3f

Please sign in to comment.