|
21 | 21 | _log = logging.getLogger(__name__) |
22 | 22 |
|
23 | 23 |
|
| 24 | +def _prevent_rasterization(draw): |
| 25 | + # We assume that by default artists are not allowed to rasterize (unless |
| 26 | + # its draw method is explicitly decorated). If it is being drawn after a |
| 27 | + # rasterized artist and it has reached a raster_depth of 0, we stop |
| 28 | + # rasterization so that it does not affect the behavior of normal artist |
| 29 | + # (e.g., change in dpi). |
| 30 | + |
| 31 | + @wraps(draw) |
| 32 | + def draw_wrapper(artist, renderer): |
| 33 | + if renderer._raster_depth == 0 and renderer._rasterizing: |
| 34 | + # Only stop when we are not in a rasterized parent |
| 35 | + # and something has been rasterized since last stop. |
| 36 | + renderer.stop_rasterizing() |
| 37 | + renderer._rasterizing = False |
| 38 | + |
| 39 | + return draw(artist, renderer) |
| 40 | + |
| 41 | + draw_wrapper._supports_rasterization = False |
| 42 | + return draw_wrapper |
| 43 | + |
| 44 | + |
24 | 45 | def allow_rasterization(draw): |
25 | 46 | """ |
26 | 47 | Decorator for Artist.draw method. Provides routines |
@@ -103,6 +124,15 @@ class Artist: |
103 | 124 | zorder = 0 |
104 | 125 |
|
105 | 126 | def __init_subclass__(cls): |
| 127 | + |
| 128 | + # Decorate draw() method so that all artists are able to stop |
| 129 | + # rastrization when necessary. If the artist's draw method is already |
| 130 | + # decorated (has a `_supports_rasterization` attribute), it won't be |
| 131 | + # decorated. |
| 132 | + |
| 133 | + if not hasattr(cls.draw, "_supports_rasterization"): |
| 134 | + cls.draw = _prevent_rasterization(cls.draw) |
| 135 | + |
106 | 136 | # Inject custom set() methods into the subclass with signature and |
107 | 137 | # docstring based on the subclasses' properties. |
108 | 138 |
|
@@ -921,7 +951,9 @@ def set_rasterized(self, rasterized): |
921 | 951 | ---------- |
922 | 952 | rasterized : bool |
923 | 953 | """ |
924 | | - if rasterized and not hasattr(self.draw, "_supports_rasterization"): |
| 954 | + supports_rasterization = getattr(self.draw, |
| 955 | + "_supports_rasterization", False) |
| 956 | + if rasterized and not supports_rasterization: |
925 | 957 | _api.warn_external(f"Rasterization of '{self}' will be ignored") |
926 | 958 |
|
927 | 959 | self._rasterized = rasterized |
|
0 commit comments