Skip to content

Commit b0c2116

Browse files
committed
Lazy-load hatch params, again for GCR instantiation speed.
1 parent 18c674a commit b0c2116

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/_mplcairo.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ GraphicsContextRenderer::GraphicsContextRenderer(
140140
/* clip_rectangle */ {},
141141
/* clip_path */ {{}, {nullptr, cairo_path_destroy}},
142142
/* hatch */ {},
143-
/* hatch_color */ to_rgba(rc_param("hatch.color")),
144-
/* hatch_linewidth */ rc_param("hatch.linewidth").cast<double>(),
143+
/* hatch_color */ {}, // Lazily loaded by get_hatch_color.
144+
/* hatch_linewidth */ {}, // Lazily loaded by get_hatch_linewidth.
145145
/* sketch */ {},
146146
/* snap */ true, // Defaults to None, i.e. True for us.
147147
/* url */ {}
@@ -912,10 +912,10 @@ void GraphicsContextRenderer::draw_path(
912912
auto hatch_gcr = GraphicsContextRenderer{
913913
hatch_cr, double(dpi), double(dpi), double(dpi)};
914914
hatch_gcr.get_additional_state().snap = false;
915-
hatch_gcr.set_linewidth(get_additional_state().hatch_linewidth);
915+
hatch_gcr.set_linewidth(get_additional_state().get_hatch_linewidth());
916916
auto const& matrix =
917917
cairo_matrix_t{double(dpi), 0, 0, -double(dpi), 0, double(dpi)};
918-
auto const& hatch_color = get_additional_state().hatch_color;
918+
auto const& hatch_color = get_additional_state().get_hatch_color();
919919
fill_and_stroke_exact(
920920
hatch_cr, *hatch_path, &matrix, hatch_color, hatch_color);
921921
auto const& hatch_pattern =
@@ -1620,12 +1620,12 @@ PYBIND11_MODULE(_mplcairo, m)
16201620
.def(
16211621
"get_hatch_color",
16221622
[](GraphicsContextRenderer& gcr) -> rgba_t {
1623-
return gcr.get_additional_state().hatch_color;
1623+
return gcr.get_additional_state().get_hatch_color();
16241624
})
16251625
.def(
16261626
"get_hatch_linewidth",
16271627
[](GraphicsContextRenderer& gcr) -> double {
1628-
return gcr.get_additional_state().hatch_linewidth;
1628+
return gcr.get_additional_state().get_hatch_linewidth();
16291629
})
16301630
// Not strictly needed now.
16311631
.def("get_linewidth", &GraphicsContextRenderer::get_linewidth)

src/_pattern_cache.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ void PatternCache::mask(
237237
auto const& raster_surface =
238238
cairo_image_surface_create(CAIRO_FORMAT_A8, width, height);
239239
auto const& raster_gcr =
240-
// FIXME: Looks like building a full new gcr is too costly :/ (involves
241-
// Python to build the additional state).
242240
GraphicsContextRenderer::make_pattern_gcr(raster_surface);
243241
key.draw(
244242
raster_gcr.cr_,

src/_util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ cairo_user_data_key_t const REFS_KEY{}, STATE_KEY{}, FT_KEY{};
3939
py::object UNIT_CIRCLE{}, PIXEL_MARKER{};
4040
}
4141

42+
rgba_t AdditionalState::get_hatch_color() {
43+
if (!hatch_color) {
44+
hatch_color = to_rgba(rc_param("hatch.color"));
45+
}
46+
return *hatch_color;
47+
}
48+
49+
double AdditionalState::get_hatch_linewidth() {
50+
if (!hatch_linewidth) {
51+
hatch_linewidth = rc_param("hatch.linewidth").cast<double>();
52+
}
53+
return *hatch_linewidth;
54+
}
55+
4256
bool py_eq(py::object obj1, py::object obj2) {
4357
return py::module::import("operator").attr("eq")(obj1, obj2).cast<bool>();
4458
}

src/_util.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,17 @@ struct AdditionalState {
8787
std::tuple<std::optional<py::object>, std::shared_ptr<cairo_path_t>>
8888
clip_path;
8989
std::optional<std::string> hatch;
90-
rgba_t hatch_color;
91-
double hatch_linewidth;
90+
// hatch_color and linewidth are made optional only to be able to lazy-load
91+
// them, which is needed to keep GCR instantiation fast... which is needed
92+
// for the pattern cache.
93+
std::optional<rgba_t> hatch_color;
94+
std::optional<double> hatch_linewidth;
9295
std::optional<py::object> sketch;
9396
bool snap;
9497
std::optional<std::string> url;
98+
99+
rgba_t get_hatch_color();
100+
double get_hatch_linewidth();
95101
};
96102

97103
bool py_eq(py::object obj1, py::object obj2);

0 commit comments

Comments
 (0)