@@ -26,6 +26,14 @@ P11X_DECLARE_ENUM(
2626 {" GOOD" , CAIRO_ANTIALIAS_GOOD},
2727 {" BEST" , CAIRO_ANTIALIAS_BEST}
2828)
29+ P11X_DECLARE_ENUM(
30+ " dither_t" , " enum.Enum" ,
31+ {" NONE" , mplcairo::detail::CAIRO_DITHER_NONE},
32+ {" DEFAULT" , mplcairo::detail::CAIRO_DITHER_DEFAULT},
33+ {" FAST" , mplcairo::detail::CAIRO_DITHER_FAST},
34+ {" GOOD" , mplcairo::detail::CAIRO_DITHER_GOOD},
35+ {" BEST" , mplcairo::detail::CAIRO_DITHER_BEST},
36+ )
2937P11X_DECLARE_ENUM(
3038 " operator_t" , " enum.Enum" ,
3139 {" CLEAR" , CAIRO_OPERATOR_CLEAR},
@@ -58,15 +66,17 @@ P11X_DECLARE_ENUM(
5866 {" HSL_COLOR" , CAIRO_OPERATOR_HSL_COLOR},
5967 {" HSL_LUMINOSITY" , CAIRO_OPERATOR_HSL_LUMINOSITY}
6068)
61- P11X_DECLARE_ENUM( // Only for error messages.
62- " _format_t " , " enum.Enum" ,
69+ P11X_DECLARE_ENUM(
70+ " format_t " , " enum.Enum" ,
6371 {" INVALID" , CAIRO_FORMAT_INVALID},
6472 {" ARGB32" , CAIRO_FORMAT_ARGB32},
6573 {" RGB24" , CAIRO_FORMAT_RGB24},
6674 {" A8" , CAIRO_FORMAT_A8},
6775 {" A1" , CAIRO_FORMAT_A1},
6876 {" RGB16_565" , CAIRO_FORMAT_RGB16_565},
69- {" RGB30" , CAIRO_FORMAT_RGB30}
77+ {" RGB30" , CAIRO_FORMAT_RGB30},
78+ {" RGB96F" , static_cast <cairo_format_t >(6 )},
79+ {" RGBA128F" , static_cast <cairo_format_t >(7 )}
7080)
7181P11X_DECLARE_ENUM( // Only for error messages.
7282 " _surface_type_t" , " enum.Enum" ,
@@ -263,7 +273,8 @@ GraphicsContextRenderer::GraphicsContextRenderer(
263273 /* hatch_linewidth */ {}, // Lazily loaded by get_hatch_linewidth.
264274 /* sketch */ {},
265275 /* snap */ true , // Defaults to None, i.e. True for us.
266- /* url */ {}
276+ /* url */ {},
277+ /* dither */ detail::CAIRO_DITHER_DEFAULT
267278 }}}));
268279}
269280
@@ -303,7 +314,7 @@ GraphicsContextRenderer::~GraphicsContextRenderer()
303314cairo_t * GraphicsContextRenderer::cr_from_image_args (int width, int height)
304315{
305316 auto const & surface =
306- cairo_image_surface_create (get_cairo_format () , width, height);
317+ cairo_image_surface_create (detail::IMAGE_FORMAT , width, height);
307318 auto const & cr = cairo_create (surface);
308319 cairo_surface_destroy (surface);
309320 return cr;
@@ -953,6 +964,9 @@ void GraphicsContextRenderer::draw_image(
953964 auto const & mtx =
954965 cairo_matrix_t {1 , 0 , 0 , -1 , -x, -y + height_};
955966 cairo_pattern_set_matrix (pattern, &mtx);
967+ if (detail::cairo_pattern_set_dither) {
968+ detail::cairo_pattern_set_dither (pattern, get_additional_state ().dither );
969+ }
956970 cairo_set_source (cr_, pattern);
957971 cairo_pattern_destroy (pattern);
958972 cairo_paint (cr_);
@@ -1049,7 +1063,7 @@ void maybe_multithread(
10491063 for (auto i = 0 ; i < detail::COLLECTION_THREADS; ++i) {
10501064 auto const & surface =
10511065 cairo_surface_create_similar_image (
1052- cairo_get_target (cr), get_cairo_format () , width, height);
1066+ cairo_get_target (cr), detail::IMAGE_FORMAT , width, height);
10531067 auto const & ctx = cairo_create (surface);
10541068 cairo_surface_destroy (surface);
10551069 ctxs.push_back (ctx);
@@ -1164,7 +1178,7 @@ void GraphicsContextRenderer::draw_markers(
11641178 auto const & raster_gcr =
11651179 make_pattern_gcr (
11661180 cairo_surface_create_similar_image (
1167- cairo_get_target (cr_), get_cairo_format () ,
1181+ cairo_get_target (cr_), detail::IMAGE_FORMAT ,
11681182 std::ceil (x1 - x0 + 1 ), std::ceil (y1 - y0 + 1 )));
11691183 auto const & raster_cr = raster_gcr.cr_ ;
11701184 cairo_set_antialias (raster_cr, cairo_get_antialias (cr_));
@@ -1669,7 +1683,8 @@ py::array GraphicsContextRenderer::_stop_filter_get_buffer()
16691683 restore ();
16701684 auto const & pattern = cairo_pop_group (cr_);
16711685 auto const & raster_surface =
1672- cairo_image_surface_create (get_cairo_format (), int (width_), int (height_));
1686+ cairo_image_surface_create (
1687+ detail::IMAGE_FORMAT, int (width_), int (height_));
16731688 auto const & raster_cr = cairo_create (raster_surface);
16741689 cairo_set_source (raster_cr, pattern);
16751690 cairo_pattern_destroy (pattern);
@@ -2012,8 +2027,8 @@ Note that the defaults below refer to the initial values of the options;
20122027options not passed to `set_options` are left unchanged.
20132028
20142029At import time, mplcairo will set the initial values of the options from the
2015- ``MPLCAIRO_<OPTION_NAME>`` environment variables (loading them as Python
2016- literals), if any such variables are set.
2030+ ``MPLCAIRO_<OPTION_NAME>`` environment variables, if any such variables are
2031+ set. (They are loaded as Python literals, e.g. strings must be quoted.)
20172032
20182033This function can also be used as a context manager
20192034(``with set_options(...): ...``). In that case, the original values of the
@@ -2028,9 +2043,11 @@ cairo_circles : bool, default: True
20282043collection_threads : int, default: 0
20292044 Number of threads to use to render markers and collections, if nonzero.
20302045
2031- float_surface : bool, default: False
2032- Whether to use a floating point surface (more accurate, but uses more
2033- memory).
2046+ image_format : format_t, default: ARGB32
2047+ The internal image format (either a `format_t`, or the corresponding name).
2048+ All backends can render ARGB32 and RGBA128F images. Qt can additionally
2049+ render RGB24, A8, RGB16_565, RGB30, and (with an extra conversion) A1. No
2050+ backend currently supports RGBA96F.
20342051
20352052miter_limit : float, default: 10
20362053 Setting for cairo_set_miter_limit__. If negative, use Matplotlib's (bad)
@@ -2048,7 +2065,7 @@ _debug: bool, default: False
20482065Notes
20492066-----
20502067An additional format-specific control knob is the ``MaxVersion`` entry in the
2051- *metadata* dict passed to ``savefig``. It can take values ``"1.4"``/``"1.5``
2068+ *metadata* dict passed to ``savefig``. It can take values ``"1.4"``/``"1.5" ``
20522069(to restrict to PDF 1.4 or 1.5 -- default: 1.5), ``"2"``/``"3"`` (to restrict
20532070to PostScript levels 2 or 3 -- default: 3), or ``"1.1"``/``"1.2"`` (to restrict
20542071to SVG 1.1 or 1.2 -- default: 1.1).
@@ -2186,12 +2203,21 @@ Only intended for debugging purposes.
21862203 .def (" set_snap" , &GraphicsContextRenderer::set_snap)
21872204 .def (" set_url" , &GraphicsContextRenderer::set_url)
21882205
2189- // This one function is specific to mplcairo .
2206+ // mplcairo- specific methods .
21902207 .def (
21912208 " set_mplcairo_operator" ,
21922209 [](GraphicsContextRenderer& gcr, cairo_operator_t op) -> void {
21932210 cairo_set_operator (gcr.cr_ , op);
21942211 })
2212+ .def (
2213+ " set_mplcairo_dither" ,
2214+ [](GraphicsContextRenderer& gcr, detail::cairo_dither_t dither) -> void {
2215+ if (!detail::cairo_pattern_set_dither) {
2216+ py::module::import (" warnings" ).attr (" warn" )(
2217+ " cairo_pattern_set_dither requires cairo>=1.18.0" );
2218+ }
2219+ gcr.get_additional_state ().dither = dither;
2220+ })
21952221
21962222 .def (
21972223 " get_clip_rectangle" ,
0 commit comments