From c4e54cfc303c203e4acac728c17cedbea22fd2ef Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 20 May 2025 11:51:03 +0200 Subject: [PATCH] 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). --- .../next_api_changes/deprecations/30088-AL.rst | 4 ++++ lib/matplotlib/_type1font.py | 10 ++++++++++ lib/matplotlib/backends/backend_pdf.py | 15 +++++++-------- 3 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 doc/api/next_api_changes/deprecations/30088-AL.rst diff --git a/doc/api/next_api_changes/deprecations/30088-AL.rst b/doc/api/next_api_changes/deprecations/30088-AL.rst new file mode 100644 index 000000000000..ae1338da7f85 --- /dev/null +++ b/doc/api/next_api_changes/deprecations/30088-AL.rst @@ -0,0 +1,4 @@ +*fontfile* parameter of ``PdfFile.createType1Descriptor`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This parameter is deprecated; all relevant pieces of information are now +directly extracted from the *t1font* argument. diff --git a/lib/matplotlib/_type1font.py b/lib/matplotlib/_type1font.py index b3e08f52c035..032b6a42ea63 100644 --- a/lib/matplotlib/_type1font.py +++ b/lib/matplotlib/_type1font.py @@ -579,6 +579,16 @@ def _parse(self): extras = ('(?i)([ -](regular|plain|italic|oblique|(semi)?bold|' '(ultra)?light|extra|condensed))+$') prop['FamilyName'] = re.sub(extras, '', prop['FullName']) + + # Parse FontBBox + toks = [*_tokenize(prop['FontBBox'].encode('ascii'), True)] + if ([tok.kind for tok in toks] + != ['delimiter', 'number', 'number', 'number', 'number', 'delimiter'] + or toks[-1].raw != toks[0].opposite()): + raise RuntimeError( + f"FontBBox should be a size-4 array, was {prop['FontBBox']}") + prop['FontBBox'] = [tok.value() for tok in toks[1:-1]] + # Decrypt the encrypted parts ndiscard = prop.get('lenIV', 4) cs = prop['CharStrings'] diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 73cf8bc19be2..073ca05bc172 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1034,14 +1034,15 @@ def _embedTeXFont(self, fontinfo): fontinfo.effects.get('extend', 1.0)) fontdesc = self._type1Descriptors.get((fontinfo.fontfile, effects)) if fontdesc is None: - fontdesc = self.createType1Descriptor(t1font, fontinfo.fontfile) + fontdesc = self.createType1Descriptor(t1font) self._type1Descriptors[(fontinfo.fontfile, effects)] = fontdesc fontdict['FontDescriptor'] = fontdesc self.writeObject(fontdictObject, fontdict) return fontdictObject - def createType1Descriptor(self, t1font, fontfile): + @_api.delete_parameter("3.11", "fontfile") + def createType1Descriptor(self, t1font, fontfile=None): # Create and write the font descriptor and the font file # of a Type-1 font fontdescObject = self.reserveObject('font descriptor') @@ -1076,16 +1077,14 @@ def createType1Descriptor(self, t1font, fontfile): if 0: flags |= 1 << 18 - ft2font = get_font(fontfile) - descriptor = { 'Type': Name('FontDescriptor'), 'FontName': Name(t1font.prop['FontName']), 'Flags': flags, - 'FontBBox': ft2font.bbox, + 'FontBBox': t1font.prop['FontBBox'], 'ItalicAngle': italic_angle, - 'Ascent': ft2font.ascender, - 'Descent': ft2font.descender, + 'Ascent': t1font.prop['FontBBox'][3], + 'Descent': t1font.prop['FontBBox'][1], 'CapHeight': 1000, # TODO: find this out 'XHeight': 500, # TODO: this one too 'FontFile': fontfileObject, @@ -1093,7 +1092,7 @@ def createType1Descriptor(self, t1font, fontfile): 'StemV': 50, # TODO # (see also revision 3874; but not all TeX distros have AFM files!) # 'FontWeight': a number where 400 = Regular, 700 = Bold - } + } self.writeObject(fontdescObject, descriptor)