-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.py
59 lines (47 loc) · 1.91 KB
/
word.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
from aabb import AABB
from PIL import ImageFont
class Word:
def __init__(self, content, font, color, top_left):
if not isinstance(font, ImageFont.FreeTypeFont):
raise TypeError('"font" argument has to be of type PIL.ImageFont.FreeTypeFont')
self.content = content
self.aabb = AABB(top_left, top_left)
self.font = font
self.color = color
@property
def position(self):
return self.aabb.position
@property
def width(self):
return self.aabb.width
@property
def height(self):
return self.aabb.height
@property
def font(self):
return self._font
@font.setter
def font(self, font):
if not isinstance(font, ImageFont.FreeTypeFont):
raise TypeError('"font" argument has to be of type PIL.ImageFont.FreeTypeFont')
self._font = font
width, height = font.getsize(self.content)
self.aabb.bottom_right = (self.aabb.top_left[0] + width, self.aabb.top_left[1] + height)
def overlaps(self, value):
try:
if(isinstance(value, AABB)):
return self.aabb.intersects(value)
elif isinstance(getattr(value, 'aabb'), AABB):
return self.aabb.intersects(value.aabb)
else:
raise TypeError('"value" argument has to be of type AABB or contain an "aabb" property of type AABB')
except AttributeError:
raise TypeError('"value" argument has to be of type AABB or contain an "aabb" property of type AABB')
def get_scaled_aabb(self, scale):
return self.aabb.scale(scale)
def draw(self, image_draw, debug=False):
top_left = self.aabb.top_left
if(debug):
bottom_right = self.aabb.bottom_right
image_draw.rectangle([top_left, bottom_right], outline=(128, 128, 128))
image_draw.text(top_left, self.content, self.color, font=self.font)