8000 FIX: move the font lock higher up the call and class tree by tacaswell · Pull Request #26302 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FIX: move the font lock higher up the call and class tree #26302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ class RendererBase:
* `draw_path_collection`
* `draw_quad_mesh`
"""

def __init__(self):
super().__init__()
self._texmanager = None
Expand Down Expand Up @@ -2147,9 +2146,10 @@ def print_figure(
functools.partial(
print_method, orientation=orientation)
)
# we do this instead of `self.figure.draw_without_rendering`
# so that we can inject the orientation
with getattr(renderer, "_draw_disabled", nullcontext)():
self.figure.draw(renderer)

if bbox_inches:
if bbox_inches == "tight":
bbox_inches = self.figure.get_tightbbox(
Expand Down
17 changes: 1 addition & 16 deletions lib/matplotlib/backends/backend_agg.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from contextlib import nullcontext
from math import radians, cos, sin
import threading

import numpy as np

Expand Down Expand Up @@ -62,19 +61,6 @@ class RendererAgg(RendererBase):
context instance that controls the colors/styles
"""

# we want to cache the fonts at the class level so that when
# multiple figures are created we can reuse them. This helps with
# a bug on windows where the creation of too many figures leads to
# too many open file handles. However, storing them at the class
# level is not thread safe. The solution here is to let the
# FigureCanvas acquire a lock on the fontd at the start of the
# draw, and release it when it is done. This allows multiple
# renderers to share the cached fonts, but only one figure can
# draw at time and so the font cache is used by only one
# renderer at a time.

lock = threading.RLock()

def __init__(self, width, height, dpi):
super().__init__()

Expand Down Expand Up @@ -395,8 +381,7 @@ def draw(self):
self.renderer = self.get_renderer()
self.renderer.clear()
# Acquire a lock on the shared font cache.
with RendererAgg.lock, \
(self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
else nullcontext()):
self.figure.draw(self.renderer)
# A GUI class may be need to update a window using this draw, so
Expand Down
53 changes: 33 additions & 20 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import itertools
import logging
from numbers import Integral
import threading

import numpy as np

Expand Down Expand Up @@ -2365,6 +2366,18 @@ class Figure(FigureBase):
*suppressComposite* is a boolean, this will override the renderer.
"""

# we want to cache the fonts and mathtext at a global level so that when
# multiple figures are created we can reuse them. This helps with a bug on
# windows where the creation of too many figures leads to too many open
# file handles and improves the performance of parsing mathtext. However,
# these global caches are not thread safe. The solution here is to let the
# Figure acquire a shared lock at the start of the draw, and release it when it
# is done. This allows multiple renderers to share the cached fonts and
# parsed text, but only one figure can draw at a time and so the font cache
# and mathtext cache are used by only one renderer at a time.

_render_lock = threading.RLock()

def __str__(self):
return "Figure(%gx%g)" % tuple(self.bbox.size)

Expand Down Expand Up @@ -3124,33 +3137,33 @@ def clear(self, keep_observers=False):
@allow_rasterization
def draw(self, renderer):
# docstring inherited

# draw the figure bounding box, perhaps none for white figure
if not self.get_visible():
return

artists = self._get_draw_artists(renderer)
try:
renderer.open_group('figure', gid=self.get_gid())
if self.axes and self.get_layout_engine() is not None:
try:
self.get_layout_engine().execute(self)
except ValueError:
pass
# ValueError can occur when resizing a window.
with self._render_lock:

self.patch.draw(renderer)
mimage._draw_list_compositing_images(
renderer, self, artists, self.suppressComposite)
artists = self._get_draw_artists(renderer)
try:
renderer.open_group('figure', gid=self.get_gid())
if self.axes and self.get_layout_engine() is not None:
try:
self.get_layout_engine().execute(self)
except ValueError:
pass
# ValueError can occur when resizing a window.

for sfig in self.subfigs:
sfig.draw(renderer)
self.patch.draw(renderer)
mimage._draw_list_compositing_images(
renderer, self, artists, self.suppressComposite)

renderer.close_group('figure')
finally:
self.stale = False
for sfig in self.subfigs:
sfig.draw(renderer)

renderer.close_group('figure')
finally:
self.stale = False

DrawEvent("draw_event", self.canvas, renderer)._process()
DrawEvent("draw_event", self.canvas, renderer)._process()

def draw_without_rendering(self):
"""
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1636,3 +1636,18 @@ def test_get_constrained_layout_pads():
fig = plt.figure(layout=mpl.layout_engine.ConstrainedLayoutEngine(**params))
with pytest.warns(PendingDeprecationWarning, match="will be deprecated"):
assert fig.get_constrained_layout_pads() == expected


def test_not_visible_figure():
fig = Figure()

buf = io.StringIO()
fig.savefig(buf, format='svg')
buf.seek(0)
assert '<g ' in buf.read()

fig.set_visible(False)
buf = io.StringIO()
fig.savefig(buf, format='svg')
buf.seek(0)
assert '<g ' not in buf.read()
0