forked from gremlation/ComfyUI-ImageLabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont_manager.py
77 lines (60 loc) · 2.46 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
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
import logging
from pathlib import Path
from typing import Tuple
from PIL.ImageFont import FreeTypeFont, load_default as load_default_font, truetype as load_truetype
from folder_paths import get_folder_paths
default_font_path = Path(get_folder_paths("custom_nodes")[0]) / "comfyui-imagelabel" / "fonts"
class FontCollection(dict):
"""
A dictionary that maps font names to PIL font objects.
"""
def __init__(self, directory: Path = default_font_path):
"""
Initialize the FontCollection with fonts found in the given directory (including subdirectories).
Args:
directory (Path): The path to the directory containing font files.
"""
paths = [font for font in directory.rglob("*.[tT][tT][fF]") if font.is_file()]
self.default_font_name, self.default_font = self.load_default_font()
fonts = {
self.default_font_name: self.default_font,
}
for path in paths:
font_info = self.load_font(path)
if font_info:
if font_info[0] in fonts:
logging.warning(f"Fonts with duplicate names found: {font_info[0]}")
fonts[font_info[0]] = font_info[1]
super().__init__(fonts)
@classmethod
def load_default_font(cls) -> Tuple[str, FreeTypeFont]:
"""
Load the default PIL font.
Returns:
tuple[str, FreeTypeFont]: The font's name and the font object.
"""
font = load_default_font()
family, style = None, None
if not isinstance(font, FreeTypeFont):
raise RuntimeError("Could not load default FreeType font.")
family, style = font.getname()
family = family or "Unknown"
style = style or "Regular"
return " ".join([family, style]), font
@classmethod
def load_font(cls, path: Path) -> Tuple[str, FreeTypeFont]:
"""
Load a font and extract its name and style.
Args:
path (Path): The path to the font file.
Returns:
tuple[str, ImageFont.FreeTypeFont]: A tuple containing the font's name and the font object.
Raises:
OSError: If the font file could not be read.
ValueError: If the font size is not greater than zero.
"""
font = load_truetype(path)
family, style = font.getname()
family = family or "Unknown"
style = style or "Regular"
return " ".join([family, style]), font