Tk_MeasureChars(3tk)

πŸ’– Tk_MeasureChars β€” bestie edition πŸ’–

okay so this is a page from the Tk Library Procedures manual, and it's about the little routines that measure and draw text. Not the whole text-layout industrial complex, just the simple single-line stuff. Come sit with me, we're gonna go through it together 🫢


✨ NAME ✨

Tk_MeasureChars, Tk_TextWidth, Tk_DrawChars, Tk_UnderlineChars β€” routines to measure and display simple single-line strings. that's it, that's the whole vibe. one font, one line, no drama πŸŽ€


✨ SYNOPSIS ✨

no cap, here's the exact shape of everything:

#include <tk.h>

int
Tk_MeasureChars(tkfont, string, numBytes, maxPixels, flags, lengthPtr)

int
Tk_TextWidth(tkfont, string, numBytes)

Tk_DrawChars(display, drawable, gc, tkfont, string, numBytes, x, y)

Tk_UnderlineChars(display, drawable, gc, tkfont, string, x, y, firstByte, lastByte)

✨ ARGUMENTS ✨

bestie, every single one of these matters, so here they all are, no skipping πŸ’…

  • Tk_Font tkfont (in) β€” the token for the font you're gonna draw or measure text in. it must have come back from an earlier call to Tk_GetFont. no random fonts, that's not the move.
  • const char *string (in) β€” the text you're measuring or showing. it does not have to be null terminated, which is lowkey a fun fact. any non-printing meta-characters in there (tabs, newlines, other control characters) get measured or displayed in a platform-dependent manner, so don't come for me about it.
  • int numBytes (in) β€” the max number of bytes to look at when measuring or drawing string. must be greater than or equal to 0. negative? absolutely not.
  • int maxPixels (in) β€” if maxPixels is >= 0, it's the longest permissible line length in pixels, and characters from string get processed only until that many pixels have been covered. if maxPixels is < 0, the line length is unbounded and the flags argument is just ignored entirely. iconic of it honestly.
  • int flags (in) β€” various flag bits OR-ed together 🧚:
    • TK_PARTIAL_OK means include a character as long as any part of it fits in the length given by maxPixels; otherwise, a character has to fit completely to be considered.
    • TK_WHOLE_WORDS means stop on a word boundary, if possible.
    • If TK_AT_LEAST_ONE is set, it means return at least one character even if no characters could fit in the length given by maxPixels.
    • If TK_AT_LEAST_ONE is set and TK_WHOLE_WORDS is also set, it means that if not even one word fits on the line, return the first few letters of the word that did fit; if not even one letter of the word fit, then the first letter will still be returned. she's persistent, we love her πŸ’€
  • int *lengthPtr (out) β€” filled with the number of pixels occupied by the number of characters returned as the result of Tk_MeasureChars. this is your output, bestie.
  • Display *display (in) β€” the display you're drawing on.
  • Drawable drawable (in) β€” the window or pixmap you're drawing in.
  • GC gc (in) β€” the graphics context for drawing characters. the font selected into this GC must be the same as the tkfont. like, they have to match. fr fr.
  • int x, y (in) β€” the coordinates where you place the left edge of the baseline when displaying string.
  • int firstByte (in) β€” the index of the first byte of the first character to underline in string. the underlining begins at the left edge of this character.
  • int lastByte (in) β€” the index of the first byte of the last character up to which the underline will be drawn. the character specified by lastByte will not itself be underlined. sneak peek behavior, we respect it.

✨ DESCRIPTION ✨

okay so, these routines are for measuring and displaying simple single-font, single-line strings. that's the lane. πŸ¦‹

If you want single-font, multi-line, justified text, go read the docs for Tk_ComputeTextLayout instead. And heads up: there is no programming interface in the core of Tk that supports multi-font, multi-line text. if you want that behavior you have to build it yourself on top of the simpler layers. the way that Tk makes you do the work... anyway.

also super important: the interfaces described here are byte-oriented, not character-oriented, so index values coming from Tcl scripts need to be converted to byte offsets using Tcl_UtfAtIndex and related routines. don't skip that step, it will bite you πŸ“

A glyph is the displayable picture of a letter, number, or some other symbol. and plot twist: not all character codes in a given font have a glyph. characters like tabs, newlines/returns, and control characters that have no glyph get measured and displayed by these procedures in a platform-dependent manner; under X, they're replaced with backslashed escape sequences, while under Windows and Macintosh hollow or solid boxes may be substituted. πŸ’€

If you want a programming interface that supports platform-independent expansion of tab characters into columns and newlines/returns into multi-line text, refer to the documentation for Tk_ComputeTextLayout. periodt.

Tk_MeasureChars is used both to compute the length of a given string and to compute how many characters from a string fit in a given amount of space. the return value is the number of bytes from string that fit in the space specified by maxPixels, subject to the conditions described by flags. if all characters fit, the return value will be numBytes. *lengthPtr gets filled with the computed width, in pixels, of the portion of the string that was measured. for example, if the return value is 5, then *lengthPtr is filled with the distance between the left edge of string[0] and the right edge of string[4]. math but make it cute ✨

Tk_TextWidth is a wrapper function that provides a simpler interface to the Tk_MeasureChars function. the return value is how much space in pixels the given string needs. she's the easygoing one πŸ’œ

Tk_DrawChars draws the string at the given location in the given drawable. straightforward, main character energy πŸ‘‘

Tk_UnderlineChars underlines the given range of characters in the given string. it does not draw the characters (those are assumed to have already been displayed previously by Tk_DrawChars) β€” it just draws the underline. this procedure exists so you can underline a few characters without having to construct an underlined font. if you want natively underlined text, you should construct and use the appropriate underlined font instead. 🌸


✨ SEE ALSO ✨


✨ KEYWORDS ✨

font, measurement


and that's the whole page, bestie!! πŸŽ€ this one comes to you from Tk 8.1 (the Tk library, Tk_MeasureChars(3tk)), so go measure some strings, underline a few characters, and remember: match your GC font to your tkfont or the vibes will be off. mwah πŸ’…βœ¨

πŸ“¦ where this page comes from