From 9a167a66728cf2480097b95be02b9e17ff16ff60 Mon Sep 17 00:00:00 2001 From: aziesemer Date: Wed, 14 Nov 2018 15:43:18 -0500 Subject: [PATCH 1/9] Add support to SVG text paint-order = "stroke" https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/paint-order --- cairosvg/surface.py | 54 +++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index f2df2ca2..78e6ad54 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -383,6 +383,12 @@ def draw(self, node): # Error in point parsing, do nothing pass + # Clean cursor's position after 'text' tags + if node.tag == 'text': + self.cursor_position = [0, 0] + self.cursor_d_position = [0, 0] + self.text_path_width = 0 + # Get stroke and fill opacity stroke_opacity = float(node.get('stroke-opacity', 1)) fill_opacity = float(node.get('fill-opacity', 1)) @@ -409,26 +415,28 @@ 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)) - 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() + for i in ([1,0] if node.get('paint-order', "fill") == "stroke" else [0,1]): + 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)) + 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() elif not visible: self.context.new_path() @@ -452,12 +460,6 @@ def draw(self, node): if filter_: apply_filter_after_painting(self, node, filter_) - # Clean cursor's position after 'text' tags - if node.tag == 'text': - self.cursor_position = [0, 0] - self.cursor_d_position = [0, 0] - self.text_path_width = 0 - self.context.restore() self.parent_node = old_parent_node self.font_size = old_font_size From bc95b9558ad8d96a66e4f59ef563f360c4ed409f Mon Sep 17 00:00:00 2001 From: aziesemer Date: Wed, 14 Nov 2018 15:50:31 -0500 Subject: [PATCH 2/9] Making Travis Happy --- cairosvg/surface.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index 78e6ad54..c64d3448 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -415,15 +415,19 @@ def draw(self, node): # Fill and stroke if self.stroke_and_fill and visible and node.tag in TAGS: - for i in ([1,0] if node.get('paint-order', "fill") == "stroke" else [0,1]): - if i==0: + for i in [1, 0] if node.get('paint-order', "fill") == "stroke" \ + else [0, 1]: + if i == 0: # Fill self.context.save() - paint_source, paint_color = paint(node.get('fill', 'black')) + 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)) + self.context.set_fill_rule( \ + cairo.FILL_RULE_EVEN_ODD) + self.context.set_source_rgba( \ + *color(paint_color, fill_opacity)) self.context.fill_preserve() self.context.restore() else: From 85d62c639d30c58d636e0120ff51102b265bf975 Mon Sep 17 00:00:00 2001 From: aziesemer Date: Wed, 14 Nov 2018 16:09:09 -0500 Subject: [PATCH 3/9] Update surface.py --- cairosvg/surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index c64d3448..372bb15d 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -420,7 +420,7 @@ def draw(self, node): if i == 0: # Fill self.context.save() - paint_source, paint_color = \ + paint_source, paint_color = \ paint(node.get('fill', 'black')) if not gradient_or_pattern(self, node, paint_source): if node.get('fill-rule') == 'evenodd': From 9d63c8df56260795fc80487101bb371a13ebff38 Mon Sep 17 00:00:00 2001 From: aziesemer Date: Wed, 14 Nov 2018 16:30:21 -0500 Subject: [PATCH 4/9] Fix a bug --- cairosvg/surface.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index 372bb15d..a7c3e6c1 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -415,20 +415,27 @@ def draw(self, node): # Fill and stroke if self.stroke_and_fill and visible and node.tag in TAGS: - for i in [1, 0] if node.get('paint-order', "fill") == "stroke" \ - else [0, 1]: - if i == 0: + 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( \ + self.context.set_fill_rule( cairo.FILL_RULE_EVEN_ODD) - self.context.set_source_rgba( \ + self.context.set_source_rgba( *color(paint_color, fill_opacity)) - self.context.fill_preserve() + if self.draw_text_as_text and TAGS[node.tag] == text: + text(self, node, draw_as_text=True) + else: + if i == order[1]: + self.context.fill() + else: + self.context.fill_preserve() self.context.restore() else: # Stroke @@ -439,7 +446,10 @@ def draw(self, node): if not gradient_or_pattern(self, node, paint_source): self.context.set_source_rgba( *color(paint_color, stroke_opacity)) - self.context.stroke_preserve() + if i == order[1]: + self.context.stroke() + else: + self.context.stroke_preserve() self.context.restore() elif not visible: self.context.new_path() From 0306cc9c5fc557a8d879486963e23aaf2c379dee Mon Sep 17 00:00:00 2001 From: aziesemer Date: Wed, 14 Nov 2018 16:34:11 -0500 Subject: [PATCH 5/9] Update surface.py --- cairosvg/surface.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index a7c3e6c1..ae392a5c 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -429,13 +429,10 @@ def draw(self, node): cairo.FILL_RULE_EVEN_ODD) self.context.set_source_rgba( *color(paint_color, fill_opacity)) - if self.draw_text_as_text and TAGS[node.tag] == text: - text(self, node, draw_as_text=True) + if i == order[1]: + self.context.fill() else: - if i == order[1]: - self.context.fill() - else: - self.context.fill_preserve() + self.context.fill_preserve() self.context.restore() else: # Stroke From a02ca290e61a61179cb30d4daef0fc44a31a899d Mon Sep 17 00:00:00 2001 From: aziesemer Date: Thu, 15 Nov 2018 08:44:09 -0500 Subject: [PATCH 6/9] Update surface.py --- cairosvg/surface.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index ae392a5c..d9f7d1ad 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -415,10 +415,10 @@ def draw(self, node): # Fill and stroke if self.stroke_and_fill and visible and node.tag in TAGS: - order = [1,0] if node.get('paint-order', "fill") == "stroke" \ - else [0,1] + order = [1, 0] if node.get('paint-order', "fill") == "stroke" \ + else [0, 1] for i in order: - if i==0: + if i == 0: # Fill self.context.save() paint_source, paint_color = \ From a09c44bbc22e40cbe8ad353d2c9e88d7de6931d8 Mon Sep 17 00:00:00 2001 From: aziesemer Date: Thu, 15 Nov 2018 09:37:52 -0500 Subject: [PATCH 7/9] Update surface.py --- cairosvg/surface.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index d9f7d1ad..6854261a 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -383,12 +383,6 @@ def draw(self, node): # Error in point parsing, do nothing pass - # Clean cursor's position after 'text' tags - if node.tag == 'text': - self.cursor_position = [0, 0] - self.cursor_d_position = [0, 0] - self.text_path_width = 0 - # Get stroke and fill opacity stroke_opacity = float(node.get('stroke-opacity', 1)) fill_opacity = float(node.get('fill-opacity', 1)) @@ -471,6 +465,12 @@ def draw(self, node): if filter_: apply_filter_after_painting(self, node, filter_) + # Clean cursor's position after 'text' tags + if node.tag == 'text': + self.cursor_position = [0, 0] + self.cursor_d_position = [0, 0] + self.text_path_width = 0 + self.context.restore() self.parent_node = old_parent_node self.font_size = old_font_size From 55adb5cb23745aa457498a7b552bfe6bdbd5c081 Mon Sep 17 00:00:00 2001 From: aziesemer Date: Thu, 15 Nov 2018 09:40:28 -0500 Subject: [PATCH 8/9] Update surface.py --- cairosvg/surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index 6854261a..6a7b9804 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -470,7 +470,7 @@ def draw(self, node): self.cursor_position = [0, 0] self.cursor_d_position = [0, 0] self.text_path_width = 0 - + self.context.restore() self.parent_node = old_parent_node self.font_size = old_font_size From 39d782014a834b2821b0a7c628137ec61508772e Mon Sep 17 00:00:00 2001 From: aziesemer Date: Mon, 26 Nov 2018 11:01:57 -0500 Subject: [PATCH 9/9] Making Travis Happy --- cairosvg/surface.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairosvg/surface.py b/cairosvg/surface.py index d305197d..238a5b1b 100644 --- a/cairosvg/surface.py +++ b/cairosvg/surface.py @@ -438,7 +438,7 @@ def draw(self, node): self.cursor_d_position = save_cursor[1] self.text_path_width = save_cursor[2] text(self, node, draw_as_text=True) - else: + else: self.context.fill_preserve() self.context.restore() else: