Repository: matplotlib/matplotlib
File: src/ft2font_wrapper.cpp (C++)

Code before:
    py::ssize_t vertices_dims[2] = { length, 2 };
    py::array_t<double> vertices_arr(vertices_dims);
    if (length > 0) {
        memcpy(vertices_arr.mutable_data(), vertices.data(), vertices_arr.nbytes());
    }
    py::ssize_t codes_dims[1] = { length };
    py::array_t<unsigned char> codes_arr(codes_dims);
    if (length > 0) {
        memcpy(codes_arr.mutable_data(), codes.data(), codes_arr.nbytes());
    }

    return py::make_tuple(vertices_arr, codes_arr);
}

const char *PyFT2Font_get_image__doc__ = R"""(
    Return the underlying image buffer for this font object.

    Returns
    -------
    np.ndarray[int]

    See Also
    --------
    .get_path
)""";

const char *PyFT2Font__get_type1_encoding_vector__doc__ = R"""(
    Return a list mapping CharString indices of a Type 1 font to FreeType glyph indices.

    Returns
    -------
    list[int]
)""";

static std::array<FT_UInt, 256>
PyFT2Font__get_type1_encoding_vector(PyFT2Font *self)
{
    auto face = self->get_face();
    auto indices = std::array<FT_UInt, 256>{};
    for (auto i = 0u; i < indices.size(); ++i) {
        auto len = FT_Get_PS_Font_Value(face, PS_DICT_ENCODING_ENTRY, i, nullptr, 0);
        if (len == -1) {
            // Explicitly ignore missing entries (mapped to glyph 0 = .notdef).
            continue;
        }
        auto buf = std::make_unique<char[]>(len);
        FT_Get_PS_Font_Value(face, PS_DICT_ENCODING_ENTRY, i, buf.get(), len);
        indices[i] = FT_Get_Name_Index(face, buf.get());
    }
    return indices;
}

Review comment (suggestion):
In the long term it would be nice if mplcairo could directly call this (instead of having to reimplement the whole thing itself as well); but I suspect the issue is the fact that this has to expose FT2Font objects and mplcairo would thus need to also use the same ft2Library instance as matplotlib, which... well I don't really know if that's something matplotlib wants to expose publically.  (I'm mostly just thinking aloud here.)
(mplcairo could also just grab the filename of the FT2Font objects and reopen them, which is not too bad with caching, but I'm not sure this would work with variable fonts at least?)

Code after:
    }
    return indices;
}

/**********************************************************************
 * Layout items
 * */

struct LayoutItem {
    PyFT2Font *ft_object;
    std::u32string character;
    int glyph_index;
    double x;
    double y;
    double prev_kern;

    LayoutItem(PyFT2Font *f, std::u32string c, int i, double x, double y, double k) :
        ft_object(f), character(c), glyph_index(i), x(x), y(y), prev_kern(k) {}
};

const char *PyFT2Font_layout__doc__ = R"""(
    Layout a string and yield information about each used glyph.

    .. warning::
        This API uses the fallback list and is both private and provisional: do not use
        it directly.

    .. versionadded:: 3.11

    Parameters
    ----------
    text : str
        The characters for which to find fonts.
    flags : LoadFlags, default: `.LoadFlags.FORCE_AUTOHINT`
        Any bitwise-OR combination of the `.LoadFlags` flags.
    features : tuple[str, ...], optional
        The font feature tags to use for the font.

        Available font feature tags may be found at
        https://learn.microsoft.com/en-us/typography/opentype/spec/featurelist
    language : str, optional
        The language of the text in a format accepted by libraqm, namely `a BCP47
        language code <https://www.w3.org/International/articles/language-tags/>`_.

    Returns
    -------
    list[LayoutItem]
)""";

static auto
PyFT2Font_layout(PyFT2Font *self, std::u32string text, LoadFlags flags,