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

Skip to content

Parse FontBBox in type1font. #30088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations/30088-AL.rst
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions lib/matplotlib/_type1font.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,16 @@
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(

Check warning on line 588 in lib/matplotlib/_type1font.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/_type1font.py#L588

Added line #L588 was not covered by tests
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']
Expand Down
15 changes: 7 additions & 8 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -1076,24 +1077,22 @@ 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,
'FontFamily': t1font.prop['FamilyName'],
'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)

Expand Down
Loading