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 elif ha == "right": ax -= w if va == "top": ay -= h elif va == "center": ay -= h / 2 if mtext.get_rotation_mode() != "anchor": # if not anchor mode, rotation is undone last v_offset = np.dot(dir_vert, [(x - ax), (y - ay)]) ax = ax + v_offset * dir_vert[0] ay = ay + v_offset * dir_vert[1] x, y = ax, ay x += self.widget.x y += self.widget.y if ismath: self.draw_mathtext(gc, x, y, s, prop, angle) else: font = resource_find(prop.get_name() + ".ttf") if font is None: plot_text = CoreLabel(font_size=prop.get_size_in_points()) else: plot_text = CoreLabel(font_size=prop.get_size_in_points(), font_name=prop.get_name()) plot_text.text = six.text_type("{}".format(s)) if prop.get_style() == 'italic': plot_text.italic = True if weight_as_number(prop.get_weight()) > 500: plot_text.bold = True plot_text.refresh() with self.widget.canvas: if isinstance(angle, float): PushMatrix() Rotate(angle=angle, origin=(int(x), int(y))) Rectangle(pos=(int(x), int(y)), texture=plot_text.texture, size=plot_text.texture.size) PopMatrix() else: Rectangle(pos=(int(x), int(y)), texture=plot_text.texture, size=plot_text.texture.size)