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 */
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. */
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
419 *len_str
= chars_fit
;
420 *chars_used
= chars_fit
;
422 /* FIXME - possible error. Since the next character is now removed
423 * this could make the text longer so that it no longer fits, and
424 * so we need a loop to test and shrink.
430 /* discard any trailing space. */
431 const WCHAR
*e
= str
+ *len_str
;
433 while (p
< e
&& *p
!= SPACE
)
435 *chars_used
= p
- str
;
436 if (p
< e
) /* i.e. loop failed because *p == SPACE */
439 /* include the whole word; it may be ellipsified later */
441 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
442 * so that it will be too long
446 /* Remeasure the string */
447 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
448 HeapFree(GetProcessHeap(),0, sla
);
451 /*********************************************************************
454 * Skip over the given number of characters, bearing in mind prefix
455 * substitution and the fact that a character may take more than one
456 * WCHAR (Unicode surrogates are two words long) (and there may have been
460 * new_count [out] The updated count
461 * new_str [out] The updated pointer
462 * start_count [in] The count of remaining characters corresponding to the
463 * start of the string
464 * start_str [in] The starting point of the string
465 * max [in] The number of characters actually in this segment of the
466 * string (the & counts)
467 * n [in] The number of characters to skip (if prefix then
469 * prefix [in] Apply prefix substitution
475 * There must be at least n characters in the string
476 * We need max because the "line" may have ended with a & followed by a tab
477 * or newline etc. which we don't want to swallow
480 static void TEXT_SkipChars (int *new_count
, const WCHAR
**new_str
,
481 int start_count
, const WCHAR
*start_str
,
482 int max
, int n
, int prefix
)
484 /* This is specific to wide characters, MSDN doesn't say anything much
485 * about Unicode surrogates yet and it isn't clear if _wcsinc will
486 * correctly handle them so we'll just do this the easy way for now
491 const WCHAR
*str_on_entry
= start_str
;
496 if ((*start_str
== PREFIX
|| *start_str
== ALPHA_PREFIX
) && max
--)
500 start_count
-= (start_str
- str_on_entry
);
507 *new_str
= start_str
;
508 *new_count
= start_count
;
511 /*********************************************************************
514 * Reanalyse the text to find the prefixed character. This is called when
515 * wordbreaking or ellipsification has shortened the string such that the
516 * previously noted prefixed character is no longer visible.
519 * str [in] The original string segment (including all characters)
520 * ns [in] The number of characters in str (including prefixes)
521 * pe [in] The ellipsification data
524 * The prefix offset within the new string segment (the one that contains the
525 * ellipses and does not contain the prefix characters) (-1 if none)
528 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
529 const ellipsis_data
*pe
)
533 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
535 for (i
= 0; i
< n
; i
++, str
++)
539 /* Reached the path ellipsis; jump over it */
540 if (ns
< pe
->under
) break;
544 if (!pe
->after
) break; /* Nothing after the path ellipsis */
548 if (*str
== PREFIX
|| *str
== ALPHA_PREFIX
)
553 result
= (i
< pe
->before
|| pe
->under
== 0) ? i
: i
- pe
->under
+ pe
->len
;
554 /* pe->len may be non-zero while pe_under is zero */
561 /*********************************************************************
562 * Returns true if and only if the remainder of the line is a single
563 * newline representation or nothing
566 static BOOL
remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
568 if (!num_chars
) return TRUE
;
569 if (*str
!= LF
&& *str
!= CR
) return FALSE
;
570 if (!--num_chars
) return TRUE
;
571 if (*str
== *(str
+1)) return FALSE
;
573 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
574 if (--num_chars
) return FALSE
;
578 /*********************************************************************
579 * Return next line of text from a string.
581 * hdc - handle to DC.
582 * str - string to parse into lines.
583 * count - length of str.
584 * dest - destination in which to return line.
585 * len - dest buffer size in chars on input, copied length into dest on output.
586 * width - maximum width of line in pixels.
587 * format - format type passed to DrawText.
588 * retsize - returned size of the line in pixels.
589 * last_line - TRUE if is the last line that will be processed
590 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
591 * the return string is built.
592 * tabwidth - The width of a tab in logical coordinates
593 * pprefix_offset - Here is where we return the offset within dest of the first
594 * prefixed (underlined) character. -1 is returned if there
595 * are none. Note that there may be more; the calling code
596 * will need to use TEXT_Reprefix to find any later ones.
597 * pellip - Here is where we return the information about any ellipsification
598 * that was carried out. Note that if tabs are being expanded then
599 * this data will correspond to the last text segment actually
600 * returned in dest; by definition there would not have been any
601 * ellipsification in earlier text segments of the line.
603 * Returns pointer to next char in str after end of the line
604 * or NULL if end of str reached.
606 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
607 WCHAR
*dest
, int *len
, int width
, DWORD format
,
608 SIZE
*retsize
, int last_line
, WCHAR
**p_retstr
,
609 int tabwidth
, int *pprefix_offset
,
610 ellipsis_data
*pellip
)
616 int seg_i
, seg_count
, seg_j
;
619 BOOL word_broken
, line_fits
, ellipsified
;
620 unsigned int j_in_seg
;
621 *pprefix_offset
= -1;
623 /* For each text segment in the line */
629 /* Skip any leading tabs */
631 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
633 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
634 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
635 while (*count
&& str
[i
] == TAB
)
638 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
643 /* Now copy as far as the next tab or cr/lf or eos */
650 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
651 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
653 if ((format
& DT_NOPREFIX
) || *count
<= 1)
655 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
659 if (str
[i
] == PREFIX
|| str
[i
] == ALPHA_PREFIX
) {
660 (*count
)--, i
++; /* Throw away the prefix itself */
661 if (str
[i
] == PREFIX
)
663 /* Swallow it before we see it again */
664 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
666 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
670 /* else the previous prefix was in an earlier segment of the
671 * line; we will leave it to the drawing code to catch this
675 else if (str
[i
] == KANA_PREFIX
)
677 /* Throw away katakana access keys */
678 (*count
)--, i
++; /* skip the prefix */
679 (*count
)--, i
++; /* skip the letter */
683 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
688 /* Measure the whole text segment and possibly WordBreak and
692 j_in_seg
= j
- seg_j
;
693 max_seg_width
= width
- plen
;
694 GetTextExtentExPointW (hdc
, dest
+ seg_j
, j_in_seg
, max_seg_width
, &num_fit
, NULL
, &size
);
696 /* The Microsoft handling of various combinations of formats is weird.
697 * The following may very easily be incorrect if several formats are
698 * combined, and may differ between versions (to say nothing of the
699 * several bugs in the Microsoft versions).
702 line_fits
= (num_fit
>= j_in_seg
);
703 if (!line_fits
&& (format
& DT_WORDBREAK
))
706 unsigned int chars_used
;
707 TEXT_WordBreak (hdc
, dest
+seg_j
, maxl
-seg_j
, &j_in_seg
,
708 max_seg_width
, format
, num_fit
, &chars_used
, &size
);
709 line_fits
= (size
.cx
<= max_seg_width
);
710 /* and correct the counts */
711 TEXT_SkipChars (count
, &s
, seg_count
, str
+seg_i
, i
-seg_i
,
712 chars_used
, !(format
& DT_NOPREFIX
));
716 pellip
->before
= j_in_seg
;
721 if (!line_fits
&& (format
& DT_PATH_ELLIPSIS
))
723 TEXT_PathEllipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
724 max_seg_width
, &size
, *p_retstr
, pellip
);
725 line_fits
= (size
.cx
<= max_seg_width
);
728 /* NB we may end up ellipsifying a word-broken or path_ellipsified
730 if ((!line_fits
&& (format
& DT_WORD_ELLIPSIS
)) ||
731 ((format
& DT_END_ELLIPSIS
) &&
732 ((last_line
&& *count
) ||
733 (remainder_is_none_or_newline (*count
, &str
[i
]) && !line_fits
))))
735 int before
, len_ellipsis
;
736 TEXT_Ellipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
737 max_seg_width
, &size
, *p_retstr
, &before
, &len_ellipsis
);
738 if (before
> pellip
->before
)
740 /* We must have done a path ellipsis too */
741 pellip
->after
= before
- pellip
->before
- pellip
->len
;
742 /* Leave the len as the length of the first ellipsis */
746 /* If we are here after a path ellipsification it must be
747 * because even the ellipsis itself didn't fit.
749 assert (pellip
->under
== 0 && pellip
->after
== 0);
750 pellip
->before
= before
;
751 pellip
->len
= len_ellipsis
;
752 /* pellip->after remains as zero as does
758 /* As an optimisation if we have ellipsified and we are expanding
759 * tabs and we haven't reached the end of the line we can skip to it
760 * now rather than going around the loop again.
762 if ((format
& DT_EXPANDTABS
) && ellipsified
)
764 if (format
& DT_SINGLELINE
)
768 while ((*count
) && str
[i
] != CR
&& str
[i
] != LF
)
775 j
= seg_j
+ j_in_seg
;
776 if (*pprefix_offset
>= seg_j
+ pellip
->before
)
778 *pprefix_offset
= TEXT_Reprefix (str
+ seg_i
, i
- seg_i
, pellip
);
779 if (*pprefix_offset
!= -1)
780 *pprefix_offset
+= seg_j
;
784 if (size
.cy
> retsize
->cy
)
785 retsize
->cy
= size
.cy
;
791 else if (str
[i
] == CR
|| str
[i
] == LF
)
794 if (*count
&& (str
[i
] == CR
|| str
[i
] == LF
) && str
[i
] != str
[i
-1])
800 /* else it was a Tab and we go around again */
812 /***********************************************************************
813 * TEXT_DrawUnderscore
815 * Draw the underline under the prefixed character
818 * hdc [in] The handle of the DC for drawing
819 * x [in] The x location of the line segment (logical coordinates)
820 * y [in] The y location of where the underscore should appear
821 * (logical coordinates)
822 * str [in] The text of the line segment
823 * offset [in] The offset of the underscored character within str
824 * rect [in] Clipping rectangle (if not NULL)
827 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
, const RECT
*rect
)
835 GetTextExtentPointW (hdc
, str
, offset
, &size
);
836 prefix_x
= x
+ size
.cx
;
837 GetTextExtentPointW (hdc
, str
, offset
+1, &size
);
838 prefix_end
= x
+ size
.cx
- 1;
839 /* The above method may eventually be slightly wrong due to kerning etc. */
841 /* Check for clipping */
843 if (prefix_x
> rect
->right
|| prefix_end
< rect
->left
|| y
< rect
->top
|| y
> rect
->bottom
)
844 return; /* Completely outside */
845 /* Partially outside */
846 if (prefix_x
< rect
->left
) prefix_x
= rect
->left
;
847 if (prefix_end
> rect
->right
) prefix_end
= rect
->right
;
850 hpen
= CreatePen (PS_SOLID
, 1, GetTextColor (hdc
));
851 oldPen
= SelectObject (hdc
, hpen
);
852 MoveToEx (hdc
, prefix_x
, y
, NULL
);
853 LineTo (hdc
, prefix_end
, y
);
854 SelectObject (hdc
, oldPen
);
858 /***********************************************************************
859 * DrawTextExW (USER32.@)
861 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
862 * is not quite complete, especially with regard to \0. We will assume that
863 * the returned string could have a length of up to i_count+3 and also have
864 * a trailing \0 (which would be 4 more than a not-null-terminated string but
865 * 3 more than a null-terminated string). If this is not so then increase
866 * the allowance in DrawTextExA.
868 #define MAX_BUFFER 1024
869 INT WINAPI
DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
870 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
874 WCHAR
*retstr
, *p_retstr
;
876 WCHAR line
[MAX_BUFFER
];
877 int len
, lh
, count
=i_count
;
879 int lmargin
= 0, rmargin
= 0;
880 int x
= rect
->left
, y
= rect
->top
;
881 int width
= rect
->right
- rect
->left
;
884 int tabwidth
/* to keep gcc happy */ = 0;
889 TRACE("%s, %d, [%s] %08x\n", debugstr_wn (str
, count
), count
,
890 wine_dbgstr_rect(rect
), flags
);
892 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
893 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
899 if (flags
& DT_SINGLELINE
)
900 flags
&= ~DT_WORDBREAK
;
902 GetTextMetricsW(hdc
, &tm
);
903 if (flags
& DT_EXTERNALLEADING
)
904 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
908 if (str
[0] && count
== 0)
911 if (dtp
&& dtp
->cbSize
!= sizeof(DRAWTEXTPARAMS
))
916 count
= strlenW(str
);
919 if( flags
& DT_CALCRECT
)
921 rect
->right
= rect
->left
;
922 if( flags
& DT_SINGLELINE
)
923 rect
->bottom
= rect
->top
+ lh
;
925 rect
->bottom
= rect
->top
;
931 if (GetGraphicsMode(hdc
) == GM_COMPATIBLE
)
933 SIZE window_ext
, viewport_ext
;
934 GetWindowExtEx(hdc
, &window_ext
);
935 GetViewportExtEx(hdc
, &viewport_ext
);
936 if ((window_ext
.cy
> 0) != (viewport_ext
.cy
> 0))
942 lmargin
= dtp
->iLeftMargin
;
943 rmargin
= dtp
->iRightMargin
;
944 if (!(flags
& (DT_CENTER
| DT_RIGHT
)))
946 dtp
->uiLengthDrawn
= 0; /* This param RECEIVES number of chars processed */
949 if (flags
& DT_EXPANDTABS
)
951 int tabstop
= ((flags
& DT_TABSTOP
) && dtp
&& dtp
->iTabLength
) ? dtp
->iTabLength
: 8;
952 tabwidth
= tm
.tmAveCharWidth
* tabstop
;
955 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
957 if (flags
& DT_MODIFYSTRING
)
959 size_retstr
= (count
+ 4) * sizeof (WCHAR
);
960 retstr
= HeapAlloc(GetProcessHeap(), 0, size_retstr
);
961 if (!retstr
) return 0;
962 memcpy (retstr
, str
, size_retstr
);
973 len
= sizeof(line
)/sizeof(line
[0]);
975 last_line
= !(flags
& DT_NOCLIP
) && y
- ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) < rect
->bottom
;
977 last_line
= !(flags
& DT_NOCLIP
) && y
+ ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) > rect
->bottom
;
978 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
, &size
, last_line
, &p_retstr
, tabwidth
, &prefix_offset
, &ellip
);
980 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
982 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
984 if (flags
& DT_SINGLELINE
)
986 if (flags
& DT_VCENTER
) y
= rect
->top
+
987 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
988 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
991 if (!(flags
& DT_CALCRECT
))
993 const WCHAR
*str
= line
;
999 if ((flags
& DT_EXPANDTABS
))
1002 p
= str
; while (p
< str
+len
&& *p
!= TAB
) p
++;
1004 if (len_seg
!= len
&& !GetTextExtentPointW(hdc
, str
, len_seg
, &size
))
1006 HeapFree (GetProcessHeap(), 0, retstr
);
1013 if (!ExtTextOutW( hdc
, xseg
, y
,
1014 ((flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
) |
1015 ((flags
& DT_RTLREADING
) ? ETO_RTLREADING
: 0),
1016 rect
, str
, len_seg
, NULL
))
1018 HeapFree (GetProcessHeap(), 0, retstr
);
1021 if (prefix_offset
!= -1 && prefix_offset
< len_seg
)
1023 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
, (flags
& DT_NOCLIP
) ? NULL
: rect
);
1029 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
1031 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
1032 if (prefix_offset
!= -1)
1034 if (prefix_offset
< len_seg
)
1036 /* We have just drawn an underscore; we ought to
1037 * figure out where the next one is. I am going
1038 * to leave it for now until I have a better model
1039 * for the line, which will make reprefixing easier.
1040 * This is where ellip would be used.
1045 prefix_offset
-= len_seg
;
1050 else if (size
.cx
> max_width
)
1051 max_width
= size
.cx
;
1058 dtp
->uiLengthDrawn
+= len
;
1060 while (strPtr
&& !last_line
);
1062 if (flags
& DT_CALCRECT
)
1064 rect
->right
= rect
->left
+ max_width
;
1067 rect
->right
+= lmargin
+ rmargin
;
1071 memcpy (str
, retstr
, size_retstr
);
1072 HeapFree (GetProcessHeap(), 0, retstr
);
1074 return y
- rect
->top
;
1077 /***********************************************************************
1078 * DrawTextExA (USER32.@)
1080 * If DT_MODIFYSTRING is specified then there must be room for up to
1081 * 4 extra characters. We take great care about just how much modified
1084 INT WINAPI
DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
1085 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
1096 if (!count
) return 0;
1097 if (!str
&& count
> 0) return 0;
1098 if( !str
|| ((count
== -1) && !(count
= strlen(str
))))
1103 if (dtp
&& dtp
->cbSize
!= sizeof(DRAWTEXTPARAMS
))
1106 GetTextMetricsA(hdc
, &tm
);
1107 if (flags
& DT_EXTERNALLEADING
)
1108 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
1112 if( flags
& DT_CALCRECT
)
1114 rect
->right
= rect
->left
;
1115 if( flags
& DT_SINGLELINE
)
1116 rect
->bottom
= rect
->top
+ lh
;
1118 rect
->bottom
= rect
->top
;
1122 cp
= GdiGetCodePage( hdc
);
1123 wcount
= MultiByteToWideChar( cp
, 0, str
, count
, NULL
, 0 );
1126 if (flags
& DT_MODIFYSTRING
)
1131 wstr
= HeapAlloc(GetProcessHeap(), 0, wmax
* sizeof(WCHAR
));
1134 MultiByteToWideChar( cp
, 0, str
, count
, wstr
, wcount
);
1135 if (flags
& DT_MODIFYSTRING
)
1136 for (i
=4, p
=wstr
+wcount
; i
--; p
++) *p
=0xFFFE;
1137 /* Initialise the extra characters so that we can see which ones
1138 * change. U+FFFE is guaranteed to be not a unicode character and
1139 * so will not be generated by DrawTextEx itself.
1141 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, dtp
);
1142 if (flags
& DT_MODIFYSTRING
)
1144 /* Unfortunately the returned string may contain multiple \0s
1145 * and so we need to measure it ourselves.
1147 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1148 WideCharToMultiByte( cp
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1150 HeapFree(GetProcessHeap(), 0, wstr
);
1155 /***********************************************************************
1156 * DrawTextW (USER32.@)
1158 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1162 memset (&dtp
, 0, sizeof(dtp
));
1163 dtp
.cbSize
= sizeof(dtp
);
1164 if (flags
& DT_TABSTOP
)
1166 dtp
.iTabLength
= (flags
>> 8) & 0xff;
1167 flags
&= 0xffff00ff;
1169 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1172 /***********************************************************************
1173 * DrawTextA (USER32.@)
1175 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1179 memset (&dtp
, 0, sizeof(dtp
));
1180 dtp
.cbSize
= sizeof(dtp
);
1181 if (flags
& DT_TABSTOP
)
1183 dtp
.iTabLength
= (flags
>> 8) & 0xff;
1184 flags
&= 0xffff00ff;
1186 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);
1189 /***********************************************************************
1191 * GrayString functions
1194 /* callback for ASCII gray string proc */
1195 static BOOL CALLBACK
gray_string_callbackA( HDC hdc
, LPARAM param
, INT len
)
1197 return TextOutA( hdc
, 0, 0, (LPCSTR
)param
, len
);
1200 /* callback for Unicode gray string proc */
1201 static BOOL CALLBACK
gray_string_callbackW( HDC hdc
, LPARAM param
, INT len
)
1203 return TextOutW( hdc
, 0, 0, (LPCWSTR
)param
, len
);
1206 /***********************************************************************
1209 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
, GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
1210 INT x
, INT y
, INT cx
, INT cy
)
1212 HBITMAP hbm
, hbmsave
;
1220 if(!hdc
) return FALSE
;
1221 if (!(memdc
= CreateCompatibleDC(hdc
))) return FALSE
;
1223 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
1224 hbmsave
= SelectObject(memdc
, hbm
);
1225 hbsave
= SelectObject( memdc
, GetStockObject(BLACK_BRUSH
) );
1226 PatBlt( memdc
, 0, 0, cx
, cy
, PATCOPY
);
1227 SelectObject( memdc
, hbsave
);
1228 SetTextColor(memdc
, RGB(255, 255, 255));
1229 SetBkColor(memdc
, RGB(0, 0, 0));
1230 hfsave
= SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
1232 retval
= fn(memdc
, lp
, slen
);
1233 SelectObject(memdc
, hfsave
);
1236 * Windows doc says that the bitmap isn't grayed when len == -1 and
1237 * the callback function returns FALSE. However, testing this on
1238 * win95 showed otherwise...
1240 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1241 if(retval
|| len
!= -1)
1244 hbsave
= SelectObject(memdc
, SYSCOLOR_Get55AABrush());
1245 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
1246 SelectObject(memdc
, hbsave
);
1249 if(hb
) hbsave
= SelectObject(hdc
, hb
);
1250 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
1251 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
1252 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
1253 SetTextColor(hdc
, fg
);
1254 SetBkColor(hdc
, bg
);
1255 if(hb
) SelectObject(hdc
, hbsave
);
1257 SelectObject(memdc
, hbmsave
);
1264 /***********************************************************************
1265 * GrayStringA (USER32.@)
1267 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1268 LPARAM lParam
, INT cch
, INT x
, INT y
,
1271 if (!cch
) cch
= strlen( (LPCSTR
)lParam
);
1272 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1275 GetTextExtentPoint32A( hdc
, (LPCSTR
)lParam
, cch
, &s
);
1276 if (cx
== 0) cx
= s
.cx
;
1277 if (cy
== 0) cy
= s
.cy
;
1279 if (!gsprc
) gsprc
= gray_string_callbackA
;
1280 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1284 /***********************************************************************
1285 * GrayStringW (USER32.@)
1287 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1288 LPARAM lParam
, INT cch
, INT x
, INT y
,
1291 if (!cch
) cch
= strlenW( (LPCWSTR
)lParam
);
1292 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1295 GetTextExtentPoint32W( hdc
, (LPCWSTR
)lParam
, cch
, &s
);
1296 if (cx
== 0) cx
= s
.cx
;
1297 if (cy
== 0) cy
= s
.cy
;
1299 if (!gsprc
) gsprc
= gray_string_callbackW
;
1300 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1304 /***********************************************************************
1305 * TEXT_TabbedTextOut
1307 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1308 * Note: this doesn't work too well for text-alignment modes other
1309 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1311 static LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCWSTR lpstr
,
1312 INT count
, INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
,
1321 if (!lpstr
|| count
== 0) return 0;
1326 GetTextMetricsW( hdc
, &tm
);
1330 defWidth
= *lpTabPos
;
1335 defWidth
= 8 * tm
.tmAveCharWidth
;
1344 /* chop the string into substrings of 0 or more <tabs>
1345 * possibly followed by 1 or more normal characters */
1346 for (i
= 0; i
< count
; i
++)
1347 if (lpstr
[i
] != '\t') break;
1348 for (j
= i
; j
< count
; j
++)
1349 if (lpstr
[j
] == '\t') break;
1350 /* get the extent of the normal character part */
1351 GetTextExtentPointW( hdc
, lpstr
+ i
, j
- i
, &extent
);
1352 /* and if there is a <tab>, calculate its position */
1354 /* get x coordinate for the drawing of this string */
1355 for (; cTabStops
>= i
; lpTabPos
++, cTabStops
--)
1357 if( nTabOrg
+ abs( *lpTabPos
) > x
) {
1358 if( lpTabPos
[ i
- 1] >= 0) {
1359 /* a left aligned tab */
1360 x0
= nTabOrg
+ lpTabPos
[i
-1];
1366 /* if tab pos is negative then text is right-aligned
1367 * to tab stop meaning that the string extends to the
1368 * left, so we must subtract the width of the string */
1369 if (nTabOrg
- lpTabPos
[ i
- 1] - extent
.cx
> x
)
1371 x
= nTabOrg
- lpTabPos
[ i
- 1];
1378 /* if we have run out of tab stops and we have a valid default tab
1379 * stop width then round x up to that width */
1380 if ((cTabStops
< i
) && (defWidth
> 0)) {
1381 x0
= nTabOrg
+ ((x
- nTabOrg
) / defWidth
+ i
) * defWidth
;
1383 } else if ((cTabStops
< i
) && (defWidth
< 0)) {
1384 x
= nTabOrg
+ ((x
- nTabOrg
+ extent
.cx
) / -defWidth
+ i
)
1395 r
.bottom
= y
+ extent
.cy
;
1396 ExtTextOutW( hdc
, x0
, y
, GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
1397 &r
, lpstr
+ i
, j
- i
, NULL
);
1404 extent
.cy
= tm
.tmHeight
;
1406 return MAKELONG(x
- start
, extent
.cy
);
1410 /***********************************************************************
1411 * TabbedTextOutA (USER32.@)
1413 * See TabbedTextOutW.
1415 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
1416 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1419 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1420 LPWSTR strW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1421 if (!strW
) return 0;
1422 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1423 ret
= TabbedTextOutW( hdc
, x
, y
, strW
, len
, cTabStops
, lpTabPos
, nTabOrg
);
1424 HeapFree( GetProcessHeap(), 0, strW
);
1429 /***********************************************************************
1430 * TabbedTextOutW (USER32.@)
1432 * Draws tabbed text aligned using the specified tab stops.
1435 * hdc [I] Handle to device context to draw to.
1436 * x [I] X co-ordinate to start drawing the text at in logical units.
1437 * y [I] Y co-ordinate to start drawing the text at in logical units.
1438 * str [I] Pointer to the characters to draw.
1439 * count [I] Number of WCHARs pointed to by str.
1440 * cTabStops [I] Number of tab stops pointed to by lpTabPos.
1441 * lpTabPos [I] Tab stops in logical units. Should be sorted in ascending order.
1442 * nTabOrg [I] Starting position to expand tabs from in logical units.
1445 * The dimensions of the string drawn. The height is in the high-order word
1446 * and the width is in the low-order word.
1449 * The tabs stops can be negative, in which case the text is right aligned to
1450 * that tab stop and, despite what MSDN says, this is supported on
1454 * MSDN says that the TA_UPDATECP from GetTextAlign causes this function to
1455 * ignore the x and y co-ordinates, but this is unimplemented at the moment.
1457 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
1458 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1460 TRACE("%p %d,%d %s %d\n", hdc
, x
, y
, debugstr_wn(str
,count
), count
);
1461 return TEXT_TabbedTextOut( hdc
, x
, y
, str
, count
, cTabStops
, lpTabPos
, nTabOrg
, TRUE
);
1465 /***********************************************************************
1466 * GetTabbedTextExtentA (USER32.@)
1468 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
1469 INT cTabStops
, const INT
*lpTabPos
)
1472 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1473 LPWSTR strW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1474 if (!strW
) return 0;
1475 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1476 ret
= GetTabbedTextExtentW( hdc
, strW
, len
, cTabStops
, lpTabPos
);
1477 HeapFree( GetProcessHeap(), 0, strW
);
1482 /***********************************************************************
1483 * GetTabbedTextExtentW (USER32.@)
1485 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
1486 INT cTabStops
, const INT
*lpTabPos
)
1488 TRACE("%p %s %d\n", hdc
, debugstr_wn(lpstr
,count
), count
);
1489 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
, lpTabPos
, 0, FALSE
);