-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutlined_text.py
More file actions
104 lines (95 loc) · 3.45 KB
/
outlined_text.py
File metadata and controls
104 lines (95 loc) · 3.45 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
98
99
100
101
102
103
104
import pygame
class OutlinedText(object):
def __init__(
self,
text,
position,
outline_width,
font_size,
screen,
foreground_color=(255, 255, 255),
background_color=(0, 0, 0)
):
"""
Outline text for pygame.
:param text: bytes or unicode text
:param position: tuple of form (x, y) you wish text to be rendered at
:param outline_width: outline width in pixels
:param font_size: font size
:param screen: pygame screen you want text rendered to
:param foreground_color: foreground color of text defaults to white
:param background_color: background color of text defaults to black
"""
self.text = text
self.position = position
self.foreground = foreground_color
self.background = background_color
self.outline_width = outline_width
self.screen = screen
self.font = pygame.font.SysFont('Arial Black', font_size)
self.text_surface = self.font.render(self.text, True, self.foreground)
self.text_outline_surface = self.font.render(self.text, True, self.background)
# There is no good way to get an outline with pygame, so we draw
# the text at 8 points around the main text to simulate an outline.
self.directions = [
(self.outline_width, self.outline_width),
(0, self.outline_width),
(-self.outline_width, self.outline_width),
(self.outline_width, 0),
(-self.outline_width, 0),
(self.outline_width, -self.outline_width),
(0, -self.outline_width),
(-self.outline_width, -self.outline_width)
]
def get_width(self):
"""
Get width of text including border.
:return: width of text, including border.
"""
return self.text_surface.get_width() + self.outline_width * 2
def change_position(self, position):
"""
change position text is blitted to.
:param position: tuple in the form of (x, y)
:return:
"""
self.position = position
def change_text(self, text):
"""
Changes text to "text"
:param text: New text
"""
self.text = text
self._update_text()
def change_foreground_color(self, color):
"""
Changes foreground color
:param color: New foreground color
"""
self.foreground = color
self._update_text()
def change_outline_color(self, color):
"""
Changes the outline color
:param color: New outline color
"""
self.background = color
self._update_text()
def _update_text(self):
"""
"protected" function to replace the text surface with a new one based on updated values.
"""
self.text_surface = self.font.render(self.text, True, self.foreground)
self.text_outline_surface = self.font.render(self.text, True, self.background)
def draw(self):
# blit outline images to screen
for direction in self.directions:
self.screen.blit(
self.text_outline_surface,
(
self.position[0] - direction[0],
self.position[1] - direction[1]
)
)
# blit foreground image to the screen
self.screen.blit(self.text_surface, self.position)