1919import sys
2020import time
2121import types
22- import typing
2322import warnings
2423import zlib
2524
3635from matplotlib .figure import Figure
3736from matplotlib .font_manager import get_font , fontManager as _fontManager
3837from matplotlib ._afm import AFM
39- from matplotlib .ft2font import (
40- FT2Font , FaceFlags , GlyphIndexType , Kerning , LoadFlags , StyleFlags )
38+ from matplotlib .ft2font import FT2Font , FaceFlags , Kerning , LoadFlags , StyleFlags
4139from matplotlib .transforms import Affine2D , BboxBase
4240from matplotlib .path import Path
4341from matplotlib .dates import UTC
@@ -962,9 +960,9 @@ def writeFonts(self):
962960 else :
963961 # a normal TrueType font
964962 _log .debug ('Writing TrueType font.' )
965- glyphs = self ._character_tracker .used .get (filename )
966- if glyphs :
967- fonts [Fx ] = self .embedTTF (filename , glyphs )
963+ charmap = self ._character_tracker .used .get (( filename , 0 ) )
964+ if charmap :
965+ fonts [Fx ] = self .embedTTF (filename , charmap )
968966 self .writeObject (self .fontObject , fonts )
969967
970968 def _write_afm_font (self , filename ):
@@ -1006,8 +1004,9 @@ def _embedTeXFont(self, dvifont):
10061004
10071005 # Reduce the font to only the glyphs used in the document, get the encoding
10081006 # for that subset, and compute various properties based on the encoding.
1009- chars = frozenset (self ._character_tracker .used [dvifont .fname ])
1010- t1font = t1font .subset (chars , self ._get_subset_prefix (chars ))
1007+ charmap = self ._character_tracker .used [(dvifont .fname , 0 )]
1008+ chars = frozenset (charmap .keys ())
1009+ t1font = t1font .subset (chars , self ._get_subset_prefix (charmap .values ()))
10111010 fontdict ['BaseFont' ] = Name (t1font .prop ['FontName' ])
10121011 # createType1Descriptor writes the font data as a side effect
10131012 fontdict ['FontDescriptor' ] = self .createType1Descriptor (t1font )
@@ -1138,7 +1137,7 @@ def _get_xobject_glyph_name(self, filename, glyph_name):
11381137end
11391138end"""
11401139
1141- def embedTTF (self , filename , glyphs ):
1140+ def embedTTF (self , filename , charmap ):
11421141 """Embed the TTF font from the named file into the document."""
11431142 font = get_font (filename )
11441143 fonttype = mpl .rcParams ['pdf.fonttype' ]
@@ -1154,7 +1153,7 @@ def cvt(length, upe=font.units_per_EM, nearest=True):
11541153 else :
11551154 return math .ceil (value )
11561155
1157- def embedTTFType3 (font , glyphs , descriptor ):
1156+ def embedTTFType3 (font , charmap , descriptor ):
11581157 """The Type 3-specific part of embedding a Truetype font"""
11591158 widthsObject = self .reserveObject ('font widths' )
11601159 fontdescObject = self .reserveObject ('font descriptor' )
@@ -1201,10 +1200,8 @@ def get_char_width(charcode):
12011200 # that we need from this font.
12021201 differences = []
12031202 multi_byte_chars = set ()
1204- charmap = {gind : ccode for ccode , gind in font .get_charmap ().items ()}
1205- for gind in glyphs :
1203+ for ccode , gind in charmap .items ():
12061204 glyph_name = font .get_glyph_name (gind )
1207- ccode = charmap .get (gind )
12081205 if ccode is not None and ccode <= 255 :
12091206 differences .append ((ccode , glyph_name ))
12101207 else :
@@ -1219,7 +1216,7 @@ def get_char_width(charcode):
12191216 last_c = c
12201217
12211218 # Make the charprocs array.
1222- rawcharprocs = _get_pdf_charprocs (filename , glyphs )
1219+ rawcharprocs = _get_pdf_charprocs (filename , charmap . values () )
12231220 charprocs = {}
12241221 for charname in sorted (rawcharprocs ):
12251222 stream = rawcharprocs [charname ]
@@ -1256,7 +1253,7 @@ def get_char_width(charcode):
12561253
12571254 return fontdictObject
12581255
1259- def embedTTFType42 (font , glyphs , descriptor ):
1256+ def embedTTFType42 (font , charmap , descriptor ):
12601257 """The Type 42-specific part of embedding a Truetype font"""
12611258 fontdescObject = self .reserveObject ('font descriptor' )
12621259 cidFontDictObject = self .reserveObject ('CID font dictionary' )
@@ -1266,8 +1263,9 @@ def embedTTFType42(font, glyphs, descriptor):
12661263 wObject = self .reserveObject ('Type 0 widths' )
12671264 toUnicodeMapObject = self .reserveObject ('ToUnicode map' )
12681265
1269- _log .debug ("SUBSET %s characters: %s" , filename , glyphs )
1270- with _backend_pdf_ps .get_glyphs_subset (filename , glyphs ) as subset :
1266+ _log .debug ("SUBSET %s characters: %s" , filename , charmap )
1267+ with _backend_pdf_ps .get_glyphs_subset (filename ,
1268+ charmap .values ()) as subset :
12711269 fontdata = _backend_pdf_ps .font_as_file (subset )
12721270 _log .debug (
12731271 "SUBSET %s %d -> %d" , filename ,
@@ -1315,11 +1313,9 @@ def embedTTFType42(font, glyphs, descriptor):
13151313 cid_to_gid_map = ['\0 ' ] * 65536
13161314 widths = []
13171315 max_ccode = 0
1318- charmap = {gind : ccode for ccode , gind in font .get_charmap ().items ()}
1319- for gind in glyphs :
1316+ for ccode , gind in charmap .items ():
13201317 glyph = font .load_glyph (gind ,
13211318 flags = LoadFlags .NO_SCALE | LoadFlags .NO_HINTING )
1322- ccode = charmap [gind ]
13231319 widths .append ((ccode , cvt (glyph .horiAdvance )))
13241320 if ccode < 65536 :
13251321 cid_to_gid_map [ccode ] = chr (gind )
@@ -1358,8 +1354,8 @@ def embedTTFType42(font, glyphs, descriptor):
13581354
13591355 # Add XObjects for unsupported chars
13601356 glyph_indices = [
1361- glyph_index for glyph_index in glyphs
1362- if not _font_supports_glyph (fonttype , charmap [ glyph_index ] )
1357+ glyph_index for ccode , glyph_index in charmap . items ()
1358+ if not _font_supports_glyph (fonttype , ccode )
13631359 ]
13641360
13651361 bbox = [cvt (x , nearest = False ) for x in full_font .bbox ]
@@ -1445,9 +1441,9 @@ def embedTTFType42(font, glyphs, descriptor):
14451441 }
14461442
14471443 if fonttype == 3 :
1448- return embedTTFType3 (font , glyphs , descriptor )
1444+ return embedTTFType3 (font , charmap , descriptor )
14491445 elif fonttype == 42 :
1450- return embedTTFType42 (font , glyphs , descriptor )
1446+ return embedTTFType42 (font , charmap , descriptor )
14511447
14521448 def alphaState (self , alpha ):
14531449 """Return name of an ExtGState that sets alpha to the given value."""
@@ -2212,7 +2208,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
22122208
22132209 self .file .output (Op .begin_text )
22142210 for font , fontsize , ccode , glyph_index , ox , oy in glyphs :
2215- self .file ._character_tracker .track_glyph (font , glyph_index )
2211+ self .file ._character_tracker .track_glyph (font , ccode , glyph_index )
22162212 fontname = font .fname
22172213 if not _font_supports_glyph (fonttype , ccode ):
22182214 # Unsupported chars (i.e. multibyte in Type 3 or beyond BMP in
@@ -2268,11 +2264,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
22682264 seq += [['font' , pdfname , text .font .size ]]
22692265 oldfont = text .font
22702266 seq += [['text' , text .x , text .y , [bytes ([text .glyph ])], text .x + text .width ]]
2271- # TODO: This should use glyph indices, not character codes, but will be
2272- # fixed soon.
2273- self .file ._character_tracker .track_glyph (text .font ,
2274- typing .cast ('GlyphIndexType' ,
2275- text .glyph ))
2267+ self .file ._character_tracker .track_glyph (text .font , text .glyph , text .index )
22762268
22772269 # Find consecutive text strings with constant y coordinate and
22782270 # combine into a sequence of strings and kerns, or just one
0 commit comments