Repository: matplotlib/matplotlib
File: lib/matplotlib/dviread.py (Python)

Code before:

    def _height_depth_of(self, char):
        """Height and depth of char in dvi units."""
        metrics = self._metrics.get_metrics(char)
        if metrics is None:
            _log.debug('No metrics for char %d in font %s', char, self.texname)
            return [0, 0]
        hd = [
            _mul1220(metrics.tex_height, self._scale),
            _mul1220(metrics.tex_depth, self._scale),
        ]
        # cmsyXX (symbols font) glyph 0 ("minus") has a nonzero descent
        # so that TeX aligns equations properly
        # (https://tex.stackexchange.com/q/526103/)
        # but we actually care about the rasterization depth to align
        # the dvipng-generated images.
        if re.match(br'^cmsy\d+$', self.texname) and char == 0:
            hd[-1] = 0
        return hd

    def resolve_path(self):
        if self._path is None:
            try:
                psfont = PsfontsMap(find_tex_file("pdftex.map"))[self.texname]
            except LookupError as exc:
                try:
                    find_tex_file(f"{self.texname.decode('ascii')}.mf")
                except FileNotFoundError:
                    raise exc from None
                else:
                    self._path = Path(find_tex_file(
                        f"{self.texname.decode('ascii')}.600pk"))
            else:
                if psfont.filename is None:
                    raise ValueError("No usable font file found for {} ({}); "
                                     "the font may lack a Type-1 version"
                                     .format(psfont.psname.decode("ascii"),
                                             psfont.texname.decode("ascii")))
                self._path = Path(psfont.filename)
        return self._path

    @cached_property
    def subfont(self):
        return 0

    @cached_property
    def effects(self):
        return ({} if self.resolve_path().match("*.600pk") else
                PsfontsMap(find_tex_file("pdftex.map"))[self.texname].effects)

    def _index_dvi_to_freetype(self, idx):

Review comment (suggestion):
I assume it would never had generated a `LookupError` anyway, but the `PsfontsMap` constructor seems like a lot to put in this `try`.

Code after:

    def _height_depth_of(self, char):
        """Height and depth of char in dvi units."""
        metrics = self._metrics.get_metrics(char)
        if metrics is None:
            _log.debug('No metrics for char %d in font %s', char, self.texname)
            return [0, 0]
        hd = [
            _mul1220(metrics.tex_height, self._scale),
            _mul1220(metrics.tex_depth, self._scale),
        ]
        # cmsyXX (symbols font) glyph 0 ("minus") has a nonzero descent
        # so that TeX aligns equations properly
        # (https://tex.stackexchange.com/q/526103/)
        # but we actually care about the rasterization depth to align
        # the dvipng-generated images.
        if re.match(br'^cmsy\d+$', self.texname) and char == 0:
            hd[-1] = 0
        return hd

    def resolve_path(self):
        if self._path is None:
            fontmap = PsfontsMap(find_tex_file("pdftex.map"))
            try:
                psfont = fontmap[self.texname]
            except LookupError as exc:
                try:
                    find_tex_file(f"{self.texname.decode('ascii')}.mf")
                except FileNotFoundError:
                    raise exc from None
                else:
                    self._path = Path(find_tex_file(
                        f"{self.texname.decode('ascii')}.600pk"))
            else:
                if psfont.filename is None:
                    raise ValueError("No usable font file found for {} ({}); "
                                     "the font may lack a Type-1 version"
                                     .format(psfont.psname.decode("ascii"),
                                             psfont.texname.decode("ascii")))
                self._path = Path(psfont.filename)
        return self._path

    @cached_property
    def subfont(self):
        return 0

    @cached_property
    def effects(self):
        if self.resolve_path().match("*.600pk"):
            return {}
        return PsfontsMap(find_tex_file("pdftex.map"))[self.texname].effects