-
Notifications
You must be signed in to change notification settings - Fork 0
/
font_manager.py
35 lines (25 loc) · 1.07 KB
/
font_manager.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
#Snaildash is a small game created in the scope of a school project
#Copyright (C) 2022 Louis HEREDERO & Mathéo BENEY
import os
import pygame
class FontManager:
"""Manages fonts using a cache"""
cache = {}
def get(font, size, bold=False, italic=False):
"""Loads a font or get it from the cache
Args:
font (str): font name
size (int): font size
bold (bool, optional): True for bold text. Defaults to False.
italic (bool, optional): True for italic text. Defaults to False.
Returns:
pygame.font.Font: the corresponding font
"""
id_ = (font, size, bold, italic)
if not id_ in FontManager.cache:
if font.startswith("file:"):
path = os.path.join("assets", "fonts", font.split(":", 1)[1])
FontManager.cache[id_] = pygame.font.Font(path, size)
else:
FontManager.cache[id_] = pygame.font.SysFont(font, size, bold=bold, italic=italic)
return FontManager.cache[id_]