ยปCore Development>Code coverage>Lib/lib-tk/tkFont.py

Python code coverage for Lib/lib-tk/tkFont.py

#countcontent
1n/a# Tkinter font wrapper
2n/a#
3n/a# written by Fredrik Lundh, February 1998
4n/a#
5n/a# FIXME: should add 'displayof' option where relevant (actual, families,
6n/a# measure, and metrics)
7n/a#
8n/a
9n/a__version__ = "0.9"
10n/a
11n/aimport Tkinter
12n/a
13n/a# weight/slant
14n/aNORMAL = "normal"
15n/aROMAN = "roman"
16n/aBOLD = "bold"
17n/aITALIC = "italic"
18n/a
19n/adef nametofont(name):
20n/a """Given the name of a tk named font, returns a Font representation.
21n/a """
22n/a return Font(name=name, exists=True)
23n/a
24n/aclass Font:
25n/a
26n/a """Represents a named font.
27n/a
28n/a Constructor options are:
29n/a
30n/a font -- font specifier (name, system font, or (family, size, style)-tuple)
31n/a name -- name to use for this font configuration (defaults to a unique name)
32n/a exists -- does a named font by this name already exist?
33n/a Creates a new named font if False, points to the existing font if True.
34n/a Raises _Tkinter.TclError if the assertion is false.
35n/a
36n/a the following are ignored if font is specified:
37n/a
38n/a family -- font 'family', e.g. Courier, Times, Helvetica
39n/a size -- font size in points
40n/a weight -- font thickness: NORMAL, BOLD
41n/a slant -- font slant: ROMAN, ITALIC
42n/a underline -- font underlining: false (0), true (1)
43n/a overstrike -- font strikeout: false (0), true (1)
44n/a
45n/a """
46n/a
47n/a def _set(self, kw):
48n/a options = []
49n/a for k, v in kw.items():
50n/a options.append("-"+k)
51n/a options.append(str(v))
52n/a return tuple(options)
53n/a
54n/a def _get(self, args):
55n/a options = []
56n/a for k in args:
57n/a options.append("-"+k)
58n/a return tuple(options)
59n/a
60n/a def _mkdict(self, args):
61n/a options = {}
62n/a for i in range(0, len(args), 2):
63n/a options[args[i][1:]] = args[i+1]
64n/a return options
65n/a
66n/a def __init__(self, root=None, font=None, name=None, exists=False, **options):
67n/a if not root:
68n/a root = Tkinter._default_root
69n/a if font:
70n/a # get actual settings corresponding to the given font
71n/a font = root.tk.splitlist(root.tk.call("font", "actual", font))
72n/a else:
73n/a font = self._set(options)
74n/a if not name:
75n/a name = "font" + str(id(self))
76n/a self.name = name
77n/a
78n/a if exists:
79n/a self.delete_font = False
80n/a # confirm font exists
81n/a if self.name not in root.tk.call("font", "names"):
82n/a raise Tkinter._tkinter.TclError, "named font %s does not already exist" % (self.name,)
83n/a # if font config info supplied, apply it
84n/a if font:
85n/a root.tk.call("font", "configure", self.name, *font)
86n/a else:
87n/a # create new font (raises TclError if the font exists)
88n/a root.tk.call("font", "create", self.name, *font)
89n/a self.delete_font = True
90n/a # backlinks!
91n/a self._root = root
92n/a self._split = root.tk.splitlist
93n/a self._call = root.tk.call
94n/a
95n/a def __str__(self):
96n/a return self.name
97n/a
98n/a def __eq__(self, other):
99n/a return self.name == other.name and isinstance(other, Font)
100n/a
101n/a def __getitem__(self, key):
102n/a return self.cget(key)
103n/a
104n/a def __setitem__(self, key, value):
105n/a self.configure(**{key: value})
106n/a
107n/a def __del__(self):
108n/a try:
109n/a if self.delete_font:
110n/a self._call("font", "delete", self.name)
111n/a except (KeyboardInterrupt, SystemExit):
112n/a raise
113n/a except Exception:
114n/a pass
115n/a
116n/a def copy(self):
117n/a "Return a distinct copy of the current font"
118n/a return Font(self._root, **self.actual())
119n/a
120n/a def actual(self, option=None):
121n/a "Return actual font attributes"
122n/a if option:
123n/a return self._call("font", "actual", self.name, "-"+option)
124n/a else:
125n/a return self._mkdict(
126n/a self._split(self._call("font", "actual", self.name))
127n/a )
128n/a
129n/a def cget(self, option):
130n/a "Get font attribute"
131n/a return self._call("font", "config", self.name, "-"+option)
132n/a
133n/a def config(self, **options):
134n/a "Modify font attributes"
135n/a if options:
136n/a self._call("font", "config", self.name,
137n/a *self._set(options))
138n/a else:
139n/a return self._mkdict(
140n/a self._split(self._call("font", "config", self.name))
141n/a )
142n/a
143n/a configure = config
144n/a
145n/a def measure(self, text):
146n/a "Return text width"
147n/a return int(self._call("font", "measure", self.name, text))
148n/a
149n/a def metrics(self, *options):
150n/a """Return font metrics.
151n/a
152n/a For best performance, create a dummy widget
153n/a using this font before calling this method."""
154n/a
155n/a if options:
156n/a return int(
157n/a self._call("font", "metrics", self.name, self._get(options))
158n/a )
159n/a else:
160n/a res = self._split(self._call("font", "metrics", self.name))
161n/a options = {}
162n/a for i in range(0, len(res), 2):
163n/a options[res[i][1:]] = int(res[i+1])
164n/a return options
165n/a
166n/adef families(root=None):
167n/a "Get font families (as a tuple)"
168n/a if not root:
169n/a root = Tkinter._default_root
170n/a return root.tk.splitlist(root.tk.call("font", "families"))
171n/a
172n/adef names(root=None):
173n/a "Get names of defined fonts (as a tuple)"
174n/a if not root:
175n/a root = Tkinter._default_root
176n/a return root.tk.splitlist(root.tk.call("font", "names"))
177n/a
178n/a# --------------------------------------------------------------------
179n/a# test stuff
180n/a
181n/aif __name__ == "__main__":
182n/a
183n/a root = Tkinter.Tk()
184n/a
185n/a # create a font
186n/a f = Font(family="times", size=30, weight=NORMAL)
187n/a
188n/a print f.actual()
189n/a print f.actual("family")
190n/a print f.actual("weight")
191n/a
192n/a print f.config()
193n/a print f.cget("family")
194n/a print f.cget("weight")
195n/a
196n/a print names()
197n/a
198n/a print f.measure("hello"), f.metrics("linespace")
199n/a
200n/a print f.metrics()
201n/a
202n/a f = Font(font=("Courier", 20, "bold"))
203n/a print f.measure("hello"), f.metrics("linespace")
204n/a
205n/a w = Tkinter.Label(root, text="Hello, world", font=f)
206n/a w.pack()
207n/a
208n/a w = Tkinter.Button(root, text="Quit!", command=root.destroy)
209n/a w.pack()
210n/a
211n/a fb = Font(font=w["font"]).copy()
212n/a fb.config(weight=BOLD)
213n/a
214n/a w.config(font=fb)
215n/a
216n/a Tkinter.mainloop()