-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont_finder.py
84 lines (73 loc) · 2.33 KB
/
font_finder.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
import pygame
import pygame.freetype
from pygame.constants import KEYDOWN, QUIT, RESIZABLE
from deck import Colors
from functools import partial
from itertools import chain
import os
main_dir = os.path.dirname(os.path.abspath(__file__))
fonts_dir = os.path.join(main_dir, 'data', 'fonts')
symbols = '{} ♠♥♣♦♞♛♚' # unicode U+2660, 2663, 2665, 2666, 265A, 265B, 265E
selection = ['dejavusans',
'dejavusansmono',
'freeserif',
'symbola',
'wenquanyimicrohei',
'wenquanyimicroheimono', ]
def render(font_name):
try:
fn, fp = font_name
except ValueError:
font = pygame.freetype.SysFont(font_name, 16)
fn = font_name
else:
font = pygame.freetype.Font(fp, 16)
return font.render(symbols.format(fn), fgcolor=Colors.white)
def print_screen(screen, screensize, flist):
m = 5
curw = px = py = 0
for font in flist:
try:
surf, (_, _, w, h) = render(font)
except OSError:
continue
py += m
curw = max(curw, w)
if py + h > screensize[1]: # new column
py = m
px += curw + m
curw = 0
if px + w > screensize[0]: # new screen
px = py = m
curw = w
yield True
screen.blit(surf, (px, py))
pygame.display.update()
py += h
yield True
def main():
try:
pygame.freetype.init()
screensize = (800, 600)
screen = pygame.display.set_mode(screensize, RESIZABLE)
screen.fill(Colors.black)
try:
custom = ((n, os.path.join(fonts_dir, n)) for n in os.listdir(fonts_dir))
except OSError:
custom = []
flist = list(chain(custom, sorted(pygame.freetype.get_fonts()),))
# print(flist)
print_s = partial(print_screen, screen, screensize)
while True:
for wait in chain(print_s(selection), print_s(flist)):
while wait:
for event in pygame.event.get():
if event.type == QUIT:
return
if event.type == KEYDOWN:
screen.fill(Colors.black)
wait = False
finally:
pygame.quit()
if __name__ == '__main__':
main()