-
Notifications
You must be signed in to change notification settings - Fork 0
/
Font.py
42 lines (40 loc) · 2.01 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
import pygame
def clip(surf,x,y,x_size,y_size):
handle_surf = surf.copy()
clipR = pygame.Rect(x,y,x_size,y_size)
handle_surf.set_clip(clipR)
image = surf.subsurface(handle_surf.get_clip())
return image.copy()
class Font():
def __init__(self, path):
self.spacing = 1
self.character_order = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',"А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж", "З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы", "Ь", "Э", "Ю", "Я", "а", "б", "в", "г", "д", "е", "ё", "ж", "з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы", "ь","э", "ю", "я",'.','-',',',':','+',"'",'!','?','0','1','2','3','4','5','6','7','8','9']
font_img_org = pygame.image.load(path)
font_img = pygame.transform.scale(font_img_org, (font_img_org.get_width()*2, font_img_org.get_height()*2))
current_char_width = 0
self.characters = {}
character_count = 0
is_transparent = False
in_gray = False
font_width = font_img.get_width()
for x in range(font_width):
c = font_img.get_at((x, 0))
if c[0] == 127:
if in_gray == False:
char_img = clip(font_img, x - current_char_width, 0, current_char_width, font_img.get_height())
self.characters[self.character_order[character_count]] = char_img.copy()
character_count += 1
current_char_width = 0
in_gray = True
else:
current_char_width += 1
in_gray = False
self.space_width = self.characters['A'].get_width()
def render(self, surf, text, loc):
x_offset = 0
for char in text:
if char != ' ':
surf.blit(self.characters[char], (loc[0] + x_offset, loc[1]))
x_offset += self.characters[char].get_width() + self.spacing
else:
x_offset += self.space_width + self.spacing