@@ -1066,12 +1066,13 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
1066
1066
self ._update_glyph_map_defs (glyph_map_new )
1067
1067
1068
1068
for glyph_id , xposition , yposition , scale in glyph_info :
1069
- attrib = {'xlink:href' : f'#{ glyph_id } ' }
1070
- if xposition != 0.0 :
1071
- attrib ['x' ] = _short_float_fmt (xposition )
1072
- if yposition != 0.0 :
1073
- attrib ['y' ] = _short_float_fmt (yposition )
1074
- writer .element ('use' , attrib = attrib )
1069
+ writer .element (
1070
+ 'use' ,
1071
+ transform = _generate_transform ([
1072
+ ('translate' , (xposition , yposition )),
1073
+ ('scale' , (scale ,)),
1074
+ ]),
1075
+ attrib = {'xlink:href' : f'#{ glyph_id } ' })
1075
1076
1076
1077
else :
1077
1078
if ismath == "TeX" :
@@ -1109,25 +1110,26 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
1109
1110
writer = self .writer
1110
1111
1111
1112
color = rgb2hex (gc .get_rgb ())
1112
- style = {}
1113
+ font_style = {}
1114
+ color_style = {}
1113
1115
if color != '#000000' :
1114
- style ['fill' ] = color
1116
+ color_s
F438
tyle ['fill' ] = color
1115
1117
1116
1118
alpha = gc .get_alpha () if gc .get_forced_alpha () else gc .get_rgb ()[3 ]
1117
1119
if alpha != 1 :
1118
- style ['opacity' ] = _short_float_fmt (alpha )
1120
+ color_style ['opacity' ] = _short_float_fmt (alpha )
1119
1121
1120
1122
if not ismath :
1121
1123
attrib = {}
1122
1124
1123
- font_parts = []
1125
+ # Separate font style in their separate attributes
1124
1126
if prop .get_style () != 'normal' :
1125
- font_parts . append ( prop .get_style () )
1127
+ font_style [ 'font-style' ] = prop .get_style ()
1126
1128
if prop .get_variant () != 'normal' :
1127
- font_parts . append ( prop .get_variant () )
1129
+ font_style [ 'font-variant' ] = prop .get_variant ()
1128
1130
weight = fm .weight_dict [prop .get_weight ()]
1129
1131
if weight != 400 :
1130
- font_parts . append ( f'{ weight } ' )
1132
+ font_style [ 'font-weight' ] = f'{ weight } '
1131
1133
1132
1134
def _normalize_sans (name ):
1133
1135
return 'sans-serif' if name in ['sans' , 'sans serif' ] else name
@@ -1150,15 +1152,15 @@ def _get_all_quoted_names(prop):
1150
1152
for entry in prop .get_family ()
1151
1153
for name in _expand_family_entry (entry )]
1152
1154
1153
- font_parts . extend ([
1154
- f' { _short_float_fmt ( prop . get_size ()) } px' ,
1155
- # ensure expansion, quoting, and dedupe of font names
1156
- ", " . join ( dict .fromkeys (_get_all_quoted_names (prop ) ))
1157
- ] )
1158
- style [ 'font' ] = ' ' . join ( font_parts )
1155
+ font_style [ 'font-size' ] = f' { _short_float_fmt ( prop . get_size ()) } px'
1156
+ # ensure expansion, quoting, and dedupe of font names
1157
+ font_style [ 'font-family' ] = ", " . join (
1158
+ dict .fromkeys (_get_all_quoted_names (prop ))
1159
+ )
1160
+
1159
1161
if prop .get_stretch () != 'normal' :
1160
- style ['font-stretch' ] = prop .get_stretch ()
1161
- attrib ['style' ] = _generate_css (style )
1162
+ font_style ['font-stretch' ] = prop .get_stretch ()
1163
+ attrib ['style' ] = _generate_css ({ ** font_style , ** color_style } )
1162
1164
1163
1165
if mtext and (angle == 0 or mtext .get_rotation_mode () == "anchor" ):
1164
1166
# If text anchoring can be supported, get the original
@@ -1180,11 +1182,11 @@ def _get_all_quoted_names(prop):
1180
1182
1181
1183
ha_mpl_to_svg = {'left' : 'start' , 'right' : 'end' ,
1182
1184
'center' : 'middle' }
1183
- style ['text-anchor' ] = ha_mpl_to_svg [mtext .get_ha ()]
1185
+ font_style ['text-anchor' ] = ha_mpl_to_svg [mtext .get_ha ()]
1184
1186
1185
1187
attrib ['x' ] = _short_float_fmt (ax )
1186
1188
attrib ['y' ] = _short_float_fmt (ay )
1187
- attrib ['style' ] = _generate_css (style )
1189
+ attrib ['style' ] = _generate_css ({ ** font_style , ** color_style } )
1188
1190
attrib ['transform' ] = _generate_transform ([
1189
1191
("rotate" , (- angle , ax , ay ))])
1190
1192
@@ -1204,7 +1206,7 @@ def _get_all_quoted_names(prop):
1204
1206
# Apply attributes to 'g', not 'text', because we likely have some
1205
1207
# rectangles as well with the same style and transformation.
1206
1208
writer .start ('g' ,
1207
- style = _generate_css (style ),
1209
+ style = _generate_css ({ ** font_style , ** color_style } ),
1208
1210
transform = _generate_transform ([
1209
1211
('translate' , (x , y )),
1210
1212
('rotate' , (- angle ,))]),
@@ -1216,43 +1218,32 @@ def _get_all_quoted_names(prop):
1216
1218
spans = {}
1217
1219
for font , fontsize , thetext , new_x , new_y in glyphs :
1218
1220
entry = fm .ttfFontProperty (font )
1219
- font_parts = []
1221
+ font_style = {}
1222
+ # Separate font style in its separate attributes
1220
1223
if entry .style != 'normal' :
1221
- font_parts . append ( entry .style )
1224
+ font_style [ 'font-style' ] = entry .style
1222
1225
if entry .variant != 'normal' :
1223
- font_parts . append ( entry .variant )
1226
+ font_style [ 'font-variant' ] = entry .variant
1224
1227
if entry .weight != 400 :
1225
- font_parts .append (f'{ entry .weight } ' )
1226
- font_parts .extend ([
1227
- f'{ _short_float_fmt (fontsize )} px' ,
1228
- f'{ entry .name !r} ' , # ensure quoting
1229
- ])
1230
- style = {'font' : ' ' .join (font_parts )}
1228
+ font_style ['font-weight' ] = f'{ entry .weight } '
1229
+ font_style ['font-size' ] = f'{ _short_float_fmt (fontsize )} px'
1230
+ font_style ['font-family' ] = f'{ entry .name !r} ' # ensure quoting
1231
1231
if entry .stretch != 'normal' :
1232
- style ['font-stretch' ] = entry .stretch
1233
- style = _generate_css (style )
1232
+ font_style ['font-stretch' ] = entry .stretch
1233
+ style = _generate_css ({ ** font_style , ** color_style } )
1234
1234
if thetext == 32 :
1235
1235
thetext = 0xa0 # non-breaking space
1236
1236
spans .setdefault (style , []).append ((new_x , - new_y , thetext ))
1237
1237
1238
1238
for style , chars in spans .items ():
1239
- chars .sort ()
1240
-
1241
- if len ({y for x , y , t in chars }) == 1 : # Are all y's the same?
1242
- ys = str (chars [0 ][1 ])
1243
- else :
1244
- ys = ' ' .join (str (c [1 ]) for c in chars )
1245
-
1246
- attrib = {
1247
- 'style' : style ,
1248
- 'x' : ' ' .join (_short_float_fmt (c [0 ]) for c in chars ),
1249
- 'y' : ys
1250
- }
1251
-
1252
- writer .element (
1253
- 'tspan' ,
1254
- '' .join (chr (c [2 ]) for c in chars ),
1255
- attrib = attrib )
1239
+ chars .sort () # Sort by increasing x position
1240
+ for x , y , t in chars : # Output one tspan for each character
1241
+ writer .element (
1242
+ 'tspan' ,
1243
+ chr (t ),
1244
+ x = _short_float_fmt (x ),
1245
+ y = _short_float_fmt (y ),
1246
+ style = style )
1256
1247
1257
1248
writer .end ('text' )
1258
1249
0 commit comments