@@ -881,27 +881,37 @@ def draw_image(self, gc, x, y, im, transform=None):
881
881
if clipid is not None :
882
882
self .writer .end ('g' )
883
883
884
+ def _update_glyph_map_defs (self , glyph_map_new ):
885
+ """
886
+ Emit definitions for not-yet-defined glyphs, and record them as having
887
+ been defined.
888
+ """
889
+ writer = self .writer
890
+ if glyph_map_new :
891
+ writer .start ('defs' )
892
+ for char_id , (vertices , codes ) in glyph_map_new .items ():
893
+ char_id = self ._adjust_char_id (char_id )
894
+ path_data = self ._convert_path (
895
+ Path (vertices , codes ), simplify = False )
896
+ writer .element ('path' , id = char_id , d = path_data )
897
+ writer .end ('defs' )
898
+ self ._glyph_map .update (glyph_map_new )
899
+
884
900
def _adjust_char_id (self , char_id ):
885
901
return char_id .replace ("%20" , "_" )
886
902
887
903
def _draw_text_as_path (self , gc , x , y , s , prop , angle , ismath , mtext = None ):
888
904
"""
889
- draw the text by converting them to paths using textpath module.
905
+ Draw the text by converting them to paths using the textpath module.
890
906
891
907
Parameters
892
908
----------
893
- prop : `matplotlib.font_manager.FontProperties`
894
- font property
895
-
896
909
s : str
897
910
text to be converted
898
-
899
- usetex : bool
900
- If True, use matplotlib usetex mode.
901
-
911
+ prop : `matplotlib.font_manager.FontProperties`
912
+ font property
902
913
ismath : bool
903
914
If True, use mathtext parser. If "TeX", use *usetex* mode.
904
-
905
915
"""
906
916
writer = self .writer
907
917
@@ -916,86 +926,46 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
916
926
style = {}
917
927
if color != '#000000' :
918
928
style ['fill' ] = color
919
-
920
929
alpha = gc .get_alpha () if gc .get_forced_alpha () else gc .get_rgb ()[3 ]
921
930
if alpha != 1 :
922
931
style ['opacity' ] = short_float_fmt (alpha )
932
+ font_scale = fontsize / text2path .FONT_SCALE
933
+ attrib = {
934
+ 'style' : generate_css (style ),
935
+ 'transform' : generate_transform ([
936
+ ('translate' , (x , y )),
937
+ ('rotate' , (- angle ,)),
938
+ ('scale' , (font_scale , - font_scale ))]),
939
+ }
940
+ writer .start ('g' , attrib = attrib )
923
941
924
942
if not ismath :
925
943
font = text2path ._get_font (prop )
926
944
_glyphs = text2path .get_glyphs_with_font (
927
945
font , s , glyph_map = glyph_map , return_new_glyphs_only = True )
928
946
glyph_info , glyph_map_new , rects = _glyphs
947
+ self ._update_glyph_map_defs (glyph_map_new )
929
948
930
- if glyph_map_new :
931
- writer .start ('defs' )
932
- for char_id , glyph_path in glyph_map_new .items ():
933
- path = Path (* glyph_path )
934
- path_data = self ._convert_path (path , simplify = False )
935
- writer .element ('path' , id = char_id , d = path_data )
936
- writer .end ('defs' )
937
-
938
- glyph_map .update (glyph_map_new )
939
-
940
- attrib = {}
941
- attrib ['style' ] = generate_css (style )
942
- font_scale = fontsize / text2path .FONT_SCALE
943
- attrib ['transform' ] = generate_transform ([
944
- ('translate' , (x , y )),
945
- ('rotate' , (- angle ,)),
946
- ('scale' , (font_scale , - font_scale ))])
947
-
948
- writer .start ('g' , attrib = attrib )
949
949
for glyph_id , xposition , yposition , scale in glyph_info :
950
950
attrib = {'xlink:href' : '#%s' % glyph_id }
951
951
if xposition != 0.0 :
952
952
attrib ['x' ] = short_float_fmt (xposition )
953
953
if yposition != 0.0 :
954
954
attrib ['y' ] = short_float_fmt (yposition )
955
- writer .element (
956
- 'use' ,
957
- attrib = attrib )
955
+ writer .element ('use' , attrib = attrib )
958
956
959
- writer .end ('g' )
960
957
else :
961
958
if ismath == "TeX" :
962
959
_glyphs = text2path .get_glyphs_tex (
963
960
prop , s , glyph_map = glyph_map , return_new_glyphs_only = True )
964
961
else :
965
962
_glyphs = text2path .get_glyphs_mathtext (
966
963
prop , s , glyph_map = glyph_map , return_new_glyphs_only = True )
967
-
968
964
glyph_info , glyph_map_new , rects = _glyphs
965
+ self ._update_glyph_map_defs (glyph_map_new )
969
966
970
- # We store the character glyphs w/o flipping. Instead, the
971
- # coordinate will be flipped when these characters are used.
972
- if glyph_map_new :
973
- writer .start ('defs' )
974
- for char_id , glyph_path in glyph_map_new .items ():
975
- char_id = self ._adjust_char_id (char_id )
976
- # Some characters are blank
977
- if not len (glyph_path [0 ]):
978
- path_data = ""
979
- else :
980
- path = Path (* glyph_path )
981
- path_data = self ._convert_path (path , simplify = False )
982
- writer .element ('path' , id = char_id , d = path_data )
983
- writer .end ('defs' )
984
-
985
- glyph_map .update (glyph_map_new )
986
-
987
- attrib = {}
988
- font_scale = fontsize / text2path .FONT_SCALE
989
- attrib ['style' ] = generate_css (style )
990
- attrib ['transform' ] = generate_transform ([
991
- ('translate' , (x , y )),
992
- ('rotate' , (- angle ,)),
993
- ('scale' , (font_scale , - font_scale ))])
994
-
995
- writer .start ('g' , attrib = attrib )
996
967
for char_id , xposition , yposition , scale in glyph_info :
997
968
char_id = self ._adjust_char_id (char_id )
998
-
999
969
writer .element (
1000
970
'use' ,
1001
971
transform = generate_transform ([
@@ -1009,7 +979,7 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
1009
979
path_data = self ._convert_path (path , simplify = False )
1010
980
writer .element ('path' , d = path_data )
1011
981
1012
- writer .end ('g' )
982
+ writer .end ('g' )
1013
983
1014
984
def _draw_text_as_text (self , gc , x , y , s , prop , angle , ismath , mtext = None ):
1015
985
writer = self .writer
0 commit comments