-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfont_loader.py
More file actions
46 lines (37 loc) · 1.6 KB
/
font_loader.py
File metadata and controls
46 lines (37 loc) · 1.6 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
import json
import os
import numpy as np
import util
class Font:
"""Represents a font loaded from a JSON file and made of pixels"""
font_dict = {}
_first_character = ""
def __init__(self, font_dict):
self.font_dict = font_dict
self._first_character = self.get_available_characters()[0]
def get_available_characters(self) -> list[str]:
"""Get a list of characters present in the font"""
return list(self.font_dict.keys())
def get_character(self, character) -> np.matrix:
"""Get a matrix representing a character in the font"""
return np.matrix(self.font_dict[character])
def get_char_width(self) -> int:
"""Get the width of each character"""
return len(self.font_dict[self._first_character][0])
def get_char_height(self) -> int:
"""Get the height of each character"""
return len(self.font_dict[self._first_character])
def __getitem__(self, key):
"""Get a character from the font"""
return self.font_dict[str(key)]
def load_font(path) -> Font:
"""Creates a font object from a given font path"""
with open(path, "r") as font_file:
content = json.loads(font_file.read())
return Font(content)
def load_preloaded_font(font_name) -> Font:
"""Creates a font object by loading it from the internal font folder"""
return load_font(util.get_file_path(os.path.join("fonts", font_name))+ ".json")
def get_fonts() -> list[str]:
"""Get a list of fonts in the font folder"""
return [x.removesuffix(".json") for x in os.listdir(util.get_file_path("fonts"))]