@@ -960,9 +960,9 @@ def writeFonts(self):
960960 else :
961961 # a normal TrueType font
962962 _log .debug ('Writing TrueType font.' )
963- chars = self ._character_tracker .used .get (filename )
964- if chars :
965- fonts [Fx ] = self .embedTTF (filename , chars )
963+ glyphs = self ._character_tracker .used .get (filename )
964+ if glyphs :
965+ fonts [Fx ] = self .embedTTF (filename , glyphs )
966966 self .writeObject (self .fontObject , fonts )
967967
968968 def _write_afm_font (self , filename ):
@@ -1136,9 +1136,8 @@ def _get_xobject_glyph_name(self, filename, glyph_name):
11361136end
11371137end"""
11381138
1139- def embedTTF (self , filename , characters ):
1139+ def embedTTF (self , filename , glyphs ):
11401140 """Embed the TTF font from the named file into the document."""
1141-
11421141 font = get_font (filename )
11431142 fonttype = mpl .rcParams ['pdf.fonttype' ]
11441143
@@ -1153,7 +1152,7 @@ def cvt(length, upe=font.units_per_EM, nearest=True):
11531152 else :
11541153 return math .ceil (value )
11551154
1156- def embedTTFType3 (font , characters , descriptor ):
1155+ def embedTTFType3 (font , glyphs , descriptor ):
11571156 """The Type 3-specific part of embedding a Truetype font"""
11581157 widthsObject = self .reserveObject ('font widths' )
11591158 fontdescObject = self .reserveObject ('font descriptor' )
@@ -1200,15 +1199,13 @@ def get_char_width(charcode):
12001199 # Make the "Differences" array, sort the ccodes < 255 from
12011200 # the multi-byte ccodes, and build the whole set of glyph ids
12021201 # that we need from this font.
1203- glyph_ids = []
12041202 differences = []
12051203 multi_byte_chars = set ()
1206- for c in characters :
1207- ccode = c
1208- gind = font .get_char_index (ccode )
1209- glyph_ids .append (gind )
1204+ charmap = {gind : ccode for ccode , gind in font .get_charmap ().items ()}
1205+ for gind in glyphs :
12101206 glyph_name = font .get_glyph_name (gind )
1211- if ccode <= 255 :
1207+ ccode = charmap .get (gind )
1208+ if ccode is not None and ccode <= 255 :
12121209 differences .append ((ccode , glyph_name ))
12131210 else :
12141211 multi_byte_chars .add (glyph_name )
@@ -1222,7 +1219,7 @@ def get_char_width(charcode):
12221219 last_c = c
12231220
12241221 # Make the charprocs array.
1225- rawcharprocs = _get_pdf_charprocs (filename , glyph_ids )
1222+ rawcharprocs = _get_pdf_charprocs (filename , glyphs )
12261223 charprocs = {}
12271224 for charname in sorted (rawcharprocs ):
12281225 stream = rawcharprocs [charname ]
@@ -1259,7 +1256,7 @@ def get_char_width(charcode):
12591256
12601257 return fontdictObject
12611258
1262- def embedTTFType42 (font , characters , descriptor ):
1259+ def embedTTFType42 (font , glyphs , descriptor ):
12631260 """The Type 42-specific part of embedding a Truetype font"""
12641261 fontdescObject = self .reserveObject ('font descriptor' )
12651262 cidFontDictObject = self .reserveObject ('CID font dictionary' )
@@ -1269,9 +1266,8 @@ def embedTTFType42(font, characters, descriptor):
12691266 wObject = self .reserveObject ('Type 0 widths' )
12701267 toUnicodeMapObject = self .reserveObject ('ToUnicode map' )
12711268
1272- subset_str = "" .join (chr (c ) for c in characters )
1273- _log .debug ("SUBSET %s characters: %s" , filename , subset_str )
1274- with _backend_pdf_ps .get_glyphs_subset (filename , subset_str ) as subset :
1269+ _log .debug ("SUBSET %s characters: %s" , filename , glyphs )
1270+ with _backend_pdf_ps .get_glyphs_subset (filename , glyphs ) as subset :
12751271 fontdata = _backend_pdf_ps .font_as_file (subset )
12761272 _log .debug (
12771273 "SUBSET %s %d -> %d" , filename ,
@@ -1319,11 +1315,11 @@ def embedTTFType42(font, characters, descriptor):
13191315 cid_to_gid_map = ['\0 ' ] * 65536
13201316 widths = []
13211317 max_ccode = 0
1322- for c in characters :
1323- ccode = c
1324- gind = font .get_char_index ( ccode )
1325- glyph = font . load_char ( ccode ,
1326- flags = LoadFlags . NO_SCALE | LoadFlags . NO_HINTING )
1318+ charmap = { gind : ccode for ccode , gind in font . get_charmap (). items ()}
1319+ for gind in glyphs :
1320+ glyph = font .load_glyph ( gind ,
1321+ flags = LoadFlags . NO_SCALE | LoadFlags . NO_HINTING )
1322+ ccode = charmap [ gind ]
13271323 widths .append ((ccode , cvt (glyph .horiAdvance )))
13281324 if ccode < 65536 :
13291325 cid_to_gid_map [ccode ] = chr (gind )
@@ -1361,11 +1357,10 @@ def embedTTFType42(font, characters, descriptor):
13611357 (len (unicode_groups ), b"\n " .join (unicode_bfrange )))
13621358
13631359 # Add XObjects for unsupported chars
1364- glyph_ids = []
1365- for ccode in characters :
1366- if not _font_supports_glyph (fonttype , ccode ):
1367- gind = full_font .get_char_index (ccode )
1368- glyph_ids .append (gind )
1360+ glyph_ids = [
1361+ gind for gind in glyphs
1362+ if not _font_supports_glyph (fonttype , charmap [gind ])
1363+ ]
13691364
13701365 bbox = [cvt (x , nearest = False ) for x in full_font .bbox ]
13711366 rawcharprocs = _get_pdf_charprocs (filename , glyph_ids )
@@ -1450,9 +1445,9 @@ def embedTTFType42(font, characters, descriptor):
14501445 }
14511446
14521447 if fonttype == 3 :
1453- return embedTTFType3 (font , characters , descriptor )
1448+ return embedTTFType3 (font , glyphs , descriptor )
14541449 elif fonttype == 42 :
1455- return embedTTFType42 (font , characters , descriptor )
1450+ return embedTTFType42 (font , glyphs , descriptor )
14561451
14571452 def alphaState (self , alpha ):
14581453 """Return name of an ExtGState that sets alpha to the given value."""
@@ -2215,28 +2210,32 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
22152210 oldx , oldy = 0 , 0
22162211 unsupported_chars = []
22172212
2213+ font_charmaps = {}
22182214 self .file .output (Op .begin_text )
2219- for font , fontsize , num , ox , oy in glyphs :
2220- self .file ._character_tracker .track_glyph (font , num )
2215+ for font , fontsize , glyph_index , ox , oy in glyphs :
2216+ self .file ._character_tracker .track_glyph (font , glyph_index )
22212217 fontname = font .fname
2222- if not _font_supports_glyph (fonttype , num ):
2218+ if font not in font_charmaps :
2219+ font_charmaps [font ] = {gind : ccode
2220+ for ccode , gind in font .get_charmap ().items ()}
2221+ ccode = font_charmaps [font ].get (glyph_index )
2222+ if ccode is None or not _font_supports_glyph (fonttype , ccode ):
22232223 # Unsupported chars (i.e. multibyte in Type 3 or beyond BMP in
22242224 # Type 42) must be emitted separately (below).
2225- unsupported_chars .append ((font , fontsize , ox , oy , num ))
2225+ unsupported_chars .append ((font , fontsize , ox , oy , glyph_index ))
22262226 else :
22272227 self ._setup_textpos (ox , oy , 0 , oldx , oldy )
22282228 oldx , oldy = ox , oy
22292229 if (fontname , fontsize ) != prev_font :
22302230 self .file .output (self .file .fontName (fontname ), fontsize ,
22312231 Op .selectfont )
22322232 prev_font = fontname , fontsize
2233- self .file .output (self .encode_string (chr (num ), fonttype ),
2233+ self .file .output (self .encode_string (chr (ccode ), fonttype ),
22342234 Op .show )
22352235 self .file .output (Op .end_text )
22362236
2237- for font , fontsize , ox , oy , num in unsupported_chars :
2238- self ._draw_xobject_glyph (
2239- font , fontsize , font .get_char_index (num ), ox , oy )
2237+ for font , fontsize , ox , oy , glyph_index in unsupported_chars :
2238+ self ._draw_xobject_glyph (font , fontsize , glyph_index , ox , oy )
22402239
22412240 # Draw any horizontal lines in the math layout
22422241 for ox , oy , width , height in rects :
@@ -2274,7 +2273,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
22742273 seq += [['font' , pdfname , dvifont .size ]]
22752274 oldfont = dvifont
22762275 seq += [['text' , x1 , y1 , [bytes ([glyph ])], x1 + width ]]
2277- self .file ._character_tracker .track (dvifont , chr ( glyph ) )
2276+ self .file ._character_tracker .track_glyph (dvifont , glyph )
22782277
22792278 # Find consecutive text strings with constant y coordinate and
22802279 # combine into a sequence of strings and kerns, or just one
@@ -2399,7 +2398,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23992398 singlebyte_chunks [- 1 ][2 ].append (item .char )
24002399 prev_was_multibyte = False
24012400 else :
2402- multibyte_glyphs .append ((item .ft_object , item .x , item .glyph_idx ))
2401+ multibyte_glyphs .append ((item .ft_object , item .x , item .glyph_index ))
24032402 prev_was_multibyte = True
24042403 # Do the rotation and global translation as a single matrix
24052404 # concatenation up front
@@ -2409,7 +2408,6 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
24092408 - math .sin (a ), math .cos (a ),
24102409 x , y , Op .concat_matrix )
24112410 # Emit all the 1-byte characters in a BT/ET group.
2412-
24132411 self .file .output (Op .begin_text )
24142412 prev_start_x = 0
24152413 for ft_object , start_x , kerns_or_chars in singlebyte_chunks :
0 commit comments