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

Code before:
        A file object with the font saved into it
    """
    fh = BytesIO()
    font.save(fh, reorderTables=False)
    return fh


class CharacterTracker:
    """
    Helper for font subsetting by the PDF and PS backends.

    Maintains a mapping of font paths to the set of glyphs that are being used from that
    font.
    """

    def __init__(self) -> None:
        self.used: dict[str, set[GlyphIndexType]] = {}

    def track(self, font: FT2Font, s: str) -> None:
        """Record that string *s* is being typeset using font *font*."""
        char_to_font = font._get_fontmap(s)
        for _c, _f in char_to_font.items():
            glyph_index = _f.get_char_index(ord(_c))
            self.used.setdefault(_f.fname, set()).add(glyph_index)

    def track_glyph(self, font: FT2Font, glyph: GlyphIndexType) -> None:
        """Record that glyph index *glyph* is being typeset using font *font*."""
        self.used.setdefault(font.fname, set()).add(glyph)


class RendererPDFPSBase(RendererBase):
    # The following attributes must be defined by the subclasses:
    # - _afm_font_dir
    # - _use_afm_rc_name

    def __init__(self, width, height):
        super().__init__()
        self.width = width
        self.height = height

    def flipy(self):
        # docstring inherited
        return False  # y increases from bottom to top.

    def option_scale_image(self):
        # docstring inherited
        return True  # PDF and PS support arbitrary image scaling.

    def option_image_nocomposite(self):
        # docstring inherited
        # Decide whether to composite image based on rcParam value.

Review comment (suggestion):
I would rename the last parameter to `glyph_idx` (or just plain `idx`, or `glyph_index`).

Code after:
        A file object with the font saved into it
    """
    fh = BytesIO()
    font.save(fh, reorderTables=False)
    return fh


class CharacterTracker:
    """
    Helper for font subsetting by the PDF and PS backends.

    Maintains a mapping of font paths to the set of glyphs that are being used from that
    font.
    """

    def __init__(self) -> None:
        self.used: dict[str, set[GlyphIndexType]] = {}

    def track(self, font: FT2Font, s: str) -> None:
        """Record that string *s* is being typeset using font *font*."""
        char_to_font = font._get_fontmap(s)
        for _c, _f in char_to_font.items():
            glyph_index = _f.get_char_index(ord(_c))
            self.used.setdefault(_f.fname, set()).add(glyph_index)

    def track_glyph(self, font: FT2Font, glyph_index: GlyphIndexType) -> None:
        """Record that glyph index *glyph_index* is being typeset using font *font*."""
        self.used.setdefault(font.fname, set()).add(glyph_index)


class RendererPDFPSBase(RendererBase):
    # The following attributes must be defined by the subclasses:
    # - _afm_font_dir
    # - _use_afm_rc_name

    def __init__(self, width, height):
        super().__init__()
        self.width = width
        self.height = height

    def flipy(self):
        # docstring inherited
        return False  # y increases from bottom to top.

    def option_scale_image(self):
        # docstring inherited
        return True  # PDF and PS support arbitrary image scaling.

    def option_image_nocomposite(self):
        # docstring inherited
        # Decide whether to composite image based on rcParam value.