| 1 |
# Copyright (c) 1996 Sun Microsystems, Inc. |
| 2 |
# See the file "license.terms" for information on usage and redistribution |
| 3 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 4 |
# |
| 5 |
# |
| 6 |
|
| 7 |
=head1 NAME |
| 8 |
|
| 9 |
Tk_ComputeTextLayout, Tk_FreeTextLayout, Tk_DrawTextLayout, Tk_UnderlineTextLayout, Tk_PointToChar, Tk_CharBbox, Tk_DistanceToTextLayout, Tk_IntersectTextLayout, Tk_TextLayoutToPostscript - routines to measure and display single-font, multi-line, justified text. |
| 10 |
|
| 11 |
=for category C Programming |
| 12 |
|
| 13 |
=head1 SYNOPSIS |
| 14 |
|
| 15 |
B<#include E<lt>tk.hE<gt>> |
| 16 |
|
| 17 |
Tk_TextLayout |
| 18 |
B<Tk_ComputeTextLayout(>I<tkfont, string, numChars, wrapLength, justify, flags, widthPtr, heightPtr>B<)> |
| 19 |
|
| 20 |
void |
| 21 |
B<Tk_FreeTextLayout(>I<layout>B<)> |
| 22 |
|
| 23 |
void |
| 24 |
B<Tk_DrawTextLayout(>I<display, drawable, gc, layout, x, y, firstChar, lastChar>B<)> |
| 25 |
|
| 26 |
void |
| 27 |
B<Tk_UnderlineTextLayout(>I<display, drawable, gc, layout, x, y, underline>B<)> |
| 28 |
|
| 29 |
int |
| 30 |
B<Tk_PointToChar(>I<layout, x, y>B<)> |
| 31 |
|
| 32 |
int |
| 33 |
B<Tk_CharBbox(>I<layout, index, xPtr, yPtr, widthPtr, heightPtr>B<)> |
| 34 |
|
| 35 |
int |
| 36 |
B<Tk_DistanceToTextLayout(>I<layout, x, y>B<)> |
| 37 |
|
| 38 |
int |
| 39 |
B<Tk_IntersectTextLayout(>I<layout, x, y, width, height>B<)> |
| 40 |
|
| 41 |
void |
| 42 |
B<Tk_TextLayoutToPostscript(>I<interp, layout>B<)> |
| 43 |
|
| 44 |
=head1 ARGUMENTS |
| 45 |
|
| 46 |
=over 4 |
| 47 |
|
| 48 |
=item Tk_Font tkfont (in) |
| 49 |
|
| 50 |
Font to use when constructing and displaying a text layout. The |
| 51 |
I<tkfont> must remain valid for the lifetime of the text layout. Must |
| 52 |
have been returned by a previous call to B<Tk_GetFont>. |
| 53 |
|
| 54 |
=item "const char" *string (in) |
| 55 |
|
| 56 |
Potentially multi-line string whose dimensions are to be computed and |
| 57 |
stored in the text layout. The I<string> must remain valid for the |
| 58 |
lifetime of the text layout. |
| 59 |
|
| 60 |
=item int numChars (in) |
| 61 |
|
| 62 |
The number of characters to consider from I<string>. If |
| 63 |
I<numChars> is less than 0, then assumes I<string> is null |
| 64 |
terminated and uses B<strlen(>I<string>B<)>. |
| 65 |
|
| 66 |
=item int wrapLength (in) |
| 67 |
|
| 68 |
Longest permissible line length, in pixels. Lines in I<string> will |
| 69 |
automatically be broken at word boundaries and wrapped when they reach |
| 70 |
this length. If I<wrapLength> is too small for even a single |
| 71 |
character to fit on a line, it will be expanded to allow one character to |
| 72 |
fit on each line. If I<wrapLength> is E<lt>= 0, there is no automatic |
| 73 |
wrapping; lines will get as long as they need to be and only wrap if a |
| 74 |
newline/return character is encountered. |
| 75 |
|
| 76 |
=item Tk_Justify justify (in) |
| 77 |
|
| 78 |
How to justify the lines in a multi-line text layout. Possible values |
| 79 |
are TK_JUSTIFY_LEFT, TK_JUSTIFY_CENTER, or TK_JUSTIFY_RIGHT. If the text |
| 80 |
layout only occupies a single line, then I<justify> is irrelevant. |
| 81 |
|
| 82 |
=item int flags (in) |
| 83 |
|
| 84 |
Various flag bits OR-ed together. TK_IGNORE_TABS means that tab characters |
| 85 |
should not be expanded to the next tab stop. TK_IGNORE_NEWLINES means that |
| 86 |
newline/return characters should not cause a line break. If either tabs or |
| 87 |
newlines/returns are ignored, then they will be treated as regular |
| 88 |
characters, being measured and displayed in a platform-dependent manner as |
| 89 |
described in B<Tk_MeasureChars>, and will not have any special behaviors. |
| 90 |
|
| 91 |
=item int *widthPtr (out) |
| 92 |
|
| 93 |
If non-NULL, filled with either the width, in pixels, of the widest |
| 94 |
line in the text layout, or the width, in pixels, of the bounding box for the |
| 95 |
character specified by I<index>. |
| 96 |
|
| 97 |
=item int *heightPtr (out) |
| 98 |
|
| 99 |
If non-NULL, filled with either the total height, in pixels, of all |
| 100 |
the lines in the text layout, or the height, in pixels, of the bounding |
| 101 |
box for the character specified by I<index>. |
| 102 |
|
| 103 |
=item Tk_TextLayout layout (in) |
| 104 |
|
| 105 |
A token that represents the cached layout information about the single-font, |
| 106 |
multi-line, justified piece of text. This token is returned by |
| 107 |
B<Tk_ComputeTextLayout>. |
| 108 |
|
| 109 |
=item Display *display (in) |
| 110 |
|
| 111 |
Display on which to draw. |
| 112 |
|
| 113 |
=item Drawable drawable (in) |
| 114 |
|
| 115 |
Window or pixmap in which to draw. |
| 116 |
|
| 117 |
=item GC gc (in) |
| 118 |
|
| 119 |
Graphics context to use for drawing text layout. The font selected in |
| 120 |
this GC must correspond to the I<tkfont> used when constructing the |
| 121 |
text layout. |
| 122 |
|
| 123 |
=item int "x, y" (in) |
| 124 |
|
| 125 |
Point, in pixels, at which to place the upper-left hand corner of the |
| 126 |
text layout when it is being drawn, or the coordinates of a point (with |
| 127 |
respect to the upper-left hand corner of the text layout) to check |
| 128 |
against the text layout. |
| 129 |
|
| 130 |
=item int firstChar (in) |
| 131 |
|
| 132 |
The index of the first character to draw from the given text layout. |
| 133 |
The number 0 means to draw from the beginning. |
| 134 |
|
| 135 |
=item int lastChar (in) |
| 136 |
|
| 137 |
The index of the last character up to which to draw. The character |
| 138 |
specified by I<lastChar> itself will not be drawn. A number less |
| 139 |
than 0 means to draw all characters in the text layout. |
| 140 |
|
| 141 |
=item int underline (in) |
| 142 |
|
| 143 |
Index of the single character to underline in the text layout, or a number |
| 144 |
less than 0 for no underline. |
| 145 |
|
| 146 |
=item int index (in) |
| 147 |
|
| 148 |
The index of the character whose bounding box is desired. The bounding |
| 149 |
box is computed with respect to the upper-left hand corner of the text layout. |
| 150 |
|
| 151 |
=item int "*xPtr, *yPtr" (out) |
| 152 |
|
| 153 |
Filled with the upper-left hand corner, in pixels, of the bounding box |
| 154 |
for the character specified by I<index>. Either or both I<xPtr> |
| 155 |
and I<yPtr> may be NULL, in which case the corresponding value |
| 156 |
is not calculated. |
| 157 |
|
| 158 |
=item int "width, height" (in) |
| 159 |
|
| 160 |
Specifies the width and height, in pixels, of the rectangular area to |
| 161 |
compare for intersection against the text layout. |
| 162 |
|
| 163 |
=item Tcl_Interp *interp (out) |
| 164 |
|
| 165 |
Postscript code that will print the text layout is appended to |
| 166 |
I<interp-E<gt>result>. |
| 167 |
|
| 168 |
=back |
| 169 |
|
| 170 |
=head1 DESCRIPTION |
| 171 |
|
| 172 |
These routines are for measuring and displaying single-font, multi-line, |
| 173 |
justified text. To measure and display simple single-font, single-line |
| 174 |
strings, refer to the documentation for B<Tk_MeasureChars>. There is |
| 175 |
no programming interface in the core of Tk that supports multi-font, |
| 176 |
multi-line text; support for that behavior must be built on top of |
| 177 |
simpler layers. |
| 178 |
|
| 179 |
The routines described here are built on top of the programming interface |
| 180 |
described in the B<Tk_MeasureChars> documentation. Tab characters and |
| 181 |
newline/return characters may be treated specially by these procedures, |
| 182 |
but all other characters are passed through to the lower level. |
| 183 |
|
| 184 |
B<Tk_ComputeTextLayout> computes the layout information needed to |
| 185 |
display a single-font, multi-line, justified I<string> of text and |
| 186 |
returns a Tk_TextLayout token that holds this information. This token is |
| 187 |
used in subsequent calls to procedures such as B<Tk_DrawTextLayout>, |
| 188 |
B<Tk_DistanceToTextLayout>, and B<Tk_FreeTextLayout>. The |
| 189 |
I<string> and I<tkfont> used when computing the layout must remain |
| 190 |
valid for the lifetime of this token. |
| 191 |
|
| 192 |
B<Tk_FreeTextLayout> is called to release the storage associated with |
| 193 |
I<layout> when it is no longer needed. A I<layout> should not be used |
| 194 |
in any other text layout procedures once it has been released. |
| 195 |
|
| 196 |
B<Tk_DrawTextLayout> uses the information in I<layout> to display a |
| 197 |
single-font, multi-line, justified string of text at the specified location. |
| 198 |
|
| 199 |
B<Tk_UnderlineTextLayout> uses the information in I<layout> to |
| 200 |
display an underline below an individual character. This procedure does |
| 201 |
not draw the text, just the underline. To produce natively underlined |
| 202 |
text, an underlined font should be constructed and used. All characters, |
| 203 |
including tabs, newline/return characters, and spaces at the ends of |
| 204 |
lines, can be underlined using this method. However, the underline will |
| 205 |
never be drawn outside of the computed width of I<layout>; the |
| 206 |
underline will stop at the edge for any character that would extend |
| 207 |
partially outside of I<layout>, and the underline will not be visible |
| 208 |
at all for any character that would be located completely outside of the |
| 209 |
layout. |
| 210 |
|
| 211 |
B<Tk_PointToChar> uses the information in I<layout> to determine the |
| 212 |
character closest to the given point. The point is specified with respect |
| 213 |
to the upper-left hand corner of the I<layout>, which is considered to be |
| 214 |
located at (0, 0). Any point whose I<y>-value is less that 0 will be |
| 215 |
considered closest to the first character in the text layout; any point |
| 216 |
whose I<y>-value is greater than the height of the text layout will be |
| 217 |
considered closest to the last character in the text layout. Any point |
| 218 |
whose I<x>-value is less than 0 will be considered closest to the first |
| 219 |
character on that line; any point whose I<x>-value is greater than the |
| 220 |
width of the text layout will be considered closest to the last character on |
| 221 |
that line. The return value is the index of the character that was closest |
| 222 |
to the point. Given a I<layout> with no characters, the value 0 will |
| 223 |
always be returned, referring to a hypothetical zero-width placeholder |
| 224 |
character. |
| 225 |
|
| 226 |
B<Tk_CharBBox> uses the information in I<layout> to return the |
| 227 |
bounding box for the character specified by I<index>. The width of the |
| 228 |
bounding box is the advance width of the character, and does not include any |
| 229 |
left or right bearing. Any character that extends partially outside of |
| 230 |
I<layout> is considered to be truncated at the edge. Any character |
| 231 |
that would be located completely outside of I<layout> is considered to |
| 232 |
be zero-width and pegged against the edge. The height of the bounding |
| 233 |
box is the line height for this font, extending from the top of the |
| 234 |
ascent to the bottom of the descent; information about the actual height |
| 235 |
of individual letters is not available. For measurement purposes, a |
| 236 |
I<layout> that contains no characters is considered to contain a |
| 237 |
single zero-width placeholder character at index 0. If I<index> was |
| 238 |
not a valid character index, the return value is 0 and I<*xPtr>, |
| 239 |
I<*yPtr>, I<*widthPtr>, and I<*heightPtr> are unmodified. |
| 240 |
Otherwise, if I<index> did specify a valid, the return value is |
| 241 |
non-zero, and I<*xPtr>, I<*yPtr>, I<*widthPtr>, and |
| 242 |
I<*heightPtr> are filled with the bounding box information for the |
| 243 |
character. If any of I<xPtr>, I<yPtr>, I<widthPtr>, or |
| 244 |
I<heightPtr> are NULL, the corresponding value is not calculated or |
| 245 |
stored. |
| 246 |
|
| 247 |
B<Tk_DistanceToTextLayout> computes the shortest distance in pixels from |
| 248 |
the given point (I<x, y>) to the characters in I<layout>. |
| 249 |
Newline/return characters and non-displaying space characters that occur at |
| 250 |
the end of individual lines in the text layout are ignored for hit detection |
| 251 |
purposes, but tab characters are not. The return value is 0 if the point |
| 252 |
actually hits the I<layout>. If the point didn't hit the I<layout> |
| 253 |
then the return value is the distance in pixels from the point to the |
| 254 |
I<layout>. |
| 255 |
|
| 256 |
B<Tk_IntersectTextLayout> determines whether a I<layout> lies |
| 257 |
entirely inside, entirely outside, or overlaps a given rectangle. |
| 258 |
Newline/return characters and non-displaying space characters that occur |
| 259 |
at the end of individual lines in the I<layout> are ignored for |
| 260 |
intersection calculations. The return value is -1 if the I<layout> is |
| 261 |
entirely outside of the rectangle, 0 if it overlaps, and 1 if it is |
| 262 |
entirely inside of the rectangle. |
| 263 |
|
| 264 |
B<Tk_TextLayoutToPostscript> outputs code consisting of a Postscript |
| 265 |
array of strings that represent the individual lines in I<layout>. It |
| 266 |
is the responsibility of the caller to take the Postscript array of |
| 267 |
strings and add some Postscript function operate on the array to render |
| 268 |
each of the lines. The code that represents the Postscript array of |
| 269 |
strings is appended to I<interp-E<gt>result>. |
| 270 |
|
| 271 |
=head1 DISPLAY MODEL |
| 272 |
|
| 273 |
When measuring a text layout, space characters that occur at the end of a |
| 274 |
line are ignored. The space characters still exist and the insertion point |
| 275 |
can be positioned amongst them, but their additional width is ignored when |
| 276 |
justifying lines or returning the total width of a text layout. All |
| 277 |
end-of-line space characters are considered to be attached to the right edge |
| 278 |
of the line; this behavior is logical for left-justified text and reasonable |
| 279 |
for center-justified text, but not very useful when editing right-justified |
| 280 |
text. Spaces are considered variable width characters; the first space that |
| 281 |
extends past the edge of the text layout is clipped to the edge, and any |
| 282 |
subsequent spaces on the line are considered zero width and pegged against |
| 283 |
the edge. Space characters that occur in the middle of a line of text are |
| 284 |
not suppressed and occupy their normal space width. |
| 285 |
|
| 286 |
Tab characters are not ignored for measurement calculations. If wrapping |
| 287 |
is turned on and there are enough tabs on a line, the next tab will wrap |
| 288 |
to the beginning of the next line. There are some possible strange |
| 289 |
interactions between tabs and justification; tab positions are calculated |
| 290 |
and the line length computed in a left-justified world, and then the |
| 291 |
whole resulting line is shifted so it is centered or right-justified, |
| 292 |
causing the tab columns not to align any more. |
| 293 |
|
| 294 |
When wrapping is turned on, lines may wrap at word breaks (space or tab |
| 295 |
characters) or newline/returns. A dash or hyphen character in the middle |
| 296 |
of a word is not considered a word break. B<Tk_ComputeTextLayout> |
| 297 |
always attempts to place at least one word on each line. If it cannot |
| 298 |
because the I<wrapLength> is too small, the word will be broken and as |
| 299 |
much as fits placed on the line and the rest on subsequent line(s). If |
| 300 |
I<wrapLength> is so small that not even one character can fit on a |
| 301 |
given line, the I<wrapLength> is ignored for that line and one |
| 302 |
character will be placed on the line anyhow. When wrapping is turned |
| 303 |
off, only newline/return characters may cause a line break. |
| 304 |
|
| 305 |
When a text layout has been created using an underlined I<tkfont>, |
| 306 |
then any space characters that occur at the end of individual lines, |
| 307 |
newlines/returns, and tabs will not be displayed underlined when |
| 308 |
B<Tk_DrawTextLayout> is called, because those characters are never |
| 309 |
actually drawn - they are merely placeholders maintained in the |
| 310 |
I<layout>. |
| 311 |
|
| 312 |
=head1 KEYWORDS |
| 313 |
|
| 314 |
font |