Tk_MeasureChars(3tk)
π 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 toTk_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 drawingstring. must be greater than or equal to 0. negative bytes? not the move. πint maxPixels(in) β ifmaxPixelsis>= 0, it's the longest permissible line length in pixels, and characters fromstringget processed only until that many pixels have been covered. ifmaxPixelsis< 0, then the line length is unbounded and theflagsargument is just... ignored. it's giving "no rules, only vibes". β¨int flags(in) β various flag bits OR-ed together:TK_PARTIAL_OKmeans include a character as long as any part of it fits in the length given bymaxPixels; otherwise, a character must fit completely to be considered.TK_WHOLE_WORDSmeans 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 bymaxPixels.- if
TK_AT_LEAST_ONEis set andTK_WHOLE_WORDSis 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_ENDmeans the last character should not be considered in context with the rest of the string β used for breaking lines, and it only applies with theTk_*InContextvariants of the functions. π
int *lengthPtr(out) β gets filled with the number of pixels occupied by the number of characters returned as the result ofTk_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 thetkfont. mismatched fonts = not slay. β οΈint x, y(in) β the coordinates where the left edge of the baseline gets placed when displayingstring.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 bylastBytewill 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
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms