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"
45 WINE_DEFAULT_DEBUG_CHANNEL(text
);
47 /*********************************************************************
52 * How many buffers to use
53 * While processing in DrawText there are potentially three different forms
54 * of the text that need to be held. How are they best held?
55 * 1. The original text is needed, of course, to see what to display.
56 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
58 * 3. The buffered text that is about to be displayed e.g. the current line.
59 * Typically this will exclude the ampersands used for prefixing etc.
62 * a. If the buffered text to be displayed includes the ampersands then
63 * we will need special measurement and draw functions that will ignore
64 * the ampersands (e.g. by copying to a buffer without the prefix and
65 * then using the normal forms). This may involve less space but may
66 * require more processing. e.g. since a line containing tabs may
67 * contain several underlined characters either we need to carry around
68 * a list of prefix locations or we may need to locate them several
70 * b. If we actually directly modify the "original text" as we go then we
71 * will need some special "caching" to handle the fact that when we
72 * ellipsify the text the ellipsis may modify the next line of text,
73 * which we have not yet processed. (e.g. ellipsification of a W at the
74 * end of a line will overwrite the W, the \n and the first character of
75 * the next line, and a \0 will overwrite the second. Try it!!)
77 * Option 1. Three separate storages. (To be implemented)
78 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
79 * the edited string in some form, either as the string itself or as some
80 * sort of "edit list" to be applied just before returning.
81 * Use a buffer that holds the ellipsified current line sans ampersands
82 * and accept the need occasionally to recalculate the prefixes (if
83 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
91 #define ALPHA_PREFIX 30 /* Win16: Alphabet prefix */
92 #define KANA_PREFIX 31 /* Win16: Katakana prefix */
94 #define FORWARD_SLASH '/'
95 #define BACK_SLASH '\\'
97 static const WCHAR ELLIPSISW
[] = {'.','.','.', 0};
99 typedef struct tag_ellipsis_data
107 /*********************************************************************
108 * TEXT_Ellipsify (static)
110 * Add an ellipsis to the end of the given string whilst ensuring it fits.
112 * If the ellipsis alone doesn't fit then it will be returned anyway.
114 * See Also TEXT_PathEllipsify
117 * hdc [in] The handle to the DC that defines the font.
118 * str [in/out] The string that needs to be modified.
119 * max_str [in] The dimension of str (number of WCHAR).
120 * len_str [in/out] The number of characters in str
121 * width [in] The maximum width permitted (in logical coordinates)
122 * size [out] The dimensions of the text
123 * modstr [out] The modified form of the string, to be returned to the
124 * calling program. It is assumed that the caller has
125 * made sufficient space available so we don't need to
126 * know the size of the space. This pointer may be NULL if
127 * the modified string is not required.
128 * len_before [out] The number of characters before the ellipsis.
129 * len_ellip [out] The number of characters in the ellipsis.
131 * See for example Microsoft article Q249678.
133 * For now we will simply use three dots rather than worrying about whether
134 * the font contains an explicit ellipsis character.
136 static void TEXT_Ellipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
137 unsigned int *len_str
, int width
, SIZE
*size
,
139 int *len_before
, int *len_ellip
)
141 unsigned int len_ellipsis
;
142 unsigned int lo
, mid
, hi
;
144 len_ellipsis
= strlenW (ELLIPSISW
);
145 if (len_ellipsis
> max_len
) len_ellipsis
= max_len
;
146 if (*len_str
> max_len
- len_ellipsis
)
147 *len_str
= max_len
- len_ellipsis
;
149 /* First do a quick binary search to get an upper bound for *len_str. */
151 GetTextExtentExPointW(hdc
, str
, *len_str
, width
, NULL
, NULL
, size
) &&
154 for (lo
= 0, hi
= *len_str
; lo
< hi
; )
157 if (!GetTextExtentExPointW(hdc
, str
, mid
, width
, NULL
, NULL
, size
))
159 if (size
->cx
> width
)
166 /* Now this should take only a couple iterations at most. */
169 memcpy(str
+ *len_str
, ELLIPSISW
, len_ellipsis
*sizeof(WCHAR
));
171 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
172 NULL
, NULL
, size
)) break;
174 if (!*len_str
|| size
->cx
<= width
) break;
178 *len_ellip
= len_ellipsis
;
179 *len_before
= *len_str
;
180 *len_str
+= len_ellipsis
;
184 memcpy (modstr
, str
, *len_str
* sizeof(WCHAR
));
185 modstr
[*len_str
] = '\0';
189 /*********************************************************************
190 * TEXT_PathEllipsify (static)
192 * Add an ellipsis to the provided string in order to make it fit within
193 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
196 * See Also TEXT_Ellipsify
199 * hdc [in] The handle to the DC that defines the font.
200 * str [in/out] The string that needs to be modified
201 * max_str [in] The dimension of str (number of WCHAR).
202 * len_str [in/out] The number of characters in str
203 * width [in] The maximum width permitted (in logical coordinates)
204 * size [out] The dimensions of the text
205 * modstr [out] The modified form of the string, to be returned to the
206 * calling program. It is assumed that the caller has
207 * made sufficient space available so we don't need to
208 * know the size of the space. This pointer may be NULL if
209 * the modified string is not required.
210 * pellip [out] The ellipsification results
212 * For now we will simply use three dots rather than worrying about whether
213 * the font contains an explicit ellipsis character.
215 * The following applies, I think to Win95. We will need to extend it for
216 * Win98 which can have both path and end ellipsis at the same time (e.g.
217 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
219 * The resulting string consists of as much as possible of the following:
220 * 1. The ellipsis itself
221 * 2. The last \ or / of the string (if any)
222 * 3. Everything after the last \ or / of the string (if any) or the whole
223 * string if there is no / or \. I believe that under Win95 this would
224 * include everything even though some might be clipped off the end whereas
225 * under Win98 that might be ellipsified too.
226 * Yet to be investigated is whether this would include wordbreaking if the
227 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
228 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
229 * broken within words).
230 * 4. All the stuff before the / or \, which is placed before the ellipsis.
232 static void TEXT_PathEllipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
233 unsigned int *len_str
, int width
, SIZE
*size
,
234 WCHAR
*modstr
, ellipsis_data
*pellip
)
239 WCHAR
*lastBkSlash
, *lastFwdSlash
, *lastSlash
;
241 len_ellipsis
= strlenW (ELLIPSISW
);
242 if (!max_len
) return;
243 if (len_ellipsis
>= max_len
) len_ellipsis
= max_len
- 1;
244 if (*len_str
+ len_ellipsis
>= max_len
)
245 *len_str
= max_len
- len_ellipsis
-1;
246 /* Hopefully this will never happen, otherwise it would probably lose
247 * the wrong character
249 str
[*len_str
] = '\0'; /* to simplify things */
251 lastBkSlash
= strrchrW (str
, BACK_SLASH
);
252 lastFwdSlash
= strrchrW (str
, FORWARD_SLASH
);
253 lastSlash
= lastBkSlash
> lastFwdSlash
? lastBkSlash
: lastFwdSlash
;
254 if (!lastSlash
) lastSlash
= str
;
255 len_trailing
= *len_str
- (lastSlash
- str
);
257 /* overlap-safe movement to the right */
258 memmove (lastSlash
+len_ellipsis
, lastSlash
, len_trailing
* sizeof(WCHAR
));
259 memcpy (lastSlash
, ELLIPSISW
, len_ellipsis
*sizeof(WCHAR
));
260 len_trailing
+= len_ellipsis
;
261 /* From this point on lastSlash actually points to the ellipsis in front
262 * of the last slash and len_trailing includes the ellipsis
268 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
269 NULL
, NULL
, size
)) break;
271 if (lastSlash
== str
|| size
->cx
<= width
) break;
273 /* overlap-safe movement to the left */
274 memmove (lastSlash
-1, lastSlash
, len_trailing
* sizeof(WCHAR
));
281 pellip
->before
= lastSlash
-str
;
282 pellip
->len
= len_ellipsis
;
283 pellip
->under
= len_under
;
284 pellip
->after
= len_trailing
- len_ellipsis
;
285 *len_str
+= len_ellipsis
;
289 memcpy(modstr
, str
, *len_str
* sizeof(WCHAR
));
290 modstr
[*len_str
] = '\0';
294 /*********************************************************************
295 * TEXT_WordBreak (static)
297 * Perform wordbreak processing on the given string
299 * Assumes that DT_WORDBREAK has been specified and not all the characters
300 * fit. Note that this function should even be called when the first character
301 * that doesn't fit is known to be a space or tab, so that it can swallow them.
303 * Note that the Windows processing has some strange properties.
304 * 1. If the text is left-justified and there is room for some of the spaces
305 * that follow the last word on the line then those that fit are included on
307 * 2. If the text is centered or right-justified and there is room for some of
308 * the spaces that follow the last word on the line then all but one of those
309 * that fit are included on the line.
310 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
311 * character of a new line it will be skipped.
314 * hdc [in] The handle to the DC that defines the font.
315 * str [in/out] The string that needs to be broken.
316 * max_str [in] The dimension of str (number of WCHAR).
317 * len_str [in/out] The number of characters in str
318 * width [in] The maximum width permitted
319 * format [in] The format flags in effect
320 * chars_fit [in] The maximum number of characters of str that are already
321 * known to fit; chars_fit+1 is known not to fit.
322 * chars_used [out] The number of characters of str that have been "used" and
323 * do not need to be included in later text. For example this will
324 * include any spaces that have been discarded from the start of
326 * size [out] The size of the returned text in logical coordinates
328 * Pedantic assumption - Assumes that the text length is monotonically
329 * increasing with number of characters (i.e. no weird kernings)
333 * Work back from the last character that did fit to either a space or the last
334 * character of a word, whichever is met first.
335 * If there was one or the first character didn't fit then
336 * If the text is centered or right justified and that one character was a
337 * space then break the line before that character
338 * Otherwise break the line after that character
339 * and if the next character is a space then discard it.
340 * Suppose there was none (and the first character did fit).
341 * If Break Within Word is permitted
342 * break the word after the last character that fits (there must be
343 * at least one; none is caught earlier).
345 * discard any trailing space.
346 * include the whole word; it may be ellipsified later
348 * Break Within Word is permitted under a set of circumstances that are not
349 * totally clear yet. Currently our best guess is:
350 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
351 * DT_PATH_ELLIPSIS is
354 static void TEXT_WordBreak (HDC hdc
, WCHAR
*str
, unsigned int max_str
,
355 unsigned int *len_str
,
356 int width
, int format
, unsigned int chars_fit
,
357 unsigned int *chars_used
, SIZE
*size
)
365 assert (format
& DT_WORDBREAK
);
366 assert (chars_fit
< *len_str
);
368 sla
= HeapAlloc(GetProcessHeap(), 0, sizeof(SCRIPT_LOGATTR
) * *len_str
);
370 memset(&sa
, 0, sizeof(SCRIPT_ANALYSIS
));
371 sa
.eScript
= SCRIPT_UNDEFINED
;
373 ScriptBreak(str
, *len_str
, &sa
, sla
);
375 /* Work back from the last character that did fit to either a space or the
376 * last character of a word, whichever is met first.
378 p
= str
+ chars_fit
; /* The character that doesn't fit */
382 ; /* we pretend that it fits anyway */
383 else if (sla
[i
].fSoftBreak
) /* chars_fit < *len_str so this is valid */
385 /* the word just fitted */
390 while (i
> 0 && !sla
[(--i
)+1].fSoftBreak
) p
--;
392 word_fits
= (i
!= 0 || sla
[i
+1].fSoftBreak
);
395 /* If there was one or the first character didn't fit then */
399 /* break the line before/after that character */
400 if (!(format
& (DT_RIGHT
| DT_CENTER
)) || *p
!= SPACE
)
402 next_is_space
= (p
- str
) < *len_str
&& *p
== SPACE
;
404 /* and if the next character is a space then discard it. */
405 *chars_used
= *len_str
;
409 /* Suppose there was none. */
412 if ((format
& (DT_EDITCONTROL
| DT_WORD_ELLIPSIS
| DT_PATH_ELLIPSIS
)) ==
415 /* break the word after the last character that fits (there must be
416 * at least one; none is caught earlier).
418 *len_str
= chars_fit
;
419 *chars_used
= chars_fit
;
421 /* FIXME - possible error. Since the next character is now removed
422 * this could make the text longer so that it no longer fits, and
423 * so we need a loop to test and shrink.
429 /* discard any trailing space. */
430 const WCHAR
*e
= str
+ *len_str
;
432 while (p
< e
&& *p
!= SPACE
)
434 *chars_used
= p
- str
;
435 if (p
< e
) /* i.e. loop failed because *p == SPACE */
438 /* include the whole word; it may be ellipsified later */
440 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
441 * so that it will be too long
445 /* Remeasure the string */
446 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
447 HeapFree(GetProcessHeap(),0, sla
);
450 /*********************************************************************
453 * Skip over the given number of characters, bearing in mind prefix
454 * substitution and the fact that a character may take more than one
455 * WCHAR (Unicode surrogates are two words long) (and there may have been
459 * new_count [out] The updated count
460 * new_str [out] The updated pointer
461 * start_count [in] The count of remaining characters corresponding to the
462 * start of the string
463 * start_str [in] The starting point of the string
464 * max [in] The number of characters actually in this segment of the
465 * string (the & counts)
466 * n [in] The number of characters to skip (if prefix then
468 * prefix [in] Apply prefix substitution
474 * There must be at least n characters in the string
475 * We need max because the "line" may have ended with a & followed by a tab
476 * or newline etc. which we don't want to swallow
479 static void TEXT_SkipChars (int *new_count
, const WCHAR
**new_str
,
480 int start_count
, const WCHAR
*start_str
,
481 int max
, int n
, int prefix
)
483 /* This is specific to wide characters, MSDN doesn't say anything much
484 * about Unicode surrogates yet and it isn't clear if _wcsinc will
485 * correctly handle them so we'll just do this the easy way for now
490 const WCHAR
*str_on_entry
= start_str
;
495 if ((*start_str
== PREFIX
|| *start_str
== ALPHA_PREFIX
) && max
--)
499 start_count
-= (start_str
- str_on_entry
);
506 *new_str
= start_str
;
507 *new_count
= start_count
;
510 /*********************************************************************
513 * Reanalyse the text to find the prefixed character. This is called when
514 * wordbreaking or ellipsification has shortened the string such that the
515 * previously noted prefixed character is no longer visible.
518 * str [in] The original string segment (including all characters)
519 * ns [in] The number of characters in str (including prefixes)
520 * pe [in] The ellipsification data
523 * The prefix offset within the new string segment (the one that contains the
524 * ellipses and does not contain the prefix characters) (-1 if none)
527 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
528 const ellipsis_data
*pe
)
532 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
534 for (i
= 0; i
< n
; i
++, str
++)
538 /* Reached the path ellipsis; jump over it */
539 if (ns
< pe
->under
) break;
543 if (!pe
->after
) break; /* Nothing after the path ellipsis */
547 if (*str
== PREFIX
|| *str
== ALPHA_PREFIX
)
552 result
= (i
< pe
->before
|| pe
->under
== 0) ? i
: i
- pe
->under
+ pe
->len
;
553 /* pe->len may be non-zero while pe_under is zero */
560 /*********************************************************************
561 * Returns true if and only if the remainder of the line is a single
562 * newline representation or nothing
565 static BOOL
remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
567 if (!num_chars
) return TRUE
;
568 if (*str
!= LF
&& *str
!= CR
) return FALSE
;
569 if (!--num_chars
) return TRUE
;
570 if (*str
== *(str
+1)) return FALSE
;
572 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
573 if (--num_chars
) return FALSE
;
577 /*********************************************************************
578 * Return next line of text from a string.
580 * hdc - handle to DC.
581 * str - string to parse into lines.
582 * count - length of str.
583 * dest - destination in which to return line.
584 * len - dest buffer size in chars on input, copied length into dest on output.
585 * width - maximum width of line in pixels.
586 * format - format type passed to DrawText.
587 * retsize - returned size of the line in pixels.
588 * last_line - TRUE if is the last line that will be processed
589 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
590 * the return string is built.
591 * tabwidth - The width of a tab in logical coordinates
592 * pprefix_offset - Here is where we return the offset within dest of the first
593 * prefixed (underlined) character. -1 is returned if there
594 * are none. Note that there may be more; the calling code
595 * will need to use TEXT_Reprefix to find any later ones.
596 * pellip - Here is where we return the information about any ellipsification
597 * that was carried out. Note that if tabs are being expanded then
598 * this data will correspond to the last text segment actually
599 * returned in dest; by definition there would not have been any
600 * ellipsification in earlier text segments of the line.
602 * Returns pointer to next char in str after end of the line
603 * or NULL if end of str reached.
605 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
606 WCHAR
*dest
, int *len
, int width
, DWORD format
,
607 SIZE
*retsize
, int last_line
, WCHAR
**p_retstr
,
608 int tabwidth
, int *pprefix_offset
,
609 ellipsis_data
*pellip
)
615 int seg_i
, seg_count
, seg_j
;
618 BOOL word_broken
, line_fits
, ellipsified
;
619 unsigned int j_in_seg
;
620 *pprefix_offset
= -1;
622 /* For each text segment in the line */
628 /* Skip any leading tabs */
630 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
632 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
633 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
634 while (*count
&& str
[i
] == TAB
)
637 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
642 /* Now copy as far as the next tab or cr/lf or eos */
649 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
650 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
652 if ((format
& DT_NOPREFIX
) || *count
<= 1)
654 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
658 if (str
[i
] == PREFIX
|| str
[i
] == ALPHA_PREFIX
) {
659 (*count
)--, i
++; /* Throw away the prefix itself */
660 if (str
[i
] == PREFIX
)
662 /* Swallow it before we see it again */
663 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
665 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
669 /* else the previous prefix was in an earlier segment of the
670 * line; we will leave it to the drawing code to catch this
674 else if (str
[i
] == KANA_PREFIX
)
676 /* Throw away katakana access keys */
677 (*count
)--, i
++; /* skip the prefix */
678 (*count
)--, i
++; /* skip the letter */
682 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
687 /* Measure the whole text segment and possibly WordBreak and
691 j_in_seg
= j
- seg_j
;
692 max_seg_width
= width
- plen
;
693 GetTextExtentExPointW (hdc
, dest
+ seg_j
, j_in_seg
, max_seg_width
, &num_fit
, NULL
, &size
);
695 /* The Microsoft handling of various combinations of formats is weird.
696 * The following may very easily be incorrect if several formats are
697 * combined, and may differ between versions (to say nothing of the
698 * several bugs in the Microsoft versions).
701 line_fits
= (num_fit
>= j_in_seg
);
702 if (!line_fits
&& (format
& DT_WORDBREAK
))
705 unsigned int chars_used
;
706 TEXT_WordBreak (hdc
, dest
+seg_j
, maxl
-seg_j
, &j_in_seg
,
707 max_seg_width
, format
, num_fit
, &chars_used
, &size
);
708 line_fits
= (size
.cx
<= max_seg_width
);
709 /* and correct the counts */
710 TEXT_SkipChars (count
, &s
, seg_count
, str
+seg_i
, i
-seg_i
,
711 chars_used
, !(format
& DT_NOPREFIX
));
715 pellip
->before
= j_in_seg
;
720 if (!line_fits
&& (format
& DT_PATH_ELLIPSIS
))
722 TEXT_PathEllipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
723 max_seg_width
, &size
, *p_retstr
, pellip
);
724 line_fits
= (size
.cx
<= max_seg_width
);
727 /* NB we may end up ellipsifying a word-broken or path_ellipsified
729 if ((!line_fits
&& (format
& DT_WORD_ELLIPSIS
)) ||
730 ((format
& DT_END_ELLIPSIS
) &&
731 ((last_line
&& *count
) ||
732 (remainder_is_none_or_newline (*count
, &str
[i
]) && !line_fits
))))
734 int before
, len_ellipsis
;
735 TEXT_Ellipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
736 max_seg_width
, &size
, *p_retstr
, &before
, &len_ellipsis
);
737 if (before
> pellip
->before
)
739 /* We must have done a path ellipsis too */
740 pellip
->after
= before
- pellip
->before
- pellip
->len
;
741 /* Leave the len as the length of the first ellipsis */
745 /* If we are here after a path ellipsification it must be
746 * because even the ellipsis itself didn't fit.
748 assert (pellip
->under
== 0 && pellip
->after
== 0);
749 pellip
->before
= before
;
750 pellip
->len
= len_ellipsis
;
751 /* pellip->after remains as zero as does
757 /* As an optimisation if we have ellipsified and we are expanding
758 * tabs and we haven't reached the end of the line we can skip to it
759 * now rather than going around the loop again.
761 if ((format
& DT_EXPANDTABS
) && ellipsified
)
763 if (format
& DT_SINGLELINE
)
767 while ((*count
) && str
[i
] != CR
&& str
[i
] != LF
)
774 j
= seg_j
+ j_in_seg
;
775 if (*pprefix_offset
>= seg_j
+ pellip
->before
)
777 *pprefix_offset
= TEXT_Reprefix (str
+ seg_i
, i
- seg_i
, pellip
);
778 if (*pprefix_offset
!= -1)
779 *pprefix_offset
+= seg_j
;
783 if (size
.cy
> retsize
->cy
)
784 retsize
->cy
= size
.cy
;
790 else if (str
[i
] == CR
|| str
[i
] == LF
)
793 if (*count
&& (str
[i
] == CR
|| str
[i
] == LF
) && str
[i
] != str
[i
-1])
799 /* else it was a Tab and we go around again */
811 /***********************************************************************
812 * TEXT_DrawUnderscore
814 * Draw the underline under the prefixed character
817 * hdc [in] The handle of the DC for drawing
818 * x [in] The x location of the line segment (logical coordinates)
819 * y [in] The y location of where the underscore should appear
820 * (logical coordinates)
821 * str [in] The text of the line segment
822 * offset [in] The offset of the underscored character within str
823 * rect [in] Clipping rectangle (if not NULL)
826 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
, const RECT
*rect
)
834 GetTextExtentPointW (hdc
, str
, offset
, &size
);
835 prefix_x
= x
+ size
.cx
;
836 GetTextExtentPointW (hdc
, str
, offset
+1, &size
);
837 prefix_end
= x
+ size
.cx
- 1;
838 /* The above method may eventually be slightly wrong due to kerning etc. */
840 /* Check for clipping */
842 if (prefix_x
> rect
->right
|| prefix_end
< rect
->left
|| y
< rect
->top
|| y
> rect
->bottom
)
843 return; /* Completely outside */
844 /* Partially outside */
845 if (prefix_x
< rect
->left
) prefix_x
= rect
->left
;
846 if (prefix_end
> rect
->right
) prefix_end
= rect
->right
;
849 hpen
= CreatePen (PS_SOLID
, 1, GetTextColor (hdc
));
850 oldPen
= SelectObject (hdc
, hpen
);
851 MoveToEx (hdc
, prefix_x
, y
, NULL
);
852 LineTo (hdc
, prefix_end
, y
);
853 SelectObject (hdc
, oldPen
);
857 /***********************************************************************
858 * DrawTextExW (USER32.@)
860 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
861 * is not quite complete, especially with regard to \0. We will assume that
862 * the returned string could have a length of up to i_count+3 and also have
863 * a trailing \0 (which would be 4 more than a not-null-terminated string but
864 * 3 more than a null-terminated string). If this is not so then increase
865 * the allowance in DrawTextExA.
867 #define MAX_BUFFER 1024
868 INT WINAPI
DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
869 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
873 WCHAR
*retstr
, *p_retstr
;
875 WCHAR line
[MAX_BUFFER
];
876 int len
, lh
, count
=i_count
;
878 int lmargin
= 0, rmargin
= 0;
879 int x
= rect
->left
, y
= rect
->top
;
880 int width
= rect
->right
- rect
->left
;
883 int tabwidth
/* to keep gcc happy */ = 0;
888 TRACE("%s, %d, [%s] %08x\n", debugstr_wn (str
, count
), count
,
889 wine_dbgstr_rect(rect
), flags
);
891 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
892 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
898 if (flags
& DT_SINGLELINE
)
899 flags
&= ~DT_WORDBREAK
;
901 GetTextMetricsW(hdc
, &tm
);
902 if (flags
& DT_EXTERNALLEADING
)
903 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
907 if (str
[0] && count
== 0)
910 if (dtp
&& dtp
->cbSize
!= sizeof(DRAWTEXTPARAMS
))
915 count
= strlenW(str
);
918 if( flags
& DT_CALCRECT
)
920 rect
->right
= rect
->left
;
921 if( flags
& DT_SINGLELINE
)
922 rect
->bottom
= rect
->top
+ lh
;
924 rect
->bottom
= rect
->top
;
930 if (GetGraphicsMode(hdc
) == GM_COMPATIBLE
)
932 SIZE window_ext
, viewport_ext
;
933 GetWindowExtEx(hdc
, &window_ext
);
934 GetViewportExtEx(hdc
, &viewport_ext
);
935 if ((window_ext
.cy
> 0) != (viewport_ext
.cy
> 0))
941 lmargin
= dtp
->iLeftMargin
;
942 rmargin
= dtp
->iRightMargin
;
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
: 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
= HeapAlloc(GetProcessHeap(), 0, size_retstr
);
960 if (!retstr
) return 0;
961 memcpy (retstr
, str
, size_retstr
);
972 len
= sizeof(line
)/sizeof(line
[0]);
974 last_line
= !(flags
& DT_NOCLIP
) && y
- ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) < rect
->bottom
;
976 last_line
= !(flags
& DT_NOCLIP
) && y
+ ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) > rect
->bottom
;
977 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
, &size
, last_line
, &p_retstr
, tabwidth
, &prefix_offset
, &ellip
);
979 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
981 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
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
))
1005 HeapFree (GetProcessHeap(), 0, retstr
);
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 HeapFree (GetProcessHeap(), 0, retstr
);
1020 if (prefix_offset
!= -1 && prefix_offset
< len_seg
)
1022 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
, (flags
& DT_NOCLIP
) ? NULL
: rect
);
1028 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
1030 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
1031 if (prefix_offset
!= -1)
1033 if (prefix_offset
< len_seg
)
1035 /* We have just drawn an underscore; we ought to
1036 * figure out where the next one is. I am going
1037 * to leave it for now until I have a better model
1038 * for the line, which will make reprefixing easier.
1039 * This is where ellip would be used.
1044 prefix_offset
-= len_seg
;
1049 else if (size
.cx
> max_width
)
1050 max_width
= size
.cx
;
1057 dtp
->uiLengthDrawn
+= len
;
1059 while (strPtr
&& !last_line
);
1061 if (flags
& DT_CALCRECT
)
1063 rect
->right
= rect
->left
+ max_width
;
1066 rect
->right
+= lmargin
+ rmargin
;
1070 memcpy (str
, retstr
, size_retstr
);
1071 HeapFree (GetProcessHeap(), 0, retstr
);
1073 return y
- rect
->top
;
1076 /***********************************************************************
1077 * DrawTextExA (USER32.@)
1079 * If DT_MODIFYSTRING is specified then there must be room for up to
1080 * 4 extra characters. We take great care about just how much modified
1083 INT WINAPI
DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
1084 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
1095 if (!count
) return 0;
1096 if (!str
&& count
> 0) return 0;
1097 if( !str
|| ((count
== -1) && !(count
= strlen(str
))))
1102 if (dtp
&& dtp
->cbSize
!= sizeof(DRAWTEXTPARAMS
))
1105 GetTextMetricsA(hdc
, &tm
);
1106 if (flags
& DT_EXTERNALLEADING
)
1107 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
1111 if( flags
& DT_CALCRECT
)
1113 rect
->right
= rect
->left
;
1114 if( flags
& DT_SINGLELINE
)
1115 rect
->bottom
= rect
->top
+ lh
;
1117 rect
->bottom
= rect
->top
;
1121 cp
= GdiGetCodePage( hdc
);
1122 wcount
= MultiByteToWideChar( cp
, 0, str
, count
, NULL
, 0 );
1125 if (flags
& DT_MODIFYSTRING
)
1130 wstr
= HeapAlloc(GetProcessHeap(), 0, wmax
* sizeof(WCHAR
));
1133 MultiByteToWideChar( cp
, 0, str
, count
, wstr
, wcount
);
1134 if (flags
& DT_MODIFYSTRING
)
1135 for (i
=4, p
=wstr
+wcount
; i
--; p
++) *p
=0xFFFE;
1136 /* Initialise the extra characters so that we can see which ones
1137 * change. U+FFFE is guaranteed to be not a unicode character and
1138 * so will not be generated by DrawTextEx itself.
1140 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, dtp
);
1141 if (flags
& DT_MODIFYSTRING
)
1143 /* Unfortunately the returned string may contain multiple \0s
1144 * and so we need to measure it ourselves.
1146 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1147 WideCharToMultiByte( cp
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1149 HeapFree(GetProcessHeap(), 0, wstr
);
1154 /***********************************************************************
1155 * DrawTextW (USER32.@)
1157 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1161 memset (&dtp
, 0, sizeof(dtp
));
1162 dtp
.cbSize
= sizeof(dtp
);
1163 if (flags
& DT_TABSTOP
)
1165 dtp
.iTabLength
= (flags
>> 8) & 0xff;
1166 flags
&= 0xffff00ff;
1168 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1171 /***********************************************************************
1172 * DrawTextA (USER32.@)
1174 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1178 memset (&dtp
, 0, sizeof(dtp
));
1179 dtp
.cbSize
= sizeof(dtp
);
1180 if (flags
& DT_TABSTOP
)
1182 dtp
.iTabLength
= (flags
>> 8) & 0xff;
1183 flags
&= 0xffff00ff;
1185 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);
1188 /***********************************************************************
1190 * GrayString functions
1193 /* callback for ASCII gray string proc */
1194 static BOOL CALLBACK
gray_string_callbackA( HDC hdc
, LPARAM param
, INT len
)
1196 return TextOutA( hdc
, 0, 0, (LPCSTR
)param
, len
);
1199 /* callback for Unicode gray string proc */
1200 static BOOL CALLBACK
gray_string_callbackW( HDC hdc
, LPARAM param
, INT len
)
1202 return TextOutW( hdc
, 0, 0, (LPCWSTR
)param
, len
);
1205 /***********************************************************************
1208 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
, GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
1209 INT x
, INT y
, INT cx
, INT cy
)
1211 HBITMAP hbm
, hbmsave
;
1219 if(!hdc
) return FALSE
;
1220 if (!(memdc
= CreateCompatibleDC(hdc
))) return FALSE
;
1222 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
1223 hbmsave
= SelectObject(memdc
, hbm
);
1224 hbsave
= SelectObject( memdc
, GetStockObject(BLACK_BRUSH
) );
1225 PatBlt( memdc
, 0, 0, cx
, cy
, PATCOPY
);
1226 SelectObject( memdc
, hbsave
);
1227 SetTextColor(memdc
, RGB(255, 255, 255));
1228 SetBkColor(memdc
, RGB(0, 0, 0));
1229 hfsave
= SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
1231 retval
= fn(memdc
, lp
, slen
);
1232 SelectObject(memdc
, hfsave
);
1235 * Windows doc says that the bitmap isn't grayed when len == -1 and
1236 * the callback function returns FALSE. However, testing this on
1237 * win95 showed otherwise...
1239 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1240 if(retval
|| len
!= -1)
1243 hbsave
= SelectObject(memdc
, SYSCOLOR_Get55AABrush());
1244 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
1245 SelectObject(memdc
, hbsave
);
1248 if(hb
) hbsave
= SelectObject(hdc
, hb
);
1249 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
1250 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
1251 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
1252 SetTextColor(hdc
, fg
);
1253 SetBkColor(hdc
, bg
);
1254 if(hb
) SelectObject(hdc
, hbsave
);
1256 SelectObject(memdc
, hbmsave
);
1263 /***********************************************************************
1264 * GrayStringA (USER32.@)
1266 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1267 LPARAM lParam
, INT cch
, INT x
, INT y
,
1270 if (!cch
) cch
= strlen( (LPCSTR
)lParam
);
1271 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1274 GetTextExtentPoint32A( hdc
, (LPCSTR
)lParam
, cch
, &s
);
1275 if (cx
== 0) cx
= s
.cx
;
1276 if (cy
== 0) cy
= s
.cy
;
1278 if (!gsprc
) gsprc
= gray_string_callbackA
;
1279 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1283 /***********************************************************************
1284 * GrayStringW (USER32.@)
1286 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1287 LPARAM lParam
, INT cch
, INT x
, INT y
,
1290 if (!cch
) cch
= strlenW( (LPCWSTR
)lParam
);
1291 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1294 GetTextExtentPoint32W( hdc
, (LPCWSTR
)lParam
, cch
, &s
);
1295 if (cx
== 0) cx
= s
.cx
;
1296 if (cy
== 0) cy
= s
.cy
;
1298 if (!gsprc
) gsprc
= gray_string_callbackW
;
1299 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1303 /***********************************************************************
1304 * TEXT_TabbedTextOut
1306 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1307 * Note: this doesn't work too well for text-alignment modes other
1308 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1310 static LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCWSTR lpstr
,
1311 INT count
, INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
,
1319 if (!lpstr
|| count
== 0) return 0;
1326 defWidth
= *lpTabPos
;
1332 GetTextMetricsW( hdc
, &tm
);
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
)
1393 r
.bottom
= y
+ extent
.cy
;
1394 ExtTextOutW( hdc
, x0
, y
, GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
1395 &r
, lpstr
+ i
, j
- i
, NULL
);
1400 return MAKELONG(x
- start
, extent
.cy
);
1404 /***********************************************************************
1405 * TabbedTextOutA (USER32.@)
1407 * See TabbedTextOutW.
1409 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
1410 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1413 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1414 LPWSTR strW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1415 if (!strW
) return 0;
1416 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1417 ret
= TabbedTextOutW( hdc
, x
, y
, strW
, len
, cTabStops
, lpTabPos
, nTabOrg
);
1418 HeapFree( GetProcessHeap(), 0, strW
);
1423 /***********************************************************************
1424 * TabbedTextOutW (USER32.@)
1426 * Draws tabbed text aligned using the specified tab stops.
1429 * hdc [I] Handle to device context to draw to.
1430 * x [I] X co-ordinate to start drawing the text at in logical units.
1431 * y [I] Y co-ordinate to start drawing the text at in logical units.
1432 * str [I] Pointer to the characters to draw.
1433 * count [I] Number of WCHARs pointed to by str.
1434 * cTabStops [I] Number of tab stops pointed to by lpTabPos.
1435 * lpTabPos [I] Tab stops in logical units. Should be sorted in ascending order.
1436 * nTabOrg [I] Starting position to expand tabs from in logical units.
1439 * The dimensions of the string drawn. The height is in the high-order word
1440 * and the width is in the low-order word.
1443 * The tabs stops can be negative, in which case the text is right aligned to
1444 * that tab stop and, despite what MSDN says, this is supported on
1448 * MSDN says that the TA_UPDATECP from GetTextAlign causes this function to
1449 * ignore the x and y co-ordinates, but this is unimplemented at the moment.
1451 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
1452 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1454 TRACE("%p %d,%d %s %d\n", hdc
, x
, y
, debugstr_wn(str
,count
), count
);
1455 return TEXT_TabbedTextOut( hdc
, x
, y
, str
, count
, cTabStops
, lpTabPos
, nTabOrg
, TRUE
);
1459 /***********************************************************************
1460 * GetTabbedTextExtentA (USER32.@)
1462 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
1463 INT cTabStops
, const INT
*lpTabPos
)
1466 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1467 LPWSTR strW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1468 if (!strW
) return 0;
1469 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1470 ret
= GetTabbedTextExtentW( hdc
, strW
, len
, cTabStops
, lpTabPos
);
1471 HeapFree( GetProcessHeap(), 0, strW
);
1476 /***********************************************************************
1477 * GetTabbedTextExtentW (USER32.@)
1479 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
1480 INT cTabStops
, const INT
*lpTabPos
)
1482 TRACE("%p %s %d\n", hdc
, debugstr_wn(lpstr
,count
), count
);
1483 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
, lpTabPos
, 0, FALSE
);