-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.py
113 lines (92 loc) · 4.63 KB
/
text.py
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
from artist import Artist
from font import Font
class Text(Artist):
"""
Represent a label on a canvas.
The font used can be either a string with the font family name in it,
or a PyGraphene Font object.
The Text object contains a color property from Artist, and if font is
a Font object, then it also contains a color property. The order of
priority for determining what color the text will be is:
1. if font is a Font, then use font's color
2. else, use the Text color
====================== ================= =======
Property Possible Values Description
====================== ================= =======
text str ('') The text that will be displayed.
font | str ('Times') The font that will be used. Either a Font object the name of a font family, or
| Font a dictionary of Font properties.
| dict
horizontalalignment | 'center' Define where the horizontal anchor point is located in reference to the text.
| 'right'
| 'left'
verticalignment | 'center' Define where the vertical anchor point is located in reference to the text.
| 'top'
| 'bottom'
xoffset int (0) A shift in the x direction of where the text will be located. This will be
applied by adding it to the text's position when drawn. But the text's position
will not be modified.
yoffset int (0) A shift in the y direction of where the text will be located. This will be
applied by adding it to the text's position when drawn. But the text's position
will not be modified.
rotation | 'horizontal' Define how much to rotate the text. If using an int, specifies degrees clockwise.
| 'vertical'
| int
| float
====================== ================= =======
"""
def __init__(self, canvas, text='', **kwprops):
"""
If text is given as a kwprops in the initialization, it will not be used.
"""
initialProperties = {'font': 'Times',
'text': text,
'horizontalalignment': 'center',
'verticalalignment': 'center',
'xoffset': 0,
'yoffset': 0,
'rotation': 'horizontal',
}
initialProperties.update(kwprops)
Artist.__init__(self, canvas, **initialProperties)
def setText(self, text):
"""Convenience method to set the text of this label."""
if isinstance(text, str):
self.setProps(text=text)
def setProps(self, props={}, **kwprops):
"""
If font is given as a string or a dict, convert it into a Font object.
If rotation is a string representing a number, convert it.
Then call Artist.setProps.
"""
# A property might be defined in both props and kwprops,
# so do all of this twice
for d in (props, kwprops):
if 'font' in d.keys():
currentFont = Font()
if 'font' in self.props().keys():
currentFont = self.props('font')
if isinstance(d['font'], str):
currentFont.setFamily(d['font'])
d['font'] = currentFont
elif isinstance(d['font'], dict):
currentFont.setProps(d['font'])
d['font'] = currentFont
if 'rotation' in d.keys():
try:
d['rotation'] = float(d['rotation'])
except ValueError:
pass
Artist.setProps(self, props, **kwprops)
def _draw(self, *args, **kwargs):
props = self.props()
if isinstance(self.props('font'), Font):
try:
props.update(color=self.props('font').props('color'))
except:
pass
return self.canvas().drawText( self._x,
self._y,
self._ox,
self._oy,
**props)