|
8 | 8 | <https://sourceforge.net/projects/irrational-numbers/> |
9 | 9 |
|
10 | 10 | Provides interface to load the extrafont library into a Tk instance by |
11 | | -loading it into the Tk interpreter. After loading additional functions |
12 | | -are available on the Tk instance: |
13 | | -
|
14 | | -load_font: Load a font file into the Tk interpreter |
15 | | -unload_font: Unload a font family from the Tk interpreter |
16 | | -loaded_fonts: Return a List of font families loaded with extrafont |
17 | | -font_info: Return information on the font contained in a font file |
18 | | -is_font_available: Return whether a font is available for usage |
| 11 | +loading it into the Tk interpreter. |
19 | 12 | """ |
20 | 13 | import tkinter as tk |
| 14 | +import tkinter.font as tkfont |
21 | 15 | # Standard Library |
22 | 16 | from contextlib import contextmanager |
23 | 17 | import os |
@@ -74,36 +68,51 @@ def get_file_directory(): |
74 | 68 | return os.path.dirname(_FILE_DIR) |
75 | 69 |
|
76 | 70 |
|
77 | | -def load_extrafont(window): |
78 | | - # type: (tk.Tk) -> None |
79 | | - """Load extrafont into a tk interpreter and provide functions""" |
80 | | - |
81 | | - def load_font(file_name): |
82 | | - # type: (str) -> None |
83 | | - window.tk.call("extrafont::load", file_name) |
84 | | - |
85 | | - def unload_font(font_name): |
86 | | - # type: (str) -> None |
87 | | - window.tk.call("extrafont::unload", font_name) |
88 | | - |
89 | | - def loaded_fonts(): |
90 | | - # type: () -> list |
91 | | - return window.tk.call("extrafont::query", "families") |
92 | | - |
93 | | - def font_info(file_name): |
94 | | - # type: (str) -> List[Dict[str, str]] |
95 | | - return list(map(_tk_dict_to_dict, window.tk.call("extrafont::nameinfo", file_name))) |
| 71 | +class Font(tkfont.Font): |
| 72 | + """ |
| 73 | + tk.Font wrapper that allows loading fonts from a file |
96 | 74 |
|
97 | | - def is_font_available(font_name): |
98 | | - # type: (str) -> bool |
99 | | - return window.tk.call("extrafont::isAvailable", font_name) |
| 75 | + Loads tkextrafont if the package has not yet been loaded. |
| 76 | + """ |
100 | 77 |
|
101 | | - window.tk.eval("set dir {}".format(get_file_directory())) |
102 | | - with chdir(get_file_directory()): |
| 78 | + def __init__(self, root=None, font=None, name=None, exists=False, file=None, **options): |
| 79 | + """ |
| 80 | + :param file: Path to the file to load a font from |
| 81 | + """ |
| 82 | + if file is None: # May as well use normal tk.Font |
| 83 | + tkfont.Font.__init__(self, root, font, name, exists, **options) |
| 84 | + return |
| 85 | + self._file = file |
| 86 | + root = root or tk._default_root |
| 87 | + if root is None: |
| 88 | + raise tk.TclError("No Tk instance available to get interpreter from") |
| 89 | + if not getattr(root, "_tkextrafont_loaded", False): |
| 90 | + load(root) |
| 91 | + # Load the font file |
| 92 | + root.tk.call("extrafont::load", file) |
| 93 | + tkfont.Font.__init__(self, root, font, name, exists, **options) |
| 94 | + |
| 95 | + def unload(self): |
| 96 | + """Unload the current font""" |
| 97 | + self._tk.call("extrafont::unload", self.name) # self._tk available after tk.font.Font.__init__ |
| 98 | + |
| 99 | + def loaded_fonts(self) -> List[str]: |
| 100 | + """Return a list of fonts loaded with extrafont""" |
| 101 | + return self._tk.call("extrafont::query", "families") |
| 102 | + |
| 103 | + def font_info(self, fname: str) -> List[Dict[str, str]]: |
| 104 | + """Return info of a font file""" |
| 105 | + return list(map(_tk_dict_to_dict, self._tk.call("extrafont::nameinfo", fname))) |
| 106 | + |
| 107 | + def is_font_available(self, font_name) -> bool: |
| 108 | + """Return a boolean whether a font is available""" |
| 109 | + return self._tk.call("extrafont::isAvailable", font_name) |
| 110 | + |
| 111 | +def load(window: tk.Tk): |
| 112 | + """Load extrafont into a Tk interpreter""" |
| 113 | + local = os.path.abspath(os.path.dirname(__file__)) |
| 114 | + print(local) |
| 115 | + with chdir(local): |
103 | 116 | window.tk.eval("source pkgIndex.tcl") |
104 | | - window.tk.call("package", "require", "extrafont") |
105 | | - window.load_font = load_font |
106 | | - window.is_font_available = is_font_available |
107 | | - window.unload_font = unload_font |
108 | | - window.loaded_fonts = loaded_fonts |
109 | | - window.font_info = font_info |
| 117 | + window.tk.eval("package require extrafont") |
| 118 | + window._tkextrafont_loaded = True |
0 commit comments