Skip to content

Commit

Permalink
Take into account all font features when drawing SVG text. (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster authored Aug 8, 2022
1 parent ed925c4 commit 897d162
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion kiva/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ def default_filter(kw1):
constants.JOIN_BEVEL: "bevel",
constants.JOIN_MITER: "miter",
}
font_style_map = {
constants.NORMAL: "normal",
constants.BOLD: "normal",
constants.ITALIC: "italic",
constants.BOLD_ITALIC: "italic",
}


xmltemplate = """<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
Expand Down Expand Up @@ -140,6 +147,10 @@ def __init__(self, size, *args, **kwargs):
self._height = size[1]
self.contents = StringIO()
self._clipmap = {}
self.face_name = "default"
self.font_size = 12
self.font_style = "normal"
self.font_weight = 400

def render(self, format):
assert format == "svg"
Expand Down Expand Up @@ -182,8 +193,13 @@ def save(self, filename, file_format=None, pil_options=None):
# Text handling code

def set_font(self, font):
self.face_name = font.face_name
if font.face_name:
self.face_name = font.face_name + ', ' + font.familymap[font.family]
else:
self.face_name = font.familymap[font.family]
self.font_size = font.size
self.font_style = font_style_map[font.style]
self.font_weight = font.weight

# actual implementation =)

Expand All @@ -203,8 +219,11 @@ def device_show_text(self, text):
kw={
"font-family": self.face_name,
"font-size": str(self.font_size),
"font-style": self.font_style,
"font-weight": self.font_weight,
"xml:space": "preserve",
"transform": transform,
"fill": "rgba({:.1%},{:.1%},{:.1%},{:.1%})".format(*self.state.fill_color)
},
)

Expand Down

0 comments on commit 897d162

Please sign in to comment.