/usr/lib/python3/dist-packages/ginga/mplw/CanvasRenderMpl.py is in python3-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #
# CanvasRenderMpl.py -- for rendering into a ImageViewMpl widget
#
# Eric Jeschke (eric@naoj.org)
#
# Copyright (c) Eric R. Jeschke. All rights reserved.
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
import matplotlib.patches as patches
import matplotlib.lines as lines
import matplotlib.text as text
from matplotlib.path import Path as MplPath
import numpy
from . import MplHelp
from ginga.canvas.mixins import *
# force registration of all canvas types
import ginga.canvas.types.all
class RenderContext(object):
def __init__(self, viewer):
self.viewer = viewer
self.shape = None
# TODO: encapsulate this drawable
self.cr = MplHelp.MplContext(self.viewer.ax_util)
self.pen = None
self.brush = None
self.font = None
def set_line_from_shape(self, shape):
# TODO: support line width and style
alpha = getattr(shape, 'alpha', 1.0)
self.pen = self.cr.get_pen(shape.color, alpha=alpha)
def set_fill_from_shape(self, shape):
fill = getattr(shape, 'fill', False)
if fill:
if hasattr(shape, 'fillcolor') and shape.fillcolor:
color = shape.fillcolor
else:
color = shape.color
alpha = getattr(shape, 'alpha', 1.0)
alpha = getattr(shape, 'fillalpha', alpha)
self.brush = self.cr.get_brush(color, alpha=alpha)
else:
self.brush = None
def set_font_from_shape(self, shape):
if hasattr(shape, 'font'):
if hasattr(shape, 'fontsize') and shape.fontsize is not None:
fontsize = shape.fontsize
else:
fontsize = shape.scale_font(self.viewer)
alpha = getattr(shape, 'alpha', 1.0)
self.font = self.cr.get_font(shape.font, fontsize, shape.color,
alpha=alpha)
else:
self.font = None
def initialize_from_shape(self, shape, line=True, fill=True, font=True):
if line:
self.set_line_from_shape(shape)
if fill:
self.set_fill_from_shape(shape)
if font:
self.set_font_from_shape(shape)
def set_line(self, color, alpha=1.0, linewidth=1, style='solid'):
# TODO: support style
self.pen = self.cr.get_pen(color, alpha=alpha, linewidth=linewidth,
linestyle=style)
def set_fill(self, color, alpha=1.0):
if color is None:
self.brush = None
else:
self.brush = self.cr.get_brush(color, alpha=alpha)
def set_font(self, fontname, fontsize):
self.font = self.cr.get_font(fontname, fontsize, 'black',
alpha=1.0)
def text_extents(self, text):
return self.cr.text_extents(text, self.font)
##### DRAWING OPERATIONS #####
def draw_text(self, cx, cy, text, rot_deg=0.0):
fontdict = self.font.get_fontdict()
self.cr.axes.text(cx, cy, text, fontdict=fontdict)
def draw_polygon(self, cpoints):
self.cr.init(closed=True, transform=None)
self.cr.update_patch(self.pen, self.brush)
xy = numpy.array(cpoints)
p = patches.Polygon(xy, **self.cr.kwdargs)
self.cr.axes.add_patch(p)
def draw_circle(self, cx, cy, cradius):
self.cr.init(radius=cradius, transform=None)
self.cr.update_patch(self.pen, self.brush)
xy = (cx, cy)
p = patches.Circle(xy, **self.cr.kwdargs)
self.cr.axes.add_patch(p)
def draw_bezier_curve(self, verts):
self.cr.init(transform=None)
self.cr.update_patch(self.pen, None)
codes = [ MplPath.MOVETO,
MplPath.CURVE4, MplPath.CURVE4, MplPath.CURVE4,
]
path = MplPath(verts, codes)
p = patches.PathPatch(path, **self.cr.kwdargs)
self.cr.axes.add_patch(p)
def draw_ellipse_bezier(self, verts):
self.cr.init(transform=None)
self.cr.update_patch(self.pen, self.brush)
# draw 4 bezier curves to make the ellipse
codes = [ MplPath.MOVETO,
MplPath.CURVE4, MplPath.CURVE4, MplPath.CURVE4,
MplPath.CURVE4, MplPath.CURVE4, MplPath.CURVE4,
MplPath.CURVE4, MplPath.CURVE4, MplPath.CURVE4,
MplPath.CURVE4, MplPath.CURVE4, MplPath.CURVE4,
]
path = MplPath(verts, codes)
p = patches.PathPatch(path, **self.cr.kwdargs)
self.cr.axes.add_patch(p)
def draw_line(self, cx1, cy1, cx2, cy2):
self.cr.init(transform=None)
self.cr.update_line(self.pen)
l = lines.Line2D((cx1, cx2), (cy1, cy2), **self.cr.kwdargs)
self.cr.axes.add_line(l)
def draw_path(self, cpoints):
self.cr.init(closed=False, transform=None)
self.cr.update_patch(self.pen, None)
xy = numpy.array(cpoints)
p = patches.Polygon(xy, **self.cr.kwdargs)
self.cr.axes.add_patch(p)
class CanvasRenderer(object):
def __init__(self, viewer):
self.viewer = viewer
def setup_cr(self, shape):
cr = RenderContext(self.viewer)
cr.initialize_from_shape(shape)
return cr
def get_dimensions(self, shape):
cr = self.setup_cr(shape)
cr.set_font_from_shape(shape)
return cr.text_extents(shape.text)
#END
|