text
stringlengths
1
93.6k
def get_graphics(self, gc, polygons, points_line, rgbFace, closed=False):
'''Return an instruction group which contains the necessary graphics
instructions to draw the respective graphics.
'''
instruction_group = InstructionGroup()
if isinstance(gc.line['dash_list'], tuple):
gc.line['dash_list'] = list(gc.line['dash_list'])
if rgbFace is not None:
if len(polygons.meshes) != 0:
instruction_group.add(Color(*rgbFace))
for vertices, indices in polygons.meshes:
instruction_group.add(Mesh(
vertices=vertices,
indices=indices,
mode=str("triangle_fan")
))
instruction_group.add(Color(*gc.get_rgb()))
if _mpl_ge_1_5 and (not _mpl_ge_2_0) and closed:
points_poly_line = points_line[:-2]
else:
points_poly_line = points_line
if gc.line['width'] > 0:
instruction_group.add(Line(points=points_poly_line,
width=int(gc.line['width'] / 2),
dash_length=gc.line['dash_length'],
dash_offset=gc.line['dash_offset'],
dash_joint=gc.line['join_style'],
dash_list=gc.line['dash_list']))
return instruction_group
def draw_image(self, gc, x, y, im):
'''Render images that can be displayed on a matplotlib figure.
These images are generally called using imshow method from pyplot.
A Texture is applied to the FigureCanvas. The position x, y is
given in matplotlib coordinates.
'''
# Clip path to define an area to mask.
clippath, clippath_trans = gc.get_clip_path()
# Normal coordinates calculated and image added.
x = self.widget.x + x
y = self.widget.y + y
bbox = gc.get_clip_rectangle()
if bbox is not None:
l, b, w, h = bbox.bounds
else:
l = 0
b = 0
w = self.widget.width
h = self.widget.height
h, w = im.get_size_out()
rows, cols, image_str = im.as_rgba_str()
texture = Texture.create(size=(w, h))
texture.blit_buffer(image_str, colorfmt='rgba', bufferfmt='ubyte')
if clippath is None:
with self.widget.canvas:
Color(1.0, 1.0, 1.0, 1.0)
Rectangle(texture=texture, pos=(x, y), size=(w, h))
else:
if _mpl_ge_2_0:
polygons = clippath.to_polygons(clippath_trans, closed_only=False)
else:
polygons = clippath.to_polygons(clippath_trans)
list_canvas_instruction = self.get_path_instructions(gc, polygons,
rgbFace=(1.0, 1.0, 1.0, 1.0))
for widget, instructions in list_canvas_instruction:
widget.canvas.add(StencilPush())
widget.canvas.add(instructions)
widget.canvas.add(StencilUse())
widget.canvas.add(Color(1.0, 1.0, 1.0, 1.0))
widget.canvas.add(Rectangle(texture=texture,
pos=(x, y), size=(w, h)))
widget.canvas.add(StencilUnUse())
widget.canvas.add(StencilPop())
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
'''Render text that is displayed in the canvas. The position x, y is
given in matplotlib coordinates. A `GraphicsContextKivy` is given
to render according to the text properties such as color, size, etc.
An angle is given to change the orientation of the text when needed.
If the text is a math expression it will be rendered using a
MathText parser.
'''
if mtext:
transform = mtext.get_transform()
ax, ay = transform.transform_point(mtext.get_position())
angle_rad = mtext.get_rotation() * np.pi / 180.
dir_vert = np.array([np.sin(angle_rad), np.cos(angle_rad)])
if mtext.get_rotation_mode() == "anchor":
# if anchor mode, rotation is undone first
v_offset = np.dot(dir_vert, [(x - ax), (y - ay)])
ax = ax + v_offset * dir_vert[0]
ay = ay + v_offset * dir_vert[1]
w, h, d = self.get_text_width_height_descent(s, prop, ismath)
ha, va = mtext.get_ha(), mtext.get_va()
if ha == "center":
ax -= w / 2