Skip to content

Commit

Permalink
Partial fix for Matplotlib 3.8's test suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Dec 24, 2023
1 parent c0c8c9e commit f85bae3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
10 changes: 3 additions & 7 deletions ext/_mplcairo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ GraphicsContextRenderer::AdditionalContext::AdditionalContext(
}, state.antialias);
// Clip, if needed. Cannot be done earlier as we need to be able to unclip.
if (auto const& rectangle = state.clip_rectangle) {
auto const& [x, y, w, h] = *rectangle;
auto const& [x, y, w, h] = rectangle->attr("bounds").cast<rectangle_t>();
cairo_save(cr);
restore_init_matrix(cr);
cairo_new_path(cr);
Expand Down Expand Up @@ -681,11 +681,7 @@ void GraphicsContextRenderer::set_capstyle(std::string capstyle)
void GraphicsContextRenderer::set_clip_rectangle(
std::optional<py::object> rectangle)
{
get_additional_state().clip_rectangle =
rectangle
// A TransformedBbox or a tuple.
? py::getattr(*rectangle, "bounds", *rectangle).cast<rectangle_t>()
: std::optional<rectangle_t>{};
get_additional_state().clip_rectangle = rectangle;
}

void GraphicsContextRenderer::set_clip_path(
Expand Down Expand Up @@ -2205,7 +2201,7 @@ Only intended for debugging purposes.

.def(
"get_clip_rectangle",
[](GraphicsContextRenderer& gcr) -> std::optional<rectangle_t> {
[](GraphicsContextRenderer& gcr) -> std::optional<py::object> {
return gcr.get_additional_state().clip_rectangle;
})
.def(
Expand Down
2 changes: 1 addition & 1 deletion ext/_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct AdditionalState {
double width, height, dpi;
std::optional<double> alpha;
std::variant<cairo_antialias_t, bool> antialias;
std::optional<rectangle_t> clip_rectangle;
std::optional<py::object> clip_rectangle;
std::tuple<std::optional<py::object>, std::shared_ptr<cairo_path_t>>
clip_path;
std::optional<std::string> hatch;
Expand Down
20 changes: 15 additions & 5 deletions src/mplcairo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ def _check_print_extra_kwargs(*,
pass


def _check_no_metadata(metadata):
if metadata is not None:
raise ValueError( # Start of error string is forced by test.
"metadata not supported for the requested output format")


class FigureCanvasCairo(FigureCanvasBase):
# Although this attribute should semantically be set from __init__ (it is
# purely an instance attribute), initializing it at the class level helps
Expand Down Expand Up @@ -314,7 +320,7 @@ def _print_ps_impl(self, is_eps, path_or_stream, *,
f"%%Orientation: {orientation}"]
if "Title" in metadata:
dsc_comments.append("%%Title: {}".format(metadata.pop("Title")))
if not is_eps:
if not is_eps and papertype != "figure":
dsc_comments.append(f"%%DocumentPaperSizes: {papertype}")
print_method = partial(self._print_vector,
GraphicsContextRendererCairo._for_eps_output
Expand Down Expand Up @@ -351,6 +357,7 @@ def _get_fresh_straight_rgba8888(self):
def print_rgba(self, path_or_stream, *,
dryrun=False, metadata=None, **kwargs):
_check_print_extra_kwargs(**kwargs)
_check_no_metadata(metadata)
img = self._get_fresh_straight_rgba8888()
if dryrun:
return
Expand Down Expand Up @@ -382,7 +389,9 @@ def print_png(self, path_or_stream, *,
**(pil_kwargs if pil_kwargs is not None else {})})

def print_jpeg(self, path_or_stream, *,
dryrun=False, pil_kwargs=None, **kwargs):
dryrun=False, metadata=None, pil_kwargs=None, **kwargs):
_check_print_extra_kwargs(**kwargs)
_check_no_metadata(metadata)
# Remove transparency by alpha-blending on an assumed white background.
r, g, b, a = mpl.colors.to_rgba(self.figure.get_facecolor())
try:
Expand All @@ -392,16 +401,16 @@ def print_jpeg(self, path_or_stream, *,
self.figure.set_facecolor((r, g, b, a))
if dryrun:
return
_check_print_extra_kwargs(**kwargs)
Image.fromarray(img).save(path_or_stream, format="jpeg", **{
"dpi": (self.figure.dpi, self.figure.dpi),
**(pil_kwargs if pil_kwargs is not None else {})})

print_jpg = print_jpeg

def print_tiff(self, path_or_stream, *,
dryrun=False, pil_kwargs=None, **kwargs):
dryrun=False, metadata=None, pil_kwargs=None, **kwargs):
_check_print_extra_kwargs(**kwargs)
_check_no_metadata(metadata)
img = self._get_fresh_straight_rgba8888()
if dryrun:
return
Expand All @@ -412,8 +421,9 @@ def print_tiff(self, path_or_stream, *,
print_tif = print_tiff

def print_webp(self, path_or_stream, *,
dryrun=False, pil_kwargs=None, **kwargs):
dryrun=False, metadata=None, pil_kwargs=None, **kwargs):
_check_print_extra_kwargs(**kwargs)
_check_no_metadata(metadata)
img = self._get_fresh_straight_rgba8888()
if dryrun:
return
Expand Down

0 comments on commit f85bae3

Please sign in to comment.