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
28 #include "wine/port.h"
38 #include "wine/unicode.h"
42 #include "user_private.h"
43 #include "wine/debug.h"
44 #include "wine/heap.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(text
);
48 /*********************************************************************
53 * How many buffers to use
54 * While processing in DrawText there are potentially three different forms
55 * of the text that need to be held. How are they best held?
56 * 1. The original text is needed, of course, to see what to display.
57 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
59 * 3. The buffered text that is about to be displayed e.g. the current line.
60 * Typically this will exclude the ampersands used for prefixing etc.
63 * a. If the buffered text to be displayed includes the ampersands then
64 * we will need special measurement and draw functions that will ignore
65 * the ampersands (e.g. by copying to a buffer without the prefix and
66 * then using the normal forms). This may involve less space but may
67 * require more processing. e.g. since a line containing tabs may
68 * contain several underlined characters either we need to carry around
69 * a list of prefix locations or we may need to locate them several
71 * b. If we actually directly modify the "original text" as we go then we
72 * will need some special "caching" to handle the fact that when we
73 * ellipsify the text the ellipsis may modify the next line of text,
74 * which we have not yet processed. (e.g. ellipsification of a W at the
75 * end of a line will overwrite the W, the \n and the first character of
76 * the next line, and a \0 will overwrite the second. Try it!!)
78 * Option 1. Three separate storages. (To be implemented)
79 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
80 * the edited string in some form, either as the string itself or as some
81 * sort of "edit list" to be applied just before returning.
82 * Use a buffer that holds the ellipsified current line sans ampersands
83 * and accept the need occasionally to recalculate the prefixes (if
84 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
92 #define ALPHA_PREFIX 30 /* Win16: Alphabet prefix */
93 #define KANA_PREFIX 31 /* Win16: Katakana prefix */
95 #define FORWARD_SLASH '/'
96 #define BACK_SLASH '\\'
98 static const WCHAR ELLIPSISW
[] = {'.','.','.', 0};
100 typedef struct tag_ellipsis_data
108 /*********************************************************************
109 * TEXT_Ellipsify (static)
111 * Add an ellipsis to the end of the given string whilst ensuring it fits.
113 * If the ellipsis alone doesn't fit then it will be returned anyway.
115 * See Also TEXT_PathEllipsify
118 * hdc [in] The handle to the DC that defines the font.
119 * str [in/out] The string that needs to be modified.
120 * max_str [in] The dimension of str (number of WCHAR).
121 * len_str [in/out] The number of characters in str
122 * width [in] The maximum width permitted (in logical coordinates)
123 * size [out] The dimensions of the text
124 * modstr [out] The modified form of the string, to be returned to the
125 * calling program. It is assumed that the caller has
126 * made sufficient space available so we don't need to
127 * know the size of the space. This pointer may be NULL if
128 * the modified string is not required.
129 * len_before [out] The number of characters before the ellipsis.
130 * len_ellip [out] The number of characters in the ellipsis.
132 * See for example Microsoft article Q249678.
134 * For now we will simply use three dots rather than worrying about whether
135 * the font contains an explicit ellipsis character.
137 static void TEXT_Ellipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
138 unsigned int *len_str
, int width
, SIZE
*size
,
140 int *len_before
, int *len_ellip
)
142 unsigned int len_ellipsis
;
143 unsigned int lo
, mid
, hi
;
145 len_ellipsis
= strlenW (ELLIPSISW
);
146 if (len_ellipsis
> max_len
) len_ellipsis
= max_len
;
147 if (*len_str
> max_len
- len_ellipsis
)
148 *len_str
= max_len
- len_ellipsis
;
150 /* First do a quick binary search to get an upper bound for *len_str. */
152 GetTextExtentExPointW(hdc
, str
, *len_str
, width
, NULL
, NULL
, size
) &&
155 for (lo
= 0, hi
= *len_str
; lo
< hi
; )
158 if (!GetTextExtentExPointW(hdc
, str
, mid
, width
, NULL
, NULL
, size
))
160 if (size
->cx
> width
)
167 /* Now this should take only a couple iterations at most. */
170 memcpy(str
+ *len_str
, ELLIPSISW
, len_ellipsis
*sizeof(WCHAR
));
172 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
173 NULL
, NULL
, size
)) break;
175 if (!*len_str
|| size
->cx
<= width
) break;
179 *len_ellip
= len_ellipsis
;
180 *len_before
= *len_str
;
181 *len_str
+= len_ellipsis
;
185 memcpy (modstr
, str
, *len_str
* sizeof(WCHAR
));
186 modstr
[*len_str
] = '\0';
190 /*********************************************************************
191 * TEXT_PathEllipsify (static)
193 * Add an ellipsis to the provided string in order to make it fit within
194 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
197 * See Also TEXT_Ellipsify
200 * hdc [in] The handle to the DC that defines the font.
201 * str [in/out] The string that needs to be modified
202 * max_str [in] The dimension of str (number of WCHAR).
203 * len_str [in/out] The number of characters in str
204 * width [in] The maximum width permitted (in logical coordinates)
205 * size [out] The dimensions of the text
206 * modstr [out] The modified form of the string, to be returned to the
207 * calling program. It is assumed that the caller has
208 * made sufficient space available so we don't need to
209 * know the size of the space. This pointer may be NULL if
210 * the modified string is not required.
211 * pellip [out] The ellipsification results
213 * For now we will simply use three dots rather than worrying about whether
214 * the font contains an explicit ellipsis character.
216 * The following applies, I think to Win95. We will need to extend it for
217 * Win98 which can have both path and end ellipsis at the same time (e.g.
218 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
220 * The resulting string consists of as much as possible of the following:
221 * 1. The ellipsis itself
222 * 2. The last \ or / of the string (if any)
223 * 3. Everything after the last \ or / of the string (if any) or the whole
224 * string if there is no / or \. I believe that under Win95 this would
225 * include everything even though some might be clipped off the end whereas
226 * under Win98 that might be ellipsified too.
227 * Yet to be investigated is whether this would include wordbreaking if the
228 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
229 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
230 * broken within words).
231 * 4. All the stuff before the / or \, which is placed before the ellipsis.
233 static void TEXT_PathEllipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
234 unsigned int *len_str
, int width
, SIZE
*size
,
235 WCHAR
*modstr
, ellipsis_data
*pellip
)
240 WCHAR
*lastBkSlash
, *lastFwdSlash
, *lastSlash
;
242 len_ellipsis
= strlenW (ELLIPSISW
);
243 if (!max_len
) return;
244 if (len_ellipsis
>= max_len
) len_ellipsis
= max_len
- 1;
245 if (*len_str
+ len_ellipsis
>= max_len
)
246 *len_str
= max_len
- len_ellipsis
-1;
247 /* Hopefully this will never happen, otherwise it would probably lose
248 * the wrong character
250 str
[*len_str
] = '\0'; /* to simplify things */
252 lastBkSlash
= strrchrW (str
, BACK_SLASH
);
253 lastFwdSlash
= strrchrW (str
, FORWARD_SLASH
);
254 lastSlash
= lastBkSlash
> lastFwdSlash
? lastBkSlash
: lastFwdSlash
;
255 if (!lastSlash
) lastSlash
= str
;
256 len_trailing
= *len_str
- (lastSlash
- str
);
258 /* overlap-safe movement to the right */
259 memmove (lastSlash
+len_ellipsis
, lastSlash
, len_trailing
* sizeof(WCHAR
));
260 memcpy (lastSlash
, ELLIPSISW
, len_ellipsis
*sizeof(WCHAR
));
261 len_trailing
+= len_ellipsis
;
262 /* From this point on lastSlash actually points to the ellipsis in front
263 * of the last slash and len_trailing includes the ellipsis
269 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
270 NULL
, NULL
, size
)) break;
272 if (lastSlash
== str
|| size
->cx
<= width
) break;
274 /* overlap-safe movement to the left */
275 memmove (lastSlash
-1, lastSlash
, len_trailing
* sizeof(WCHAR
));
282 pellip
->before
= lastSlash
-str
;
283 pellip
->len
= len_ellipsis
;
284 pellip
->under
= len_under
;
285 pellip
->after
= len_trailing
- len_ellipsis
;
286 *len_str
+= len_ellipsis
;
290 memcpy(modstr
, str
, *len_str
* sizeof(WCHAR
));
291 modstr
[*len_str
] = '\0';
295 /*********************************************************************
296 * TEXT_WordBreak (static)
298 * Perform wordbreak processing on the given string
300 * Assumes that DT_WORDBREAK has been specified and not all the characters
301 * fit. Note that this function should even be called when the first character
302 * that doesn't fit is known to be a space or tab, so that it can swallow them.
304 * Note that the Windows processing has some strange properties.
305 * 1. If the text is left-justified and there is room for some of the spaces
306 * that follow the last word on the line then those that fit are included on
308 * 2. If the text is centered or right-justified and there is room for some of
309 * the spaces that follow the last word on the line then all but one of those
310 * that fit are included on the line.
311 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
312 * character of a new line it will be skipped.
315 * hdc [in] The handle to the DC that defines the font.
316 * str [in/out] The string that needs to be broken.
317 * max_str [in] The dimension of str (number of WCHAR).
318 * len_str [in/out] The number of characters in str
319 * width [in] The maximum width permitted
320 * format [in] The format flags in effect
321 * chars_fit [in] The maximum number of characters of str that are already
322 * known to fit; chars_fit+1 is known not to fit.
323 * chars_used [out] The number of characters of str that have been "used" and
324 * do not need to be included in later text. For example this will
325 * include any spaces that have been discarded from the start of
327 * size [out] The size of the returned text in logical coordinates
329 * Pedantic assumption - Assumes that the text length is monotonically
330 * increasing with number of characters (i.e. no weird kernings)
334 * Work back from the last character that did fit to either a space or the last
335 * character of a word, whichever is met first.
336 * If there was one or the first character didn't fit then
337 * If the text is centered or right justified and that one character was a
338 * space then break the line before that character
339 * Otherwise break the line after that character
340 * and if the next character is a space then discard it.
341 * Suppose there was none (and the first character did fit).
342 * If Break Within Word is permitted
343 * break the word after the last character that fits (there must be
344 * at least one; none is caught earlier).
346 * discard any trailing space.
347 * include the whole word; it may be ellipsified later
349 * Break Within Word is permitted under a set of circumstances that are not
350 * totally clear yet. Currently our best guess is:
351 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
352 * DT_PATH_ELLIPSIS is
355 static void TEXT_WordBreak (HDC hdc
, WCHAR
*str
, unsigned int max_str
,
356 unsigned int *len_str
,
357 int width
, int format
, unsigned int chars_fit
,
358 unsigned int *chars_used
, SIZE
*size
)
366 assert (format
& DT_WORDBREAK
);
367 assert (chars_fit
< *len_str
);
369 sla
= heap_alloc(sizeof(SCRIPT_LOGATTR
) * *len_str
);
371 memset(&sa
, 0, sizeof(SCRIPT_ANALYSIS
));
372 sa
.eScript
= SCRIPT_UNDEFINED
;
374 ScriptBreak(str
, *len_str
, &sa
, sla
);
376 /* Work back from the last character that did fit to either a space or the
377 * last character of a word, whichever is met first.
379 p
= str
+ chars_fit
; /* The character that doesn't fit */
384 else if (sla
[i
].fSoftBreak
) /* chars_fit < *len_str so this is valid */
386 /* the word just fitted */
391 while (i
> 0 && !sla
[(--i
)+1].fSoftBreak
) p
--;
393 word_fits
= (i
!= 0 || sla
[i
+1].fSoftBreak
);
396 /* If there was one. */
400 /* break the line before/after that character */
401 if (!(format
& (DT_RIGHT
| DT_CENTER
)) || *p
!= SPACE
)
403 next_is_space
= (p
- str
) < *len_str
&& *p
== SPACE
;
405 /* and if the next character is a space then discard it. */
406 *chars_used
= *len_str
;
410 /* Suppose there was none. */
413 if ((format
& (DT_EDITCONTROL
| DT_WORD_ELLIPSIS
| DT_PATH_ELLIPSIS
)) ==
416 /* break the word after the last character that fits (there must be
420 *len_str
= chars_fit
;
421 *chars_used
= chars_fit
;
423 /* FIXME - possible error. Since the next character is now removed
424 * this could make the text longer so that it no longer fits, and
425 * so we need a loop to test and shrink.
431 /* discard any trailing space. */
432 const WCHAR
*e
= str
+ *len_str
;
434 while (p
< e
&& *p
!= SPACE
)
436 *chars_used
= p
- str
;
437 if (p
< e
) /* i.e. loop failed because *p == SPACE */
440 /* include the whole word; it may be ellipsified later */
442 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
443 * so that it will be too long
447 /* Remeasure the string */
448 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
452 /*********************************************************************
455 * Skip over the given number of characters, bearing in mind prefix
456 * substitution and the fact that a character may take more than one
457 * WCHAR (Unicode surrogates are two words long) (and there may have been
461 * new_count [out] The updated count
462 * new_str [out] The updated pointer
463 * start_count [in] The count of remaining characters corresponding to the
464 * start of the string
465 * start_str [in] The starting point of the string
466 * max [in] The number of characters actually in this segment of the
467 * string (the & counts)
468 * n [in] The number of characters to skip (if prefix then
470 * prefix [in] Apply prefix substitution
476 * There must be at least n characters in the string
477 * We need max because the "line" may have ended with a & followed by a tab
478 * or newline etc. which we don't want to swallow
481 static void TEXT_SkipChars (int *new_count
, const WCHAR
**new_str
,
482 int start_count
, const WCHAR
*start_str
,
483 int max
, int n
, int prefix
)
485 /* This is specific to wide characters, MSDN doesn't say anything much
486 * about Unicode surrogates yet and it isn't clear if _wcsinc will
487 * correctly handle them so we'll just do this the easy way for now
492 const WCHAR
*str_on_entry
= start_str
;
497 if ((*start_str
== PREFIX
|| *start_str
== ALPHA_PREFIX
) && max
--)
501 start_count
-= (start_str
- str_on_entry
);
508 *new_str
= start_str
;
509 *new_count
= start_count
;
512 /*********************************************************************
515 * Reanalyse the text to find the prefixed character. This is called when
516 * wordbreaking or ellipsification has shortened the string such that the
517 * previously noted prefixed character is no longer visible.
520 * str [in] The original string segment (including all characters)
521 * ns [in] The number of characters in str (including prefixes)
522 * pe [in] The ellipsification data
525 * The prefix offset within the new string segment (the one that contains the
526 * ellipses and does not contain the prefix characters) (-1 if none)
529 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
530 const ellipsis_data
*pe
)
534 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
536 for (i
= 0; i
< n
; i
++, str
++)
540 /* Reached the path ellipsis; jump over it */
541 if (ns
< pe
->under
) break;
545 if (!pe
->after
) break; /* Nothing after the path ellipsis */
549 if (*str
== PREFIX
|| *str
== ALPHA_PREFIX
)
554 result
= (i
< pe
->before
|| pe
->under
== 0) ? i
: i
- pe
->under
+ pe
->len
;
555 /* pe->len may be non-zero while pe_under is zero */
562 /*********************************************************************
563 * Returns true if and only if the remainder of the line is a single
564 * newline representation or nothing
567 static BOOL
remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
569 if (!num_chars
) return TRUE
;
570 if (*str
!= LF
&& *str
!= CR
) return FALSE
;
571 if (!--num_chars
) return TRUE
;
572 if (*str
== *(str
+1)) return FALSE
;
574 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
575 if (--num_chars
) return FALSE
;
579 /*********************************************************************
580 * Return next line of text from a string.
582 * hdc - handle to DC.
583 * str - string to parse into lines.
584 * count - length of str.
585 * dest - destination in which to return line.
586 * len - dest buffer size in chars on input, copied length into dest on output.
587 * width - maximum width of line in pixels.
588 * format - format type passed to DrawText.
589 * retsize - returned size of the line in pixels.
590 * last_line - TRUE if is the last line that will be processed
591 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
592 * the return string is built.
593 * tabwidth - The width of a tab in logical coordinates
594 * pprefix_offset - Here is where we return the offset within dest of the first
595 * prefixed (underlined) character. -1 is returned if there
596 * are none. Note that there may be more; the calling code
597 * will need to use TEXT_Reprefix to find any later ones.
598 * pellip - Here is where we return the information about any ellipsification
599 * that was carried out. Note that if tabs are being expanded then
600 * this data will correspond to the last text segment actually
601 * returned in dest; by definition there would not have been any
602 * ellipsification in earlier text segments of the line.
604 * Returns pointer to next char in str after end of the line
605 * or NULL if end of str reached.
607 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
608 WCHAR
*dest
, int *len
, int width
, DWORD format
,
609 SIZE
*retsize
, int last_line
, WCHAR
*modstr
,
610 int tabwidth
, int *pprefix_offset
,
611 ellipsis_data
*pellip
)
617 int seg_i
, seg_count
, seg_j
;
620 BOOL word_broken
, line_fits
, ellipsified
;
621 unsigned int j_in_seg
;
622 *pprefix_offset
= -1;
624 /* For each text segment in the line */
630 /* Skip any leading tabs */
632 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
634 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
635 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
636 while (*count
&& str
[i
] == TAB
)
639 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
644 /* Now copy as far as the next tab or cr/lf or eos */
651 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
652 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
654 if ((format
& DT_NOPREFIX
) || *count
<= 1)
656 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
660 if (str
[i
] == PREFIX
|| str
[i
] == ALPHA_PREFIX
) {
661 (*count
)--, i
++; /* Throw away the prefix itself */
662 if (str
[i
] == PREFIX
)
664 /* Swallow it before we see it again */
665 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
667 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
671 /* else the previous prefix was in an earlier segment of the
672 * line; we will leave it to the drawing code to catch this
676 else if (str
[i
] == KANA_PREFIX
)
678 /* Throw away katakana access keys */
679 (*count
)--, i
++; /* skip the prefix */
680 (*count
)--; i
++; /* skip the letter */
684 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
689 /* Measure the whole text segment and possibly WordBreak and
693 j_in_seg
= j
- seg_j
;
694 max_seg_width
= width
- plen
;
695 GetTextExtentExPointW (hdc
, dest
+ seg_j
, j_in_seg
, max_seg_width
, &num_fit
, NULL
, &size
);
697 /* The Microsoft handling of various combinations of formats is weird.
698 * The following may very easily be incorrect if several formats are
699 * combined, and may differ between versions (to say nothing of the
700 * several bugs in the Microsoft versions).
703 line_fits
= (num_fit
>= j_in_seg
);
704 if (!line_fits
&& (format
& DT_WORDBREAK
))
707 unsigned int chars_used
;
708 TEXT_WordBreak (hdc
, dest
+seg_j
, maxl
-seg_j
, &j_in_seg
,
709 max_seg_width
, format
, num_fit
, &chars_used
, &size
);
710 line_fits
= (size
.cx
<= max_seg_width
);
711 /* and correct the counts */
712 TEXT_SkipChars (count
, &s
, seg_count
, str
+seg_i
, i
-seg_i
,
713 chars_used
, !(format
& DT_NOPREFIX
));
717 pellip
->before
= j_in_seg
;
722 if (!line_fits
&& (format
& DT_PATH_ELLIPSIS
))
724 TEXT_PathEllipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
725 max_seg_width
, &size
, modstr
, pellip
);
726 line_fits
= (size
.cx
<= max_seg_width
);
729 /* NB we may end up ellipsifying a word-broken or path_ellipsified
731 if ((!line_fits
&& (format
& DT_WORD_ELLIPSIS
)) ||
732 ((format
& DT_END_ELLIPSIS
) &&
733 ((last_line
&& *count
) ||
734 (remainder_is_none_or_newline (*count
, &str
[i
]) && !line_fits
))))
736 int before
, len_ellipsis
;
737 TEXT_Ellipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
738 max_seg_width
, &size
, modstr
, &before
, &len_ellipsis
);
739 if (before
> pellip
->before
)
741 /* We must have done a path ellipsis too */
742 pellip
->after
= before
- pellip
->before
- pellip
->len
;
743 /* Leave the len as the length of the first ellipsis */
747 /* If we are here after a path ellipsification it must be
748 * because even the ellipsis itself didn't fit.
750 assert (pellip
->under
== 0 && pellip
->after
== 0);
751 pellip
->before
= before
;
752 pellip
->len
= len_ellipsis
;
753 /* pellip->after remains as zero as does
759 /* As an optimisation if we have ellipsified and we are expanding
760 * tabs and we haven't reached the end of the line we can skip to it
761 * now rather than going around the loop again.
763 if ((format
& DT_EXPANDTABS
) && ellipsified
)
765 if (format
& DT_SINGLELINE
)
769 while ((*count
) && str
[i
] != CR
&& str
[i
] != LF
)
776 j
= seg_j
+ j_in_seg
;
777 if (*pprefix_offset
>= seg_j
+ pellip
->before
)
779 *pprefix_offset
= TEXT_Reprefix (str
+ seg_i
, i
- seg_i
, pellip
);
780 if (*pprefix_offset
!= -1)
781 *pprefix_offset
+= seg_j
;
785 if (size
.cy
> retsize
->cy
)
786 retsize
->cy
= size
.cy
;
792 else if (str
[i
] == CR
|| str
[i
] == LF
)
795 if (*count
&& (str
[i
] == CR
|| str
[i
] == LF
) && str
[i
] != str
[i
-1])
801 /* else it was a Tab and we go around again */
813 /***********************************************************************
814 * TEXT_DrawUnderscore
816 * Draw the underline under the prefixed character
819 * hdc [in] The handle of the DC for drawing
820 * x [in] The x location of the line segment (logical coordinates)
821 * y [in] The y location of where the underscore should appear
822 * (logical coordinates)
823 * str [in] The text of the line segment
824 * offset [in] The offset of the underscored character within str
825 * rect [in] Clipping rectangle (if not NULL)
828 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
, const RECT
*rect
)
836 GetTextExtentPointW (hdc
, str
, offset
, &size
);
837 prefix_x
= x
+ size
.cx
;
838 GetTextExtentPointW (hdc
, str
, offset
+1, &size
);
839 prefix_end
= x
+ size
.cx
- 1;
840 /* The above method may eventually be slightly wrong due to kerning etc. */
842 /* Check for clipping */
844 if (prefix_x
> rect
->right
|| prefix_end
< rect
->left
|| y
< rect
->top
|| y
> rect
->bottom
)
845 return; /* Completely outside */
846 /* Partially outside */
847 if (prefix_x
< rect
->left
) prefix_x
= rect
->left
;
848 if (prefix_end
> rect
->right
) prefix_end
= rect
->right
;
851 hpen
= CreatePen (PS_SOLID
, 1, GetTextColor (hdc
));
852 oldPen
= SelectObject (hdc
, hpen
);
853 MoveToEx (hdc
, prefix_x
, y
, NULL
);
854 LineTo (hdc
, prefix_end
, y
);
855 SelectObject (hdc
, oldPen
);
859 /***********************************************************************
860 * DrawTextExW (USER32.@)
862 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
863 * is not quite complete, especially with regard to \0. We will assume that
864 * the returned string could have a length of up to i_count+3 and also have
865 * a trailing \0 (which would be 4 more than a not-null-terminated string but
866 * 3 more than a null-terminated string). If this is not so then increase
867 * the allowance in DrawTextExA.
869 #define MAX_BUFFER 1024
870 INT WINAPI
DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
871 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
877 WCHAR line
[MAX_BUFFER
];
878 int len
, lh
, count
=i_count
;
880 int lmargin
= 0, rmargin
= 0;
881 int x
= rect
->left
, y
= rect
->top
;
882 int width
= rect
->right
- rect
->left
;
885 int tabwidth
/* to keep gcc happy */ = 0;
891 TRACE("%s, %d, [%s] %08x\n", debugstr_wn (str
, count
), count
,
892 wine_dbgstr_rect(rect
), flags
);
894 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
895 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
901 if (flags
& DT_SINGLELINE
)
902 flags
&= ~DT_WORDBREAK
;
904 GetTextMetricsW(hdc
, &tm
);
905 if (flags
& DT_EXTERNALLEADING
)
906 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
910 if (str
[0] && count
== 0)
913 if (dtp
&& dtp
->cbSize
!= sizeof(DRAWTEXTPARAMS
))
918 count
= strlenW(str
);
921 if( flags
& DT_CALCRECT
)
923 rect
->right
= rect
->left
;
924 if( flags
& DT_SINGLELINE
)
925 rect
->bottom
= rect
->top
+ lh
;
927 rect
->bottom
= rect
->top
;
933 if (GetGraphicsMode(hdc
) == GM_COMPATIBLE
)
935 SIZE window_ext
, viewport_ext
;
936 GetWindowExtEx(hdc
, &window_ext
);
937 GetViewportExtEx(hdc
, &viewport_ext
);
938 if ((window_ext
.cy
> 0) != (viewport_ext
.cy
> 0))
944 lmargin
= dtp
->iLeftMargin
;
945 rmargin
= dtp
->iRightMargin
;
946 width
-= lmargin
+ rmargin
;
947 if (!(flags
& (DT_CENTER
| DT_RIGHT
)))
949 dtp
->uiLengthDrawn
= 0; /* This param RECEIVES number of chars processed */
952 if (flags
& DT_EXPANDTABS
)
954 int tabstop
= ((flags
& DT_TABSTOP
) && dtp
&& dtp
->iTabLength
) ? dtp
->iTabLength
: 8;
955 tabwidth
= tm
.tmAveCharWidth
* tabstop
;
958 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
960 if (flags
& DT_MODIFYSTRING
)
962 size_retstr
= (count
+ 4) * sizeof (WCHAR
);
963 retstr
= heap_alloc(size_retstr
);
964 if (!retstr
) return 0;
965 memcpy (retstr
, str
, size_retstr
);
975 len
= ARRAY_SIZE(line
);
977 last_line
= !(flags
& DT_NOCLIP
) && y
- ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) < rect
->bottom
;
979 last_line
= !(flags
& DT_NOCLIP
) && y
+ ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) > rect
->bottom
;
980 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
, &size
, last_line
, retstr
, tabwidth
, &prefix_offset
, &ellip
);
982 if (flags
& DT_CENTER
)
983 x
= (rect
->left
+ lmargin
+ rect
->right
- rmargin
- size
.cx
) / 2;
984 else if (flags
& DT_RIGHT
)
985 x
= rect
->right
- size
.cx
- rmargin
;
987 if (flags
& DT_SINGLELINE
)
989 if (flags
& DT_VCENTER
) y
= rect
->top
+
990 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
991 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
994 if (!(flags
& DT_CALCRECT
))
996 const WCHAR
*str
= line
;
1002 if ((flags
& DT_EXPANDTABS
))
1005 p
= str
; while (p
< str
+len
&& *p
!= TAB
) p
++;
1007 if (len_seg
!= len
&& !GetTextExtentPointW(hdc
, str
, len_seg
, &size
)) goto done
;
1012 if (!ExtTextOutW( hdc
, xseg
, y
,
1013 ((flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
) |
1014 ((flags
& DT_RTLREADING
) ? ETO_RTLREADING
: 0),
1015 rect
, str
, len_seg
, NULL
))
1017 if (prefix_offset
!= -1 && prefix_offset
< len_seg
)
1019 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
, (flags
& DT_NOCLIP
) ? NULL
: rect
);
1025 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
1027 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
1028 if (prefix_offset
!= -1)
1030 if (prefix_offset
< len_seg
)
1032 /* We have just drawn an underscore; we ought to
1033 * figure out where the next one is. I am going
1034 * to leave it for now until I have a better model
1035 * for the line, which will make reprefixing easier.
1036 * This is where ellip would be used.
1041 prefix_offset
-= len_seg
;
1046 else if (size
.cx
> max_width
)
1047 max_width
= size
.cx
;
1054 dtp
->uiLengthDrawn
+= len
;
1056 while (strPtr
&& !last_line
);
1058 if (flags
& DT_CALCRECT
)
1060 rect
->right
= rect
->left
+ max_width
;
1063 rect
->right
+= lmargin
+ rmargin
;
1066 if (retstr
) memcpy(str
, retstr
, size_retstr
);
1068 ret
= y
- rect
->top
;
1069 if (ret
== 0) ret
= 1;
1075 /***********************************************************************
1076 * DrawTextExA (USER32.@)
1078 * If DT_MODIFYSTRING is specified then there must be room for up to
1079 * 4 extra characters. We take great care about just how much modified
1082 INT WINAPI
DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
1083 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
1094 if (!count
) return 0;
1095 if (!str
&& count
> 0) return 0;
1096 if( !str
|| ((count
== -1) && !(count
= strlen(str
))))
1101 if (dtp
&& dtp
->cbSize
!= sizeof(DRAWTEXTPARAMS
))
1104 GetTextMetricsA(hdc
, &tm
);
1105 if (flags
& DT_EXTERNALLEADING
)
1106 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
1110 if( flags
& DT_CALCRECT
)
1112 rect
->right
= rect
->left
;
1113 if( flags
& DT_SINGLELINE
)
1114 rect
->bottom
= rect
->top
+ lh
;
1116 rect
->bottom
= rect
->top
;
1120 cp
= GdiGetCodePage( hdc
);
1121 wcount
= MultiByteToWideChar( cp
, 0, str
, count
, NULL
, 0 );
1124 if (flags
& DT_MODIFYSTRING
)
1129 wstr
= heap_alloc(wmax
* sizeof(WCHAR
));
1132 MultiByteToWideChar( cp
, 0, str
, count
, wstr
, wcount
);
1133 if (flags
& DT_MODIFYSTRING
)
1134 for (i
=4, p
=wstr
+wcount
; i
--; p
++) *p
=0xFFFE;
1135 /* Initialise the extra characters so that we can see which ones
1136 * change. U+FFFE is guaranteed to be not a unicode character and
1137 * so will not be generated by DrawTextEx itself.
1139 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, dtp
);
1140 if (flags
& DT_MODIFYSTRING
)
1142 /* Unfortunately the returned string may contain multiple \0s
1143 * and so we need to measure it ourselves.
1145 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1146 WideCharToMultiByte( cp
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1153 /***********************************************************************
1154 * DrawTextW (USER32.@)
1156 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1160 memset (&dtp
, 0, sizeof(dtp
));
1161 dtp
.cbSize
= sizeof(dtp
);
1162 if (flags
& DT_TABSTOP
)
1164 dtp
.iTabLength
= (flags
>> 8) & 0xff;
1165 flags
&= 0xffff00ff;
1167 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1170 /***********************************************************************
1171 * DrawTextA (USER32.@)
1173 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1177 memset (&dtp
, 0, sizeof(dtp
));
1178 dtp
.cbSize
= sizeof(dtp
);
1179 if (flags
& DT_TABSTOP
)
1181 dtp
.iTabLength
= (flags
>> 8) & 0xff;
1182 flags
&= 0xffff00ff;
1184 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);
1187 /***********************************************************************
1189 * GrayString functions
1192 /* callback for ASCII gray string proc */
1193 static BOOL CALLBACK
gray_string_callbackA( HDC hdc
, LPARAM param
, INT len
)
1195 return TextOutA( hdc
, 0, 0, (LPCSTR
)param
, len
);
1198 /* callback for Unicode gray string proc */
1199 static BOOL CALLBACK
gray_string_callbackW( HDC hdc
, LPARAM param
, INT len
)
1201 return TextOutW( hdc
, 0, 0, (LPCWSTR
)param
, len
);
1204 /***********************************************************************
1207 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
, GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
1208 INT x
, INT y
, INT cx
, INT cy
)
1210 HBITMAP hbm
, hbmsave
;
1218 if(!hdc
) return FALSE
;
1219 if (!(memdc
= CreateCompatibleDC(hdc
))) return FALSE
;
1221 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
1222 hbmsave
= SelectObject(memdc
, hbm
);
1223 hbsave
= SelectObject( memdc
, GetStockObject(BLACK_BRUSH
) );
1224 PatBlt( memdc
, 0, 0, cx
, cy
, PATCOPY
);
1225 SelectObject( memdc
, hbsave
);
1226 SetTextColor(memdc
, RGB(255, 255, 255));
1227 SetBkColor(memdc
, RGB(0, 0, 0));
1228 hfsave
= SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
1230 retval
= fn(memdc
, lp
, slen
);
1231 SelectObject(memdc
, hfsave
);
1234 * Windows doc says that the bitmap isn't grayed when len == -1 and
1235 * the callback function returns FALSE. However, testing this on
1236 * win95 showed otherwise...
1238 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1239 if(retval
|| len
!= -1)
1242 hbsave
= SelectObject(memdc
, SYSCOLOR_Get55AABrush());
1243 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
1244 SelectObject(memdc
, hbsave
);
1247 if(hb
) hbsave
= SelectObject(hdc
, hb
);
1248 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
1249 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
1250 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
1251 SetTextColor(hdc
, fg
);
1252 SetBkColor(hdc
, bg
);
1253 if(hb
) SelectObject(hdc
, hbsave
);
1255 SelectObject(memdc
, hbmsave
);
1262 /***********************************************************************
1263 * GrayStringA (USER32.@)
1265 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1266 LPARAM lParam
, INT cch
, INT x
, INT y
,
1269 if (!cch
) cch
= strlen( (LPCSTR
)lParam
);
1270 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1273 GetTextExtentPoint32A( hdc
, (LPCSTR
)lParam
, cch
, &s
);
1274 if (cx
== 0) cx
= s
.cx
;
1275 if (cy
== 0) cy
= s
.cy
;
1277 if (!gsprc
) gsprc
= gray_string_callbackA
;
1278 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1282 /***********************************************************************
1283 * GrayStringW (USER32.@)
1285 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1286 LPARAM lParam
, INT cch
, INT x
, INT y
,
1289 if (!cch
) cch
= strlenW( (LPCWSTR
)lParam
);
1290 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1293 GetTextExtentPoint32W( hdc
, (LPCWSTR
)lParam
, cch
, &s
);
1294 if (cx
== 0) cx
= s
.cx
;
1295 if (cy
== 0) cy
= s
.cy
;
1297 if (!gsprc
) gsprc
= gray_string_callbackW
;
1298 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1302 /***********************************************************************
1303 * TEXT_TabbedTextOut
1305 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1306 * Note: this doesn't work too well for text-alignment modes other
1307 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1309 static LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCWSTR lpstr
,
1310 INT count
, INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
,
1319 if (!lpstr
|| count
== 0) return 0;
1324 GetTextMetricsW( hdc
, &tm
);
1328 defWidth
= *lpTabPos
;
1333 defWidth
= 8 * tm
.tmAveCharWidth
;
1342 /* chop the string into substrings of 0 or more <tabs>
1343 * possibly followed by 1 or more normal characters */
1344 for (i
= 0; i
< count
; i
++)
1345 if (lpstr
[i
] != '\t') break;
1346 for (j
= i
; j
< count
; j
++)
1347 if (lpstr
[j
] == '\t') break;
1348 /* get the extent of the normal character part */
1349 GetTextExtentPointW( hdc
, lpstr
+ i
, j
- i
, &extent
);
1350 /* and if there is a <tab>, calculate its position */
1352 /* get x coordinate for the drawing of this string */
1353 for (; cTabStops
>= i
; lpTabPos
++, cTabStops
--)
1355 if( nTabOrg
+ abs( *lpTabPos
) > x
) {
1356 if( lpTabPos
[ i
- 1] >= 0) {
1357 /* a left aligned tab */
1358 x0
= nTabOrg
+ lpTabPos
[i
-1];
1364 /* if tab pos is negative then text is right-aligned
1365 * to tab stop meaning that the string extends to the
1366 * left, so we must subtract the width of the string */
1367 if (nTabOrg
- lpTabPos
[ i
- 1] - extent
.cx
> x
)
1369 x
= nTabOrg
- lpTabPos
[ i
- 1];
1376 /* if we have run out of tab stops and we have a valid default tab
1377 * stop width then round x up to that width */
1378 if ((cTabStops
< i
) && (defWidth
> 0)) {
1379 x0
= nTabOrg
+ ((x
- nTabOrg
) / defWidth
+ i
) * defWidth
;
1381 } else if ((cTabStops
< i
) && (defWidth
< 0)) {
1382 x
= nTabOrg
+ ((x
- nTabOrg
+ extent
.cx
) / -defWidth
+ i
)
1389 if (!extent
.cy
) extent
.cy
= tm
.tmHeight
;
1395 r
.bottom
= y
+ extent
.cy
;
1396 ExtTextOutW( hdc
, x0
, y
, GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
1397 &r
, lpstr
+ i
, j
- i
, NULL
);
1403 return MAKELONG(x
- start
, extent
.cy
);
1407 /***********************************************************************
1408 * TabbedTextOutA (USER32.@)
1410 * See TabbedTextOutW.
1412 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
1413 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1416 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1417 LPWSTR strW
= heap_alloc( len
* sizeof(WCHAR
) );
1418 if (!strW
) return 0;
1419 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1420 ret
= TabbedTextOutW( hdc
, x
, y
, strW
, len
, cTabStops
, lpTabPos
, nTabOrg
);
1426 /***********************************************************************
1427 * TabbedTextOutW (USER32.@)
1429 * Draws tabbed text aligned using the specified tab stops.
1432 * hdc [I] Handle to device context to draw to.
1433 * x [I] X co-ordinate to start drawing the text at in logical units.
1434 * y [I] Y co-ordinate to start drawing the text at in logical units.
1435 * str [I] Pointer to the characters to draw.
1436 * count [I] Number of WCHARs pointed to by str.
1437 * cTabStops [I] Number of tab stops pointed to by lpTabPos.
1438 * lpTabPos [I] Tab stops in logical units. Should be sorted in ascending order.
1439 * nTabOrg [I] Starting position to expand tabs from in logical units.
1442 * The dimensions of the string drawn. The height is in the high-order word
1443 * and the width is in the low-order word.
1446 * The tabs stops can be negative, in which case the text is right aligned to
1447 * that tab stop and, despite what MSDN says, this is supported on
1451 * MSDN says that the TA_UPDATECP from GetTextAlign causes this function to
1452 * ignore the x and y co-ordinates, but this is unimplemented at the moment.
1454 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
1455 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1457 TRACE("%p %d,%d %s %d\n", hdc
, x
, y
, debugstr_wn(str
,count
), count
);
1458 return TEXT_TabbedTextOut( hdc
, x
, y
, str
, count
, cTabStops
, lpTabPos
, nTabOrg
, TRUE
);
1462 /***********************************************************************
1463 * GetTabbedTextExtentA (USER32.@)
1465 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
1466 INT cTabStops
, const INT
*lpTabPos
)
1469 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1470 LPWSTR strW
= heap_alloc( len
* sizeof(WCHAR
) );
1471 if (!strW
) return 0;
1472 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1473 ret
= GetTabbedTextExtentW( hdc
, strW
, len
, cTabStops
, lpTabPos
);
1479 /***********************************************************************
1480 * GetTabbedTextExtentW (USER32.@)
1482 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
1483 INT cTabStops
, const INT
*lpTabPos
)
1485 TRACE("%p %s %d\n", hdc
, debugstr_wn(lpstr
,count
), count
);
1486 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
, lpTabPos
, 0, FALSE
);