@@ -960,9 +960,9 @@ def writeFonts(self):
960
960
else :
961
961
# a normal TrueType font
962
962
_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 )
966
966
self .writeObject (self .fontObject , fonts )
967
967
968
968
def _write_afm_font (self , filename ):
@@ -1136,9 +1136,8 @@ def _get_xobject_glyph_name(self, filename, glyph_name):
1136
1136
end
1137
1137
end"""
1138
1138
1139
- def embedTTF (self , filename , characters ):
1139
+ def embedTTF (self , filename , glyphs ):
1140
1140
"""Embed the TTF font from the named file into the document."""
1141
-
1142
1141
font = get_font (filename )
1143
1142
fonttype = mpl .rcParams ['pdf.fonttype' ]
1144
1143
@@ -1153,7 +1152,7 @@ def cvt(length, upe=font.units_per_EM, nearest=True):
1153
1152
else :
1154
1153
return math .ceil (value )
1155
1154
1156
- def embedTTFType3 (font , characters , descriptor ):
1155
+ def embedTTFType3 (font , glyphs , descriptor ):
1157
1156
"""The Type 3-specific part of embedding a Truetype font"""
1158
1157
widthsObject = self .reserveObject ('font widths' )
1159
1158
fontdescObject = self .reserveObject ('font descriptor' )
@@ -1200,15 +1199,13 @@ def get_char_width(charcode):
1200
1199
# Make the "Differences" array, sort the ccodes < 255 from
1201
1200
# the multi-byte ccodes, and build the whole set of glyph ids
1202
1201
# that we need from this font.
1203
- glyph_ids = []
1204
1202
differences = []
1205
1203
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 :
1210
1206
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 :
1212
1209
differences .append ((ccode , glyph_name ))
1213
1210
else :
1214
1211
multi_byte_chars .add (glyph_name )
@@ -1222,7 +1219,7 @@ def get_char_width(charcode):
1222
1219
last_c = c
1223
1220
1224
1221
# Make the charprocs array.
1225
- rawcharprocs = _get_pdf_charprocs (filename , glyph_ids )
1222
+ rawcharprocs = _get_pdf_charprocs (filename , glyphs )
1226
1223
charprocs = {}
1227
1224
for charname in sorted (rawcharprocs ):
1228
1225
stream = rawcharprocs [charname ]
@@ -1259,7 +1256,7 @@ def get_char_width(charcode):
1259
1256
1260
1257
return fontdictObject
1261
1258
1262
- def embedTTFType42 (font , characters , descriptor ):
1259
+ def embedTTFType42 (font , glyphs , descriptor ):
1263
1260
"""The Type 42-specific part of embedding a Truetype font"""
1264
1261
fontdescObject = self .reserveObject ('font descriptor' )
1265
1262
cidFontDictObject = self .reserveObject ('CID font dictionary' )
@@ -1269,9 +1266,8 @@ def embedTTFType42(font, characters, descriptor):
1269
1266
wObject = self .reserveObject ('Type 0 widths' )
1270
1267
toUnicodeMapObject = self .reserveObject ('ToUnicode map' )
1271
1268
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 :
1275
1271
fontdata = _backend_pdf_ps .font_as_file (subset )
1276
1272
_log .debug (
1277
1273
"SUBSET %s %d -> %d" , filename ,
@@ -1319,11 +1315,11 @@ def embedTTFType42(font, characters, descriptor):
1319
1315
cid_to_gid_map = ['\0 ' ] * 65536
1320
1316
widths = []
1321
1317
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 ]
1327
1323
widths .append ((ccode , cvt (glyph .horiAdvance )))
1328
1324
if ccode < 65536 :
1329
1325
cid_to_gid_map [ccode ] = chr (gind )
@@ -1361,11 +1357,10 @@ def embedTTFType42(font, characters, descriptor):
1361
1357
(len (unicode_groups ), b"\n " .join (unicode_bfrange )))
1362
1358
1363
1359
# 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
+ ]
1369
1364
1370
1365
bbox = [cvt (x , nearest = False ) for x in full_font .bbox ]
1371
1366
rawcharprocs = _get_pdf_charprocs (filename , glyph_ids )
@@ -1450,9 +1445,9 @@ def embedTTFType42(font, characters, descriptor):
1450
1445
}
1451
1446
1452
1447
if fonttype == 3 :
1453
- return embedTTFType3 (font , characters , descriptor )
1448
+ return embedTTFType3 (font , glyphs , descriptor )
1454
1449
elif fonttype == 42 :
1455
- return embedTTFType42 (font , characters , descriptor )
1450
+ return embedTTFType42 (font , glyphs , descriptor )
1456
1451
1457
1452
def alphaState (self , alpha ):
1458
1453
"""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):
2215
2210
oldx , oldy = 0 , 0
2216
2211
unsupported_chars = []
2217
2212
2213
+ font_charmaps = {}
2218
2214
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 )
2221
2217
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 ):
2223
2223
# Unsupported chars (i.e. multibyte in Type 3 or beyond BMP in
2224
2224
# 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 ))
2226
2226
else :
2227
2227
self ._setup_textpos (ox , oy , 0 , oldx , oldy )
2228
2228
oldx , oldy = ox , oy
2229
2229
if (fontname , fontsize ) != prev_font :
2230
2230
self .file .output (self .file .fontName (fontname ), fontsize ,
2231
2231
Op .selectfont )
2232
2232
prev_font = fontname , fontsize
2233
- self .file .output (self .encode_string (chr (num ), fonttype ),
2233
+ self .file .output (self .encode_string (chr (ccode ), fonttype ),
2234
2234
Op .show )
2235
2235
self .file .output (Op .end_text )
2236
2236
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 )
2240
2239
2241
2240
# Draw any horizontal lines in the math layout
2242
2241
for ox , oy , width , height in rects :
@@ -2399,7 +2398,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
2399
2398
singlebyte_chunks [- 1 ][2 ].append (item .char )
2400
2399
prev_was_multibyte = False
2401
2400
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 ))
2403
2402
prev_was_multibyte = True
2404
2403
# Do the rotation and global translation as a single matrix
2405
2404
# concatenation up front
@@ -2409,7 +2408,6 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
2409
2408
- math .sin (a ), math .cos (a ),
2410
2409
x , y , Op .concat_matrix )
2411
2410
# Emit all the 1-byte characters in a BT/ET group.
2412
-
2413
2411
self .file .output (Op .begin_text )
2414
2412
prev_start_x = 0
2415
2413
for ft_object , start_x , kerns_or_chars in singlebyte_chunks :
0 commit comments