-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.py
621 lines (517 loc) · 21.4 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
"""
Wrappers around pygame.freetype for easy text rendering.
Also supports expressions with overbars and truth tables.
"""
import pygame
import pygame.gfxdraw
import pygame.freetype
import alignment
import interfaces
import boolean
# TODO: Add normal tables
# TODO: Use textwrap to wrap text
# pygame.init does not call pygame.freetype.init
pygame.freetype.init()
# Text be itself should not be interactable or draggable, but if you need it then
# create a new class that inherits from both Text and IInteractable/IDraggable
class Text(interfaces.IRenderable):
"""
A container for text that can be rendered.
This is a wrapper for pygame.freetype.Font, with the small addition of being
able to render multiple lines of text.
"""
def __init__(self,
text,
hitbox, # (x, y) or (x, y, w, h)
hidden=False,
size=50, # font size
fgcolor=pygame.color.THECOLORS["black"],
bgcolor=pygame.color.THECOLORS["white"],
underline=False,
strong=False, # bold
oblique=False, # italic
name="Calibri"):
# TODO: Consider caching fonts
# Fonts could be cached but that would mean changing
# their attributes before rendering for every Text instance
file = pygame.freetype.match_font(name)
self.font = pygame.freetype.Font(file)
self.size = size
self.text = text
self.bgcolor = bgcolor
self.fgcolor = fgcolor
self.underline = underline
self.strong = strong
self.oblique = oblique
# Hitbox can either be (x, y, w, h) or (x, y)
# When it is (x, y), we need to fit the surface to the hitbox
if len(hitbox) == 2:
hitbox += self.fit_surface()
super().__init__(hitbox, hidden)
def __repr__(self):
return "<{name}({hitbox}, {text})>".format(name=self.__class__.__name__,
hitbox=repr(self.hitbox),
text=repr(self.text))
# More wrappers, this time around font's attributes
@property
def size(self):
return self.font.size
@size.setter
def size(self, size):
self.font.size = size
self.dirty = True
@property
def text(self):
return self._text
@text.setter
def text(self, string):
# Although there is no need to worry about storing a direct copy, this
# ensures that _text is of type string for when we call text.split in
#fit_surface and update_surface
self._text = str(string)
self.dirty = True
# bgcolor is not an attribute saved in font, but is passed when calling
# font.render_to
@property
def bgcolor(self):
return self._bgcolor
@bgcolor.setter
def bgcolor(self, bgcolor):
# For some odd reason, pygame.color.THECOLORS[<color_name>] returns
# a tuple instead of a pygame.color.Color
# pygame.color.Color takes a name, rgba or rgbavalue
if isinstance(bgcolor, (tuple, list, pygame.color.Color)):
# Unpack the tuple or list
# Create a new color, to ensure that we don't store a direct copy
# of the tuple and bypass this function
self._bgcolor = pygame.color.Color(*bgcolor)
else:
# this is if bgcolor is a name or rgbavalue
self._bgcolor = pygame.color.Color(bgcolor)
self.dirty = True
@property
def fgcolor(self):
return self.font.fgcolor
@fgcolor.setter
def fgcolor(self, fgcolor):
# The same as bgcolor
if isinstance(fgcolor, (tuple, list, pygame.color.Color)):
self.font.fgcolor = pygame.color.Color(*fgcolor)
else:
self.font.fgcolor = pygame.color.Color(fgcolor)
self.dirty = True
@property
def underline(self):
return self.font.underline
@underline.setter
def underline(self, underline):
self.font.underline = underline
self.dirty = True
@property
def strong(self):
return self.font.strong
@strong.setter
def strong(self, strong):
self.font.strong = strong
self.dirty = True
@property
def oblique(self):
return self.font.oblique
@oblique.setter
def oblique(self, oblique):
self.font.oblique = oblique
self.dirty = True
def fit_surface(self):
"""
Returns the width and height for a surface that would perfectly fit the text.
The returned value leaves a 1px border around the text.
"""
lines = self.text.split("\n")
max_height = max_height = max(
self.font.get_rect(line).h for line in lines)
w = max(self.font.get_rect(line).w for line in lines)
h = (len(lines) - 1) * max_height + self.font.get_rect(lines[-1]).h
# When pygame.freetype.Font.render_to is given a bgcolor, it goes over
# by 1 pixels of the actual font, the extra is to add a small border
return (w + 2, h + 2)
def update_surface(self):
super().update_surface()
# pygame.freetype.Font.render_to only fills the boxes for each line
self.surface.fill(self.bgcolor)
# Render Text
lines = self.text.split("\n")
max_height = max(self.font.get_rect(line).h for line in lines)
for i in range(len(lines)):
x = 1
y = 1 + max_height * i
# Add a small border around the font to prevent the text bleeding
# if blitted onto a surface of the same color
# giving a bgcolor makes the font more "sharp"
self.font.render_to(self.surface,
(x, y),
lines[i],
bgcolor=self.bgcolor)
class Expression(Text):
"""
Renderable boolean expressions.
Boolean expressions that are rendered includes the proper overbars.
"""
def __init__(self,
expression,
hitbox,
hidden=False,
size=50,
fgcolor=pygame.color.THECOLORS["black"],
bgcolor=pygame.color.THECOLORS["white"],
name="Calibri"):
# align = alignment.top_left):
super().__init__(expression,
hitbox,
hidden=hidden,
size=size,
fgcolor=fgcolor,
bgcolor=bgcolor,
name=name)
# Ignore the programmer if he wants to set any of the following values
# That also means that assigning these does not make the object dirty
@property
def underline(self):
return super().underline
@underline.setter
def underline(self, underline):
self.font.underline = False
@property
def strong(self):
return super().strong
@strong.setter
def strong(self, strong):
self.font.strong = False
@property
def oblique(self):
return super().oblique
@oblique.setter
def oblique(self, oblique):
self.font.oblique = False
# Assign expression
@property
def text(self):
return str(self.expression)
@text.setter
def text(self, string):
self.expression = string
@property
def expression(self):
return self._expression
@expression.setter
def expression(self, expression):
if isinstance(expression, str):
expression = boolean.parse(expression, False)
if not isinstance(expression, boolean.Expression):
raise TypeError("Argument must be str or Expression but it is {}"
.format(expression.__class__))
self._expression = expression
self.dirty = True
@staticmethod
def max_not_count(expr):
"""
Recursively calculates the maximum multiplier needed when drawing an overbar.
"""
if isinstance(expr, boolean.BaseElement):
return 0
if isinstance(expr, boolean.Symbol):
return 0
if isinstance(expr, boolean.NOT):
return 1 + max(Expression.max_not_count(e) for e in expr.args)
return max(Expression.max_not_count(e) for e in expr.args)
def overbar_surface(self):
"""
Returns a surface containing an overbar that would fit the entire expression.
The overbar scales with the text size.
"""
old_underline = self.font.underline
old_underline_adjustment = self.font.underline_adjustment
# As we are using an overbar for each NOT, we need to remove them
text = self.text.replace(boolean.NOT.operator, "")
without_overbar = self.font.get_rect(text)
# Underline adjustment of -1 makes the underline become an overbar
self.font.underline = True
self.font.underline_adjustment = -1
with_overbar = self.font.render(text, bgcolor=self.bgcolor)
# font.render returns (<surface>, <rect>)
width = with_overbar[1].w
height = with_overbar[1].h - without_overbar.h
overbar_surface = pygame.surface.Surface((width, height))
overbar_surface.blit(with_overbar[0], (0, 0), (0, 0, width, height))
self.font.underline = old_underline
self.font.underline_adjustment = old_underline_adjustment
return overbar_surface
def fit_surface(self):
# As we are using an overbar for each NOT, we need to remove them
w = self.font.get_rect(self.text.replace(boolean.NOT.operator, "")).w
# adjust hight for every overbar we need
overbar_height = self.max_not_count(
self.expression) * self.overbar_surface().get_rect().h
h = self.font.get_rect(
self.text.replace(boolean.NOT.operator, "")).h + overbar_height
# 1px border on each side
return (w + 2, h + 2)
def update_surface(self):
# Text.update_surface renders the text so that it starts at the top left of the
# bounding box, instead we want to move it down if there are any overbars that need
# to be drawn.
# update_overbar will modify self.render_surface, which is created here
# to be safe.
interfaces.IRenderable.update_surface(self)
# Cache the overbar surface before the update_overbar function. The overbar surface
# depends on the font size and expression, so it should not change during an
# update_surface call.
overbar_surface = self.overbar_surface()
# pygame.freetype.Font.render_to only fills the boxes for each line
self.surface.fill(self.bgcolor)
# Recursively draw overbars
# TODO?: clean + optimize this code, without breaking it
def update_overbar(expr, top_left):
# I'm going to leave t here as it'll be really useful when trying
# to refactor / clean / optimize this code
# DEBUG CODE
# DEBUG_TEXT_SIZE = str(expr).replace(boolean.NOT.operator, "")
# DEBUG_TEXT_SIZE = self.font.get_rect(DEBUG_TEXT_SIZE)
# DEBUG_TEXT_SIZE = (DEBUG_TEXT_SIZE.w, DEBUG_TEXT_SIZE.h)
# pygame.gfxdraw.rectangle(
# self.surface, pygame.rect.Rect(top_left, DEBUG_TEXT_SIZE),
# DEBUG_ORANGE)
if isinstance(expr, boolean.NOT):
overbar_height = self.max_not_count(
expr) * overbar_surface.get_rect().h
dest = (top_left[0], top_left[1] - overbar_height)
text = str(expr).replace(boolean.NOT.operator, "")
expr_rect = self.font.get_rect(text)
area = (0, 0, expr_rect.w, overbar_surface.get_rect().h)
self.surface.blit(overbar_surface, dest, area)
if isinstance(expr.args[0], boolean.NOT):
update_overbar(expr.args[0], top_left)
elif isinstance(expr.args[0], boolean.DualBase):
# The "size" of a character depends on the next character
# If the argument of a NOT is a DualBase, it will always have parenthesis
# surrounding it
arg_rect = self.font.get_rect(text[1:])
parenthesis_w = expr_rect.w - arg_rect.w
new_top_left = (top_left[0] + parenthesis_w, top_left[1])
update_overbar(expr.args[0], new_top_left)
elif isinstance(expr, boolean.DualBase):
# The actual printed args, with extra parenthesis
args = []
for arg in expr.args:
if arg.isliteral or isinstance(arg, boolean.NOT):
args.append(str(arg).replace(boolean.NOT.operator, ""))
else:
args.append(
"({})".format(str(arg).replace(boolean.NOT.operator, "")))
for arg in expr.args:
if isinstance(arg, boolean.Symbol) or isinstance(arg, boolean.BaseElement):
# End of the line, don't need to do anything
continue
# Bounding box for the whole expression
expr_rect = self.font.get_rect(
str(expr).replace(boolean.NOT.operator, ""))
# Text for the arg and every arg after it
after_text = expr.operator.join(
args[i] for i in range(expr.args.index(arg), len(expr.args)))
# Bounding box for the arg and every arg after it
after_rect = self.font.get_rect(after_text)
# Character size depends the next character
w = expr_rect.w - after_rect.w
new_top_left = (top_left[0] + w, top_left[1])
# Literals are not surrounded by parenthesis
# The NOT function puts an overbar around the parenthesis
if arg.isliteral or isinstance(arg, boolean.NOT):
update_overbar(arg, new_top_left)
else:
# Correct poisition for extra parenthesis
expr_rect = after_rect
after_rect = self.font.get_rect(after_text[1:])
parenthesis_w = expr_rect.w - after_rect.w
new_top_left = (
new_top_left[0] + parenthesis_w, new_top_left[1])
update_overbar(arg, new_top_left)
text = self.text.replace(boolean.NOT.operator, "")
overbar_height = self.max_not_count(
self.expression) * overbar_surface.get_rect().h
x = 1
y = 1 + overbar_height
# Draw the expression
self.font.render_to(self.surface,
(x, y),
text,
bgcolor=self.bgcolor)
# Actually draw the overbar
update_overbar(self.expression, (x, y))
# TODO: Make the boxes different sizes so the truth table takes less space
class TruthTable(interfaces.IContainer):
"""
A renderable truth table.
"""
def __init__(self,
expression,
hitbox,
hidden=False,
font_size=50,
fgcolor=pygame.color.THECOLORS["black"],
bgcolor=pygame.color.THECOLORS["white"],
font_name="Calibri"):
self.expression = expression
self.fgcolor = fgcolor
self.font_name = font_name
self.font_size = font_size
if len(hitbox) == 2:
hitbox += self.fit_surface()
super().__init__(hitbox,
hidden=hidden,
bgcolor=bgcolor)
def __repr__(self):
return "<{name}({hitbox}, {text})>".format(name=self.__class__.__name__,
hitbox=repr(self.hitbox),
text=repr(self.text))
@property
def text(self):
return str(self.expression)
@text.setter
def text(self, string):
self.expression = string
@property
def expression(self):
return self._expression
@expression.setter
def expression(self, expression):
if isinstance(expression, str):
expression = boolean.parse(expression, False)
if not isinstance(expression, boolean.Expression):
raise TypeError(
"Argument must be str or Expression but it is {}"
.format(expression.__class__))
self._expression = expression
self.table = expression
# Table is in the format:
# [
# {<column_heading>:<value>, <column_heading>:<value>},
# {<column_heading>:<value>, <column_heading>:<value>},
# ...
# ]
# Each index of the array is dictionary which contains the values of a different row.
# Columns are given headings which can also the a number.
@property
def table(self):
return self._table
@table.setter
def table(self, table):
if isinstance(table, str):
table = boolean.parse(table, False)
if isinstance(table, boolean.Expression):
table = boolean.truth_table(table)
else:
raise TypeError(
"Argument must be Expression but it is {}"
.format(table.__class__))
# Table should not be directly modified
self._table = tuple(table)
self.dirty = True
@property
def font_size(self):
return self._font_size
@font_size.setter
def font_size(self, font_size):
self._font_size = float(font_size)
self.dirty = True
@property
def fgcolor(self):
return self._fgcolor
@fgcolor.setter
def fgcolor(self, fgcolor):
if isinstance(fgcolor, (tuple, list, pygame.color.Color)):
self._fgcolor = pygame.color.Color(*fgcolor)
else:
self._fgcolor = pygame.color.Color(fgcolor)
self.dirty = True
@property
def font_name(self):
return self._font_name
# I'm not sure why you'd want to do this...
@font_name.setter
def font_name(self, font_name):
# Throws an error if the font doesn't exist
if pygame.freetype.match_font(font_name) is None:
raise ValueError(
"Font of name {name} not found".format(name=font_name))
self._font_name = str(font_name)
self.dirty = True
def box_size(self):
# Helper function to create a new expression quickly
# Font color and background color don't matter if you just
# want the size
def new_expression(expr, pos=(0, 0)):
return Expression(expr,
pos,
size=self.font_size,
name=self.font_name)
all_text = [new_expression(e) for e in self.table[0].keys()] +\
[new_expression(boolean.TRUE),
new_expression(boolean.FALSE)]
box_w = max(e.w for e in all_text)
box_h = max(e.h for e in all_text)
return (box_w + 2, box_h + 2)
def fit_surface(self):
box_size = self.box_size()
w = box_size[0] * len(self.table[0].keys())
h = box_size[1] * (len(self.table) + 1)
return (w + 2, h + 2)
def update_renderable_list(self):
def new_expression(expr, pos=(0, 0)):
return Expression(expr,
pos,
size=self.font_size,
fgcolor=self.fgcolor,
bgcolor=self.bgcolor,
name=self.font_name)
def sort_key(expr):
symbol_length = len(expr.symbols)
string_length = len(str(expr))
string_value = float("inf")
if isinstance(expr, boolean.Symbol):
string_value = str(expr)
return (symbol_length, string_length, string_value)
renderable_list = []
sorted_column_headings = sorted(self.table[0].keys(),
key=sort_key)
box_size = self.box_size()
box_w = box_size[0]
box_h = box_size[1]
# TODO: Use enumerate instead
x = 1
for column in sorted_column_headings:
y = 1
heading = new_expression(column)
heading.top_left = (x, y)
heading.align((x, y, box_w, box_h), alignment.center_middle)
renderable_list.append(heading)
for row in self.table:
y += box_h
value = new_expression(row[column])
value.top_left = (x, y)
value.align((x, y, box_w, box_h), alignment.center_middle)
renderable_list.append(value)
x += box_w
self.renderable_list = renderable_list
def update_surface(self):
super().update_surface()
box_size = self.box_size()
box_w = box_size[0]
box_h = box_size[1]
# Draw the lines
for i in range(len(self.table[0].keys()) + 1):
x = box_w * i if i != 0 else 1
pygame.gfxdraw.line(
self.surface, x, 1, x, self.h - 2, self.fgcolor)
for i in range(len(self.table) + 2):
y = box_h * i if i != 0 else 1
pygame.gfxdraw.line(
self.surface, 1, y, self.w - 2, y, self.fgcolor)