|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from matplotlib.font_manager import FontProperties |
| 4 | + |
| 5 | +from .artist import Artist |
| 6 | +from .description import Desc |
| 7 | +from .conversion_edge import Graph, CoordinateEdge, coord_and_default |
| 8 | + |
| 9 | + |
| 10 | +class Text(Artist): |
| 11 | + def __init__(self, container, edges=None, **kwargs): |
| 12 | + super().__init__(container, edges, **kwargs) |
| 13 | + |
| 14 | + edges = [ |
| 15 | + CoordinateEdge.from_coords( |
| 16 | + "xycoords", {"x": Desc((), "auto"), "y": Desc((), "auto")}, "data" |
| 17 | + ), |
| 18 | + *coord_and_default("text", default_value=""), |
| 19 | + *coord_and_default("color", default_rc="text.color"), |
| 20 | + *coord_and_default("alpha", default_value=1), |
| 21 | + *coord_and_default("fontproperties", default_value=FontProperties()), |
| 22 | + *coord_and_default("usetex", default_rc="text.usetex"), |
| 23 | + *coord_and_default("rotation", default_value=0), |
| 24 | + *coord_and_default("antialiased", default_rc="text.antialiased"), |
| 25 | + ] |
| 26 | + |
| 27 | + self._graph = self._graph + Graph(edges) |
| 28 | + |
| 29 | + def draw(self, renderer, graph: Graph) -> None: |
| 30 | + if not self.get_visible(): |
| 31 | + return |
| 32 | + g = graph + self._graph |
| 33 | + conv = g.evaluator( |
| 34 | + self._container.describe(), |
| 35 | + { |
| 36 | + "x": Desc((), "display"), |
| 37 | + "y": Desc((), "display"), |
| 38 | + "text": Desc((), "display"), |
| 39 | + "color": Desc((), "display"), |
| 40 | + "alpha": Desc((), "display"), |
| 41 | + "fontproperties": Desc((), "display"), |
| 42 | + "usetex": Desc((), "display"), |
| 43 | + # "parse_math": Desc((), "display"), |
| 44 | + # "wrap": Desc((), "display"), |
| 45 | + # "verticalalignment": Desc((), "display"), |
| 46 | + # "horizontalalignment": Desc((), "display"), |
| 47 | + "rotation": Desc((), "display"), |
| 48 | + # "linespacing": Desc((), "display"), |
| 49 | + # "rotation_mode": Desc((), "display"), |
| 50 | + "antialiased": Desc((), "display"), |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + query, _ = self._container.query(g) |
| 55 | + evald = conv.evaluate(query) |
| 56 | + |
| 57 | + text = evald["text"] |
| 58 | + if text == "": |
| 59 | + return |
| 60 | + |
| 61 | + x = evald["x"] |
| 62 | + y = evald["y"] |
| 63 | + |
| 64 | + _, canvash = renderer.get_canvas_width_height() |
| 65 | + if renderer.flipy(): |
| 66 | + y = canvash - y |
| 67 | + |
| 68 | + if not np.isfinite(x) or not np.isfinite(y): |
| 69 | + # TODO: log? |
| 70 | + return |
| 71 | + |
| 72 | + # TODO bbox? |
| 73 | + # TODO implement wrapping/layout? |
| 74 | + # TODO implement math? |
| 75 | + # TODO implement path_effects? |
| 76 | + |
| 77 | + # TODO gid? |
| 78 | + renderer.open_group("text", None) |
| 79 | + |
| 80 | + gc = renderer.new_gc() |
| 81 | + gc.set_foreground(evald["color"]) |
| 82 | + gc.set_alpha(evald["alpha"]) |
| 83 | + # TODO url? |
| 84 | + gc.set_antialiased(evald["antialiased"]) |
| 85 | + # TODO clipping? |
| 86 | + |
| 87 | + if evald["usetex"]: |
| 88 | + renderer.draw_tex( |
| 89 | + gc, x, y, text, evald["fontproperties"], evald["rotation"] |
| 90 | + ) |
| 91 | + else: |
| 92 | + renderer.draw_text( |
| 93 | + gc, x, y, text, evald["fontproperties"], evald["rotation"] |
| 94 | + ) |
| 95 | + |
| 96 | + gc.restore() |
| 97 | + renderer.close_group("text") |
0 commit comments