Skip to content

Make PdfFile font-related attributes private. #30027

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

Merged
merged 1 commit into from
May 8, 2025
Merged
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
3 changes: 3 additions & 0 deletions doc/api/next_api_changes/deprecations/30027-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``PdfFile.fontNames``, ``PdfFile.dviFontNames``, ``PdfFile.type1Descriptors``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... are deprecated with no replacement.
35 changes: 20 additions & 15 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,11 @@ def __init__(self, filename, metadata=None):

self.infoDict = _create_pdf_info_dict('pdf', metadata or {})

self.fontNames = {} # maps filenames to internal font names
self._internal_font_seq = (Name(f'F{i}') for i in itertools.count(1))
self.dviFontInfo = {} # maps dvi font names to embedding information
self._fontNames = {} # maps filenames to internal font names
self._dviFontInfo = {} # maps dvi font names to embedding information
# differently encoded Type-1 fonts may share the same descriptor
self.type1Descriptors = {}
self._type1Descriptors = {}
self._character_tracker = _backend_pdf_ps.CharacterTracker()

self.alphaStates = {} # maps alpha values to graphics state objects
Expand Down Expand Up @@ -765,6 +765,11 @@ def __init__(self, filename, metadata=None):
'ProcSet': procsets}
self.writeObject(self.resourceObject, resources)

fontNames = _api.deprecated("3.11")(property(lambda self: self._fontNames))
dviFontNames = _api.deprecated("3.11")(property(lambda self: self._dviFontNames))
type1Descriptors = _api.deprecated("3.11")(
property(lambda self: self._type1Descriptors))

def newPage(self, width, height):
self.endStream()

Expand Down Expand Up @@ -894,7 +899,7 @@ def _write_annotations(self):
def fontName(self, fontprop):
"""
Select a font based on fontprop and return a name suitable for
Op.selectfont. If fontprop is a string, it will be interpreted
``Op.selectfont``. If fontprop is a string, it will be interpreted
as the filename of the font.
"""

Expand All @@ -908,12 +913,12 @@ def fontName(self, fontprop):
filenames = _fontManager._find_fonts_by_props(fontprop)
first_Fx = None
for fname in filenames:
Fx = self.fontNames.get(fname)
Fx = self._fontNames.get(fname)
if not first_Fx:
first_Fx = Fx
if Fx is None:
Fx = next(self._internal_font_seq)
self.fontNames[fname] = Fx
self._fontNames[fname] = Fx
_log.debug('Assigning font %s = %r', Fx, fname)
if not first_Fx:
first_Fx = Fx
Expand All @@ -925,11 +930,11 @@ def fontName(self, fontprop):
def dviFontName(self, dvifont):
"""
Given a dvi font object, return a name suitable for Op.selectfont.
This registers the font information in ``self.dviFontInfo`` if not yet
registered.
This registers the font information internally (in ``_dviFontInfo``) if
not yet registered.
"""

dvi_info = self.dviFontInfo.get(dvifont.texname)
dvi_info = self._dviFontInfo.get(dvifont.texname)
if dvi_info is not None:
return dvi_info.pdfname

Expand All @@ -943,7 +948,7 @@ def dviFontName(self, dvifont):

pdfname = next(self._internal_font_seq)
_log.debug('Assigning font %s = %s (dvi)', pdfname, dvifont.texname)
self.dviFontInfo[dvifont.texname] = types.SimpleNamespace(
self._dviFontInfo[dvifont.texname] = types.SimpleNamespace(
dvifont=dvifont,
pdfname=pdfname,
fontfile=psfont.filename,
Expand All @@ -954,12 +959,12 @@ def dviFontName(self, dvifont):

def writeFonts(self):
fonts = {}
for dviname, info in sorted(self.dviFontInfo.items()):
for dviname, info in sorted(self._dviFontInfo.items()):
Fx = info.pdfname
_log.debug('Embedding Type-1 font %s from dvi.', dviname)
fonts[Fx] = self._embedTeXFont(info)
for filename in sorted(self.fontNames):
Fx = self.fontNames[filename]
for filename in sorted(self._fontNames):
Fx = self._fontNames[filename]
_log.debug('Embedding font %s.', filename)
if filename.endswith('.afm'):
# from pdf.use14corefonts
Expand Down Expand Up @@ -1039,10 +1044,10 @@ def _embedTeXFont(self, fontinfo):
# existing descriptor for this font.
effects = (fontinfo.effects.get('slant', 0.0),
fontinfo.effects.get('extend', 1.0))
fontdesc = self.type1Descriptors.get((fontinfo.fontfile, effects))
fontdesc = self._type1Descriptors.get((fontinfo.fontfile, effects))
if fontdesc is None:
fontdesc = self.createType1Descriptor(t1font, fontinfo.fontfile)
self.type1Descriptors[(fontinfo.fontfile, effects)] = fontdesc
self._type1Descriptors[(fontinfo.fontfile, effects)] = fontdesc
fontdict['FontDescriptor'] = fontdesc

self.writeObject(fontdictObject, fontdict)
Expand Down
Loading