Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to SVG text paint-order = "stroke" #217

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
60 changes: 34 additions & 26 deletions cairosvg/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down