8000 Backend plt/gcf refactor by tacaswell · Pull Request #2624 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Backend plt/gcf refactor #2624

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

Closed
wants to merge 36 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5501f4e
first steps to extract FigureManager* and friends from pyplot
tacaswell Nov 29, 2013
373909d
split backend_qt4 into two parts, with and without Gcf
tacaswell Nov 29, 2013
3014931
split backend_qt4agg into two parts.
tacaswell Nov 29, 2013
37e600e
Added a demo-file to show how to use the FigureManager classes to
tacaswell Nov 29, 2013
82f3dea
removed un-needed import of Gcf
tacaswell Nov 29, 2013
1ad0ebf
pep8 on backend_gtk.py
tacaswell Dec 3, 2013
58ea364
pep8 clean up in backend_gdk
tacaswell Dec 3, 2013
f355c0c
removed un-needed Gcf import
tacaswell Dec 3, 2013
d5e47fb
split backend_gcf into two parts,
tacaswell Dec 3, 2013
87781c5
pep8 on backend_gtkagg.py
tacaswell Dec 3, 2013
ce7c2ac
split backend_gktagg.py in to two parts
tacaswell Dec 3, 2013
ec24d0c
updated exclude list
tacaswell Dec 3, 2013
d3ecb67
pep8 clean up on backend_gtk3.py
tacaswell Dec 3, 2013
26fc122
split backend_gtk3 into two parts
tacaswell Dec 3, 2013
fe7cdc0
updated coding standards
tacaswell Dec 3, 2013
b099ddd
pep8 on backend_gtkcairo.py
tacaswell Dec 4, 2013
70939fe
split backend_gtkcairo.py into Gcf dependent and independent
tacaswell Dec 4, 2013
4ec7908
import from _backend_bases in backend_agg to avoid implicit Gcf import
tacaswell Dec 4, 2013
4f9f5b5
pep8 on backend_agg.py
tacaswell Dec 4, 2013
03e86ee
split backend_gtk3agg.py into two parts, the gcf dependent and
tacaswell Dec 4, 2013
d97d98f
updated coding standards exclude list
tacaswell Dec 4, 2013
83caa44
pep8 on backend_gtk3cairo.py
tacaswell Dec 4, 2013
9e74042
pep8 on backend_cairo.py
tacaswell Dec 4, 2013
51ccd5e
changed backend_cairo.py to import from _backend_bases to avoid
tacaswell Dec 4, 2013
efb1881
updated coding standards
tacaswell Dec 4, 2013
f4bdd22
Split backend_gtk3cairo.py into two parts, one dependent on Gcf
tacaswell Dec 4, 2013
a4bda49
Updated embedding_with_qt4_manager.py
tacaswell Dec 5, 2013
407b1ee
Improved class-aliases in _backend_qt*.py
tacaswell Dec 5, 2013
03c43aa
adjusting imports
fariza Dec 18, 2013
35afa92
renamed _backend_bases.py -> base_backend_bases.py
tacaswell Jan 30, 2014
2b213c0
massive rename operation
tacaswell Jan 30, 2014
93fc457
removed rouge merge notation
tacaswell Jan 30, 2014
6401be2
fixed broken import
tacaswell Jan 30, 2014
3ea9f08
pep8 on backend_tkagg.py
tacaswell Jan 30, 2014
b528125
pep8 conformance
tacaswell Jan 31, 2014
1dde78d
removed files that no longer exist
tacaswell Jan 31, 2014
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
8000
Diff view
Prev Previous commit
Next Next commit
pep8 on backend_agg.py
  • Loading branch information
tacaswell committed Jan 30, 2014
commit 4f9f5b5442dc02d9b03907a743ccda345da89c43
96 changes: 60 additions & 36 deletions lib/matplotlib/backends/backend_agg.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

backend_version = 'v2.2'


