-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFont.py
executable file
·73 lines (67 loc) · 2.82 KB
/
Font.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
# This file is part of BobGUI, a free GUI library for Pygame.
# Copyright (C) 2016 Lumidify Productions <lumidify@openmailbox.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pygame
from pygame.locals import *
class FontManager():
def __init__(self):
#Family, size, bold, italic, underline
#TODO: Implement underline in actual text class so that
#it doesn't break when setting word or letter spacing.
self.fonts = {}
def get_font(self, style={}):
if style in self.fonts:
return self.fonts[style]
else:
font_name = style.get("font", None)
try:
font = pygame.font.Font(font_name, self.fontsize)
except:
font = pygame.font.SysFont(font_name, self.fontsize)
font.set_underline(self.underline)
font.set_bold(self.bold)
font.set_italic(self.italic)
self.fonts[style] = font
return font
class Font():
def __init__(self, **kwargs):
self.italic = kwargs.get("italic", False)
self.bold = kwargs.get("bold", False)
self.underline = kwargs.get("underline", False)
self.family = kwargs.get("family", None)
self.size = kwargs.get("size", 20)
def load_font(self):
try:
self.font = pygame.font.Font(self.family, self.size)
except:
self.font = pygame.font.SysFont(self.family, self.size)
self.font.set_underline(self.underline)
self.font.set_bold(self.bold)
self.font.set_italic(self.italic)
def get_size_list(self, **kwargs):
text = kwargs.get("text", "")
letter_spacing = kwargs.get("letter_spacing", 0)
word_spacing = kwargs.get("word_spacing", 0)
extra = 0
size_list = [0]
for index, letter in enumerate(self.text[startindex:]):
if letter_spacing:
extra += letter_spacing
if word_spacing and letter == " ":
extra += word_spacing
size_list.append(self.font.size(self.text[:index + startindex + 1])[0] + extra)
def render(self, **kwargs):
#TODO: Render underline manually so as not to break it when word or letter spacing is set.
pass