|
20 | 20 |
|
21 | 21 | _log = logging.getLogger(__name__) |
22 | 22 |
|
| 23 | +def _prevent_rasterization(draw): |
| 24 | + # we assume that by default artists are not allowed to rasterize (unless |
| 25 | + # its draw method is explicitly decorated). If it is being drawn after a |
| 26 | + # rasterized artist and it has reached the rater_depth of 0. We stop |
| 27 | + # rasterization so that it does not affect the behavior of normal artist |
| 28 | + # (e.g., change in dpi). If the artist's draw method is decorated |
| 29 | + # (draw._supports_rasterization is True), it won't be decorated by |
| 30 | + # `_prevent_rasterization`. |
| 31 | + |
| 32 | + if hasattr(draw, "_supports_rasterization"): |
| 33 | + return draw |
| 34 | + |
| 35 | + @wraps(draw) |
| 36 | + def draw_wrapper(artist, renderer): |
| 37 | + if renderer._raster_depth == 0 and renderer._rasterizing: |
| 38 | + # Only stop when we are not in a rasterized parent |
| 39 | + # and something has be rasterized since last stop |
| 40 | + renderer.stop_rasterizing() |
| 41 | + renderer._rasterizing = False |
| 42 | + |
| 43 | + return draw(artist, renderer) |
| 44 | + |
| 45 | + return draw_wrapper |
| 46 | + |
23 | 47 |
|
24 | 48 | def allow_rasterization(draw): |
25 | 49 | """ |
@@ -119,6 +143,8 @@ def __init_subclass__(cls): |
119 | 143 | cls.set.__qualname__ = f"{cls.__qualname__}.set" |
120 | 144 | cls._update_set_signature_and_docstring() |
121 | 145 |
|
| 146 | + cls.draw = _prevent_rasterization(cls.draw) |
| 147 | + |
122 | 148 | _PROPERTIES_EXCLUDED_FROM_SET = [ |
123 | 149 | 'navigate_mode', # not a user-facing function |
124 | 150 | 'figure', # changing the figure is such a profound operation |
@@ -921,7 +947,10 @@ def set_rasterized(self, rasterized): |
921 | 947 | ---------- |
922 | 948 | rasterized : bool |
923 | 949 | """ |
924 | | - if rasterized and not hasattr(self.draw, "_supports_rasterization"): |
| 950 | + support_rasterization = getattr(self.draw, |
| 951 | + "_supports_rasterization", False) |
| 952 | + if (rasterized and |
| 953 | + not getattr(self.draw, "_supports_rasterization", False)): |
925 | 954 | _api.warn_external(f"Rasterization of '{self}' will be ignored") |
926 | 955 |
|
927 | 956 | self._rasterized = rasterized |
|
0 commit comments