Skip to content

Commit f85bae3

Browse files
committed
Partial fix for Matplotlib 3.8's test suite.
1 parent c0c8c9e commit f85bae3

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

Diff for: ext/_mplcairo.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ GraphicsContextRenderer::AdditionalContext::AdditionalContext(
180180
}, state.antialias);
181181
// Clip, if needed. Cannot be done earlier as we need to be able to unclip.
182182
if (auto const& rectangle = state.clip_rectangle) {
183-
auto const& [x, y, w, h] = *rectangle;
183+
auto const& [x, y, w, h] = rectangle->attr("bounds").cast<rectangle_t>();
184184
cairo_save(cr);
185185
restore_init_matrix(cr);
186186
cairo_new_path(cr);
@@ -681,11 +681,7 @@ void GraphicsContextRenderer::set_capstyle(std::string capstyle)
681681
void GraphicsContextRenderer::set_clip_rectangle(
682682
std::optional<py::object> rectangle)
683683
{
684-
get_additional_state().clip_rectangle =
685-
rectangle
686-
// A TransformedBbox or a tuple.
687-
? py::getattr(*rectangle, "bounds", *rectangle).cast<rectangle_t>()
688-
: std::optional<rectangle_t>{};
684+
get_additional_state().clip_rectangle = rectangle;
689685
}
690686

691687
void GraphicsContextRenderer::set_clip_path(
@@ -2205,7 +2201,7 @@ Only intended for debugging purposes.
22052201

22062202
.def(
22072203
"get_clip_rectangle",
2208-
[](GraphicsContextRenderer& gcr) -> std::optional<rectangle_t> {
2204+
[](GraphicsContextRenderer& gcr) -> std::optional<py::object> {
22092205
return gcr.get_additional_state().clip_rectangle;
22102206
})
22112207
.def(

Diff for: ext/_util.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ struct AdditionalState {
131131
double width, height, dpi;
132132
std::optional<double> alpha;
133133
std::variant<cairo_antialias_t, bool> antialias;
134-
std::optional<rectangle_t> clip_rectangle;
134+
std::optional<py::object> clip_rectangle;
135135
std::tuple<std::optional<py::object>, std::shared_ptr<cairo_path_t>>
136136
clip_path;
137137
std::optional<std::string> hatch;

Diff for: src/mplcairo/base.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ def _check_print_extra_kwargs(*,
195195
pass
196196

197197

198+
def _check_no_metadata(metadata):
199+
if metadata is not None:
200+
raise ValueError( # Start of error string is forced by test.
201+
"metadata not supported for the requested output format")
202+
203+
198204
class FigureCanvasCairo(FigureCanvasBase):
199205
# Although this attribute should semantically be set from __init__ (it is
200206
# purely an instance attribute), initializing it at the class level helps
@@ -314,7 +320,7 @@ def _print_ps_impl(self, is_eps, path_or_stream, *,
314320
f"%%Orientation: {orientation}"]
315321
if "Title" in metadata:
316322
dsc_comments.append("%%Title: {}".format(metadata.pop("Title")))
317-
if not is_eps:
323+
if not is_eps and papertype != "figure":
318324
dsc_comments.append(f"%%DocumentPaperSizes: {papertype}")
319325
print_method = partial(self._print_vector,
320326
GraphicsContextRendererCairo._for_eps_output
@@ -351,6 +357,7 @@ def _get_fresh_straight_rgba8888(self):
351357
def print_rgba(self, path_or_stream, *,
352358
dryrun=False, metadata=None, **kwargs):
353359
_check_print_extra_kwargs(**kwargs)
360+
_check_no_metadata(metadata)
354361
img = self._get_fresh_straight_rgba8888()
355362
if dryrun:
356363
return
@@ -382,7 +389,9 @@ def print_png(self, path_or_stream, *,
382389
**(pil_kwargs if pil_kwargs is not None else {})})
383390

384391
def print_jpeg(self, path_or_stream, *,
385-
dryrun=False, pil_kwargs=None, **kwargs):
392+
dryrun=False, metadata=None, pil_kwargs=None, **kwargs):
393+
_check_print_extra_kwargs(**kwargs)
394+
_check_no_metadata(metadata)
386395
# Remove transparency by alpha-blending on an assumed white background.
387396
r, g, b, a = mpl.colors.to_rgba(self.figure.get_facecolor())
388397
try:
@@ -392,16 +401,16 @@ def print_jpeg(self, path_or_stream, *,
392401
self.figure.set_facecolor((r, g, b, a))
393402
if dryrun:
394403
return
395-
_check_print_extra_kwargs(**kwargs)
396404
Image.fromarray(img).save(path_or_stream, format="jpeg", **{
397405
"dpi": (self.figure.dpi, self.figure.dpi),
398406
**(pil_kwargs if pil_kwargs is not None else {})})
399407

400408
print_jpg = print_jpeg
401409

402410
def print_tiff(self, path_or_stream, *,
403-
dryrun=False, pil_kwargs=None, **kwargs):
411+
dryrun=False, metadata=None, pil_kwargs=None, **kwargs):
404412
_check_print_extra_kwargs(**kwargs)
413+
_check_no_metadata(metadata)
405414
img = self._get_fresh_straight_rgba8888()
406415
if dryrun:
407416
return
@@ -412,8 +421,9 @@ def print_tiff(self, path_or_stream, *,
412421
print_tif = print_tiff
413422

414423
def print_webp(self, path_or_stream, *,
415-
dryrun=False, pil_kwargs=None, **kwargs):
424+
dryrun=False, metadata=None, pil_kwargs=None, **kwargs):
416425
_check_print_extra_kwargs(**kwargs)
426+
_check_no_metadata(metadata)
417427
img = self._get_fresh_straight_rgba8888()
418428
if dryrun:
419429
return

0 commit comments

Comments
 (0)