Font(3pm)

πŸ’– NAME πŸ’–

Tk::Font β€” create and inspect fonts. that's it, that's the whole vibe ✨


πŸŽ€ SYNOPSIS πŸŽ€

$widget->Font(option?, arg, arg, ...?)

$font->Option?(arg, arg, ...)?

πŸ¦‹ DESCRIPTION πŸ¦‹

okay bestie, so the Font method is literally your one-stop shop for everything font-related: defining named fonts, snooping on the actual attributes of a font, all of it. The command has a bunch of different forms, and which one you get is decided by the very first argument. Here's what's currently supported, no cap πŸ‘‘

πŸ’« $font->actual(-option?)

πŸ’« $widget->fontActual(font?, -option?)

This one hands you the tea about the actual attributes you get when font is used on $font's display. and here's the thing β€” the actual attributes can totally differ from the ones you asked for, because platforms have ✨limitations✨, like maybe that font family doesn't even exist, or that pointsize isn't available. font is a font description; peep "FONT DESCRIPTION" below. If option is specified, you get the value of that one attribute back; if you leave it out, you get a list of all the attributes and their values. The possible attributes are listed in "FONT OPTIONS" below 🫢

πŸ’« $font->configure(-option??=>value, -option=>value, ...?)

Query or modify the desired attributes for $font. If no -option is given, you get back a list describing every option and its value for fontname. If you give exactly one -option with no value, you get the current value of that attribute. And if you pass one or more option-value pairs, the method modifies the given named font to have those values β€” and in that case, every widget using that font will redisplay itself with the new attributes. See "FONT OPTIONS" below for the list of possible attributes.

⚠️ Note: the behaviour above differs in detail from configure on widgets, images etc. it's giving "similar but not the same", so don't get it twisted πŸ’€

πŸ’« $font = $widget->Font(-option=>value, ...>?)

πŸ’« $font = $widget->fontCreate(?fontname??, -option=>value, ...>?)

Creates a brand new font object and returns a reference to it. πŸŽ‰ fontname is the name for the font; if you omit it, Tk just makes one up of the form fontx, where x is an integer. You can throw in any number of option-value pairs to give the new named font the attributes you want. Possible attributes live in "FONT OPTIONS" below.

⚠️ Note: the created font is not shared between widgets of different MainWindows. keep that in mind bestie.

πŸ’« $font->delete

πŸ’« $widget->fontDelete(fontname?, fontname, ...?)

Deletes the specified named fonts. but listen β€” if there are widgets still using that named font, it won't actually get deleted until all the instances are released. those widgets keep displaying with the last known values for the named font. and if a deleted named font gets recreated later with another call to fontCreate, the widgets will use the new named font and redisplay themselves with the new attributes. it's giving resurrection arc 🌸

πŸ’« $widget->fontFamilies

Returns a list of the case-insensitive names of every font family that exists on $widget's display. all of them. the whole family reunion πŸ’…

πŸ’« $font->measure(text)

πŸ’« $widget->fontMeasure(font, text)

Measures how much space the string text would use in the given font when displayed in $widget. font is a font description; see "FONT DESCRIPTION" below. The return value is the total width in pixels of text, and it does not include the extra pixels used by highly exagerrated characters such as cursive ``f''. If the string contains newlines or tabs, those characters are not expanded or treated specially when measuring the string. no special treatment, they're just there 😭

πŸ’« $font->metrics(-option?)

πŸ’« $widget->fontMetrics(font?, -option?)

Returns info about the metrics (the font-specific data) for font when it's used on $widget's display. font is a font description; see "FONT DESCRIPTION" below. If option is specified, you get the value of that metric; if it's omitted, you get a list of all the metrics and their values. See "FONT METRICS" below for the list of possible metrics.

πŸ’« $widget->fontNames

Returns a list of all font objects that are currently defined for $widget's MainWindow. the whole roster 🫢


🌈 FONT DESCRIPTION 🌈

The following formats are all accepted as a font description, anywhere font is specified as an argument up above. and btw, these same forms are also allowed when you're specifying the -font option for widgets. so learn them once, use them everywhere ✨

πŸ’œ [1] fontname

The name of a named font, created using the fontCreate method. When a widget uses a named font, it is guaranteed that this will never cause an error, as long as the named font exists β€” no matter how potentially invalid or meaningless the set of attributes on that named font is. iconic behavior tbh. If the named font can't be displayed with exactly the specified attributes, some other close font gets substituted automatically. no drama πŸ’…

