Repository: matplotlib/matplotlib
File: lib/matplotlib/backends/backend_pdf.py (Python)

Code before:
                # from pdf.use14corefonts
                _log.debug('Writing AFM font.')
                fonts[Fx] = self._write_afm_font(filename)
            else:
                # a normal TrueType font
                _log.debug('Writing TrueType font.')
                chars = self._character_tracker.used.get(filename)
                if chars:
                    fonts[Fx] = self.embedTTF(filename, chars)
        self.writeObject(self.fontObject, fonts)

    def _write_afm_font(self, filename):
        with open(filename, 'rb') as fh:
            font = AFM(fh)
        fontname = font.get_fontname()
        fontdict = {'Type': Name('Font'),
                    'Subtype': Name('Type1'),
                    'BaseFont': Name(fontname),
                    'Encoding': Name('WinAnsiEncoding')}
        fontdictObject = self.reserveObject('font dictionary')
        self.writeObject(fontdictObject, fontdict)
        return fontdictObject

    def _embedTeXFont(self, dvifont):
        psfont = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))[
            dvifont.texname]
        if psfont.filename is None:
            raise ValueError(
                "No usable font file found for {} (TeX: {}); "
                "the font may lack a Type-1 version"
                .format(psfont.psname, dvifont.texname))

        # Widths
        widthsObject = self.reserveObject('font widths')
        tfm = dvifont._tfm
        # convert from TeX's 12.20 representation to 1/1000 text space units.
        widths = [(1000 * metrics.tex_width) >> 20
                  if (metrics := tfm.get_metrics(char)) else 0
                  for char in range(max(tfm._glyph_metrics, default=-1) + 1)]
        self.writeObject(widthsObject, widths)

        # Font dictionary
        fontdictObject = self.reserveObject('font dictionary')
        fontdict = {
            'Type':      Name('Font'),
            'Subtype':   Name('Type1'),
            'FirstChar': 0,
            'LastChar':  len(widths) - 1,
            'Widths':    widthsObject,
            }


Review comment (suggestion):
Might as well keep the separate variable if this is going to wrap to two lines anyway.
```suggestion
        tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))
        psfont = tex_font_map[dvifont.texname]
```

Code after:
    def writeFonts(self):
        fonts = {}
        for pdfname, dvifont in sorted(self._dviFontInfo.items()):
            _log.debug('Embedding Type-1 font %s from dvi.', dvifont.texname)
            fonts[pdfname] = self._embedTeXFont(dvifont)
        for filename in sorted(self._fontNames):
            Fx = self._fontNames[filename]
            _log.debug('Embedding font %s.', filename)
            if filename.endswith('.afm'):
                # from pdf.use14corefonts
                _log.debug('Writing AFM font.')
                fonts[Fx] = self._write_afm_font(filename)
            else:
                # a normal TrueType font
                _log.debug('Writing TrueType font.')
                chars = self._character_tracker.used.get(filename)
                if chars:
                    fonts[Fx] = self.embedTTF(filename, chars)
        self.writeObject(self.fontObject, fonts)

    def _write_afm_font(self, filename):
        with open(filename, 'rb') as fh:
            font = AFM(fh)
        fontname = font.get_fontname()
        fontdict = {'Type': Name('Font'),
                    'Subtype': Name('Type1'),
                    'BaseFont': Name(fontname),
                    'Encoding': Name('WinAnsiEncoding')}
        fontdictObject = self.reserveObject('font dictionary')
        self.writeObject(fontdictObject, fontdict)
        return fontdictObject

    def _embedTeXFont(self, dvifont):
        tex_font_map = dviread.PsfontsMap(dviread.find_tex_file('pdftex.map'))
        psfont = tex_font_map[dvifont.texname]
        if psfont.filename is None:
            raise ValueError(
                "No usable font file found for {} (TeX: {}); "
                "the font may lack a Type-1 version"
                .format(psfont.psname, dvifont.texname))

        # The font dictionary is the top-level object describing a font
        fontdictObject = self.reserveObject('font dictionary')
        fontdict = {
            'Type':      Name('Font'),
            'Subtype':   Name('Type1'),
        }

        # Read the font file and apply any encoding changes and effects
        t1font = _type1font.Type1Font(psfont.filename)
        if psfont.encoding is not None: