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

Code before:
        .def_property_readonly(
          "underline_thickness", [](PyFT2Font *self) {
            return self->x->get_face()->underline_thickness;
          }, "Thickness of the underline bar.")
        .def_property_readonly(
          "fname", &PyFT2Font_fname,
          "The original filename for this object.")

        .def_buffer([](PyFT2Font &self) -> py::buffer_info {
            return self.x->get_image().request();
        })

        // TODO: Return a nicer structure than dicts.
        // NOTE: The lifetime of the buffers is limited and could get invalidated...
        // TODO: Real antialiasing flag.
        // TODO: throw_ft_error.
        // x, y are upwards here
        .def("_render_glyph", [](PyFT2Font *self, FT_UInt idx,
                                 double x, double y, double angle,
                                 LoadFlags flags) {
            auto face = self->x->get_face();
            auto hf = self->x->get_hinting_factor();
            auto c = std::cos(angle * M_PI / 180) * 0x10000L,
                 s = std::sin(angle * M_PI / 180) * 0x10000L;
            auto matrix = FT_Matrix{
            std::lround(c / hf), std::lround(-s), std::lround(s / hf), std::lround(c)};
            auto delta = FT_Vector{std::lround(x * 64), std::lround(y * 64)};
            FT_Set_Transform(face, &matrix, &delta);
            FT_CHECK(FT_Load_Glyph, face, idx, static_cast<FT_Int32>(flags));
            FT_CHECK(FT_Render_Glyph, face->glyph, FT_RENDER_MODE_NORMAL);
            py::dict d;
            d["left"] = face->glyph->bitmap_left;
            d["top"] = face->glyph->bitmap_top;
            d["buffer"] = py::array_t<uint8_t>{
                {face->glyph->bitmap.rows, face->glyph->bitmap.width},
                {face->glyph->bitmap.pitch, 1},
                face->glyph->bitmap.buffer};
            return d;
        })
        .def("_render_glyphs", [](PyFT2Font *self, double x, double y) {
            auto origin = FT_Vector{std::lround(x * 64), std::lround(y * 64)};
            py::list gs;
            for (auto &g: self->x->glyphs) {
                FT_CHECK(FT_Glyph_To_Bitmap, &g, FT_RENDER_MODE_NORMAL, &origin, 1);
                auto bg = reinterpret_cast<FT_BitmapGlyph>(g);
                py::dict d;
                d["left"] = bg->left;
                d["top"] = bg->top;
                d["buffer"] = py::array_t<uint8_t>{
                    {bg->bitmap.rows, bg->bitmap.width},
                    {bg->bitmap.pitch, 1},

Review comment (suggestion):
```suggestion
            auto matrix = FT_Matrix{
                std::lround(c / hf), std::lround(-s), std::lround(s / hf), std::lround(c)};
```
or maybe, since it's 2D:
```suggestion
            auto matrix = FT_Matrix{
                std::lround(c / hf), std::lround(-s),
                std::lround(s / hf), std::lround(c)};
```

Code after:
        .def("get_bitmap_offset", &PyFT2Font::get_bitmap_offset,
             PyFT2Font_get_bitmap_offset__doc__)
        .def("get_descent", &PyFT2Font::get_descent, PyFT2Font_get_descent__doc__)
        .def("draw_glyphs_to_bitmap", &PyFT2Font::draw_glyphs_to_bitmap,
             py::kw_only(), "antialiased"_a=true,
             PyFT2Font_draw_glyphs_to_bitmap__doc__);
        // The generated docstring uses an unqualified "Buffer" as type hint,
        // which causes an error in sphinx.  This is fixed as of pybind11
        // master (since #5566) which now uses "collections.abc.Buffer";
        // restore the signature once that version is released.
        {
            py::options options{};
            options.disable_function_signatures();
            cls
            .def("draw_glyph_to_bitmap", &PyFT2Font_draw_glyph_to_bitmap,
                "image"_a, "x"_a, "y"_a, "glyph"_a, py::kw_only(), "antialiased"_a=true,
                PyFT2Font_draw_glyph_to_bitmap__doc__);
        }
        cls
        .def("get_glyph_name", &PyFT2Font::get_glyph_name, "index"_a,
             PyFT2Font_get_glyph_name__doc__)
        .def("get_charmap", &PyFT2Font_get_charmap, PyFT2Font_get_charmap__doc__)
        .def("get_char_index", &PyFT2Font::get_char_index,
             "codepoint"_a, py::kw_only(), "_fallback"_a=true,
             PyFT2Font_get_char_index__doc__)
        .def("get_sfnt", &PyFT2Font_get_sfnt, PyFT2Font_get_sfnt__doc__)
        .def("get_name_index", &PyFT2Font::get_name_index, "name"_a,
             PyFT2Font_get_name_index__doc__)
        .def("get_ps_font_info", &PyFT2Font_get_ps_font_info,
             PyFT2Font_get_ps_font_info__doc__)
        .def("get_sfnt_table", &PyFT2Font_get_sfnt_table, "name"_a,
             PyFT2Font_get_sfnt_table__doc__)
        .def("get_path", &PyFT2Font_get_path, PyFT2Font_get_path__doc__)
        .def("get_image", &PyFT2Font::get_image, PyFT2Font_get_image__doc__)
        .def("_get_type1_encoding_vector", &PyFT2Font__get_type1_encoding_vector,
             PyFT2Font__get_type1_encoding_vector__doc__)

        .def_property_readonly(
          "postscript_name", [](PyFT2Font *self) {
            if (const char *name = FT_Get_Postscript_Name(self->get_face())) {
              return name;
            } else {
              return "UNAVAILABLE";
            }
          }, "PostScript name of the font.")
        .def_property_readonly(
          "num_faces", [](PyFT2Font *self) {
            return self->get_face()->num_faces & 0xffff;
          }, "Number of faces in file.")
        .def_property_readonly(
          "face_index", [](PyFT2Font *self) {