def get_hinting_flag():
mapping = {
True: LOAD_FORCE_AUTOHINT,
Expand All @@ -67,7 +68,7 @@ class RendererAgg(RendererBase):
The renderer handles all the drawing primitives using a graphics
context instance that controls the colors/styles
"""
debug=1
debug = 1

# we want to cache the fonts at the class level so that when
# multiple figures are created we can reuse them. This helps with
Expand All @@ -82,26 +83,34 @@ class RendererAgg(RendererBase):

lock = threading.RLock()
_fontd = maxdict(50)

def __init__(self, width, height, dpi):
if __debug__: verbose.report('RendererAgg.__init__', 'debug-annoying')
if __debug__:
verbose.report('RendererAgg.__init__', 'debug-annoying')
RendererBase.__init__(self)
self.texd = maxdict(50) # a cache of tex image rasters

self.dpi = dpi
self.width = width
self.height = height
if __debug__: verbose.report('RendererAgg.__init__ width=%s, height=%s'%(width, height), 'debug-annoying')
self._renderer = _RendererAgg(int(width), int(height), dpi, debug=False)
if __debug__:
verbose.report('RendererAgg.__init__ ' +
'width=%s,height=%s' % (width, height),
'debug-annoying')
self._renderer = _RendererAgg(int(width), int(height), dpi,
debug=False)
self._filter_renderers = []

if __debug__: verbose.report('RendererAgg.__init__ _RendererAgg done',
if __debug__:
verbose.report('RendererAgg.__init__ _RendererAgg done',
'debug-annoying')

self._update_methods()
self.mathtext_parser = MathTextParser('Agg')

self.bbox = Bbox.from_bounds(0, 0, self.width, self.height)
if __debug__: verbose.report('RendererAgg.__init__ done',
if __debug__:
verbose.report('RendererAgg.__init__ done',
'debug-annoying')

def _get_hinting_flag(self):
Expand Down Expand Up @@ -133,7 +142,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
"""
Draw the path
"""
nmax = rcParams['agg.path.chunksize'] # here at least for testing
nmax = rcParams['agg.path.chunksize'] # here at least for testing
npts = path.vertices.shape[0]
if (nmax > 100 and npts > nmax and path.should_simplify and
rgbFace is None and gc.get_hatch() is None):
Expand All @@ -144,11 +153,11 @@ def draw_path(self, gc, path, transform, rgbFace=None):
i1[:-1] = i0[1:] - 1
i1[-1] = npts
for ii0, ii1 in zip(i0, i1):
v = path.vertices[ii0:ii1,:]
v = path.vertices[ii0:ii1, :]
c = path.codes
if c is not None:
c = c[ii0:ii1]
c[0] = Path.MOVETO # move to end of last chunk
c[0] = Path.MOVETO # move to end of last chunk
p = Path(v, c)
self._renderer.draw_path(gc, p, transform, rgbFace)
else:
Expand All @@ -158,7 +167,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
"""
Draw the math text using matplotlib.mathtext
"""
if __debug__: verbose.report('RendererAgg.draw_mathtext',
if __debug__:
verbose.report('RendererAgg.draw_mathtext',
'debug-annoying')
ox, oy, width, height, descent, font_image, used_characters = \
self.mathtext_parser.parse(s, self.dpi, prop)
8000 Expand All @@ -169,18 +179,21 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
y = np.round(y - oy + yd)
self._renderer.draw_text_image(font_image, x, y + 1, angle, gc)

def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
def draw_text(self, gc, x, y, s, prop, angle,
ismath=False, mtext=None):
"""
Render the text
"""
if __debug__: verbose.report('RendererAgg.draw_text', 'debug-annoying')
if __debug__:
verbose.report('RendererAgg.draw_text', 'debug-annoying')

if ismath:
return self.draw_mathtext(gc, x, y, s, prop, angle)

flags = get_hinting_flag()
font = self._get_agg_font(prop)
if font is None: return None
if font is None:
return None
if len(s) == 1 and ord(s) > 127:
font.load_char(ord(s), flags=flags)
else:
Expand All @@ -194,8 +207,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
yd = d * np.cos(np.deg2rad(angle))

#print x, y, int(x), int(y), s
self._renderer.draw_text_image(
font.get_image(), np.round(x - xd), np.round(y + yd) + 1, angle, gc)
self._renderer.draw_text_image(font.get_image(), np.round(x - xd),
np.round(y + yd) + 1, angle, gc)

def get_text_width_height_descent(self, s, prop, ismath):
"""
Expand All @@ -222,15 +235,15 @@ def get_text_width_height_descent(self, s, prop, ismath):

flags = get_hinting_flag()
font = self._get_agg_font(prop)
font.set_text(s, 0.0, flags=flags) # the width and height of unrotated string
# the width and height of unrotated string
font.set_text(s, 0.0, flags=flags)
w, h = font.get_width_height()
d = font.get_descent()
w /= 64.0 # convert from subpixels
h /= 64.0
d /= 64.0
return w, h, d


def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None):
# todo, handle props, angle, origins
size = prop.get_size_in_points()
Expand Down Expand Up @@ -258,7 +271,8 @@ def _get_agg_font(self, prop):
"""
Get the font for text instance t, cacheing for efficiency
"""
if __debug__: verbose.report('RendererAgg._get_agg_font',
if __debug__:
verbose.report('RendererAgg._get_agg_font',
'debug-annoying')

key = hash(prop)
Expand All @@ -285,22 +299,26 @@ def points_to_pixels(self, points):
convert point measures to pixes using dpi and the pixels per
inch of the display
"""
if __debug__: verbose.report('RendererAgg.points_to_pixels',
if __debug__:
verbose.report('RendererAgg.points_to_pixels',
'debug-annoying')
return points*self.dpi/72.0

def tostring_rgb(self):
if __debug__: verbose.report('RendererAgg.tostring_rgb',
if __debug__:
verbose.report('RendererAgg.tostring_rgb',
'debug-annoying')
return self._renderer.tostring_rgb()

def tostring_argb(self):
if __debug__: verbose.report('RendererAgg.tostring_argb',
if __debug__:
verbose.report('RendererAgg.tostring_argb',
'debug-annoying')
return self._renderer.tostring_argb()

def buffer_rgba(self):
if __debug__: verbose.report('RendererAgg.buffer_rgba',
if __debug__:
verbose.report('RendererAgg.buffer_rgba',
'debug-annoying')
return self._renderer.buffer_rgba()

Expand Down Expand Up @@ -378,9 +396,9 @@ def post_processing(image, dpi):
post_processing is plotted (using draw_image) on it.
"""

# WARNING.
# For agg_filter to work, the rendere's method need
# to overridden in the class. See draw_markers, and draw_path_collections
# WARNING. For agg_filter to work, the rendere's method need
# to overridden in the class. See draw_markers, and
# draw_path_collections

from matplotlib._image import fromarray

Expand All @@ -390,7 +408,6 @@ def post_processing(image, dpi):

l, b, w, h = bounds


self._renderer = self._filter_renderers.pop()
self._update_methods()

Expand All @@ -403,18 +420,18 @@ def post_processing(image, dpi):

gc = self.new_gc()
self._renderer.draw_image(gc,
l+ox, height - b - h +oy,
l+ox, height - b - h + oy,
image)


def new_figure_manager(num, *args, **kwargs):
"""
Create a new figure manager instance
"""
if __debug__: verbose.report('backend_agg.new_figure_manager',
if __debug__:
verbose.report('backend_agg.new_figure_manager',
'debug-annoying')


FigureClass = kwargs.pop('FigureClass', Figure)
thisFig = FigureClass(*args, **kwargs)
return new_figure_manager_given_figure(num, thisFig)
Expand Down Expand Up @@ -451,7 +468,8 @@ def draw(self):
"""
Draw the figure using the renderer
"""
if __debug__: verbose.report('FigureCanvasAgg.draw', 'debug-annoying')
if __debug__:
verbose.report('FigureCanvasAgg.draw', 'debug-annoying')

self.renderer = self.get_renderer(cleared=True)
# acquire a lock on the shared font cache
Expand All @@ -465,9 +483,12 @@ def draw(self):
def get_renderer(self, cleared=False):
l, b, w, h = self.figure.bbox.bounds
key = w, h, self.figure.dpi
try: self._lastKey, self.renderer
except AttributeError: need_new_renderer = True
else: need_new_renderer = (self._lastKey != key)
try:
self._lastKey, self.renderer
except AttributeError:
need_new_renderer = True
else:
need_new_renderer = (self._lastKey != key)

if need_new_renderer:
self.renderer = RendererAgg(w, h, self.figure.dpi)
Expand All @@ -477,17 +498,20 @@ def get_renderer(self, cleared=False):
return self.renderer

def tostring_rgb(self):
if __debug__: verbose.report('FigureCanvasAgg.tostring_rgb',
if __debug__:
verbose.report('FigureCanvasAgg.tostring_rgb',
'debug-annoying')
return self.renderer.tostring_rgb()

def tostring_argb(self):
if __debug__: verbose.report('FigureCanvasAgg.tostring_argb',
if __debug__:
verbose.report('FigureCanvasAgg.tostring_argb',
'debug-annoying')
return self.renderer.tostring_argb()

def buffer_rgba(self):
if __debug__: verbose.report('FigureCanvasAgg.buffer_rgba',
if __debug__:
verbose.report('FigureCanvasAgg.buffer_rgba',
'debug-annoying')
return self.renderer.buffer_rgba()

Expand Down
0