Tk_ComputeTextLayout(3)
πΈ Tk_ComputeTextLayout(3) πΈ
okay bestie, welcome to the Tk_ComputeTextLayout(3) page from the Tk Library Procedures β this is the one about measuring and displaying single-font, multi-line, justified text, and honestly it's giving main-character energy for every widget that ever had to draw a paragraph π
β¨ NAME β¨
A whole friend group of routines, all here to measure and display single-font, multi-line, justified text:
Tk_ComputeTextLayoutTk_FreeTextLayoutTk_DrawTextLayoutTk_UnderlineTextLayoutTk_PointToCharTk_CharBboxTk_DistanceToTextLayoutTk_IntersectTextLayoutTk_TextLayoutToPostscript
like, they're literally all in this one manpage together. iconic. π«Ά
π¦ SYNOPSIS π¦
#include <tk.h>
Tk_TextLayout
Tk_ComputeTextLayout(tkfont, string, numChars, wrapLength, justify, flags, widthPtr, heightPtr)
Tk_FreeTextLayout(layout)
Tk_DrawTextLayout(display, drawable, gc, layout, x, y, firstChar, lastChar)
Tk_UnderlineTextLayout(display, drawable, gc, layout, x, y, underline)
int
Tk_PointToChar(layout, x, y)
int
Tk_CharBbox(layout, index, xPtr, yPtr, widthPtr, heightPtr)
int
Tk_DistanceToTextLayout(layout, x, y)
int
Tk_IntersectTextLayout(layout, x, y, width, height)
Tk_TextLayoutToPostscript(interp, layout)
π ARGUMENTS π
okay so here's the whole cast β every single one matters, no skipping:
Tk_Font tkfont(in) β the font used when constructing and displaying a text layout. Thetkfontmust stay valid for the lifetime of the text layout, and it must have come back from a previous call toTk_GetFont.const char *string(in) β the potentially multi-line string whose dimensions get computed and stored in the text layout. Thestringmust stay valid for the lifetime of the text layout too. no cap, keep it alive.int numChars(in) β how many characters to consider fromstring. IfnumCharsis negative, it assumesstringis null terminated and usesTcl_NumUtfCharsto figure out the length ofstring.int wrapLength(in) β the longest permissible line length, in pixels. Lines instringget automatically broken at word boundaries and wrapped when they reach this length. IfwrapLengthis too small for even a single character to fit on a line, it gets expanded to allow one character to fit on each line. IfwrapLengthis<= 0, there's no automatic wrapping β lines get as long as they need to be and only wrap if a newline/return character is encountered.Tk_Justify justify(in) β how to justify the lines in a multi-line text layout. Possible values areTK_JUSTIFY_LEFT,TK_JUSTIFY_CENTER, orTK_JUSTIFY_RIGHT. If the text layout only occupies a single line, thenjustifyis irrelevant.int flags(in) β various flag bits OR-ed together.TK_IGNORE_TABSmeans tab characters should not be expanded to the next tab stop.TK_IGNORE_NEWLINESmeans newline/return characters should not cause a line break. If either tabs or newlines/returns are ignored, then they get treated as regular characters, being measured and displayed in a platform-dependent manner as described inTk_MeasureChars, and they will not have any special behaviors.int *widthPtr(out) β if non-NULL, filled with either the width, in pixels, of the widest line in the text layout, or the width, in pixels, of the bounding box for the character specified byindex.int *heightPtr(out) β if non-NULL, filled with either the total height, in pixels, of all the lines in the text layout, or the height, in pixels, of the bounding box for the character specified byindex.Tk_TextLayout layout(in) β a token representing the cached layout information about the single-font, multi-line, justified piece of text. This token is returned byTk_ComputeTextLayout.Display *display(in) β the display on which to draw.Drawable drawable(in) β the window or pixmap in which to draw.GC gc(in) β the graphics context to use for drawing the text layout. The font selected in thisgcmust correspond to thetkfontused when constructing the text layout.int x, y(in) β the point, in pixels, at which to place the upper-left hand corner of the text layout when it's being drawn, or the coordinates of a point (with respect to the upper-left hand corner of the text layout) to check against the text layout.Tcl_Size firstChar(in) β the index of the first character to draw from the given text layout. The number0means to draw from the beginning.Tcl_Size lastChar(in) β the index of the last character up to which to draw. The character specified bylastCharitself will not be drawn. A number less than0means to draw all characters in the text layout.int underline(in) β index of the single character to underline in the text layout, or a negative number counting backwards from the end of the string. Any out-of-range number (e.g.INT_MIN) means no underline. πTcl_Size index(in) β the index of the character whose bounding box is desired. The bounding box is computed with respect to the upper-left hand corner of the text layout.int *xPtr, *yPtr(out) β filled with the upper-left hand corner, in pixels, of the bounding box for the character specified byindex. Either or bothxPtrandyPtrmay beNULL, in which case the corresponding value is not calculated.int width, height(in) β specifies the width and height, in pixels, of the rectangular area to compare for intersection against the text layout.Tcl_Interp *interp(out) β Postscript code that will print the text layout is appended to the result of interpreterinterp.
continued in part 2 β¨
π DESCRIPTION π
Okay bestie, so these routines are literally for measuring and displaying single-font, multi-line, justified text. π If you just need to measure and display simple single-font, single-line strings, go look at the docs for Tk_MeasureChars. And no cap: there is no programming interface in the core of Tk that supports multi-font, multi-line text; support for that behavior must be built on top of simpler layers. π«Ά
Also lowkey important: unlike the lower level text display routines, all the functions described here operate on character-oriented lengths and indices rather than byte-oriented values. See the description of Tcl_UtfAtIndex for more details on converting between character and byte offsets. β¨
The routines here are built on top of the programming interface described in the Tk_MeasureChars documentation. Tab characters and newline/return characters may be treated specially by these procedures, but all other characters are passed through to the lower level. π
Tk_ComputeTextLayoutβ computes the layout information needed to display a single-font, multi-line, justified string of text and returns aTk_TextLayouttoken that holds this information. This token is used in subsequent calls to procedures such asTk_DrawTextLayout,Tk_DistanceToTextLayout, andTk_FreeTextLayout. Thestringandtkfontused when computing the layout must remain valid for the lifetime of this token. πTk_FreeTextLayoutβ called to release the storage associated withlayoutwhen it is no longer needed. A layout should not be used in any other text layout procedures once it has been released. πTk_DrawTextLayoutβ uses the information inlayoutto display a single-font, multi-line, justified string of text at the specified location. β¨Tk_UnderlineTextLayoutβ uses the information inlayoutto display an underline below an individual character. This procedure does not draw the text, just the underline. To produce natively underlined text, an underlined font should be constructed and used. All characters, includingtabs,newline/returncharacters, and spaces at the ends of lines, can be underlined using this method. However, the underline will never be drawn outside of the computed width oflayout; the underline will stop at the edge for any character that would extend partially outside oflayout, and the underline will not be visible at all for any character that would be located completely outside of thelayout. πTk_PointToCharβ uses the information inlayoutto determine the character closest to the givenpoint. Thepointis specified with respect to the upper-left hand corner of thelayout, which is considered to be located at(0, 0). Any point whose y-value is less that0will be considered closest to the first character in the text layout; any point whose y-value is greater than the height of the text layout will be considered closest to the last character in the text layout. Any point whose x-value is negative will be considered closest to the first character on that line; any point whose x-value is greater than the width of the text layout will be considered closest to the last character on that line. The return value is the index of the character that was closest to thepoint, or one more than the index of any character (to indicate that thepointwas after the end of the string and that the corresponding caret would be at the end of the string). Given alayoutwith no characters, the value0will always be returned, referring to a hypothetical zero-width placeholder character. πΈTk_CharBboxβ uses the information inlayoutto return the bounding box for the character specified byindex. The width of the bounding box is the advance width of the character, and does not include any left or right bearing. Any character that extends partially outside oflayoutis considered to be truncated at the edge. Any character that would be located completely outside oflayoutis considered to be zero-width and pegged against the edge. The height of the bounding box is the line height for this font, extending from the top of the ascent to the bottom of the descent; information about the actual height of individual letters is not available. For measurement purposes, alayoutthat contains no characters is considered to contain a single zero-width placeholder character at index0. Ifindexwas not a valid character index, the return value is0and*xPtr,*yPtr,*widthPtr, and*heightPtrare unmodified. Otherwise, ifindexdid specify a valid, the return value is non-zero, and*xPtr,*yPtr,*widthPtr, and*heightPtrare filled with the bounding box information for the character. If any ofxPtr,yPtr,widthPtr, orheightPtrareNULL, the corresponding value is not calculated or stored. π¦Tk_DistanceToTextLayoutβ computes the shortest distance in pixels from the given point(x, y)to the characters inlayout.Newline/returncharacters and non-displaying space characters that occur at the end of individual lines in the text layout are ignored for hit detection purposes, buttabcharacters are not. The return value is0if thepointactually hits thelayout. If thepointdid not hit thelayoutthen the return value is the distance in pixels from thepointto thelayout. π₯Tk_IntersectTextLayoutβ determines whether alayoutlies entirely inside, entirely outside, or overlaps a given rectangle.Newline/returncharacters and non-displaying space characters that occur at the end of individual lines in thelayoutare ignored for intersection calculations. The return value is-1if thelayoutis entirely outside of the rectangle,0if it overlaps, and1if it is entirely inside of the rectangle. πTk_TextLayoutToPostscriptβ outputs code consisting of a Postscript array of strings that represent the individual lines inlayout. It is the responsibility of the caller to take the Postscript array of strings and add some Postscript function operate on the array to render each of the lines. The code that represents the Postscript array of strings is appended to interpreterinterp's result. π
β¨ DISPLAY MODEL β¨
When measuring a text layout, space characters that occur at the end of a line are ignored. The space characters still exist and the insertion point can be positioned amongst them, but their additional width is ignored when justifying lines or returning the total width of a text layout. All end-of-line space characters are considered to be attached to the right edge of the line; this behavior is logical for left-justified text and reasonable for center-justified text, but not very useful when editing right-justified text. Spaces are considered variable width characters; the first space that extends past the edge of the text layout is clipped to the edge, and any subsequent spaces on the line are considered zero width and pegged against the edge. Space characters that occur in the middle of a line of text are not suppressed and occupy their normal space width. π«Ά
Tab characters are not ignored for measurement calculations. If wrapping is turned on and there are enough tabs on a line, the next tab will wrap to the beginning of the next line. There are some possible strange interactions between tabs and justification; tab positions are calculated and the line length computed in a left-justified world, and then the whole resulting line is shifted so it is centered or right-justified, causing the tab columns not to align any more. π
When wrapping is turned on, lines may wrap at word breaks (space or tab characters) or newline/returns. A dash or hyphen character in the middle of a word is not considered a word break. Tk_ComputeTextLayout always attempts to place at least one word on each line. If it cannot because the wrapLength is too small, the word will be broken and as much as fits placed on the line and the rest on subsequent line(s). If wrapLength is so small that not even one character can fit on a given line, the wrapLength is ignored for that line and one character will be placed on the line anyhow. When wrapping is turned off, only newline/return characters may cause a line break. π
When a text layout has been created using an underlined tkfont, then any space characters that occur at the end of individual lines, newlines/returns, and tabs will not be displayed underlined when Tk_DrawTextLayout is called, because those characters are never actually drawn β they are merely placeholders maintained in the layout. π
π KEYWORDS π
font
And that's the whole vibe, bestie β this page is from Tk 8.1 and it's literally called Tk_ComputeTextLayout(3). The footer doesn't give a date, so I'm not gonna invent one. Periodt. π
β¨
π¦ where this page comes from
- alpine 3.23: tk9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tk9-doc 9.0.3-r2 Β· TCL
- fedora 43: tk-devel 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 43: tk-devel 1:9.0.2-1.fc43 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk-devel 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- fedora 44: tk-devel 1:9.0.2-1.fc44 Β· TCL AND HPND-Pbmplus AND CC-BY-SA-3.0 AND MIT-open-group AND MIT
- nixos 26.05: tk 9.0.3
- Copyright (c) 1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms