diff --git a/cairosvg/helpers.py b/cairosvg/helpers.py index ec254c49..a2b440cf 100644 --- a/cairosvg/helpers.py +++ b/cairosvg/helpers.py @@ -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 diff --git a/cairosvg/surface.py b/cairosvg/surface.py index 69b08bbb..47ce32af 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -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: @@ -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())