1Font(3) User Contributed Perl Documentation Font(3)
2
3
4
6 Tk::Font - Create and inspect fonts.
7
9 $widget->Font(option?, arg, arg, ...?)
10
11 $font->Option?(arg, arg, ...)?
12
14 The Font method provides several facilities for dealing with fonts,
15 such as defining named fonts and inspecting the actual attributes of a
16 font. The command has several different forms, determined by the first
17 argument. The following forms are currently supported:
18
19 $font->actual(-option?)
20 $widget->fontActual(font?, -option?)
21 Returns information about the actual attributes that are obtained
22 when font is used on $font's display; the actual attributes
23 obtained may differ from the attributes requested due to platform-
24 dependant limitations, such as the availability of font families
25 and pointsizes. font is a font description; see "FONT DESCRIPTION"
26 below. If option is specified, returns the value of that
27 attribute; if it is omitted, the return value is a list of all the
28 attributes and their values. See "FONT OPTIONS" below for a list
29 of the possible attributes.
30
31 $font->configure(-option??=>value, -option=>value, ...?)
32 Query or modify the desired attributes for $font. If no -option is
33 specified, returns a list describing all the options and their
34 values for fontname. If a single -option is specified with no
35 value, then returns the current value of that attribute. If one or
36 more option-value pairs are specified, then the method modifies the
37 given named font to have the given values; in this case, all
38 widgets using that font will redisplay themselves using the new
39 attributes for the font. See "FONT OPTIONS" below for a list of
40 the possible attributes.
41
42 Note: the above behaviour differs in detail to configure on
43 widgets, images etc.
44
45 $font = $widget->Font(-option=>value, ...>?)
46 $font = $widget->fontCreate(?fontname??, -option=>value, ...>?)
47 Creates a new font object and returns a reference to it. fontname
48 specifies the name for the font; if it is omitted, then Tk
49 generates a new name of the form fontx, where x is an integer.
50 There may be any number of option-value pairs, which provide the
51 desired attributes for the new named font. See "FONT OPTIONS"
52 below for a list of the possible attributes.
53
54 Note: the created font is not shared between widgets of different
55 MainWindows.
56
57 $font->delete
58 $widget->fontDelete(fontname?, fontname, ...?)
59 Delete the specified named fonts. If there are widgets using the
60 named font, the named font won't actually be deleted until all the
61 instances are released. Those widgets will continue to display
62 using the last known values for the named font. If a deleted named
63 font is subsequently recreated with another call to fontCreate, the
64 widgets will use the new named font and redisplay themselves using
65 the new attributes of that font.
66
67 $widget->fontFamilies
68 The return value is a list of the case-insensitive names of all
69 font families that exist on $widget's display.
70
71 $font->measure(text)
72 $widget->fontMeasure(font, text)
73 Measures the amount of space the string text would use in the given
74 font when displayed in $widget. font is a font description; see
75 "FONT DESCRIPTION" below. The return value is the total width in
76 pixels of text, not including the extra pixels used by highly
77 exagerrated characters such as cursive ``f''. If the string
78 contains newlines or tabs, those characters are not expanded or
79 treated specially when measuring the string.
80
81 $font->metrics(-option?)
82 $widget->fontMetrics(font?, -option?)
83 Returns information about the metrics (the font-specific data), for
84 font when it is used on $widget's display. font is a font
85 description; see "FONT DESCRIPTION" below. If option is specified,
86 returns the value of that metric; if it is omitted, the return
87 value is a list of all the metrics and their values. See "FONT
88 METRICS" below for a list of the possible metrics.
89
90 $widget->fontNames
91 The return value is a list of all font objects that are currently
92 defined for $widget's MainWindow.
93
95 The following formats are accepted as a font description anywhere font
96 is specified as an argument above; these same forms are also permitted
97 when specifying the -font option for widgets.
98
99 [1] fontname
100 The name of a named font, created using the fontCreate method.
101 When a widget uses a named font, it is guaranteed that this will
102 never cause an error, as long as the named font exists, no matter
103 what potentially invalid or meaningless set of attributes the named
104 font has. If the named font cannot be displayed with exactly the
105 specified attributes, some other close font will be substituted
106 automatically.
107
108 [1a] $font
109 A font object created using the Font method. This is essentially
110 the same as using a named font. The object is a reference to the
111 name, and carries additional information e.g. which MainWindow it
112 relates to in an manner peculiar to perl/Tk.
113
114 [2] systemfont
115 The platform-specific name of a font, interpreted by the graphics
116 server. This also includes, under X, an XLFD (see [4]) for which a
117 single ``*'' character was used to elide more than one field in the
118 middle of the name. See "PLATFORM-SPECIFIC ISSUES" for a list of
119 the system fonts.
120
121 [3] [family,?size,??style,??style ...?]
122 A properly formed list whose first element is the desired font
123 family and whose optional second element is the desired size. The
124 interpretation of the size attribute follows the same rules
125 described for -size in "FONT OPTIONS" below. Any additional
126 optional arguments following the size are font styles. Possible
127 values for the style arguments are as follows:
128
129 normal bold roman italic
130 underline overstrike
131
132 [4] X-font names (XLFD)
133 A Unix-centric font name of the form
134 -foundry-family-weight-slant-setwidth-addstyle-pixel-point-resx-resy-spacing-width-charset-encoding.
135 The ``*'' character may be used to skip individual fields that the
136 user does not care about. There must be exactly one ``*'' for each
137 field skipped, except that a ``*'' at the end of the XLFD skips any
138 remaining fields; the shortest valid XLFD is simply ``*'',
139 signifying all fields as defaults. Any fields that were skipped
140 are given default values. For compatibility, an XLFD always
141 chooses a font of the specified pixel size (not point size);
142 although this interpretation is not strictly correct, all existing
143 applications using XLFDs assumed that one ``point'' was in fact one
144 pixel and would display incorrectly (generally larger) if the
145 correct size font were actually used.
146
147 [5] option value ?option value ...?
148 A properly formed list of option-value pairs that specify the
149 desired attributes of the font, in the same format used when
150 defining a named font; see "FONT OPTIONS" below.
151
152 When font description font is used, the system attempts to parse the
153 description according to each of the above five rules, in the order
154 specified. Cases [1] and [2] must match the name of an existing named
155 font or of a system font. Cases [3], [4], and [5] are accepted on all
156 platforms and the closest available font will be used. In some
157 situations it may not be possible to find any close font (e.g., the
158 font family was a garbage value); in that case, some system-dependant
159 default font is chosen. If the font description does not match any of
160 the above patterns, an error is generated.
161
163 The following options are used by the metrics/fontMetrics method to
164 query font-specific data determined when the font was created. These
165 properties are for the whole font itself and not for individual
166 characters drawn in that font. In the following definitions, the
167 ``baseline'' of a font is the horizontal line where the bottom of most
168 letters line up; certain letters, such as lower-case ``g'' stick below
169 the baseline.
170
171 -ascent
172 The amount in pixels that the tallest letter sticks up above the
173 baseline of the font, plus any extra blank space added by the
174 designer of the font. ($font->ascent is provided for
175 compatibility.)
176
177 -descent
178 The largest amount in pixels that any letter sticks down below the
179 baseline of the font, plus any extra blank space added by the
180 designer of the font. ($font->descent is provided for
181 compatibility.)
182
183 -linespace
184 Returns how far apart vertically in pixels two lines of text using
185 the same font should be placed so that none of the characters in
186 one line overlap any of the characters in the other line. This is
187 generally the sum of the ascent above the baseline line plus the
188 descent below the baseline.
189
190 -fixed
191 Returns a boolean flag that is ``1'' if this is a fixed-width font,
192 where each normal character is the the same width as all the other
193 characters, or is ``0'' if this is a proportionally-spaced font,
194 where individual characters have different widths. The widths of
195 control characters, tab characters, and other non-printing
196 characters are not included when calculating this value.
197
199 The following options are supported on all platforms, and are used when
200 constructing a named font or when specifying a font using style [5] as
201 above:
202
203 -family => name
204 The case-insensitive font family name. Tk guarantees to support
205 the font families named Courier (a monospaced ``typewriter'' font),
206 Times (a serifed ``newspaper'' font), and Helvetica (a sans-serif
207 ``European'' font). The most closely matching native font family
208 will automatically be substituted when one of the above font
209 families is used. The name may also be the name of a native,
210 platform-specific font family; in that case it will work as desired
211 on one platform but may not display correctly on other platforms.
212 If the family is unspecified or unrecognized, a platform-specific
213 default font will be chosen.
214
215 -size => size
216 The desired size of the font. If the size argument is a positive
217 number, it is interpreted as a size in points. If size is a
218 negative number, its absolute value is interpreted as a size in
219 pixels. If a font cannot be displayed at the specified size, a
220 nearby size will be chosen. If size is unspecified or zero, a
221 platform-dependent default size will be chosen.
222
223 The original Tcl/Tk authors believe sizes should normally be
224 specified in points so the application will remain the same ruler
225 size on the screen, even when changing screen resolutions or moving
226 scripts across platforms. While this is an admirable goal it does
227 not work as well in practice as they hoped. The mapping between
228 points and pixels is set when the application starts, based on
229 alleged properties of the installed monitor, but it can be
230 overridden by calling the scaling command. However this can be
231 problematic when system has no way of telling if (say) an 11" or
232 22" monitor is attached, also if it can tell then some monitor
233 sizes may result in poorer quality scaled fonts being used rather
234 than a "tuned" bitmap font. In addition specifying pixels is
235 useful in certain circumstances such as when a piece of text must
236 line up with respect to a fixed-size bitmap.
237
238 At present the Tcl/Tk scheme is used unchanged, with "point" size
239 being returned by actual (as an integer), and used internally.
240 Suggestions for work-rounds to undesirable behaviour welcome.
241
242 -weight => weight
243 The nominal thickness of the characters in the font. The value
244 normal specifies a normal weight font, while bold specifies a bold
245 font. The closest available weight to the one specified will be
246 chosen. The default weight is normal.
247
248 -slant => slant
249 The amount the characters in the font are slanted away from the
250 vertical. Valid values for slant are roman and italic. A roman
251 font is the normal, upright appearance of a font, while an italic
252 font is one that is tilted some number of degrees from upright.
253 The closest available slant to the one specified will be chosen.
254 The default slant is roman.
255
256 -underline => boolean
257 The value is a boolean flag that specifies whether characters in
258 this font should be underlined. The default value for underline is
259 false.
260
261 -overstrike => boolean
262 The value is a boolean flag that specifies whether a horizontal
263 line should be drawn through the middle of characters in this font.
264 The default value for overstrike is false.
265
267 The following named system fonts are supported:
268
269 X Windows:
270 All valid X font names, including those listed by xlsfonts(1), are
271 available.
272
273 MS Windows:
274 system ansi device
275 systemfixed ansifixed oemfixed
276
277 Macintosh:
278 system application
279
281 In prior versions of perl/Tk the $widget->Font method was a perl
282 wrapper on the original "[4] X-font names (XLFD)" style as described
283 above (which was the only form supported by versions of core tk prior
284 to version tk8.0). This module is provided in its original form (it
285 has just been renamed) via:
286
287 use Tk::X11Font;
288 I<$widget>-E<gt>B<X11Font>(...)
289
290 However the methods of the old scheme have been mimiced as closely as
291 possible with the new scheme. It is intended that code should work
292 without modification, except for the case of using :
293
294 @names = $font->Name;
295
296 i.e. the Name method in an array/list context. This now returns one
297 element on all platforms (as it did on Win32), while previously on X
298 systems it returned a list of fonts that matched an under-specified
299 pattern.
300
301 Briefly the methods supported for compatibilty are as follows:
302
303 $newfont = $font->Clone(-option=>value, ...>?)
304 Returns a new font object $newfont related to the original $font by
305 changing the values of the specified -options.
306
307 $font->Family - maps to -family
308 $font->Weight - maps to -weight
309 $font->Slant - maps to -slant
310 $font->Pixel and Point - map to -size
311
312 New code should use $font->configure to achieve same effect as last
313 four items above.
314
315 Foundry, Swidth, Adstyle, Xres, Yres, Space, Avgwidth, Registry,
316 Encoding
317 Are all ignored if set, and return '*' if queried.
318
319 $font->Name
320 Returns the name of a named font, or a string representation of an
321 unnamed font. Using $font in a scalar context does the same. Note
322 this is distinctly different from behaviour of X11Font's Name in a
323 list context.
324
325 $font->Pattern
326 Returns a XLFD string for the font based on actual values, and some
327 heuristics to map Tk's forms to the "standard" X conventions.
328
330 Tk::options
331
332 Tk::X11Font
333
335 font
336
337
338
339perl v5.36.0 2022-07-22 Font(3)