Skip to content

Commit 18c674a

Browse files
committed
Cheapen a bit GCR instantiation by sharing Python members.
1 parent 4b3a286 commit 18c674a

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

lib/mplcairo/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ def option_image_nocomposite(self):
112112

113113
# Based on the backend_pdf implementation.
114114
def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!", mtext=None):
115-
texmanager = self.get_texmanager()
116115
fontsize = prop.get_size_in_points()
117-
dvifile = texmanager.make_dvi(s, fontsize)
116+
dvifile = self.get_texmanager().make_dvi(s, fontsize)
118117
with dviread.Dvi(dvifile, self.dpi) as dvi:
119118
page = next(iter(dvi))
120119
mb = _mplcairo.MathtextBackendCairo()

src/_mplcairo.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace mplcairo {
1717

1818
using namespace pybind11::literals;
1919

20+
py::object GraphicsContextRenderer::mathtext_parser_{};
21+
2022
Region::Region(cairo_rectangle_int_t bbox, std::unique_ptr<uint8_t[]> buf) :
2123
bbox{bbox}, buf{std::move(buf)}
2224
{}
@@ -108,12 +110,7 @@ GraphicsContextRenderer::additional_context()
108110
GraphicsContextRenderer::GraphicsContextRenderer(
109111
cairo_t* cr, double width, double height, double dpi) :
110112
// This does *not* incref the cairo_t, but the destructor *will* decref it.
111-
cr_{cr},
112-
mathtext_parser_{
113-
py::module::import("matplotlib.mathtext")
114-
.attr("MathTextParser")("mplcairo")},
115-
texmanager_{py::none()},
116-
text2path_{py::module::import("matplotlib.textpath").attr("TextToPath")()}
113+
cr_{cr}
117114
{
118115
if (auto const& status = cairo_status(cr);
119116
status == CAIRO_STATUS_INVALID_SIZE) {
@@ -1474,18 +1471,29 @@ PYBIND11_MODULE(_mplcairo, m)
14741471
#undef LOAD_PTR
14751472
#endif
14761473

1474+
FT_CHECK(FT_Init_FreeType, &detail::ft_library);
1475+
14771476
detail::UNIT_CIRCLE =
14781477
py::module::import("matplotlib.path").attr("Path").attr("unit_circle")();
14791478
detail::PIXEL_MARKER =
14801479
py::module::import("matplotlib.markers").attr("MarkerStyle")(",");
1480+
GraphicsContextRenderer::mathtext_parser_ =
1481+
py::module::import("matplotlib.mathtext")
1482+
.attr("MathTextParser")("mplcairo");
14811483

1482-
FT_CHECK(FT_Init_FreeType, &detail::ft_library);
1483-
auto ft_cleanup = py::cpp_function{
1484-
[&](py::handle /* weakref */) -> void {
1484+
auto cleanup = py::cpp_function{
1485+
[&](py::handle weakref) -> void {
14851486
FT_Done_FreeType(detail::ft_library);
1487+
1488+
// Make sure that these objects don't outlive the Python interpreter.
1489+
detail::UNIT_CIRCLE = {};
1490+
detail::PIXEL_MARKER = {};
1491+
GraphicsContextRenderer::mathtext_parser_ = {};
1492+
1493+
weakref.dec_ref();
14861494
}
14871495
};
1488-
py::weakref(m, ft_cleanup).release();
1496+
py::weakref(m, cleanup).release();
14891497

14901498
// Export symbols.
14911499

@@ -1647,9 +1655,19 @@ PYBIND11_MODULE(_mplcairo, m)
16471655
[](GraphicsContextRenderer& gcr) -> double {
16481656
return gcr.get_additional_state().dpi;
16491657
})
1650-
// Needed for usetex and patheffects.
1651-
.def_readwrite("_texmanager", &GraphicsContextRenderer::texmanager_)
1652-
.def_readonly("_text2path", &GraphicsContextRenderer::text2path_)
1658+
// Needed for usetex and patheffects. These actually return constants.
1659+
.def_property_readonly(
1660+
"_texmanager", // No need to instantiate *another* texmanager.
1661+
[](GraphicsContextRenderer& /* gcr */) -> py::object {
1662+
return
1663+
py::module::import("matplotlib.textpath")
1664+
.attr("text_to_path").attr("get_texmanager")();
1665+
})
1666+
.def_property_readonly(
1667+
"_text2path",
1668+
[](GraphicsContextRenderer& /* gcr */) -> py::object {
1669+
return py::module::import("matplotlib.textpath").attr("text_to_path");
1670+
})
16531671

16541672
.def(
16551673
"get_canvas_width_height",

src/_mplcairo.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ class GraphicsContextRenderer {
3535
AdditionalContext additional_context();
3636

3737
public:
38+
static py::object mathtext_parser_;
39+
3840
cairo_t* const cr_;
39-
py::object mathtext_parser_;
40-
py::object texmanager_;
41-
py::object text2path_;
4241

4342
GraphicsContextRenderer(
4443
cairo_t* cr, double width, double height, double dpi);

0 commit comments

Comments
 (0)