4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 2002 Bill Medland
8 * 1. DrawText functions
9 * 2. GrayString functions
10 * 3. TabbedText functions
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
38 #include "user_private.h"
39 #include "wine/debug.h"
40 #include "wine/heap.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(text
);
44 /*********************************************************************
49 * How many buffers to use
50 * While processing in DrawText there are potentially three different forms
51 * of the text that need to be held. How are they best held?
52 * 1. The original text is needed, of course, to see what to display.
53 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
55 * 3. The buffered text that is about to be displayed e.g. the current line.
56 * Typically this will exclude the ampersands used for prefixing etc.
59 * a. If the buffered text to be displayed includes the ampersands then
60 * we will need special measurement and draw functions that will ignore
61 * the ampersands (e.g. by copying to a buffer without the prefix and
62 * then using the normal forms). This may involve less space but may
63 * require more processing. e.g. since a line containing tabs may
64 * contain several underlined characters either we need to carry around
65 * a list of prefix locations or we may need to locate them several
67 * b. If we actually directly modify the "original text" as we go then we
68 * will need some special "caching" to handle the fact that when we
69 * ellipsify the text the ellipsis may modify the next line of text,
70 * which we have not yet processed. (e.g. ellipsification of a W at the
71 * end of a line will overwrite the W, the \n and the first character of
72 * the next line, and a \0 will overwrite the second. Try it!!)
74 * Option 1. Three separate storages. (To be implemented)
75 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
76 * the edited string in some form, either as the string itself or as some
77 * sort of "edit list" to be applied just before returning.
78 * Use a buffer that holds the ellipsified current line sans ampersands
79 * and accept the need occasionally to recalculate the prefixes (if
80 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
88 #define ALPHA_PREFIX 30 /* Win16: Alphabet prefix */
89 #define KANA_PREFIX 31 /* Win16: Katakana prefix */
91 #define FORWARD_SLASH '/'
92 #define BACK_SLASH '\\'
94 static const WCHAR ELLIPSISW
[] = L
"...";
96 typedef struct tag_ellipsis_data
104 /*********************************************************************
105 * TEXT_Ellipsify (static)
107 * Add an ellipsis to the end of the given string whilst ensuring it fits.
109 * If the ellipsis alone doesn't fit then it will be returned anyway.
111 * See Also TEXT_PathEllipsify
114 * hdc [in] The handle to the DC that defines the font.
115 * str [in/out] The string that needs to be modified.
116 * max_str [in] The dimension of str (number of WCHAR).
117 * len_str [in/out] The number of characters in str
118 * width [in] The maximum width permitted (in logical coordinates)
119 * size [out] The dimensions of the text
120 * modstr [out] The modified form of the string, to be returned to the
121 * calling program. It is assumed that the caller has
122 * made sufficient space available so we don't need to
123 * know the size of the space. This pointer may be NULL if
124 * the modified string is not required.
125 * len_before [out] The number of characters before the ellipsis.
126 * len_ellip [out] The number of characters in the ellipsis.
128 * See for example Microsoft article Q249678.
130 * For now we will simply use three dots rather than worrying about whether
131 * the font contains an explicit ellipsis character.
133 static void TEXT_Ellipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
134 unsigned int *len_str
, int width
, SIZE
*size
,
136 int *len_before
, int *len_ellip
)
138 unsigned int len_ellipsis
;
139 unsigned int lo
, mid
, hi
;
141 len_ellipsis
= lstrlenW (ELLIPSISW
);
142 if (len_ellipsis
> max_len
) len_ellipsis
= max_len
;
143 if (*len_str
> max_len
- len_ellipsis
)
144 *len_str
= max_len
- len_ellipsis
;
146 /* First do a quick binary search to get an upper bound for *len_str. */
148 GetTextExtentExPointW(hdc
, str
, *len_str
, width
, NULL
, NULL
, size
) &&
151 for (lo
= 0, hi
= *len_str
; lo
< hi
; )
154 if (!GetTextExtentExPointW(hdc
, str
, mid
, width
, NULL
, NULL
, size
))
156 if (size
->cx
> width
)
163 /* Now this should take only a couple iterations at most. */
166 memcpy(str
+ *len_str
, ELLIPSISW
, len_ellipsis
*sizeof(WCHAR
));
168 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
169 NULL
, NULL
, size
)) break;
171 if (!*len_str
|| size
->cx
<= width
) break;
175 *len_ellip
= len_ellipsis
;
176 *len_before
= *len_str
;
177 *len_str
+= len_ellipsis
;
181 memcpy (modstr
, str
, *len_str
* sizeof(WCHAR
));
182 modstr
[*len_str
] = '\0';
186 /*********************************************************************
187 * TEXT_PathEllipsify (static)
189 * Add an ellipsis to the provided string in order to make it fit within
190 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
193 * See Also TEXT_Ellipsify
196 * hdc [in] The handle to the DC that defines the font.
197 * str [in/out] The string that needs to be modified
198 * max_str [in] The dimension of str (number of WCHAR).
199 * len_str [in/out] The number of characters in str
200 * width [in] The maximum width permitted (in logical coordinates)
201 * size [out] The dimensions of the text
202 * modstr [out] The modified form of the string, to be returned to the
203 * calling program. It is assumed that the caller has
204 * made sufficient space available so we don't need to
205 * know the size of the space. This pointer may be NULL if
206 * the modified string is not required.
207 * pellip [out] The ellipsification results
209 * For now we will simply use three dots rather than worrying about whether
210 * the font contains an explicit ellipsis character.
212 * The following applies, I think to Win95. We will need to extend it for
213 * Win98 which can have both path and end ellipsis at the same time (e.g.
214 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
216 * The resulting string consists of as much as possible of the following:
217 * 1. The ellipsis itself
218 * 2. The last \ or / of the string (if any)
219 * 3. Everything after the last \ or / of the string (if any) or the whole
220 * string if there is no / or \. I believe that under Win95 this would
221 * include everything even though some might be clipped off the end whereas
222 * under Win98 that might be ellipsified too.
223 * Yet to be investigated is whether this would include wordbreaking if the
224 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
225 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
226 * broken within words).
227 * 4. All the stuff before the / or \, which is placed before the ellipsis.
229 static void TEXT_PathEllipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
230 unsigned int *len_str
, int width
, SIZE
*size
,
231 WCHAR
*modstr
, ellipsis_data
*pellip
)
236 WCHAR
*lastBkSlash
, *lastFwdSlash
, *lastSlash
;
238 len_ellipsis
= lstrlenW (ELLIPSISW
);
239 if (!max_len
) return;
240 if (len_ellipsis
>= max_len
) len_ellipsis
= max_len
- 1;
241 if (*len_str
+ len_ellipsis
>= max_len
)
242 *len_str
= max_len
- len_ellipsis
-1;
243 /* Hopefully this will never happen, otherwise it would probably lose
244 * the wrong character
246 str
[*len_str
] = '\0'; /* to simplify things */
248 lastBkSlash
= wcsrchr (str
, BACK_SLASH
);
249 lastFwdSlash
= wcsrchr (str
, FORWARD_SLASH
);
250 lastSlash
= lastBkSlash
> lastFwdSlash
? lastBkSlash
: lastFwdSlash
;
251 if (!lastSlash
) lastSlash
= str
;
252 len_trailing
= *len_str
- (lastSlash
- str
);
254 /* overlap-safe movement to the right */
255 memmove (lastSlash
+len_ellipsis
, lastSlash
, len_trailing
* sizeof(WCHAR
));
256 memcpy (lastSlash
, ELLIPSISW
, len_ellipsis
*sizeof(WCHAR
));
257 len_trailing
+= len_ellipsis
;
258 /* From this point on lastSlash actually points to the ellipsis in front
259 * of the last slash and len_trailing includes the ellipsis
265 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
266 NULL
, NULL
, size
)) break;
268 if (lastSlash
== str
|| size
->cx
<= width
) break;
270 /* overlap-safe movement to the left */
271 memmove (lastSlash
-1, lastSlash
, len_trailing
* sizeof(WCHAR
));
278 pellip
->before
= lastSlash
-str
;
279 pellip
->len
= len_ellipsis
;
280 pellip
->under
= len_under
;
281 pellip
->after
= len_trailing
- len_ellipsis
;
282 *len_str
+= len_ellipsis
;
286 memcpy(modstr
, str
, *len_str
* sizeof(WCHAR
));
287 modstr
[*len_str
] = '\0';
291 /*********************************************************************
292 * TEXT_WordBreak (static)
294 * Perform wordbreak processing on the given string
296 * Assumes that DT_WORDBREAK has been specified and not all the characters
297 * fit. Note that this function should even be called when the first character
298 * that doesn't fit is known to be a space or tab, so that it can swallow them.
300 * Note that the Windows processing has some strange properties.
301 * 1. If the text is left-justified and there is room for some of the spaces
302 * that follow the last word on the line then those that fit are included on
304 * 2. If the text is centered or right-justified and there is room for some of
305 * the spaces that follow the last word on the line then all but one of those
306 * that fit are included on the line.
307 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
308 * character of a new line it will be skipped.
311 * hdc [in] The handle to the DC that defines the font.
312 * str [in/out] The string that needs to be broken.
313 * max_str [in] The dimension of str (number of WCHAR).
314 * len_str [in/out] The number of characters in str
315 * width [in] The maximum width permitted
316 * format [in] The format flags in effect
317 * chars_fit [in] The maximum number of characters of str that are already
318 * known to fit; chars_fit+1 is known not to fit.
319 * chars_used [out] The number of characters of str that have been "used" and
320 * do not need to be included in later text. For example this will
321 * include any spaces that have been discarded from the start of
323 * size [out] The size of the returned text in logical coordinates
325 * Pedantic assumption - Assumes that the text length is monotonically
326 * increasing with number of characters (i.e. no weird kernings)
330 * Work back from the last character that did fit to either a space or the last
331 * character of a word, whichever is met first.
332 * If there was one or the first character didn't fit then
333 * If the text is centered or right justified and that one character was a
334 * space then break the line before that character
335 * Otherwise break the line after that character
336 * and if the next character is a space then discard it.
337 * Suppose there was none (and the first character did fit).
338 * If Break Within Word is permitted
339 * break the word after the last character that fits (there must be
340 * at least one; none is caught earlier).
342 * discard any trailing space.
343 * include the whole word; it may be ellipsified later
345 * Break Within Word is permitted under a set of circumstances that are not
346 * totally clear yet. Currently our best guess is:
347 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
348 * DT_PATH_ELLIPSIS is
351 static void TEXT_WordBreak (HDC hdc
, WCHAR
*str
, unsigned int max_str
,
352 unsigned int *len_str
,
353 int width
, int format
, unsigned int chars_fit
,
354 unsigned int *chars_used
, SIZE
*size
)
362 assert (format
& DT_WORDBREAK
);
363 assert (chars_fit
< *len_str
);
365 sla
= heap_alloc(sizeof(SCRIPT_LOGATTR
) * *len_str
);
367 memset(&sa
, 0, sizeof(SCRIPT_ANALYSIS
));
368 sa
.eScript
= SCRIPT_UNDEFINED
;
370 ScriptBreak(str
, *len_str
, &sa
, sla
);
372 /* Work back from the last character that did fit to either a space or the
373 * last character of a word, whichever is met first.
375 p
= str
+ chars_fit
; /* The character that doesn't fit */
380 else if (sla
[i
].fSoftBreak
) /* chars_fit < *len_str so this is valid */
382 /* the word just fitted */
387 while (i
> 0 && !sla
[(--i
)+1].fSoftBreak
) p
--;
389 word_fits
= (i
!= 0 || sla
[i
+1].fSoftBreak
);
392 /* If there was one. */
396 /* break the line before/after that character */
397 if (!(format
& (DT_RIGHT
| DT_CENTER
)) || *p
!= SPACE
)
399 next_is_space
= (p
- str
) < *len_str
&& *p
== SPACE
;
401 /* and if the next character is a space then discard it. */
402 *chars_used
= *len_str
;
406 /* Suppose there was none. */
409 if ((format
& (DT_EDITCONTROL
| DT_WORD_ELLIPSIS
| DT_PATH_ELLIPSIS
)) ==
412 /* break the word after the last character that fits (there must be
416 *len_str
= chars_fit
;
417 *chars_used
= chars_fit
;
419 /* FIXME - possible error. Since the next character is now removed
420 * this could make the text longer so that it no longer fits, and
421 * so we need a loop to test and shrink.
427 /* discard any trailing space. */
428 const WCHAR
*e
= str
+ *len_str
;
430 while (p
< e
&& *p
!= SPACE
)
432 *chars_used
= p
- str
;
433 if (p
< e
) /* i.e. loop failed because *p == SPACE */
436 /* include the whole word; it may be ellipsified later */
438 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
439 * so that it will be too long
443 /* Remeasure the string */
444 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
448 /*********************************************************************
451 * Skip over the given number of characters, bearing in mind prefix
452 * substitution and the fact that a character may take more than one
453 * WCHAR (Unicode surrogates are two words long) (and there may have been
457 * new_count [out] The updated count
458 * new_str [out] The updated pointer
459 * start_count [in] The count of remaining characters corresponding to the
460 * start of the string
461 * start_str [in] The starting point of the string
462 * max [in] The number of characters actually in this segment of the
463 * string (the & counts)
464 * n [in] The number of characters to skip (if prefix then
466 * prefix [in] Apply prefix substitution
472 * There must be at least n characters in the string
473 * We need max because the "line" may have ended with a & followed by a tab
474 * or newline etc. which we don't want to swallow
477 static void TEXT_SkipChars (int *new_count
, const WCHAR
**new_str
,
478 int start_count
, const WCHAR
*start_str
,
479 int max
, int n
, int prefix
)
481 /* This is specific to wide characters, MSDN doesn't say anything much
482 * about Unicode surrogates yet and it isn't clear if _wcsinc will
483 * correctly handle them so we'll just do this the easy way for now
488 const WCHAR
*str_on_entry
= start_str
;
493 if ((*start_str
== PREFIX
|| *start_str
== ALPHA_PREFIX
) && max
--)
497 start_count
-= (start_str
- str_on_entry
);
504 *new_str
= start_str
;
505 *new_count
= start_count
;
508 /*********************************************************************
511 * Reanalyse the text to find the prefixed character. This is called when
512 * wordbreaking or ellipsification has shortened the string such that the
513 * previously noted prefixed character is no longer visible.
516 * str [in] The original string segment (including all characters)
517 * ns [in] The number of characters in str (including prefixes)
518 * pe [in] The ellipsification data
521 * The prefix offset within the new string segment (the one that contains the
522 * ellipses and does not contain the prefix characters) (-1 if none)
525 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
526 const ellipsis_data
*pe
)
530 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
532 for (i
= 0; i
< n
; i
++, str
++)
536 /* Reached the path ellipsis; jump over it */
537 if (ns
< pe
->under
) break;
541 if (!pe
->after
) break; /* Nothing after the path ellipsis */
545 if (*str
== PREFIX
|| *str
== ALPHA_PREFIX
)
550 result
= (i
< pe
->before
|| pe
->under
== 0) ? i
: i
- pe
->under
+ pe
->len
;
551 /* pe->len may be non-zero while pe_under is zero */
558 /*********************************************************************
559 * Returns true if and only if the remainder of the line is a single
560 * newline representation or nothing
563 static BOOL
remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
565 if (!num_chars
) return TRUE
;
566 if (*str
!= LF
&& *str
!= CR
) return FALSE
;
567 if (!--num_chars
) return TRUE
;
568 if (*str
== *(str
+1)) return FALSE
;
570 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
571 if (--num_chars
) return FALSE
;
575 /*********************************************************************
576 * Return next line of text from a string.
578 * hdc - handle to DC.
579 * str - string to parse into lines.
580 * count - length of str.
581 * dest - destination in which to return line.
582 * len - dest buffer size in chars on input, copied length into dest on output.
583 * width - maximum width of line in pixels.
584 * format - format type passed to DrawText.
585 * retsize - returned size of the line in pixels.
586 * last_line - TRUE if is the last line that will be processed
587 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
588 * the return string is built.
589 * tabwidth - The width of a tab in logical coordinates
590 * pprefix_offset - Here is where we return the offset within dest of the first
591 * prefixed (underlined) character. -1 is returned if there
592 * are none. Note that there may be more; the calling code
593 * will need to use TEXT_Reprefix to find any later ones.
594 * pellip - Here is where we return the information about any ellipsification
595 * that was carried out. Note that if tabs are being expanded then
596 * this data will correspond to the last text segment actually
597 * returned in dest; by definition there would not have been any
598 * ellipsification in earlier text segments of the line.
600 * Returns pointer to next char in str after end of the line
601 * or NULL if end of str reached.
603 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
604 WCHAR
*dest
, int *len
, int width
, DWORD format
,
605 SIZE
*retsize
, int last_line
, WCHAR
*modstr
,
606 int tabwidth
, int *pprefix_offset
,
607 ellipsis_data
*pellip
)
613 int seg_i
, seg_count
, seg_j
;
616 BOOL word_broken
, line_fits
, ellipsified
;
617 unsigned int j_in_seg
;
618 *pprefix_offset
= -1;
620 /* For each text segment in the line */
626 /* Skip any leading tabs */
628 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
630 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
631 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
632 while (*count
&& str
[i
] == TAB
)
635 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
640 /* Now copy as far as the next tab or cr/lf or eos */
647 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
648 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
650 if ((format
& DT_NOPREFIX
) || *count
<= 1)
652 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
656 if (str
[i
] == PREFIX
|| str
[i
] == ALPHA_PREFIX
) {
657 (*count
)--, i
++; /* Throw away the prefix itself */
658 if (str
[i
] == PREFIX
)
660 /* Swallow it before we see it again */
661 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
663 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
667 /* else the previous prefix was in an earlier segment of the
668 * line; we will leave it to the drawing code to catch this
672 else if (str
[i
] == KANA_PREFIX
)
674 /* Throw away katakana access keys */
675 (*count
)--, i
++; /* skip the prefix */
676 (*count
)--; i
++; /* skip the letter */
680 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
685 /* Measure the whole text segment and possibly WordBreak and
689 j_in_seg
= j
- seg_j
;
690 max_seg_width
= width
- plen
;
691 GetTextExtentExPointW (hdc
, dest
+ seg_j
, j_in_seg
, max_seg_width
, &num_fit
, NULL
, &size
);
693 /* The Microsoft handling of various combinations of formats is weird.
694 * The following may very easily be incorrect if several formats are
695 * combined, and may differ between versions (to say nothing of the
696 * several bugs in the Microsoft versions).
699 line_fits
= (num_fit
>= j_in_seg
);
700 if (!line_fits
&& (format
& DT_WORDBREAK
))
703 unsigned int chars_used
;
704 TEXT_WordBreak (hdc
, dest
+seg_j
, maxl
-seg_j
, &j_in_seg
,
705 max_seg_width
, format
, num_fit
, &chars_used
, &size
);
706 line_fits
= (size
.cx
<= max_seg_width
);
707 /* and correct the counts */
708 TEXT_SkipChars (count
, &s
, seg_count
, str
+seg_i
, i
-seg_i
,
709 chars_used
, !(format
& DT_NOPREFIX
));
713 pellip
->before
= j_in_seg
;
718 if (!line_fits
&& (format
& DT_PATH_ELLIPSIS
))
720 TEXT_PathEllipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
721 max_seg_width
, &size
, modstr
, pellip
);
722 line_fits
= (size
.cx
<= max_seg_width
);
725 /* NB we may end up ellipsifying a word-broken or path_ellipsified
727 if ((!line_fits
&& (format
& DT_WORD_ELLIPSIS
)) ||
728 ((format
& DT_END_ELLIPSIS
) &&
729 ((last_line
&& *count
) ||
730 (remainder_is_none_or_newline (*count
, &str
[i
]) && !line_fits
))))
732 int before
, len_ellipsis
;
733 TEXT_Ellipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
734 max_seg_width
, &size
, modstr
, &before
, &len_ellipsis
);
735 if (before
> pellip
->before
)
737 /* We must have done a path ellipsis too */
738 pellip
->after
= before
- pellip
->before
- pellip
->len
;
739 /* Leave the len as the length of the first ellipsis */
743 /* If we are here after a path ellipsification it must be
744 * because even the ellipsis itself didn't fit.
746 assert (pellip
->under
== 0 && pellip
->after
== 0);
747 pellip
->before
= before
;
748 pellip
->len
= len_ellipsis
;
749 /* pellip->after remains as zero as does
755 /* As an optimisation if we have ellipsified and we are expanding
756 * tabs and we haven't reached the end of the line we can skip to it
757 * now rather than going around the loop again.
759 if ((format
& DT_EXPANDTABS
) && ellipsified
)
761 if (format
& DT_SINGLELINE
)
765 while ((*count
) && str
[i
] != CR
&& str
[i
] != LF
)
772 j
= seg_j
+ j_in_seg
;
773 if (*pprefix_offset
>= seg_j
+ pellip
->before
)
775 *pprefix_offset
= TEXT_Reprefix (str
+ seg_i
, i
- seg_i
, pellip
);
776 if (*pprefix_offset
!= -1)
777 *pprefix_offset
+= seg_j
;
781 if (size
.cy
> retsize
->cy
)
782 retsize
->cy
= size
.cy
;
788 else if (str
[i
] == CR
|| str
[i
] == LF
)
791 if (*count
&& (str
[i
] == CR
|| str
[i
] == LF
) && str
[i
] != str
[i
-1])
797 /* else it was a Tab and we go around again */
809 /***********************************************************************
810 * TEXT_DrawUnderscore
812 * Draw the underline under the prefixed character
815 * hdc [in] The handle of the DC for drawing
816 * x [in] The x location of the line segment (logical coordinates)
817 * y [in] The y location of where the underscore should appear
818 * (logical coordinates)
819 * str [in] The text of the line segment
820 * offset [in] The offset of the underscored character within str
821 * rect [in] Clipping rectangle (if not NULL)
824 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
, const RECT
*rect
)
832 GetTextExtentPointW (hdc
, str
, offset
, &size
);
833 prefix_x
= x
+ size
.cx
;
834 GetTextExtentPointW (hdc
, str
, offset
+1, &size
);
835 prefix_end
= x
+ size
.cx
- 1;
836 /* The above method may eventually be slightly wrong due to kerning etc. */
838 /* Check for clipping */
840 if (prefix_x
> rect
->right
|| prefix_end
< rect
->left
|| y
< rect
->top
|| y
> rect
->bottom
)
841 return; /* Completely outside */
842 /* Partially outside */
843 if (prefix_x
< rect
->left
) prefix_x
= rect
->left
;
844 if (prefix_end
> rect
->right
) prefix_end
= rect
->right
;
847 hpen
= CreatePen (PS_SOLID
, 1, GetTextColor (hdc
));
848 oldPen
= SelectObject (hdc
, hpen
);
849 MoveToEx (hdc
, prefix_x
, y
, NULL
);
850 LineTo (hdc
, prefix_end
, y
);
851 SelectObject (hdc
, oldPen
);
855 /***********************************************************************
856 * DrawTextExW (USER32.@)
858 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
859 * is not quite complete, especially with regard to \0. We will assume that
860 * the returned string could have a length of up to i_count+3 and also have
861 * a trailing \0 (which would be 4 more than a not-null-terminated string but
862 * 3 more than a null-terminated string). If this is not so then increase
863 * the allowance in DrawTextExA.
865 #define MAX_BUFFER 1024
866 INT WINAPI
DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
867 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
873 WCHAR line
[MAX_BUFFER
];
874 int len
, lh
, count
=i_count
;
876 int lmargin
= 0, rmargin
= 0;
877 int x
= rect
->left
, y
= rect
->top
;
878 int width
= rect
->right
- rect
->left
;
881 int tabwidth
/* to keep gcc happy */ = 0;
887 TRACE("%s, %d, [%s] %08x\n", debugstr_wn (str
, count
), count
,
888 wine_dbgstr_rect(rect
), flags
);
890 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
891 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
897 if (flags
& DT_SINGLELINE
)
898 flags
&= ~DT_WORDBREAK
;
900 GetTextMetricsW(hdc
, &tm
);
901 if (flags
& DT_EXTERNALLEADING
)
902 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
906 if (str
[0] && count
== 0)
909 if (dtp
&& dtp
->cbSize
!= sizeof(DRAWTEXTPARAMS
))
914 count
= lstrlenW(str
);
917 if( flags
& DT_CALCRECT
)
919 rect
->right
= rect
->left
;
920 if( flags
& DT_SINGLELINE
)
921 rect
->bottom
= rect
->top
+ lh
;
923 rect
->bottom
= rect
->top
;
929 if (GetGraphicsMode(hdc
) == GM_COMPATIBLE
)
931 SIZE window_ext
, viewport_ext
;
932 GetWindowExtEx(hdc
, &window_ext
);
933 GetViewportExtEx(hdc
, &viewport_ext
);
934 if ((window_ext
.cy
> 0) != (viewport_ext
.cy
> 0))
940 lmargin
= dtp
->iLeftMargin
;
941 rmargin
= dtp
->iRightMargin
;
942 width
-= lmargin
+ rmargin
;
943 if (!(flags
& (DT_CENTER
| DT_RIGHT
)))
945 dtp
->uiLengthDrawn
= 0; /* This param RECEIVES number of chars processed */
948 if (flags
& DT_EXPANDTABS
)
950 int tabstop
= ((flags
& DT_TABSTOP
) && dtp
&& dtp
->iTabLength
) ? dtp
->iTabLength
: 8;
951 tabwidth
= tm
.tmAveCharWidth
* tabstop
;
954 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
956 if (flags
& DT_MODIFYSTRING
)
958 size_retstr
= (count
+ 4) * sizeof (WCHAR
);
959 retstr
= heap_alloc(size_retstr
);
960 if (!retstr
) return 0;
961 memcpy (retstr
, str
, size_retstr
);
971 len
= ARRAY_SIZE(line
);
973 last_line
= !(flags
& DT_NOCLIP
) && y
- ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) < rect
->bottom
;
975 last_line
= !(flags
& DT_NOCLIP
) && y
+ ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) > rect
->bottom
;
976 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
, &size
, last_line
, retstr
, tabwidth
, &prefix_offset
, &ellip
);
978 if (flags
& DT_CENTER
)
979 x
= (rect
->left
+ lmargin
+ rect
->right
- rmargin
- size
.cx
) / 2;
980 else if (flags
& DT_RIGHT
)
981 x
= rect
->right
- size
.cx
- rmargin
;
983 if (flags
& DT_SINGLELINE
)
985 if (flags
& DT_VCENTER
) y
= rect
->top
+
986 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
987 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
990 if (!(flags
& DT_CALCRECT
))
992 const WCHAR
*str
= line
;
998 if ((flags
& DT_EXPANDTABS
))
1001 p
= str
; while (p
< str
+len
&& *p
!= TAB
) p
++;
1003 if (len_seg
!= len
&& !GetTextExtentPointW(hdc
, str
, len_seg
, &size
)) goto done
;
1008 if (!ExtTextOutW( hdc
, xseg
, y
,
1009 ((flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
) |
1010 ((flags
& DT_RTLREADING
) ? ETO_RTLREADING
: 0),
1011 rect
, str
, len_seg
, NULL
))
1013 if (prefix_offset
!= -1 && prefix_offset
< len_seg
)
1015 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
, (flags
& DT_NOCLIP
) ? NULL
: rect
);
1021 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
1023 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
1024 if (prefix_offset
!= -1)
1026 if (prefix_offset
< len_seg
)
1028 /* We have just drawn an underscore; we ought to
1029 * figure out where the next one is. I am going
1030 * to leave it for now until I have a better model
1031 * for the line, which will make reprefixing easier.
1032 * This is where ellip would be used.
1037 prefix_offset
-= len_seg
;
1042 else if (size
.cx
> max_width
)
1043 max_width
= size
.cx
;
1050 dtp
->uiLengthDrawn
+= len
;
1052 while (strPtr
&& !last_line
);
1054 if (flags
& DT_CALCRECT
)
1056 rect
->right
= rect
->left
+ max_width
;
1059 rect
->right
+= lmargin
+ rmargin
;
1062 if (retstr
) memcpy(str
, retstr
, size_retstr
);
1064 ret
= y
- rect
->top
;
1065 if (ret
== 0) ret
= 1;
1071 /***********************************************************************
1072 * DrawTextExA (USER32.@)
1074 * If DT_MODIFYSTRING is specified then there must be room for up to
1075 * 4 extra characters. We take great care about just how much modified
1078 INT WINAPI
DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
1079 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
1090 if (!count
) return 0;
1091 if (!str
&& count
> 0) return 0;
1092 if( !str
|| ((count
== -1) && !(count
= strlen(str
))))
1097 if (dtp
&& dtp
->cbSize
!= sizeof(DRAWTEXTPARAMS
))
1100 GetTextMetricsA(hdc
, &tm
);
1101 if (flags
& DT_EXTERNALLEADING
)
1102 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
1106 if( flags
& DT_CALCRECT
)
1108 rect
->right
= rect
->left
;
1109 if( flags
& DT_SINGLELINE
)
1110 rect
->bottom
= rect
->top
+ lh
;
1112 rect
->bottom
= rect
->top
;
1116 cp
= GdiGetCodePage( hdc
);
1117 wcount
= MultiByteToWideChar( cp
, 0, str
, count
, NULL
, 0 );
1120 if (flags
& DT_MODIFYSTRING
)
1125 wstr
= heap_alloc(wmax
* sizeof(WCHAR
));
1128 MultiByteToWideChar( cp
, 0, str
, count
, wstr
, wcount
);
1129 if (flags
& DT_MODIFYSTRING
)
1130 for (i
=4, p
=wstr
+wcount
; i
--; p
++) *p
=0xFFFE;
1131 /* Initialise the extra characters so that we can see which ones
1132 * change. U+FFFE is guaranteed to be not a unicode character and
1133 * so will not be generated by DrawTextEx itself.
1135 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, dtp
);
1136 if (flags
& DT_MODIFYSTRING
)
1138 /* Unfortunately the returned string may contain multiple \0s
1139 * and so we need to measure it ourselves.
1141 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1142 WideCharToMultiByte( cp
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1149 /***********************************************************************
1150 * DrawTextW (USER32.@)
1152 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1156 memset (&dtp
, 0, sizeof(dtp
));
1157 dtp
.cbSize
= sizeof(dtp
);
1158 if (flags
& DT_TABSTOP
)
1160 dtp
.iTabLength
= (flags
>> 8) & 0xff;
1161 flags
&= 0xffff00ff;
1163 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1166 /***********************************************************************
1167 * DrawTextA (USER32.@)
1169 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1173 memset (&dtp
, 0, sizeof(dtp
));
1174 dtp
.cbSize
= sizeof(dtp
);
1175 if (flags
& DT_TABSTOP
)
1177 dtp
.iTabLength
= (flags
>> 8) & 0xff;
1178 flags
&= 0xffff00ff;
1180 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);
1183 /***********************************************************************
1185 * GrayString functions
1188 /* callback for ASCII gray string proc */
1189 static BOOL CALLBACK
gray_string_callbackA( HDC hdc
, LPARAM param
, INT len
)
1191 return TextOutA( hdc
, 0, 0, (LPCSTR
)param
, len
);
1194 /* callback for Unicode gray string proc */
1195 static BOOL CALLBACK
gray_string_callbackW( HDC hdc
, LPARAM param
, INT len
)
1197 return TextOutW( hdc
, 0, 0, (LPCWSTR
)param
, len
);
1200 /***********************************************************************
1203 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
, GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
1204 INT x
, INT y
, INT cx
, INT cy
)
1206 HBITMAP hbm
, hbmsave
;
1214 if(!hdc
) return FALSE
;
1215 if (!(memdc
= CreateCompatibleDC(hdc
))) return FALSE
;
1217 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
1218 hbmsave
= SelectObject(memdc
, hbm
);
1219 hbsave
= SelectObject( memdc
, GetStockObject(BLACK_BRUSH
) );
1220 PatBlt( memdc
, 0, 0, cx
, cy
, PATCOPY
);
1221 SelectObject( memdc
, hbsave
);
1222 SetTextColor(memdc
, RGB(255, 255, 255));
1223 SetBkColor(memdc
, RGB(0, 0, 0));
1224 hfsave
= SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
1226 retval
= fn(memdc
, lp
, slen
);
1227 SelectObject(memdc
, hfsave
);
1230 * Windows doc says that the bitmap isn't grayed when len == -1 and
1231 * the callback function returns FALSE. However, testing this on
1232 * win95 showed otherwise...
1234 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1235 if(retval
|| len
!= -1)
1238 hbsave
= SelectObject(memdc
, SYSCOLOR_Get55AABrush());
1239 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
1240 SelectObject(memdc
, hbsave
);
1243 if(hb
) hbsave
= SelectObject(hdc
, hb
);
1244 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
1245 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
1246 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
1247 SetTextColor(hdc
, fg
);
1248 SetBkColor(hdc
, bg
);
1249 if(hb
) SelectObject(hdc
, hbsave
);
1251 SelectObject(memdc
, hbmsave
);
1258 /***********************************************************************
1259 * GrayStringA (USER32.@)
1261 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1262 LPARAM lParam
, INT cch
, INT x
, INT y
,
1265 if (!cch
) cch
= strlen( (LPCSTR
)lParam
);
1266 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1269 GetTextExtentPoint32A( hdc
, (LPCSTR
)lParam
, cch
, &s
);
1270 if (cx
== 0) cx
= s
.cx
;
1271 if (cy
== 0) cy
= s
.cy
;
1273 if (!gsprc
) gsprc
= gray_string_callbackA
;
1274 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1278 /***********************************************************************
1279 * GrayStringW (USER32.@)
1281 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1282 LPARAM lParam
, INT cch
, INT x
, INT y
,
1285 if (!cch
) cch
= lstrlenW( (LPCWSTR
)lParam
);
1286 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1289 GetTextExtentPoint32W( hdc
, (LPCWSTR
)lParam
, cch
, &s
);
1290 if (cx
== 0) cx
= s
.cx
;
1291 if (cy
== 0) cy
= s
.cy
;
1293 if (!gsprc
) gsprc
= gray_string_callbackW
;
1294 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1298 /***********************************************************************
1299 * TEXT_TabbedTextOut
1301 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1302 * Note: this doesn't work too well for text-alignment modes other
1303 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1305 static LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCWSTR lpstr
,
1306 INT count
, INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
,
1315 if (!lpstr
|| count
== 0) return 0;
1320 GetTextMetricsW( hdc
, &tm
);
1324 defWidth
= *lpTabPos
;
1329 defWidth
= 8 * tm
.tmAveCharWidth
;
1338 /* chop the string into substrings of 0 or more <tabs>
1339 * possibly followed by 1 or more normal characters */
1340 for (i
= 0; i
< count
; i
++)
1341 if (lpstr
[i
] != '\t') break;
1342 for (j
= i
; j
< count
; j
++)
1343 if (lpstr
[j
] == '\t') break;
1344 /* get the extent of the normal character part */
1345 GetTextExtentPointW( hdc
, lpstr
+ i
, j
- i
, &extent
);
1346 /* and if there is a <tab>, calculate its position */
1348 /* get x coordinate for the drawing of this string */
1349 for (; cTabStops
>= i
; lpTabPos
++, cTabStops
--)
1351 if( nTabOrg
+ abs( *lpTabPos
) > x
) {
1352 if( lpTabPos
[ i
- 1] >= 0) {
1353 /* a left aligned tab */
1354 x0
= nTabOrg
+ lpTabPos
[i
-1];
1360 /* if tab pos is negative then text is right-aligned
1361 * to tab stop meaning that the string extends to the
1362 * left, so we must subtract the width of the string */
1363 if (nTabOrg
- lpTabPos
[ i
- 1] - extent
.cx
> x
)
1365 x
= nTabOrg
- lpTabPos
[ i
- 1];
1372 /* if we have run out of tab stops and we have a valid default tab
1373 * stop width then round x up to that width */
1374 if ((cTabStops
< i
) && (defWidth
> 0)) {
1375 x0
= nTabOrg
+ ((x
- nTabOrg
) / defWidth
+ i
) * defWidth
;
1377 } else if ((cTabStops
< i
) && (defWidth
< 0)) {
1378 x
= nTabOrg
+ ((x
- nTabOrg
+ extent
.cx
) / -defWidth
+ i
)
1385 if (!extent
.cy
) extent
.cy
= tm
.tmHeight
;
1391 r
.bottom
= y
+ extent
.cy
;
1392 ExtTextOutW( hdc
, x0
, y
, GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
1393 &r
, lpstr
+ i
, j
- i
, NULL
);
1399 return MAKELONG(x
- start
, extent
.cy
);
1403 /***********************************************************************
1404 * TabbedTextOutA (USER32.@)
1406 * See TabbedTextOutW.
1408 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
1409 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1412 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1413 LPWSTR strW
= heap_alloc( len
* sizeof(WCHAR
) );
1414 if (!strW
) return 0;
1415 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1416 ret
= TabbedTextOutW( hdc
, x
, y
, strW
, len
, cTabStops
, lpTabPos
, nTabOrg
);
1422 /***********************************************************************
1423 * TabbedTextOutW (USER32.@)
1425 * Draws tabbed text aligned using the specified tab stops.
1428 * hdc [I] Handle to device context to draw to.
1429 * x [I] X co-ordinate to start drawing the text at in logical units.
1430 * y [I] Y co-ordinate to start drawing the text at in logical units.
1431 * str [I] Pointer to the characters to draw.
1432 * count [I] Number of WCHARs pointed to by str.
1433 * cTabStops [I] Number of tab stops pointed to by lpTabPos.
1434 * lpTabPos [I] Tab stops in logical units. Should be sorted in ascending order.
1435 * nTabOrg [I] Starting position to expand tabs from in logical units.
1438 * The dimensions of the string drawn. The height is in the high-order word
1439 * and the width is in the low-order word.
1442 * The tabs stops can be negative, in which case the text is right aligned to
1443 * that tab stop and, despite what MSDN says, this is supported on
1447 * MSDN says that the TA_UPDATECP from GetTextAlign causes this function to
1448 * ignore the x and y co-ordinates, but this is unimplemented at the moment.
1450 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
1451 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1453 TRACE("%p %d,%d %s %d\n", hdc
, x
, y
, debugstr_wn(str
,count
), count
);
1454 return TEXT_TabbedTextOut( hdc
, x
, y
, str
, count
, cTabStops
, lpTabPos
, nTabOrg
, TRUE
);
1458 /***********************************************************************
1459 * GetTabbedTextExtentA (USER32.@)
1461 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
1462 INT cTabStops
, const INT
*lpTabPos
)
1465 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1466 LPWSTR strW
= heap_alloc( len
* sizeof(WCHAR
) );
1467 if (!strW
) return 0;
1468 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1469 ret
= GetTabbedTextExtentW( hdc
, strW
, len
, cTabStops
, lpTabPos
);
1475 /***********************************************************************
1476 * GetTabbedTextExtentW (USER32.@)
1478 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
1479 INT cTabStops
, const INT
*lpTabPos
)
1481 TRACE("%p %s %d\n", hdc
, debugstr_wn(lpstr
,count
), count
);
1482 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
, lpTabPos
, 0, FALSE
);