Skip to content

Commit 0ef09b2

Browse files
committed
Partial fix for Matplotlib 3.9's test suite.
1 parent 4a940ee commit 0ef09b2

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

Diff for: ext/_mplcairo.cpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,14 @@ void GraphicsContextRenderer::_set_size(
618618
}
619619
}
620620

621+
void GraphicsContextRenderer::_set_init_translation(double x, double y)
622+
{
623+
auto mtx = cairo_matrix_t{1, 0, 0, 1, x, y};
624+
cairo_set_matrix(cr_, &mtx);
625+
CAIRO_CHECK_SET_USER_DATA_NEW(
626+
cairo_set_user_data, cr_, &detail::INIT_MATRIX_KEY, mtx);
627+
}
628+
621629
void GraphicsContextRenderer::_show_page()
622630
{
623631
cairo_show_page(cr_);
@@ -1588,10 +1596,12 @@ void GraphicsContextRenderer::draw_text(
15881596
next_glyphs_pos = glyphs_pos + cluster.num_glyphs;
15891597
for (auto j = glyphs_pos; j < next_glyphs_pos; ++j) {
15901598
if (!gac.glyphs[j].index) {
1591-
auto missing =
1592-
py::cast(s.substr(bytes_pos, cluster.num_bytes))
1593-
.attr("encode")("ascii", "namereplace");
1594-
warn_on_missing_glyph(missing.cast<std::string>());
1599+
auto missing = py::cast(s.substr(bytes_pos, cluster.num_bytes));
1600+
warn_on_missing_glyph( // Format forced by test_mathtext_ticks.
1601+
"{} ({})"_format(
1602+
py::module::import("builtins").attr("ord")(missing),
1603+
missing.attr("encode")("ascii", "namereplace").attr("decode")())
1604+
.cast<std::string>());
15951605
}
15961606
}
15971607
bytes_pos = next_bytes_pos;
@@ -2158,6 +2168,7 @@ Only intended for debugging purposes.
21582168
.def("_set_path", &GraphicsContextRenderer::_set_path)
21592169
.def("_set_metadata", &GraphicsContextRenderer::_set_metadata)
21602170
.def("_set_size", &GraphicsContextRenderer::_set_size)
2171+
.def("_set_init_translation", &GraphicsContextRenderer::_set_init_translation)
21612172
.def("_show_page", &GraphicsContextRenderer::_show_page)
21622173
.def("_get_context", &GraphicsContextRenderer::_get_context)
21632174
.def("_get_buffer", &GraphicsContextRenderer::_get_buffer)

Diff for: ext/_mplcairo.h

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class GraphicsContextRenderer {
7777
void _set_path(std::optional<std::string> path);
7878
void _set_metadata(std::optional<py::dict> metadata);
7979
void _set_size(double width, double height, double dpi);
80+
void _set_init_translation(double x, double y);
8081
void _show_page();
8182
py::object _get_context();
8283
py::array _get_buffer();

Diff for: ext/_util.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ void warn_on_missing_glyph(std::string s)
903903
{
904904
PY_CHECK(
905905
PyErr_WarnEx,
906-
nullptr,
907-
"Requested glyph ({}) missing from current font."_format(s)
906+
PyExc_UserWarning,
907+
"Glyph {} missing from current font."_format(s)
908908
.cast<std::string>().c_str(),
909909
1);
910910
}

Diff for: src/mplcairo/base.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def restore_region(self, region):
264264

265265
def _print_vector(self, renderer_factory,
266266
path_or_stream, *, metadata=None,
267-
_fixed_72dpi=True,
267+
_fixed_72dpi=True, _forced_size=None, _offset=None,
268268
**kwargs):
269269
_check_print_extra_kwargs(**kwargs)
270270
dpi = self.figure.get_dpi()
@@ -273,6 +273,10 @@ def _print_vector(self, renderer_factory,
273273
draw_raises_done = False
274274
with cbook.open_file_cm(path_or_stream, "wb") as stream:
275275
renderer = renderer_factory(stream, *self.figure.bbox.size, dpi)
276+
if _forced_size is not None:
277+
renderer._set_size(*_forced_size, dpi)
278+
if _offset is not None:
279+
renderer._set_init_translation(*_offset)
276280
try:
277281
# Setting invalid metadata can also throw, in which case the
278282
# rendered needs to be _finish()ed (to avoid later writing to a
@@ -332,21 +336,33 @@ def _print_ps_impl(self, is_eps, path_or_stream, *,
332336
f"%%Orientation: {orientation}"]
333337
if "Title" in metadata:
334338
dsc_comments.append("%%Title: {}".format(metadata.pop("Title")))
339+
fig_wh = self.figure.get_size_inches() * np.array(72)
335340
if not is_eps and papertype != "figure":
336341
dsc_comments.append(f"%%DocumentPaperSizes: {papertype}")
337-
print_method = partial(self._print_vector,
338-
GraphicsContextRendererCairo._for_eps_output
339-
if is_eps else
340-
GraphicsContextRendererCairo._for_ps_output)
342+
paper_wh = backend_ps.papersize[papertype] * np.array(72)
343+
if orientation == "landscape":
344+
paper_wh = paper_wh[::-1]
345+
fig_wh = fig_wh[::-1]
346+
offset = (paper_wh - fig_wh) / 2 * [1, -1]
347+
# FIXME: We should set the init transform, including the rotation
348+
# for landscape orientation, instead of just the offset.
349+
else:
350+
paper_wh = offset = None
351+
print_method = partial(
352+
self._print_vector,
353+
GraphicsContextRendererCairo._for_eps_output if is_eps else
354+
GraphicsContextRendererCairo._for_ps_output,
355+
_forced_size=paper_wh, _offset=offset)
341356
if mpl.rcParams["ps.usedistiller"]:
342357
with TemporaryDirectory() as tmp_dirname:
343358
tmp_name = Path(tmp_dirname, "tmp")
344359
print_method(tmp_name, metadata=metadata, **kwargs)
345-
# Assume we can get away without passing the bbox.
346360
{"ghostscript": backend_ps.gs_distill,
347361
"xpdf": backend_ps.xpdf_distill}[
348362
mpl.rcParams["ps.usedistiller"]](
349-
str(tmp_name), is_eps, ptype=papertype)
363+
str(tmp_name), is_eps, ptype=papertype,
364+
# Assume we can get away with just bbox width/height.
365+
bbox=(None, None, *fig_wh))
350366
# If path_or_stream is *already* a text-mode stream then
351367
# tmp_name needs to be opened in text-mode too.
352368
with cbook.open_file_cm(path_or_stream, "wb") as stream, \

0 commit comments

Comments
 (0)