19
19
import sys
20
20
import time
21
21
import types
22
+ import typing as T
22
23
import warnings
23
24
import zlib
24
25
@@ -989,24 +990,12 @@ def _embedTeXFont(self, fontinfo):
989
990
_log .debug ('Embedding TeX font %s - fontinfo=%s' ,
990
991
fontinfo .dvifont .texname , fontinfo .__dict__ )
991
992
992
- # Widths
993
- widthsObject = self .reserveObject ('font widths' )
994
- tfm = fontinfo .dvifont ._tfm
995
- # convert from TeX's 12.20 representation to 1/1000 text space units.
996
- widths = [(1000 * metrics .tex_width ) >> 20
997
- if (metrics := tfm .get_metrics (char )) else 0
998
- for char in range (max (tfm ._glyph_metrics , default = - 1 ) + 1 )]
999
- self .writeObject (widthsObject , widths )
1000
-
1001
993
# Font dictionary
1002
994
fontdictObject = self .reserveObject ('font dictionary' )
1003
- fontdict = {
995
+ fontdict : dict [ str , T . Any ] = {
1004
996
'Type' : Name ('Font' ),
1005
997
'Subtype' : Name ('Type1' ),
1006
- 'FirstChar' : 0 ,
1007
- 'LastChar' : len (widths ) - 1 ,
1008
- 'Widths' : widthsObject ,
1009
- }
998
+ }
1010
999
1011
1000
# Encoding (if needed)
1012
1001
if fontinfo .encodingfile is not None :
@@ -1035,7 +1024,8 @@ def _embedTeXFont(self, fontinfo):
1035
1024
chars = self ._character_tracker .used [fontinfo .dvifont .fname ]
1036
1025
t1font = t1font .subset (chars )
1037
1026
fontdict ['BaseFont' ] = Name (t1font .prop ['FontName' ])
1038
-
1027
+ fc = fontdict ['FirstChar' ] = min (t1font .prop ['Encoding' ].keys (), default = 0 )
1028
+ lc = fontdict ['LastChar' ] = max (t1font .prop ['Encoding' ].keys (), default = 255 )
1039
1029
# Font descriptors may be shared between differently encoded
1040
1030
# Type-1 fonts, so only create a new descriptor if there is no
1041
1031
# existing descriptor for this font.
@@ -1047,7 +1037,15 @@ def _embedTeXFont(self, fontinfo):
1047
1037
self .type1Descriptors [(fontinfo .fontfile , effects )] = fontdesc
1048
1038
fontdict ['FontDescriptor' ] = fontdesc
1049
1039
1040
+ # Use TeX Font Metrics file to get glyph widths (TeX uses its 12.20 fixed point
1041
+ # representation and we want 1/1000 text space units)
1042
+ tfm = fontinfo .dvifont ._tfm
1043
+ widths = [(1000 * metrics .tex_width ) >> 20
1044
+ if (metrics := tfm .get_metrics (char )) else 0
1045
+ for char in range (fc , lc + 1 )]
1046
+ fontdict ['Widths' ] = (widthsObject := self .reserveObject ('glyph widths' ))
1050
1047
self .writeObject (fontdictObject , fontdict )
1048
+ self .writeObject (widthsObject , widths )
1051
1049
return fontdictObject
1052
1050
1053
1051
def createType1Descriptor (self , t1font , fontfile ):
@@ -1087,6 +1085,14 @@ def createType1Descriptor(self, t1font, fontfile):
1087
1085
1088
1086
ft2font = get_font (fontfile )
1089
1087
1088
+ encoding = t1font .prop ['Encoding' ]
1089
+ charset = '' .join (
1090
+ sorted (
1091
+ f'/{ c } ' for c in encoding .values ()
1092
+ if c != '.notdef'
1093
+ )
1094
+ )
1095
+
1090
1096
descriptor = {
1091
1097
'Type' : Name ('FontDescriptor' ),
1092
1098
'FontName' : Name (t1font .prop ['FontName' ]),
@@ -1100,6 +1106,7 @@ def createType1Descriptor(self, t1font, fontfile):
1100
1106
'FontFile' : fontfileObject ,
1101
1107
'FontFamily' : t1font .prop ['FamilyName' ],
1102
1108
'StemV' : 50 , # TODO
1109
+ 'CharSet' : charset ,
1103
1110
# (see also revision 3874; but not all TeX distros have AFM files!)
1104
1111
# 'FontWeight': a number where 400 = Regular, 700 = Bold
1105
1112
}
0 commit comments