Skip to content

Commit 63bfcd5

Browse files
committed
Make an OOP implementation
1 parent db95cbe commit 63bfcd5

File tree

3 files changed

+61
-82
lines changed

3 files changed

+61
-82
lines changed

src/pkgIndex.tcl

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,6 @@
1-
package ifneeded extrafont 1.2 [list apply { dir {
2-
package require Tk
3-
4-
if {![info exists $dir]} {
5-
set dir [pwd]
6-
}
7-
8-
set os $::tcl_platform(platform)
9-
switch -- $os {
10-
windows { set os win }
11-
unix {
12-
switch -- $::tcl_platform(os) {
13-
Darwin { set os darwin }
14-
Linux { set os linux }
15-
}
16-
}
17-
}
18-
set libfile libextrafont[info sharedlibextension]
19-
# Try to guess the tcl-interpreter architecture (32/64 bit) ...
20-
set arch $::tcl_platform(pointerSize)
21-
switch -- $arch {
22-
4 { set arch x32 }
23-
8 { set arch x64 }
24-
default { error "extrafont: Unsupported architecture: Unexpected pointer-size $arch"}
25-
}
26-
27-
28-
set libfile_abspath [file join [file normalize $dir] $libfile]
29-
load $libfile_abspath
30-
31-
namespace eval extrafont {}
32-
source [file join $dir extrafont.tcl]
33-
source [file join $dir futmp.tcl]
34-
35-
package provide extrafont 1.2
36-
37-
}} $dir] ;# end of lambda apply
1+
package ifneeded extrafont 1.2 \
2+
"pwd;
3+
source extrafont.tcl; \
4+
source futmp.tcl; \
5+
load [file join [list [pwd]] libextrafont[info sharedlibextension]];
6+
package provide extrafont 1.2;"

tests/test_tkextrafont.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ class TestTkExtraFont(TestCase):
1616

1717
def setUp(self):
1818
self.window = tk.Tk()
19-
tkextrafont.load_extrafont(self.window)
19+
tkextrafont.load(self.window)
2020

2121
def test_font_load(self):
22-
assert not self.window.is_font_available("Overhaul")
23-
loaded = set(self.window.loaded_fonts())
24-
self.window.load_font(os.path.join(self.PATH, "overhaul.ttf"))
25-
assert len(list(set(self.window.loaded_fonts()) - loaded)) != 0
26-
assert self.window.is_font_available("Overhaul")
27-
assert "Overhaul" in self.window.loaded_fonts()
22+
font = tkextrafont.Font()
23+
assert not font.is_font_available("Overhaul")
24+
loaded = set(font.loaded_fonts())
25+
loaded_font = tkextrafont.Font(file=os.path.join(self.PATH, "overhaul.ttf"))
26+
assert len(list(set(font.loaded_fonts()) - loaded)) != 0
27+
assert font.is_font_available("Overhaul")
28+
assert "Overhaul" in font.loaded_fonts()
2829

2930
label = tk.Label(self.window, text="Overhaul font", font=("Overhaul", 12, "bold"))
3031
label.pack()

tkextrafont/__init__.py

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88
<https://sourceforge.net/projects/irrational-numbers/>
99
1010
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.
1912
"""
2013
import tkinter as tk
14+
import tkinter.font as tkfont
2115
# Standard Library
2216
from contextlib import contextmanager
2317
import os
@@ -74,36 +68,51 @@ def get_file_directory():
7468
return os.path.dirname(_FILE_DIR)
7569

7670

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
9674
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+
"""
10077

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):
103116
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

Comments
 (0)