πŸ’œ [1a] $font

A font object created using the Font method. This is essentially the same as using a named font. The object is a reference to the name, and it carries additional information, e.g. which MainWindow it relates to, in a manner peculiar to perl/Tk. very iykyk 🧚

πŸ’œ [2] systemfont

The platform-specific name of a font, interpreted by the graphics server. This also includes, under X, an XLFD (see [4]) for which a single ``*'' character was used to elide more than one field in the middle of the name. See "PLATFORM-SPECIFIC ISSUES" for a list of the system fonts.

πŸ’œ [3] [family,?size,??style,??style ...?]

A properly formed list whose first element is the desired font family and whose optional second element is the desired size. The interpretation of the size attribute follows the same rules described for -size in "FONT OPTIONS" below. Any additional optional arguments after the size are font styles. Possible values for the style arguments are:

normal      bold    roman   italic
underline   overstrike

πŸ’œ [4] X-font names (XLFD)

A Unix-centric font name of the form

-foundry-family-weight-slant-setwidth-addstyle-pixel-point-resx-resy-spacing-width-charset-encoding.

The *'' character can be used to skip individual fields that the user does not care about. There must be exactly one '' for each field skipped, except that a *'' at the end of the XLFD skips any remaining fields; the shortest valid XLFD is simply '', signifying all fields as defaults. Any fields that were skipped get default values. For compatibility, an XLFD always chooses a font of the specified pixel size (not point size); although this interpretation isn't strictly correct, all existing applications using XLFDs assumed that one ``point'' was in fact one pixel, and would display incorrectly (generally larger) if the correct size font were actually used. the way legacy code just keeps on haunting us 😭

πŸ’œ [5] option value ?option value ...?

A properly formed list of option-value pairs that specify the desired attributes of the font, in the same format used when defining a named font; see "FONT OPTIONS" below.


okay so when a font description font is used, the system attempts to parse the description according to each of the above five rules, in the order specified. Cases [1] and [2] must match the name of an existing named font or of a system font. Cases [3], [4], and [5] are accepted on all platforms and the closest available font will be used. In some situations it may not be possible to find any close font (e.g., the font family was a garbage value); in that case, some system-dependant default font is chosen. And if the font description doesn't match any of the above patterns, an error is generated. periodt πŸ’€


🧚 FONT METRICS 🧚

The following options are used by the metrics/fontMetrics method to query font-specific data determined when the font was created. These properties are for the whole font itself, not for individual characters drawn in that font. And just so we're on the same page: in the definitions below, the baseline'' of a font is the horizontal line where the bottom of most letters line up; certain letters, such as lower-case g'', stick below the baseline. 🫢

  • -ascent β€” The amount in pixels that the tallest letter sticks up above the baseline of the font, plus any extra blank space added by the designer of the font. ($font->ascent is provided for compatibility.)
  • -descent β€” The largest amount in pixels that any letter sticks down below the baseline of the font, plus any extra blank space added by the designer of the font. ($font->descent is provided for compatibility.)
  • -linespace β€” Returns how far apart vertically in pixels two lines of text using the same font should be placed so that none of the characters in one line overlap any of the characters in the other line. This is generally the sum of the ascent above the baseline line plus the descent below the baseline.
  • -fixed β€” Returns a boolean flag that is 1'' if this is a fixed-width font, where each normal character is the the same width as all the other characters, or is 0'' if this is a proportionally-spaced font, where individual characters have different widths. The widths of control characters, tab characters, and other non-printing characters are not included when calculating this value.

πŸŽ€ FONT OPTIONS πŸŽ€

Okay bestie, these ones work on every single platform, and you use them when you're constructing a named font or when you're specifying a font with style [5] up above πŸ‘†:

  • -family => name β€” the font family name, and it's case-insensitive, so don't stress the caps. Tk pinky-promises to support the families named Courier (a monospaced "typewriter" font), Times (a serifed "newspaper" font), and Helvetica (a sans-serif "European" font). When you use one of those, the most closely matching native font family gets swapped in automatically ✨. The name can also be a native, platform-specific font family β€” but lowkey, that'll work exactly how you want on one platform and may not display correctly on the others πŸ’€. If the family is unspecified or unrecognized, a platform-specific default font gets chosen for you.
  • -size => size β€” the desired size of the font, and the sign matters bestie. If size is a positive number, it's read as a size in points. If size is negative, its absolute value is read as a size in pixels. If a font can't be displayed at the specified size, a nearby size will be chosen. And if size is unspecified or zero, a platform-dependent default size gets picked.

