@@ -1050,12 +1050,13 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
1050
1050
self ._update_glyph_map_defs (glyph_map_new )
1051
1051
1052
1052
for glyph_id , xposition , yposition , scale in glyph_info :
1053
- attrib = {'xlink:href' : f'#{ glyph_id } ' }
1054
- if xposition != 0.0 :
1055
- attrib ['x' ] = _short_float_fmt (xposition )
1056
- if yposition != 0.0 :
1057
- attrib ['y' ] = _short_float_fmt (yposition )
1058
- writer .element ('use' , attrib = attrib )
1053
+ writer .element (
1054
+ 'use' ,
1055
+ transform = _generate_transform ([
1056
+ ('translate' , (xposition , yposition )),
1057
+ ('scale' , (scale ,)),
1058
+ ]),
1059
+ attrib = {'xlink:href' : f'#{ glyph_id } ' })
1059
1060
1060
1061
else :
1061
1062
if ismath == "TeX" :
@@ -1093,25 +1094,26 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
1093
1094
writer = self .writer
1094
1095
1095
1096
color = rgb2hex (gc .get_rgb ())
1096
- style = {}
1097
+ font_style = {}
1098
+ color_style = {}
1097
1099
if color != '#000000' :
1098
- style ['fill' ] = color
1100
+ color_style ['fill' ] = color
1099
1101
1100
1102
alpha = gc .get_alpha () if gc .get_forced_alpha () else gc .get_rgb ()[3 ]
1101
1103
if alpha != 1 :
1102
- style ['opacity' ] = _short_float_fmt (alpha )
1104
+ color_style ['opacity' ] = _short_float_fmt (alpha )
1103
1105
1104
1106
if not ismath :
1105
1107
attrib = {}
1106
1108
1107
- font_parts = []
1109
+ # Separate font style in their separate attributes
1108
1110
if prop .get_style () != 'normal' :
1109
- font_parts . append ( prop .get_style () )
1111
+ font_style [ 'font-style' ] = prop .get_style ()
1110
1112
if prop .get_variant () != 'normal' :
1111
- font_parts . append ( prop .get_variant () )
1113
+ font_style [ 'font-variant' ] = prop .get_variant ()
1112
1114
weight = fm .weight_dict [prop .get_weight ()]
1113
1115
if weight != 400 :
1114
- font_parts . append ( f'{ weight } ' )
1116
+ font_style [ 'font-weight' ] = f'{ weight } '
1115
1117
1116
1118
def _normalize_sans (name ):
1117
1119
return 'sans-serif' if name in ['sans' , 'sans serif' ] else name
@@ -1134,15 +1136,15 @@ def _get_all_quoted_names(prop):
1134
1136
for entry in prop .get_family ()
1135
1137
for name in _expand_family_entry (entry )]
1136
1138
1137
- font_parts . extend ([
1138
- f' { _short_float_fmt ( prop . get_size ()) } px' ,
1139
- # ensure expansion, quoting, and dedupe of font names
1140
- ", " . join ( dict .fromkeys (_get_all_quoted_names (prop ) ))
1141
- ] )
1142
- style [ 'font' ] = ' ' . join ( font_parts )
1139
+ font_style [ 'font-size' ] = f' { _short_float_fmt ( prop . get_size ()) } px'
1140
+ # ensure expansion, quoting, and dedupe of font names
1141
+ font_style [ 'font-family' ] = ", " . join (
1142
+ dict .fromkeys (_get_all_quoted_names (prop ))
1143
+ )
1144
+
1143
1145
if prop .get_stretch () != 'normal' :
1144
- style ['font-stretch' ] = prop .get_stretch ()
1145
- attrib ['style' ] = _generate_css (style )
1146
+ font_style ['font-stretch' ] = prop .get_stretch ()
1147
+ attrib ['style' ] = _generate_css ({ ** font_style , ** color_style } )
1146
1148
1147
1149
if mtext and (angle == 0 or mtext .get_rotation_mode () == "anchor" ):
1148
1150
# If text anchoring can be supported, get the original
@@ -1164,11 +1166,11 @@ def _get_all_quoted_names(prop):
1164
1166
1165
1167
ha_mpl_to_svg = {'left' : 'start' , 'right' : 'end' ,
1166
1168
'center' : 'middle' }
1167
- style ['text-anchor' ] = ha_mpl_to_svg [mtext .get_ha ()]
1169
+ font_style ['text-anchor' ] = ha_mpl_to_svg [mtext .get_ha ()]
1168
1170
1169
1171
attrib ['x' ] = _short_float_fmt (ax )
1170
1172
attrib ['y' ] = _short_float_fmt (ay )
1171
- attrib ['style' ] = _generate_css (style )
1173
+ attrib ['style' ] = _generate_css ({ ** font_style , ** color_style } )
1172
1174
attrib ['transform' ] = _generate_transform ([
1173
1175
("rotate" , (- angle , ax , ay ))])
1174
1176
@@ -1188,7 +1190,7 @@ def _get_all_quoted_names(prop):
1188
1190
# Apply attributes to 'g', not 'text', because we likely have some
1189
1191
# rectangles as well with the same style and transformation.
1190
1192
writer .start ('g' ,
1191
- style = _generate_css (style ),
1193
+ style = _generate_css ({ ** font_style , ** color_style } ),
1192
1194
transform = _generate_transform ([
1193
1195
('translate' , (x , y )),
1194
1196
('rotate' , (- angle ,))]),
@@ -1200,43 +1202,32 @@ def _get_all_quoted_names(prop):
1200
1202
spans = {}
1201
1203
for font , fontsize , thetext , new_x , new_y in glyphs :
1202
1204
entry = fm .ttfFontProperty (font )
1203
- font_parts = []
1205
+ font_style = {}
1206
+ # Separate font style in its separate attributes
1204
1207
if entry .style != 'normal' :
1205
- font_parts . append ( entry .style )
1208
+ font_style [ 'font-style' ] = entry .style
1206
1209
if entry .variant != 'normal' :
1207
- font_parts . append ( entry .variant )
1210
+ font_style [ 'font-variant' ] = entry .variant
1208
1211
if entry .weight != 400 :
1209
- font_parts .append (f'{ entry .weight } ' )
1210
- font_parts .extend ([
1211
- f'{ _short_float_fmt (fontsize )} px' ,
1212
- f'{ entry .name !r} ' , # ensure quoting
1213
- ])
1214
- style = {'font' : ' ' .join (font_parts )}
1212
+ font_style ['font-weight' ] = f'{ entry .weight } '
1213
+ font_style ['font-size' ] = f'{ _short_float_fmt (fontsize )} px'
1214
+ font_style ['font-family' ] = f'{ entry .name !r} ' # ensure quoting
1215
1215
if entry .stretch != 'normal' :
1216
- style ['font-stretch' ] = entry .stretch
1217
- style = _generate_css (style )
1216
+ font_style ['font-stretch' ] = entry .stretch
1217
+ style = _generate_css ({ ** font_style , ** color_style } )
1218
1218
if thetext == 32 :
1219
1219
thetext = 0xa0 # non-breaking space
1220
1220
spans .setdefault (style , []).append ((new_x , - new_y , thetext ))
1221
1221
1222
1222
for style , chars in spans .items ():
1223
- chars .sort ()
1224
-
1225
- if len ({y for x , y , t in chars }) == 1 : # Are all y's the same?
1226
- ys = str (chars [0 ][1 ])
1227
- else :
1228
- ys = ' ' .join (str (c [1 ]) for c in chars )
1229
-
1230
- attrib = {
1231
- 'style' : style ,
1232
- 'x' : ' ' .join (_short_float_fmt (c [0 ]) for c in chars ),
1233
- 'y' : ys
1234
- }
1235
-
1236
- writer .element (
1237
- 'tspan' ,
1238
- '' .join (chr (c [2 ]) for c in chars ),
1239
- attrib = attrib )
1223
+ chars .sort () # Sort by increasing x position
1224
+ for x , y , t in chars : # Output one tspan for each character
1225
+ writer .element (
1226
+ 'tspan' ,
1227
+ chr (t ),
1228
+ x = _short_float_fmt (x ),
1229
+ y = _short_float_fmt (y ),
1230
+ style = style )
1240
1231
1241
1232
writer .end ('text' )
1242
1233
0 commit comments