-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtext.py
More file actions
97 lines (80 loc) · 3.1 KB
/
text.py
File metadata and controls
97 lines (80 loc) · 3.1 KB
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
import numpy as np
from matplotlib.font_manager import FontProperties
from .artist import Artist
from .description import Desc
from .conversion_edge import Graph, CoordinateEdge, coord_and_default
class Text(Artist):
def __init__(self, container, edges=None, **kwargs):
super().__init__(container, edges, **kwargs)
edges = [
CoordinateEdge.from_coords(
"xycoords", {"x": Desc((), "auto"), "y": Desc((), "auto")}, "data"
),
*coord_and_default("text", default_value=""),
*coord_and_default("color", default_rc="text.color"),
*coord_and_default("alpha", default_value=1),
*coord_and_default("fontproperties", default_value=FontProperties()),
*coord_and_default("usetex", default_rc="text.usetex"),
*coord_and_default("rotation", default_value=0),
*coord_and_default("antialiased", default_rc="text.antialiased"),
]
self._graph = self._graph + Graph(edges)
def draw(self, renderer, graph: Graph) -> None:
if not self.get_visible():
return
g = graph + self._graph
conv = g.evaluator(
self._container.describe(),
{
"x": Desc((), "display"),
"y": Desc((), "display"),
"text": Desc((), "display"),
"color": Desc((), "display"),
"alpha": Desc((), "display"),
"fontproperties": Desc((), "display"),
"usetex": Desc((), "display"),
# "parse_math": Desc((), "display"),
# "wrap": Desc((), "display"),
# "verticalalignment": Desc((), "display"),
# "horizontalalignment": Desc((), "display"),
"rotation": Desc((), "display"),
# "linespacing": Desc((), "display"),
# "rotation_mode": Desc((), "display"),
"antialiased": Desc((), "display"),
},
)
query, _ = self._container.query(g)
evald = conv.evaluate(query)
text = evald["text"]
if text == "":
return
x = evald["x"]
y = evald["y"]
_, canvash = renderer.get_canvas_width_height()
if renderer.flipy():
y = canvash - y
if not np.isfinite(x) or not np.isfinite(y):
# TODO: log?
return
# TODO bbox?
# TODO implement wrapping/layout?
# TODO implement math?
# TODO implement path_effects?
# TODO gid?
renderer.open_group("text", None)
gc = renderer.new_gc()
gc.set_foreground(evald["color"])
gc.set_alpha(evald["alpha"])
# TODO url?
gc.set_antialiased(evald["antialiased"])
# TODO clipping?
if evald["usetex"]:
renderer.draw_tex(
gc, x, y, text, evald["fontproperties"], evald["rotation"]
)
else:
renderer.draw_text(
gc, x, y, text, evald["fontproperties"], evald["rotation"]
)
gc.restore()
renderer.close_group("text")