Now listen, the original Tcl/Tk authors believe sizes should normally be specified in points so the application stays the same ruler size on screen, even when you change screen resolutions or move scripts across platforms 🫢. It's an admirable goal, but ngl it doesn't work as well in practice as they hoped 😭. The mapping between points and pixels is set when the application starts, based on alleged properties of the installed monitor, but it can be overridden by calling the scaling command. However, this can be problematic when the system has no way of telling if (say) an 11" or 22" monitor is attached β€” and also, if it can tell, then some monitor sizes may result in poorer quality scaled fonts being used rather than a "tuned" bitmap font. On top of that, specifying pixels is useful in certain circumstances, such as when a piece of text must line up with respect to a fixed-size bitmap πŸ‘‘.

At present the Tcl/Tk scheme is used unchanged, with "point" size being returned by actual (as an integer), and used internally. Suggestions for work-rounds to undesirable behaviour are welcome, fr fr πŸ™.

  • -weight => weight β€” the nominal thickness of the characters in the font. The value normal specifies a normal weight font, while bold specifies a bold font. The closest available weight to the one you specified will be chosen. The default weight is normal.
  • -slant => slant β€” how much the characters are slanted away from vertical. Valid values for slant are roman and italic. A roman font is the normal, upright appearance of a font, while an italic font is one that's tilted some number of degrees from upright. The closest available slant to the one you specified will be chosen. The default slant is roman.
  • -underline => boolean β€” a boolean flag saying whether characters in this font should be underlined. The default value for underline is false.
  • -overstrike => boolean β€” a boolean flag saying whether a horizontal line should be drawn through the middle of the characters in this font. The default value for overstrike is false.

🌈 PLATFORM-SPECIFIC ISSUES 🌈

The following named system fonts are supported πŸ’…:

X Windows:

  • All valid X font names, including those listed by xlsfonts(1), are available.

MS Windows:

system       ansi       device
systemfixed  ansifixed  oemfixed

Macintosh:

system       application

πŸ¦‹ COMPATIBILITY WITH PREVIOUS VERSIONS πŸ¦‹

So back in prior versions of perl/Tk, the $widget->Font method was a perl wrapper on the original "[4] X-font names (XLFD)" style as described above β€” which was, btw, the only form supported by versions of core tk prior to version tk8.0. This module is provided in its original form (it has just been renamed) via:

use Tk::X11Font;
$widget->X11Font(...)

However, the methods of the old scheme have been mimicked as closely as possible with the new scheme 🫢. The intention is that code should work without modification β€” except for the case of using:

@names = $font->Name;

i.e. the Name method in an array/list context. This now returns one element on all platforms (as it did on Win32), while previously on X systems it returned a list of fonts that matched an under-specified pattern. Not me getting nostalgic 😭.

Briefly, the methods supported for compatibility are as follows:

  • $newfont = $font->Clone(-option=>value, ...>?) β€” returns a new font object $newfont related to the original $font by changing the values of the specified -options.
  • $font->Family β€” maps to -family
  • $font->Weight β€” maps to -weight
  • $font->Slant β€” maps to -slant
  • $font->Pixel and Point β€” map to -size

New code should use $font->configure to achieve the same effect as the last four items above πŸ‘‘.

  • Foundry, Swidth, Adstyle, Xres, Yres, Space, Avgwidth, Registry, Encoding β€” are all ignored if set, and return '*' if queried. Iconic behaviour tbh, they simply don't participate πŸ’….
  • $font->Name β€” returns the name of a named font, or a string representation of an unnamed font. Using $font in a scalar context does the same. Note this is distinctly different from the behaviour of X11Font's Name in a list context.
  • $font->Pattern β€” returns a XLFD string for the font based on actual values, and some heuristics to map Tk's forms to the "standard" X conventions ✨.

πŸ’œ SEE ALSO πŸ’œ

  • Tk::options
  • Tk::X11Font

⭐ KEYWORDS ⭐

  • font

And that's the whole page, bestie, you ate it πŸ“βœ¨ This one's from perl v5.40.1, dated 2025-04-13, and it's the Font(3pm) page β€” go forth and render some gorgeous glyphs πŸ’…πŸ‘‘πŸ¦‹

πŸ“¦ where this page comes from