Tk_MeasureChars(3tk)

πŸ“¦
version 9.0.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– Tk_MeasureChars(3tk) β€” the Tk Library Procedures glow-up πŸ’–

okay bestie, sit down, hydrate, and let me tell you about the little Tk family that measures and draws your one-line, one-font strings. this is Tk_MeasureChars and its whole squad, straight out of the Tk Library Procedures manual. it's giving "text layout but make it minimal" and honestly? iconic. πŸŽ€


✨ NAME ✨

Tk_MeasureChars, Tk_MeasureCharsInContext, Tk_TextWidth, Tk_DrawChars, Tk_DrawCharsInContext, Tk_UnderlineChars, Tk_UnderlineCharsInContext β€” routines to measure and display simple single-line strings. no cap, that's the whole vibe. πŸ¦‹


✨ SYNOPSIS ✨

#include <tk.h>

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

int
Tk_MeasureCharsInContext(tkfont, string, numBytes, rangeStart, rangeLength, maxPixels, flags, lengthPtr)

int
Tk_TextWidth(tkfont, string, numBytes)

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

Tk_DrawCharsInContext(display, drawable, gc, tkfont, string, numBytes, rangeStart, rangeLength, x, y)

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

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

✨ ARGUMENTS ✨

  • Tk_Font tkfont (in) β€” the token for the font your text is gonna be drawn or measured in. it must have come back from a previous call to Tk_GetFont. no random fonts, bestie, we check credentials here. 🫢
  • const char *string (in) β€” the text to measure or display. it does not need to be null terminated, so don't stress about that. any non-printing meta-characters hanging out in there (tabs, newlines, other control characters, the usual chaos gremlins) get measured or displayed in a platform-dependent way. πŸ’€
  • Tcl_Size numBytes (in) β€” the maximum number of bytes to consider when measuring or drawing string. must be greater than or equal to 0. negative bytes? not the move. πŸ™ˆ
  • 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, then the line length is unbounded and the flags argument is just... ignored. it's giving "no rules, only vibes". ✨
  • 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 must fit completely to be considered.
    • TK_WHOLE_WORDS means stop on a word boundary, if possible.
    • TK_AT_LEAST_ONE, if set, 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. she's committed. we love her.) πŸ’…
    • TK_ISOLATE_END means the last character should not be considered in context with the rest of the string β€” used for breaking lines, and it only applies with the Tk_*InContext variants of the functions. πŸ‘‘
  • int *lengthPtr (out) β€” gets filled with the number of pixels occupied by the number of characters returned as the result of Tk_MeasureChars. it's the receipts. 🧾
  • Display *display (in) β€” the display to draw on.
  • Drawable drawable (in) β€” the window or pixmap to draw in.
  • GC gc (in) β€” the graphics context for drawing characters. the font selected into this GC must be the same as the tkfont. mismatched fonts = not slay. ⚠️
  • int x, y (in) β€” the coordinates where the left edge of the baseline gets placed when displaying string.
  • int firstByte (in) β€” the index of the first byte of the first character to underline in the string. 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. so close, and yet so un-underlined. 😭
  • Tcl_Size rangeStart (in) β€” index of first byte to draw or to measure.
  • Tcl_Size rangeLength (in) β€” length of range to draw or to measure, in bytes.

✨ DESCRIPTION ✨

so these routines are for measuring and displaying simple single-font, single-line strings. that's the whole brief. if you want single-font, multi-line, justified text, go read the docs for Tk_ComputeTextLayout instead, because that's a whole other girl. 🌸

and listen: there is no programming interface in the core of Tk that supports multi-font, multi-line text. none. zero. if you want that behavior, you have to build it yourself on top of simpler layers. sorry bestie, you're the architect now. πŸ—οΈ

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. do not skip this step, i'm literally begging. 😀

a glyph is the displayable picture of a letter, number, or some other symbol. not all character codes in a given font have a glyph. characters like tabs, newlines/returns, and control characters that have no glyph are measured and displayed by these procedures in a platform-dependent manner:

  • under X, they're replaced with backslashed escape sequences
  • 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. she does it properly. πŸ’–


🎯 Tk_MeasureChars

Tk_MeasureChars does double duty: it computes the length of a given string and figures out 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 the 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 cute. ✨


🎯 Tk_TextWidth

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. that's it. that's the function. short and efficient, main character energy. πŸ‘‘


🎯 Tk_DrawChars

Tk_DrawChars draws the string at the given location in the given drawable. simple. gorgeous. no notes. πŸ’…


🎯 Tk_UnderlineChars

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. it's the accessory, not the outfit. πŸŽ€

this procedure is used to underline a few characters without having to construct an underlined font. to produce natively underlined text, the appropriate underlined font should be constructed and used instead.


🎯 the InContext girlies

Tk_MeasureCharsInContext, Tk_DrawCharsInContext, and Tk_UnderlineCharsInContext are variants of respectively Tk_MeasureChars, Tk_DrawChars and Tk_UnderlineChars, but with access to all the characters on the line for context. 🌈

except on macOS, this context isn't actually consulted (meaning the out-of-context variants are called). so on most platforms she's holding the context and not even reading it. relatable honestly. πŸ’€

✨ SEE ALSO ✨

font(3tk), FontId(3tk) β€” go read those too, bestie, they're the supporting cast. 🫢


✨ KEYWORDS ✨

font, measurement 🌸


and that's the tea, bestie! this whole page is from Tk 8.1, page Tk_MeasureChars(3tk). go forth and measure your strings responsibly. mwah! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸ’…

πŸ“¦ where this page comes from