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