2525from matplotlib .backend_bases import (
2626 _Backend , FigureCanvasBase , FigureManagerBase , GraphicsContextBase ,
2727 RendererBase )
28- from matplotlib .cbook import (get_realpath_and_stat , is_writable_file_like ,
29- file_requires_unicode )
28+ from matplotlib .cbook import is_writable_file_like , file_requires_unicode
3029from matplotlib .font_manager import is_opentype_cff_font , get_font
3130from matplotlib .ft2font import LOAD_NO_HINTING
3231from matplotlib .ttconv import convert_ttf_to_ps
@@ -202,9 +201,7 @@ def __init__(self, width, height, pswriter, imagedpi=72):
202201 # Although postscript itself is dpi independent, we need to inform the
203202 # image code about a requested dpi to generate high resolution images
204203 # and them scale them before embedding them.
205- RendererBase .__init__ (self )
206- self .width = width
207- self .height = height
204+ super ().__init__ (width , height )
208205 self ._pswriter = pswriter
209206 if rcParams ['text.usetex' ]:
210207 self .textcnt = 0
@@ -224,21 +221,22 @@ def __init__(self, width, height, pswriter, imagedpi=72):
224221 self ._clip_paths = {}
225222 self ._path_collection_id = 0
226223
227- self .used_characters = {}
224+ self ._character_tracker = _backend_pdf_ps . CharacterTracker ()
228225 self .mathtext_parser = MathTextParser ("PS" )
229226
230- def track_characters (self , font , s ):
227+ @cbook .deprecated ("3.3" )
228+ @property
229+ def used_characters (self ):
230+ return self ._character_tracker .used_characters
231+
232+ @cbook .deprecated ("3.3" )
233+ def track_characters (self , * args , ** kwargs ):
231234 """Keeps track of which characters are required from each font."""
232- realpath , stat_key = get_realpath_and_stat (font .fname )
233- used_characters = self .used_characters .setdefault (
234- stat_key , (realpath , set ()))
235- used_characters [1 ].update (map (ord , s ))
235+ self ._character_tracker .track (* args , ** kwargs )
236236
237- def merge_used_characters (self , other ):
238- for stat_key , (realpath , charset ) in other .items ():
239- used_characters = self .used_characters .setdefault (
240- stat_key , (realpath , set ()))
241- used_characters [1 ].update (charset )
237+ @cbook .deprecated ("3.3" )
238+ def merge_used_characters (self , * args , ** kwargs ):
239+ self ._character_tracker .merge (* args , ** kwargs )
242240
243241 def set_color (self , r , g , b , store = 1 ):
244242 if (r , g , b ) != self .color :
@@ -621,7 +619,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
621619 else :
622620 font = self ._get_font_ttf (prop )
623621 font .set_text (s , 0 , flags = LOAD_NO_HINTING )
624- self .track_characters (font , s )
622+ self ._character_tracker . track (font , s )
625623
626624 self .set_color (* gc .get_rgb ())
627625 ps_name = (font .postscript_name
@@ -650,7 +648,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
650648
651649 width , height , descent , pswriter , used_characters = \
652650 self .mathtext_parser .parse (s , 72 , prop )
653- self .merge_used_characters (used_characters )
651+ self ._character_tracker . merge (used_characters )
654652 self .set_color (* gc .get_rgb ())
655653 thetext = pswriter .getvalue ()
656654 self ._pswriter .write (f"""\
@@ -980,46 +978,40 @@ def print_figure_impl(fh):
980978 Ndict = len (psDefs )
981979 print ("%%BeginProlog" , file = fh )
982980 if not rcParams ['ps.useafm' ]:
983- Ndict += len (ps_renderer .used_characters )
981+ Ndict += len (ps_renderer ._character_tracker . used )
984982 print ("/mpldict %d dict def" % Ndict , file = fh )
985983 print ("mpldict begin" , file = fh )
986984 for d in psDefs :
987985 d = d .strip ()
988986 for l in d .split ('\n ' ):
989987 print (l .strip (), file = fh )
990988 if not rcParams ['ps.useafm' ]:
991- for font_filename , chars in \
992- ps_renderer .used_characters .values ():
993- if len (chars ):
994- font = get_font (font_filename )
995- glyph_ids = [font .get_char_index (c ) for c in chars ]
996-
997- fonttype = rcParams ['ps.fonttype' ]
998-
999- # Can not use more than 255 characters from a
1000- # single font for Type 3
1001- if len (glyph_ids ) > 255 :
1002- fonttype = 42
1003-
1004- # The ttf to ps (subsetting) support doesn't work for
1005- # OpenType fonts that are Postscript inside (like the
1006- # STIX fonts). This will simply turn that off to avoid
1007- # errors.
1008- if is_opentype_cff_font (font_filename ):
1009- raise RuntimeError (
1010- "OpenType CFF fonts can not be saved using "
1011- "the internal Postscript backend at this "
1012- "time; consider using the Cairo backend" )
1013- else :
1014- fh .flush ()
1015- try :
1016- convert_ttf_to_ps (os .fsencode (font_filename ),
1017- fh , fonttype , glyph_ids )
1018- except RuntimeError :
1019- _log .warning ("The PostScript backend does not "
1020- "currently support the selected "
1021- "font." )
1022- raise
989+ for font_path , chars \
990+ in ps_renderer ._character_tracker .used .items ():
991+ if not chars :
992+ continue
993+ font = get_font (font_path )
994+ glyph_ids = [font .get_char_index (c ) for c in chars ]
995+ fonttype = rcParams ['ps.fonttype' ]
996+ # Can't use more than 255 chars from a single Type 3 font.
997+ if len (glyph_ids ) > 255 :
998+ fonttype = 42
999+ # The ttf to ps (subsetting) support doesn't work for
1000+ # OpenType fonts that are Postscript inside (like the STIX
1001+ # fonts). This will simply turn that off to avoid errors.
1002+ if is_opentype_cff_font (font_path ):
1003+ raise RuntimeError (
1004+ "OpenType CFF fonts can not be saved using "
1005+ "the internal Postscript backend at this "
1006+ "time; consider using the Cairo backend" )
1007+ fh .flush ()
1008+ try :
1009+ convert_ttf_to_ps (os .fsencode (font_path ),
1010+ fh , fonttype , glyph_ids )
1011+ except RuntimeError :
1012+ _log .warning ("The PostScript backend does not "
1013+ "currently support the selected font." )
1014+ raise
10231015 print ("end" , file = fh )
10241016 print ("%%EndProlog" , file = fh )
10251017
0 commit comments