Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Lib / lib-tk / tkFont.py
blob5e9abed66a028e4b899f292d21945d27d77054ee
2 # Tkinter
3 # $Id$
5 # font wrapper
7 # written by Fredrik Lundh <fredrik@pythonware.com>, February 1998
9 # FIXME: should add 'displayof' option where relevant (actual, families,
10 # measure, and metrics)
12 # Copyright (c) Secret Labs AB 1998.
14 # info@pythonware.com
15 # http://www.pythonware.com
18 __version__ = "0.9"
20 import Tkinter
21 import string
23 # weight/slant
24 NORMAL = "normal"
25 BOLD = "bold"
26 ITALIC = "italic"
28 class Font:
30 """Represents a named font.
32 Constructor options are:
34 font -- font specifier (name, system font, or (family, size, style)-tuple)
36 or any combination of
38 family -- font 'family', e.g. Courier, Times, Helvetica
39 size -- font size in points
40 weight -- font thickness: NORMAL, BOLD
41 slant -- font slant: NORMAL, ITALIC
42 underline -- font underlining: false (0), true (1)
43 overstrike -- font strikeout: false (0), true (1)
44 name -- name to use for this font configuration (defaults to a unique name)
45 """
47 def _set(self, kw):
48 options = []
49 for k, v in kw.items():
50 options.append("-"+k)
51 options.append(str(v))
52 return tuple(options)
54 def _get(self, args):
55 options = []
56 for k in args:
57 options.append("-"+k)
58 return tuple(options)
60 def _mkdict(self, args):
61 options = {}
62 for i in range(0, len(args), 2):
63 options[args[i][1:]] = args[i+1]
64 return options
66 def __init__(self, root=None, font=None, name=None, **options):
67 if not root:
68 root = Tkinter._default_root
69 if font:
70 # get actual settings corresponding to the given font
71 font = root.tk.splitlist(root.tk.call("font", "actual", font))
72 else:
73 font = self._set(options)
74 if not name:
75 name = "font" + str(id(self))
76 self.name = name
77 apply(root.tk.call, ("font", "create", name) + font)
78 # backlinks!
79 self._root = root
80 self._split = root.tk.splitlist
81 self._call = root.tk.call
83 def __str__(self):
84 return self.name
86 def __del__(self):
87 try:
88 self._call("font", "delete", self.name)
89 except (AttributeError, Tkinter.TclError):
90 pass
92 def copy(self):
93 "Return a distinct copy of the current font"
94 return apply(Font, (self._root,), self.actual())
96 def actual(self, option=None):
97 "Return actual font attributes"
98 if option:
99 return self._call("font", "actual", self.name, "-"+option)
100 else:
101 return self._mkdict(
102 self._split(self._call("font", "actual", self.name))
105 def cget(self, option):
106 "Get font attribute"
107 return self._call("font", "config", self.name, "-"+option)
109 def config(self, **options):
110 "Modify font attributes"
111 if options:
112 apply(self._call, ("font", "config", self.name) +
113 self._set(options))
114 else:
115 return self._mkdict(
116 self._split(self._call("font", "config", self.name))
119 configure = config
121 def measure(self, text):
122 "Return text width"
123 return string.atoi(self._call("font", "measure", self.name, text))
125 def metrics(self, *options):
126 """Return font metrics.
128 For best performance, create a dummy widget
129 using this font before calling this method."""
131 if options:
132 return string.atoi(
133 self._call("font", "metrics", self.name, self._get(options))
135 else:
136 res = self._split(self._call("font", "metrics", self.name))
137 options = {}
138 for i in range(0, len(res), 2):
139 options[res[i][1:]] = string.atoi(res[i+1])
140 return options
142 def families(root=None):
143 "Get font families (as a tuple)"
144 if not root:
145 root = Tkinter._default_root
146 return root.tk.splitlist(root.tk.call("font", "families"))
148 def names(root=None):
149 "Get names of defined fonts (as a tuple)"
150 if not root:
151 root = Tkinter._default_root
152 return root.tk.splitlist(root.tk.call("font", "names"))
154 # --------------------------------------------------------------------
155 # test stuff
157 if __name__ == "__main__":
159 root = Tkinter.Tk()
161 # create a font
162 f = Font(family="times", size=30, weight=NORMAL)
164 print f.actual()
165 print f.actual("family")
166 print f.actual("weight")
168 print f.config()
169 print f.cget("family")
170 print f.cget("weight")
172 print names()
174 print f.measure("hello"), f.metrics("linespace")
176 print f.metrics()
178 f = Font(font=("Courier", 20, "bold"))
179 print f.measure("hello"), f.metrics("linespace")
181 w = Tkinter.Label(root, text="Hello, world", font=f)
182 w.pack()
184 w = Tkinter.Button(root, text="Quit!", command=root.destroy)
185 w.pack()
187 fb = Font(font=w["font"]).copy()
188 fb.config(weight=BOLD)
190 w.config(font=fb)
192 Tkinter.mainloop()