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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 | import copy
def _check_string(name, value):
if value is not None and not isinstance(value, str):
raise Exception(
"Attribute '{}' has to be a string or None, not {}.".format(
name, repr(type(value))
)
)
return value
def _check_number(name, value):
if (
value is not None
and not isinstance(value, int)
and not isinstance(value, float)
):
raise Exception(
"Attribute '{}' has to be a number or None, not {}.".format(
name, repr(type(value))
)
)
return value
def _check_bool(name, value):
if value is not None and not isinstance(value, bool):
raise Exception(
"Attribute '{}' has to be a bool or None, not {}.".format(
name, repr(type(value))
)
)
return value
def _check_choice(name, value, choices):
if value not in choices:
raise Exception(
"Attribute '{}' has to be one of {}, not {}.".format(
name, choices, repr(value)
)
)
return value
class TextStyle:
"""
Holds information about the style of text.
"""
ALIGN_VALUES = (None, "left", "middle", "right")
VARIANT_NUMERIC_VALUES = (
None,
"normal",
"ordinal",
"slashed-zero",
"lining-nums",
"oldstyle-nums",
"proportinal-nums",
"tabular-nums",
"diagonal-fractions",
"stacked-fractions",
)
__slots__ = (
"_font",
"_size",
"_align",
"_line_spacing",
"_color",
"_bold",
"_italic",
"_variant_numeric",
)
def __init__(
self,
*,
font=None,
size=None,
align=None,
line_spacing=None,
color=None,
bold=None,
italic=None,
variant_numeric=None
):
"""
Parameters
----------
font: str
Font used to render the text.
size: float
Size of the font.
align: {"center", "left", "right"}
Alignment of the text.
line_spacing: float
Space between lines. Scales with font size.
color: str
Color of the text.
bold: bool
If True, the text will be rendered as bold.
italic: bool
If True, the text will be rendered as italic.
variant_numeric: str
SVG mode for rendering of digits (e.g. "lining-nums").
"""
self.font = font
self.size = size
self.align = align
self.line_spacing = line_spacing
self.color = color
self.bold = bold
self.italic = italic
self.variant_numeric = variant_numeric
@property
def font(self):
return self._font
@font.setter
def font(self, value):
self._font = _check_string("font", value)
@property
def size(self):
return self._size
@size.setter
def size(self, value):
self._size = _check_number("size", value)
@property
def align(self):
return self._align
@align.setter
def align(self, value):
self._align = _check_choice("align", value, self.ALIGN_VALUES)
@property
def line_spacing(self):
return self._line_spacing
@line_spacing.setter
def line_spacing(self, value):
self._line_spacing = _check_number("line_spacing", value)
@property
def color(self):
return self._color
@color.setter
def color(self, value):
self._color = _check_string("color", value)
@property
def bold(self):
return self._bold
@bold.setter
def bold(self, value):
self._bold = _check_bool("bold", value)
@property
def italic(self):
return self._italic
@italic.setter
def italic(self, value):
self._italic = _check_bool("italic", value)
@property
def variant_numeric(self):
return self._variant_numeric
@variant_numeric.setter
def variant_numeric(self, value):
self._variant_numeric = _check_choice(
"variant_numeric", value, self.VARIANT_NUMERIC_VALUES
)
def copy(self) -> "TextStyle":
"""Copies the text style."""
return copy.copy(self)
def update(self, style: "TextStyle"):
"""Updates the text style in-place."""
assert isinstance(style, TextStyle)
for slot in self.__slots__:
value = getattr(style, slot)
if value is not None:
setattr(self, slot, value)
def compose(self, style: "TextStyle") -> "TextStyle":
"""Create a new style that will be the combination of the current and the given style."""
copy = self.copy()
copy.update(style)
return copy
def compose_style(styles, style, full_style):
if isinstance(style, str):
style_name = style
style = styles.get(style_name)
if style is None:
raise Exception("Style '{}' not found".format(style_name))
if style_name == "default":
return style.copy()
elif not isinstance(style, TextStyle):
raise Exception("Invalid type used as a style: {}".format(repr(type(style))))
if not full_style:
return style.copy()
return styles["default"].compose(style)
|