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

Code before:
            The corresponding glyph index to record.

        Returns
        -------
        subset : int
            The subset in which the returned character code resides. If *subset_size*
            was not specified on this instance, then this is always 0.
        subset_charcode : CharacterCodeType
            The character code within the above subset. If *subset_size* was not
            specified on this instance, then this is just *charcode* unmodified.
        """
        # Normalize for the key.
        if isinstance(chars, str):
            charcode = tuple(ord(c) for c in chars)
        elif not isinstance(chars, tuple):
            charcode = (chars, )
        else:
            charcode = chars

        glyph_map = self.glyph_map.setdefault(font.fname, {})
        key = (charcode, glyph)
        if key in glyph_map:
            return glyph_map[key]

        subset_maps = self.used.setdefault(font.fname, [{}])
        use_next_charmap = False
        if len(charcode) > 1:
            # Multi-character glyphs always go in the non-0 subset.
            use_next_charmap = True
        else:
            # Default to preserving the character code as it was.
            subset = 0
            subset_charcode = charcode[0]
            if self.subset_size != 0:
                # But start filling a new subset if outside the first block; this
                # preserves ASCII (for Type 3) or the Basic Multilingual Plane
                # (for Type 42).
                if charcode[0] >= self.subset_size:
                    use_next_charmap = True
                # Or, use a new subset if the character code is already mapped for the
                # first block. This means it's using an alternate glyph.
                elif charcode[0] in subset_maps[0]:
                    use_next_charmap = True
        if use_next_charmap:
            if len(subset_maps) == 1 or len(subset_maps[-1]) == self.subset_size:
                subset_maps.append({})
            subset = len(subset_maps) - 1
            subset_charcode = len(subset_maps[-1])
        subset_maps[subset][subset_charcode] = glyph
        glyph_map[key] = (subset, subset_charcode)
        return (subset, subset_charcode)

Review comment (suggestion):
I would write this as
```python
use_next_charmap = (
    # Multi-character glyphs always go...
    len(charcodes) > 1
    or (self.subset_size != 0
        # Start filling a new subset...
        and (charcodes[0] >= self.subset_size or charcodes[0] in subset_maps[0]))
)
if use_next_charmap:
    ...
else:
    ...
```
Otherwise it's a bit harder to follow that you first set subset and subset_charcodes, but may later override them if use_next_charmap = True.

Code after:
        `.subset_to_unicode` method instead of this attribute.
    """

    def __init__(self, subset_size: int = 0):
        """
        Parameters
        ----------
        subset_size : int, optional
            The maximum size that is supported for an embedded font. If provided, then
            characters will be grouped into these sized subsets.
        """
        self.used: dict[str, list[dict[CharacterCodeType, GlyphIndexType]]] = {}
        self.glyph_maps: dict[str, GlyphMap] = {}
        self.subset_size = subset_size

    def track(self, font: FT2Font, s: str) -> list[tuple[int, CharacterCodeType]]:
        """
        Record that string *s* is being typeset using font *font*.

        Parameters
        ----------
        font : FT2Font
            A font that is being used for the provided string.
        s : str
            The string that should be marked as tracked by the provided font.

        Returns
        -------
        list[tuple[int, CharacterCodeType]]
            A list of subset and character code pairs corresponding to the input string.
            If a *subset_size* is specified on this instance, then the character code
            will correspond with the given subset (and not necessarily the string as a
            whole). If *subset_size* is not specified, then the subset will always be 0
            and the character codes will be returned from the string unchanged.
        """
        return [
            self.track_glyph(f, ord(c), f.get_char_index(ord(c)))
            for c, f in font._get_fontmap(s).items()
        ]

    def track_glyph(self, font: FT2Font, chars: str | CharacterCodeType,
                    glyph: GlyphIndexType) -> tuple[int, CharacterCodeType]:
        """
        Record character code *charcode* at glyph index *glyph* as using font *font*.

        Parameters
        ----------
        font : FT2Font
            A font that is being used for the provided string.
        chars : str or CharacterCodeType
            The character(s) to record. This may be a single character code, or multiple