Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4e54cf

Browse files
committed
Parse FontBBox in type1font.
... instead of having to go through ft2font in createType1Descriptor just to extract the font bbox, ascender and descender. FontBBox is gauranteed to exist in the type1 font definition by the standard; its parsing as a size-4 array matches freetype's behavior (see ps_parser_load_field); and using bbox entries as ascender and descender also matches freetype's behavior (T1_Face_Init directly assigns `root->ascender = (FT_Short)(root->bbox.yMax)` and likewise for the descender; see also the docs for ascender and descender in FT_FaceRec).
1 parent 1c881a3 commit c4e54cf

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*fontfile* parameter of ``PdfFile.createType1Descriptor``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
This parameter is deprecated; all relevant pieces of information are now
4+
directly extracted from the *t1font* argument.

lib/matplotlib/_type1font.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,16 @@ def _parse(self):
579579
extras = ('(?i)([ -](regular|plain|italic|oblique|(semi)?bold|'
580580
'(ultra)?light|extra|condensed))+$')
581581
prop['FamilyName'] = re.sub(extras, '', prop['FullName'])
582+
583+
# Parse FontBBox
584+
toks = [*_tokenize(prop['FontBBox'].encode('ascii'), True)]
585+
if ([tok.kind for tok in toks]
586+
!= ['delimiter', 'number', 'number', 'number', 'number', 'delimiter']
587+
or toks[-1].raw != toks[0].opposite()):
588+
raise RuntimeError(
589+
f"FontBBox should be a size-4 array, was {prop['FontBBox']}")
590+
prop['FontBBox'] = [tok.value() for tok in toks[1:-1]]
591+
582592
# Decrypt the encrypted parts
583593
ndiscard = prop.get('lenIV', 4)
584594
cs = prop['CharStrings']

lib/matplotlib/backends/backend_pdf.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,14 +1034,15 @@ def _embedTeXFont(self, fontinfo):
10341034
fontinfo.effects.get('extend', 1.0))
10351035
fontdesc = self._type1Descriptors.get((fontinfo.fontfile, effects))
10361036
if fontdesc is None:
1037-
fontdesc = self.createType1Descriptor(t1font, fontinfo.fontfile)
1037+
fontdesc = self.createType1Descriptor(t1font)
10381038
self._type1Descriptors[(fontinfo.fontfile, effects)] = fontdesc
10391039
fontdict['FontDescriptor'] = fontdesc
10401040

10411041
self.writeObject(fontdictObject, fontdict)
10421042
return fontdictObject
10431043

1044-
def createType1Descriptor(self, t1font, fontfile):
1044+
@_api.delete_parameter("3.11", "fontfile")
1045+
def createType1Descriptor(self, t1font, fontfile=None):
10451046
# Create and write the font descriptor and the font file
10461047
# of a Type-1 font
10471048
fontdescObject = self.reserveObject('font descriptor')
@@ -1076,24 +1077,22 @@ def createType1Descriptor(self, t1font, fontfile):
10761077
if 0:
10771078
flags |= 1 << 18
10781079

1079-
ft2font = get_font(fontfile)
1080-
10811080
descriptor = {
10821081
'Type': Name('FontDescriptor'),
10831082
'FontName': Name(t1font.prop['FontName']),
10841083
'Flags': flags,
1085-
'FontBBox': ft2font.bbox,
1084+
'FontBBox': t1font.prop['FontBBox'],
10861085
'ItalicAngle': italic_angle,
1087-
'Ascent': ft2font.ascender,
1088-
'Descent': ft2font.descender,
1086+
'Ascent': t1font.prop['FontBBox'][3],
1087+
'Descent': t1font.prop['FontBBox'][1],
10891088
'CapHeight': 1000, # TODO: find this out
10901089
'XHeight': 500, # TODO: this one too
10911090
'FontFile': fontfileObject,
10921091
'FontFamily': t1font.prop['FamilyName'],
10931092
'StemV': 50, # TODO
10941093
# (see also revision 3874; but not all TeX distros have AFM files!)
10951094
# 'FontWeight': a number where 400 = Regular, 700 = Bold
1096-
}
1095+
}
10971096

10981097
self.writeObject(fontdescObject, descriptor)
10991098

0 commit comments

Comments
 (0)