diff --git a/cairosvg/surface.py b/cairosvg/surface.py index fee6d78f..238a5b1b 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -419,32 +419,40 @@ def draw(self, node): # Fill and stroke if self.stroke_and_fill and visible and node.tag in TAGS: - # Fill - self.context.save() - paint_source, paint_color = paint(node.get('fill', 'black')) - if not gradient_or_pattern(self, node, paint_source): - if node.get('fill-rule') == 'evenodd': - self.context.set_fill_rule(cairo.FILL_RULE_EVEN_ODD) - self.context.set_source_rgba(*color(paint_color, fill_opacity)) - if TAGS[node.tag] == text: - self.cursor_position = save_cursor[0] - self.cursor_d_position = save_cursor[1] - self.text_path_width = save_cursor[2] - text(self, node, draw_as_text=True) - else: - self.context.fill_preserve() - self.context.restore() - - # Stroke - self.context.save() - self.context.set_line_width( - size(self, node.get('stroke-width', '1'))) - paint_source, paint_color = paint(node.get('stroke')) - if not gradient_or_pattern(self, node, paint_source): - self.context.set_source_rgba( - *color(paint_color, stroke_opacity)) - self.context.stroke() - self.context.restore() + order = [1, 0] if node.get('paint-order', "fill") == "stroke" \ + else [0, 1] + for i in order: + if i == 0: + # Fill + self.context.save() + paint_source, paint_color = \ + paint(node.get('fill', 'black')) + if not gradient_or_pattern(self, node, paint_source): + if node.get('fill-rule') == 'evenodd': + self.context.set_fill_rule( + cairo.FILL_RULE_EVEN_ODD) + self.context.set_source_rgba( + *color(paint_color, fill_opacity)) + if TAGS[node.tag] == text: + self.cursor_position = save_cursor[0] + self.cursor_d_position = save_cursor[1] + self.text_path_width = save_cursor[2] + text(self, node, draw_as_text=True) + else: + self.context.fill_preserve() + self.context.restore() + else: + # Stroke + self.context.save() + self.context.set_line_width( + size(self, node.get('stroke-width', '1'))) + paint_source, paint_color = paint(node.get('stroke')) + if not gradient_or_pattern(self, node, paint_source): + self.context.set_source_rgba( + *color(paint_color, stroke_opacity)) + self.context.stroke_preserve() + self.context.restore() + self.context.new_path() elif not visible: self.context.new_path()