19
19
import sys
20
20
import time
21
21
import types
22
- import typing
23
22
import warnings
24
23
import zlib
25
24
36
35
from matplotlib .figure import Figure
37
36
from matplotlib .font_manager import get_font , fontManager as _fontManager
38
37
from 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
41
39
from matplotlib .transforms import Affine2D , BboxBase
42
40
from matplotlib .path import Path
43
41
from matplotlib .dates import UTC
@@ -962,9 +960,9 @@ def writeFonts(self):
962
960
else :
963
961
# a normal TrueType font
964
962
_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 )
968
966
self .writeObject (self .fontObject , fonts )
969
967
970
968
def _write_afm_font (self , filename ):
@@ -1006,8 +1004,9 @@ def _embedTeXFont(self, dvifont):
1006
1004
1007
1005
# Reduce the font to only the glyphs used in the document, get the encoding
1008
1006
# 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 ()))
1011
1010
fontdict ['BaseFont' ] = Name (t1font .prop ['FontName' ])
1012
1011
# createType1Descriptor writes the font data as a side effect
1013
1012
fontdict ['FontDescriptor' ] = self .createType1Descriptor (t1font )
@@ -1138,7 +1137,7 @@ def _get_xobject_glyph_name(self, filename, glyph_name):
1138
1137
end
1139
1138
end"""
1140
1139
1141
- def embedTTF (self , filename , glyphs ):
1140
+ def embedTTF (self , filename , charmap ):
1142
1141
"""Embed the TTF font from the named file into the document."""
1143
1142
font = get_font (filename )
1144
1143
fonttype = mpl .rcParams ['pdf.fonttype' ]
@@ -1154,7 +1153,7 @@ def cvt(length, upe=font.units_per_EM, nearest=True):
1154
1153
else :
1155
1154
return math .ceil (value )
1156
1155
1157
- def embedTTFType3 (font , glyphs , descriptor ):
1156
+ def embedTTFType3 (font , charmap , descriptor ):
1158
1157
"""The Type 3-specific part of embedding a Truetype font"""
1159
1158
widthsObject = self .reserveObject ('font widths' )
1160
1159
fontdescObject = self .reserveObject ('font descriptor' )
@@ -1201,10 +1200,8 @@ def get_char_width(charcode):
1201
1200
# that we need from this font.
1202
1201
differences = []
1203
1202
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 ():
1206
1204
glyph_name = font .get_glyph_name (gind )
1207
- ccode = charmap .get (gind )
1208
1205
if ccode is not None and ccode <= 255 :
1209
1206
differences .append ((ccode , glyph_name ))
1210
1207
else :
@@ -1219,7 +1216,7 @@ def get_char_width(charcode):
1219
1216
last_c = c
1220
1217
1221
1218
# Make the charprocs array.
1222
- rawcharprocs = _get_pdf_charprocs (filename , glyphs )
1219
+ rawcharprocs = _get_pdf_charprocs (filename , charmap . values () )
1223
1220
charprocs = {}
1224
1221
for charname in sorted (rawcharprocs ):
1225
1222
stream = rawcharprocs [charname ]
@@ -1256,7 +1253,7 @@ def get_char_width(charcode):
1256
1253
1257
1254
return fontdictObject
1258
1255
1259
- def embedTTFType42 (font , glyphs , descriptor ):
1256
+ def embedTTFType42 (font , charmap , descriptor ):
1260
1257
"""The Type 42-specific part of embedding a Truetype font"""
1261
1258
fontdescObject = self .reserveObject ('font descriptor' )
1262
1259
cidFontDictObject = self .reserveObject ('CID font dictionary' )
@@ -1266,8 +1263,9 @@ def embedTTFType42(font, glyphs, descriptor):
1266
1263
wObject = self .reserveObject ('Type 0 widths' )
1267
1264
toUnicodeMapObject = self .reserveObject ('ToUnicode map' )
1268
1265
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 :
1271
1269
fontdata = _backend_pdf_ps .font_as_file (subset )
1272
1270
_log .debug (
1273
1271
"SUBSET %s %d -> %d" , filename ,
@@ -1315,11 +1313,9 @@ def embedTTFType42(font, glyphs, descriptor):
1315
1313
cid_to_gid_map = ['\0 ' ] * 65536
1316
1314
widths = []
1317
1315
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 ():
1320
1317
glyph = font .load_glyph (gind ,
1321
1318
flags = LoadFlags .NO_SCALE | LoadFlags .NO_HINTING )
1322
- ccode = charmap [gind ]
1323
1319
widths .append ((ccode , cvt (glyph .horiAdvance )))
1324
1320
if ccode < 65536 :
1325
1321
cid_to_gid_map [ccode ] = chr (gind )
@@ -1358,8 +1354,8 @@ def embedTTFType42(font, glyphs, descriptor):
1358
1354
1359
1355
# Add XObjects for unsupported chars
1360
1356
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 )
1363
1359
]
1364
1360
1365
1361
bbox = [cvt (x , nearest = False ) for x in full_font .bbox ]
@@ -1445,9 +1441,9 @@ def embedTTFType42(font, glyphs, descriptor):
1445
1441
}
1446
1442
1447
1443
if fonttype == 3 :
1448
- return embedTTFType3 (font , glyphs , descriptor )
1444
+ return embedTTFType3 (font , charmap , descriptor )
1449
1445
elif fonttype == 42 :
1450
- return embedTTFType42 (font , glyphs , descriptor )
1446
+ return embedTTFType42 (font , charmap , descriptor )
1451
1447
1452
1448
def alphaState (self , alpha ):
1453
1449
"""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):
2212
2208
2213
2209
self .file .output (Op .begin_text )
2214
2210
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 )
2216
2212
fontname = font .fname
2217
2213
if not _font_supports_glyph (fonttype , ccode ):
2218
2214
# 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):
2268
2264
seq += [['font' , pdfname , text .font .size ]]
2269
2265
oldfont = text .font
2270
2266
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 )
2276
2268
2277
2269
# Find consecutive text strings with constant y coordinate and
2278
2270
# combine into a sequence of strings and kerns, or just one
0 commit comments