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"
41 #include "user_private.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(text);
46 /*********************************************************************
51 * How many buffers to use
52 * While processing in DrawText there are potentially three different forms
53 * of the text that need to be held. How are they best held?
54 * 1. The original text is needed, of course, to see what to display.
55 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
57 * 3. The buffered text that is about to be displayed e.g. the current line.
58 * Typically this will exclude the ampersands used for prefixing etc.
61 * a. If the buffered text to be displayed includes the ampersands then
62 * we will need special measurement and draw functions that will ignore
63 * the ampersands (e.g. by copying to a buffer without the prefix and
64 * then using the normal forms). This may involve less space but may
65 * require more processing. e.g. since a line containing tabs may
66 * contain several underlined characters either we need to carry around
67 * a list of prefix locations or we may need to locate them several
69 * b. If we actually directly modify the "original text" as we go then we
70 * will need some special "caching" to handle the fact that when we
71 * ellipsify the text the ellipsis may modify the next line of text,
72 * which we have not yet processed. (e.g. ellipsification of a W at the
73 * end of a line will overwrite the W, the \n and the first character of
74 * the next line, and a \0 will overwrite the second. Try it!!)
76 * Option 1. Three separate storages. (To be implemented)
77 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
78 * the edited string in some form, either as the string itself or as some
79 * sort of "edit list" to be applied just before returning.
80 * Use a buffer that holds the ellipsified current line sans ampersands
81 * and accept the need occasionally to recalculate the prefixes (if
82 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
90 #define ALPHA_PREFIX 30 /* Win16: Alphabet prefix */
91 #define KANA_PREFIX 31 /* Win16: Katakana prefix */
93 #define FORWARD_SLASH '/'
94 #define BACK_SLASH '\\'
96 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
98 typedef struct tag_ellipsis_data
106 /*********************************************************************
107 * TEXT_Ellipsify (static)
109 * Add an ellipsis to the end of the given string whilst ensuring it fits.
111 * If the ellipsis alone doesn't fit then it will be returned anyway.
113 * See Also TEXT_PathEllipsify
116 * hdc [in] The handle to the DC that defines the font.
117 * str [in/out] The string that needs to be modified.
118 * max_str [in] The dimension of str (number of WCHAR).
119 * len_str [in/out] The number of characters in str
120 * width [in] The maximum width permitted (in logical coordinates)
121 * size [out] The dimensions of the text
122 * modstr [out] The modified form of the string, to be returned to the
123 * calling program. It is assumed that the caller has
124 * made sufficient space available so we don't need to
125 * know the size of the space. This pointer may be NULL if
126 * the modified string is not required.
127 * len_before [out] The number of characters before the ellipsis.
128 * len_ellip [out] The number of characters in the ellipsis.
130 * See for example Microsoft article Q249678.
132 * For now we will simply use three dots rather than worrying about whether
133 * the font contains an explicit ellipsis character.
135 static void TEXT_Ellipsify (HDC hdc, WCHAR *str, unsigned int max_len,
136 unsigned int *len_str, int width, SIZE *size,
138 int *len_before, int *len_ellip)
140 unsigned int len_ellipsis;
141 unsigned int lo, mid, hi;
143 len_ellipsis = strlenW (ELLIPSISW);
144 if (len_ellipsis > max_len) len_ellipsis = max_len;
145 if (*len_str > max_len - len_ellipsis)
146 *len_str = max_len - len_ellipsis;
148 /* First do a quick binary search to get an upper bound for *len_str. */
150 GetTextExtentExPointW(hdc, str, *len_str, width, NULL, NULL, size) &&
153 for (lo = 0, hi = *len_str; lo < hi; )
156 if (!GetTextExtentExPointW(hdc, str, mid, width, NULL, NULL, size))
158 if (size->cx > width)
165 /* Now this should take only a couple iterations at most. */
168 memcpy(str + *len_str, ELLIPSISW, len_ellipsis*sizeof(WCHAR));
170 if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
171 NULL, NULL, size)) break;
173 if (!*len_str || size->cx <= width) break;
177 *len_ellip = len_ellipsis;
178 *len_before = *len_str;
179 *len_str += len_ellipsis;
183 memcpy (modstr, str, *len_str * sizeof(WCHAR));
184 modstr[*len_str] = '\0';
188 /*********************************************************************
189 * TEXT_PathEllipsify (static)
191 * Add an ellipsis to the provided string in order to make it fit within
192 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
195 * See Also TEXT_Ellipsify
198 * hdc [in] The handle to the DC that defines the font.
199 * str [in/out] The string that needs to be modified
200 * max_str [in] The dimension of str (number of WCHAR).
201 * len_str [in/out] The number of characters in str
202 * width [in] The maximum width permitted (in logical coordinates)
203 * size [out] The dimensions of the text
204 * modstr [out] The modified form of the string, to be returned to the
205 * calling program. It is assumed that the caller has
206 * made sufficient space available so we don't need to
207 * know the size of the space. This pointer may be NULL if
208 * the modified string is not required.
209 * pellip [out] The ellipsification results
211 * For now we will simply use three dots rather than worrying about whether
212 * the font contains an explicit ellipsis character.
214 * The following applies, I think to Win95. We will need to extend it for
215 * Win98 which can have both path and end ellipsis at the same time (e.g.
216 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
218 * The resulting string consists of as much as possible of the following:
219 * 1. The ellipsis itself
220 * 2. The last \ or / of the string (if any)
221 * 3. Everything after the last \ or / of the string (if any) or the whole
222 * string if there is no / or \. I believe that under Win95 this would
223 * include everything even though some might be clipped off the end whereas
224 * under Win98 that might be ellipsified too.
225 * Yet to be investigated is whether this would include wordbreaking if the
226 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
227 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
228 * broken within words).
229 * 4. All the stuff before the / or \, which is placed before the ellipsis.
231 static void TEXT_PathEllipsify (HDC hdc, WCHAR *str, unsigned int max_len,
232 unsigned int *len_str, int width, SIZE *size,
233 WCHAR *modstr, ellipsis_data *pellip)
238 WCHAR *lastBkSlash, *lastFwdSlash, *lastSlash;
240 len_ellipsis = strlenW (ELLIPSISW);
241 if (!max_len) return;
242 if (len_ellipsis >= max_len) len_ellipsis = max_len - 1;
243 if (*len_str + len_ellipsis >= max_len)
244 *len_str = max_len - len_ellipsis-1;
245 /* Hopefully this will never happen, otherwise it would probably lose
246 * the wrong character
248 str[*len_str] = '\0'; /* to simplify things */
250 lastBkSlash = strrchrW (str, BACK_SLASH);
251 lastFwdSlash = strrchrW (str, FORWARD_SLASH);
252 lastSlash = lastBkSlash > lastFwdSlash ? lastBkSlash : lastFwdSlash;
253 if (!lastSlash) lastSlash = str;
254 len_trailing = *len_str - (lastSlash - str);
256 /* overlap-safe movement to the right */
257 memmove (lastSlash+len_ellipsis, lastSlash, len_trailing * sizeof(WCHAR));
258 memcpy (lastSlash, ELLIPSISW, len_ellipsis*sizeof(WCHAR));
259 len_trailing += len_ellipsis;
260 /* From this point on lastSlash actually points to the ellipsis in front
261 * of the last slash and len_trailing includes the ellipsis
267 if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
268 NULL, NULL, size)) break;
270 if (lastSlash == str || size->cx <= width) break;
272 /* overlap-safe movement to the left */
273 memmove (lastSlash-1, lastSlash, len_trailing * sizeof(WCHAR));
280 pellip->before = lastSlash-str;
281 pellip->len = len_ellipsis;
282 pellip->under = len_under;
283 pellip->after = len_trailing - len_ellipsis;
284 *len_str += len_ellipsis;
288 memcpy(modstr, str, *len_str * sizeof(WCHAR));
289 modstr[*len_str] = '\0';
293 /*********************************************************************
294 * TEXT_WordBreak (static)
296 * Perform wordbreak processing on the given string
298 * Assumes that DT_WORDBREAK has been specified and not all the characters
299 * fit. Note that this function should even be called when the first character
300 * that doesn't fit is known to be a space or tab, so that it can swallow them.
302 * Note that the Windows processing has some strange properties.
303 * 1. If the text is left-justified and there is room for some of the spaces
304 * that follow the last word on the line then those that fit are included on
306 * 2. If the text is centered or right-justified and there is room for some of
307 * the spaces that follow the last word on the line then all but one of those
308 * that fit are included on the line.
309 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
310 * character of a new line it will be skipped.
313 * hdc [in] The handle to the DC that defines the font.
314 * str [in/out] The string that needs to be broken.
315 * max_str [in] The dimension of str (number of WCHAR).
316 * len_str [in/out] The number of characters in str
317 * width [in] The maximum width permitted
318 * format [in] The format flags in effect
319 * chars_fit [in] The maximum number of characters of str that are already
320 * known to fit; chars_fit+1 is known not to fit.
321 * chars_used [out] The number of characters of str that have been "used" and
322 * do not need to be included in later text. For example this will
323 * include any spaces that have been discarded from the start of
325 * size [out] The size of the returned text in logical coordinates
327 * Pedantic assumption - Assumes that the text length is monotonically
328 * increasing with number of characters (i.e. no weird kernings)
332 * Work back from the last character that did fit to either a space or the last
333 * character of a word, whichever is met first.
334 * If there was one or the first character didn't fit then
335 * If the text is centered or right justified and that one character was a
336 * space then break the line before that character
337 * Otherwise break the line after that character
338 * and if the next character is a space then discard it.
339 * Suppose there was none (and the first character did fit).
340 * If Break Within Word is permitted
341 * break the word after the last character that fits (there must be
342 * at least one; none is caught earlier).
344 * discard any trailing space.
345 * include the whole word; it may be ellipsified later
347 * Break Within Word is permitted under a set of circumstances that are not
348 * totally clear yet. Currently our best guess is:
349 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
350 * DT_PATH_ELLIPSIS is
353 static void TEXT_WordBreak (HDC hdc, WCHAR *str, unsigned int max_str,
354 unsigned int *len_str,
355 int width, int format, unsigned int chars_fit,
356 unsigned int *chars_used, SIZE *size)
360 assert (format & DT_WORDBREAK);
361 assert (chars_fit < *len_str);
363 /* Work back from the last character that did fit to either a space or the
364 * last character of a word, whichever is met first.
366 p = str + chars_fit; /* The character that doesn't fit */
369 ; /* we pretend that it fits anyway */
370 else if (*p == SPACE) /* chars_fit < *len_str so this is valid */
371 p--; /* the word just fitted */
374 while (p > str && *(--p) != SPACE)
376 word_fits = (p != str || *p == SPACE);
378 /* If there was one or the first character didn't fit then */
382 /* break the line before/after that character */
383 if (!(format & (DT_RIGHT | DT_CENTER)) || *p != SPACE)
385 next_is_space = (p - str) < *len_str && *p == SPACE;
387 /* and if the next character is a space then discard it. */
388 *chars_used = *len_str;
392 /* Suppose there was none. */
395 if ((format & (DT_EDITCONTROL | DT_WORD_ELLIPSIS | DT_PATH_ELLIPSIS)) ==
398 /* break the word after the last character that fits (there must be
399 * at least one; none is caught earlier).
401 *len_str = chars_fit;
402 *chars_used = chars_fit;
404 /* FIXME - possible error. Since the next character is now removed
405 * this could make the text longer so that it no longer fits, and
406 * so we need a loop to test and shrink.
412 /* discard any trailing space. */
413 const WCHAR *e = str + *len_str;
415 while (p < e && *p != SPACE)
417 *chars_used = p - str;
418 if (p < e) /* i.e. loop failed because *p == SPACE */
421 /* include the whole word; it may be ellipsified later */
423 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
424 * so that it will be too long
428 /* Remeasure the string */
429 GetTextExtentExPointW (hdc, str, *len_str, 0, NULL, NULL, size);
432 /*********************************************************************
435 * Skip over the given number of characters, bearing in mind prefix
436 * substitution and the fact that a character may take more than one
437 * WCHAR (Unicode surrogates are two words long) (and there may have been
441 * new_count [out] The updated count
442 * new_str [out] The updated pointer
443 * start_count [in] The count of remaining characters corresponding to the
444 * start of the string
445 * start_str [in] The starting point of the string
446 * max [in] The number of characters actually in this segment of the
447 * string (the & counts)
448 * n [in] The number of characters to skip (if prefix then
450 * prefix [in] Apply prefix substitution
456 * There must be at least n characters in the string
457 * We need max because the "line" may have ended with a & followed by a tab
458 * or newline etc. which we don't want to swallow
461 static void TEXT_SkipChars (int *new_count, const WCHAR **new_str,
462 int start_count, const WCHAR *start_str,
463 int max, int n, int prefix)
465 /* This is specific to wide characters, MSDN doesn't say anything much
466 * about Unicode surrogates yet and it isn't clear if _wcsinc will
467 * correctly handle them so we'll just do this the easy way for now
472 const WCHAR *str_on_entry = start_str;
477 if ((*start_str == PREFIX || *start_str == ALPHA_PREFIX) && max--)
481 start_count -= (start_str - str_on_entry);
488 *new_str = start_str;
489 *new_count = start_count;
492 /*********************************************************************
495 * Reanalyse the text to find the prefixed character. This is called when
496 * wordbreaking or ellipsification has shortened the string such that the
497 * previously noted prefixed character is no longer visible.
500 * str [in] The original string segment (including all characters)
501 * ns [in] The number of characters in str (including prefixes)
502 * pe [in] The ellipsification data
505 * The prefix offset within the new string segment (the one that contains the
506 * ellipses and does not contain the prefix characters) (-1 if none)
509 static int TEXT_Reprefix (const WCHAR *str, unsigned int ns,
510 const ellipsis_data *pe)
514 unsigned int n = pe->before + pe->under + pe->after;
516 for (i = 0; i < n; i++, str++)
520 /* Reached the path ellipsis; jump over it */
521 if (ns < pe->under) break;
525 if (!pe->after) break; /* Nothing after the path ellipsis */
529 if (*str == PREFIX || *str == ALPHA_PREFIX)
534 result = (i < pe->before || pe->under == 0) ? i : i - pe->under + pe->len;
535 /* pe->len may be non-zero while pe_under is zero */
542 /*********************************************************************
543 * Returns true if and only if the remainder of the line is a single
544 * newline representation or nothing
547 static int remainder_is_none_or_newline (int num_chars, const WCHAR *str)
549 if (!num_chars) return TRUE;
550 if (*str != LF && *str != CR) return FALSE;
551 if (!--num_chars) return TRUE;
552 if (*str == *(str+1)) return FALSE;
554 if (*str != CR && *str != LF) return FALSE;
555 if (--num_chars) return FALSE;
559 /*********************************************************************
560 * Return next line of text from a string.
562 * hdc - handle to DC.
563 * str - string to parse into lines.
564 * count - length of str.
565 * dest - destination in which to return line.
566 * len - dest buffer size in chars on input, copied length into dest on output.
567 * width - maximum width of line in pixels.
568 * format - format type passed to DrawText.
569 * retsize - returned size of the line in pixels.
570 * last_line - TRUE if is the last line that will be processed
571 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
572 * the return string is built.
573 * tabwidth - The width of a tab in logical coordinates
574 * pprefix_offset - Here is where we return the offset within dest of the first
575 * prefixed (underlined) character. -1 is returned if there
576 * are none. Note that there may be more; the calling code
577 * will need to use TEXT_Reprefix to find any later ones.
578 * pellip - Here is where we return the information about any ellipsification
579 * that was carried out. Note that if tabs are being expanded then
580 * this data will correspond to the last text segment actually
581 * returned in dest; by definition there would not have been any
582 * ellipsification in earlier text segments of the line.
584 * Returns pointer to next char in str after end of the line
585 * or NULL if end of str reached.
587 static const WCHAR *TEXT_NextLineW( HDC hdc, const WCHAR *str, int *count,
588 WCHAR *dest, int *len, int width, DWORD format,
589 SIZE *retsize, int last_line, WCHAR **p_retstr,
590 int tabwidth, int *pprefix_offset,
591 ellipsis_data *pellip)
597 int seg_i, seg_count, seg_j;
602 unsigned int j_in_seg;
604 *pprefix_offset = -1;
606 /* For each text segment in the line */
612 /* Skip any leading tabs */
614 if (str[i] == TAB && (format & DT_EXPANDTABS))
616 plen = ((plen/tabwidth)+1)*tabwidth;
617 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
618 while (*count && str[i] == TAB)
621 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
626 /* Now copy as far as the next tab or cr/lf or eos */
633 (str[i] != TAB || !(format & DT_EXPANDTABS)) &&
634 ((str[i] != CR && str[i] != LF) || (format & DT_SINGLELINE)))
636 if ((format & DT_NOPREFIX) || *count <= 1)
638 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
642 if (str[i] == PREFIX || str[i] == ALPHA_PREFIX) {
643 (*count)--, i++; /* Throw away the prefix itself */
644 if (str[i] == PREFIX)
646 /* Swallow it before we see it again */
647 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
649 else if (*pprefix_offset == -1 || *pprefix_offset >= seg_j)
653 /* else the previous prefix was in an earlier segment of the
654 * line; we will leave it to the drawing code to catch this
658 else if (str[i] == KANA_PREFIX)
660 /* Throw away katakana access keys */
661 (*count)--, i++; /* skip the prefix */
662 (*count)--, i++; /* skip the letter */
666 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
671 /* Measure the whole text segment and possibly WordBreak and
675 j_in_seg = j - seg_j;
676 max_seg_width = width - plen;
677 GetTextExtentExPointW (hdc, dest + seg_j, j_in_seg, max_seg_width, &num_fit, NULL, &size);
679 /* The Microsoft handling of various combinations of formats is weird.
680 * The following may very easily be incorrect if several formats are
681 * combined, and may differ between versions (to say nothing of the
682 * several bugs in the Microsoft versions).
685 line_fits = (num_fit >= j_in_seg);
686 if (!line_fits && (format & DT_WORDBREAK))
689 unsigned int chars_used;
690 TEXT_WordBreak (hdc, dest+seg_j, maxl-seg_j, &j_in_seg,
691 max_seg_width, format, num_fit, &chars_used, &size);
692 line_fits = (size.cx <= max_seg_width);
693 /* and correct the counts */
694 TEXT_SkipChars (count, &s, seg_count, str+seg_i, i-seg_i,
695 chars_used, !(format & DT_NOPREFIX));
699 pellip->before = j_in_seg;
704 if (!line_fits && (format & DT_PATH_ELLIPSIS))
706 TEXT_PathEllipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
707 max_seg_width, &size, *p_retstr, pellip);
708 line_fits = (size.cx <= max_seg_width);
711 /* NB we may end up ellipsifying a word-broken or path_ellipsified
713 if ((!line_fits && (format & DT_WORD_ELLIPSIS)) ||
714 ((format & DT_END_ELLIPSIS) &&
715 ((last_line && *count) ||
716 (remainder_is_none_or_newline (*count, &str[i]) && !line_fits))))
718 int before, len_ellipsis;
719 TEXT_Ellipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
720 max_seg_width, &size, *p_retstr, &before, &len_ellipsis);
721 if (before > pellip->before)
723 /* We must have done a path ellipsis too */
724 pellip->after = before - pellip->before - pellip->len;
725 /* Leave the len as the length of the first ellipsis */
729 /* If we are here after a path ellipsification it must be
730 * because even the ellipsis itself didn't fit.
732 assert (pellip->under == 0 && pellip->after == 0);
733 pellip->before = before;
734 pellip->len = len_ellipsis;
735 /* pellip->after remains as zero as does
741 /* As an optimisation if we have ellipsified and we are expanding
742 * tabs and we haven't reached the end of the line we can skip to it
743 * now rather than going around the loop again.
745 if ((format & DT_EXPANDTABS) && ellipsified)
747 if (format & DT_SINGLELINE)
751 while ((*count) && str[i] != CR && str[i] != LF)
758 j = seg_j + j_in_seg;
759 if (*pprefix_offset >= seg_j + pellip->before)
761 *pprefix_offset = TEXT_Reprefix (str + seg_i, i - seg_i, pellip);
762 if (*pprefix_offset != -1)
763 *pprefix_offset += seg_j;
767 if (size.cy > retsize->cy)
768 retsize->cy = size.cy;
774 else if (str[i] == CR || str[i] == LF)
777 if (*count && (str[i] == CR || str[i] == LF) && str[i] != str[i-1])
783 /* else it was a Tab and we go around again */
795 /***********************************************************************
796 * TEXT_DrawUnderscore
798 * Draw the underline under the prefixed character
801 * hdc [in] The handle of the DC for drawing
802 * x [in] The x location of the line segment (logical coordinates)
803 * y [in] The y location of where the underscore should appear
804 * (logical coordinates)
805 * str [in] The text of the line segment
806 * offset [in] The offset of the underscored character within str
807 * rect [in] Clipping rectangle (if not NULL)
810 static void TEXT_DrawUnderscore (HDC hdc, int x, int y, const WCHAR *str, int offset, const RECT *rect)
818 GetTextExtentPointW (hdc, str, offset, &size);
819 prefix_x = x + size.cx;
820 GetTextExtentPointW (hdc, str, offset+1, &size);
821 prefix_end = x + size.cx - 1;
822 /* The above method may eventually be slightly wrong due to kerning etc. */
824 /* Check for clipping */
826 if (prefix_x > rect->right || prefix_end < rect->left || y < rect->top || y > rect->bottom)
827 return; /* Completely outside */
828 /* Partially outside */
829 if (prefix_x < rect->left ) prefix_x = rect->left;
830 if (prefix_end > rect->right) prefix_end = rect->right;
833 hpen = CreatePen (PS_SOLID, 1, GetTextColor (hdc));
834 oldPen = SelectObject (hdc, hpen);
835 MoveToEx (hdc, prefix_x, y, NULL);
836 LineTo (hdc, prefix_end, y);
837 SelectObject (hdc, oldPen);
841 /***********************************************************************
842 * DrawTextExW (USER32.@)
844 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
845 * is not quite complete, especially with regard to \0. We will assume that
846 * the returned string could have a length of up to i_count+3 and also have
847 * a trailing \0 (which would be 4 more than a not-null-terminated string but
848 * 3 more than a null-terminated string). If this is not so then increase
849 * the allowance in DrawTextExA.
851 #define MAX_BUFFER 1024
852 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
853 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
857 WCHAR *retstr, *p_retstr;
859 WCHAR line[MAX_BUFFER];
860 int len, lh, count=i_count;
862 int lmargin = 0, rmargin = 0;
863 int x = rect->left, y = rect->top;
864 int width = rect->right - rect->left;
867 int tabwidth /* to keep gcc happy */ = 0;
872 TRACE("%s, %d, [%s] %08x\n", debugstr_wn (str, count), count,
873 wine_dbgstr_rect(rect), flags);
875 if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
876 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
882 if (flags & DT_SINGLELINE)
883 flags &= ~DT_WORDBREAK;
885 GetTextMetricsW(hdc, &tm);
886 if (flags & DT_EXTERNALLEADING)
887 lh = tm.tmHeight + tm.tmExternalLeading;
891 if (str[0] && count == 0)
894 if (dtp && dtp->cbSize != sizeof(DRAWTEXTPARAMS))
899 count = strlenW(str);
902 if( flags & DT_CALCRECT)
904 rect->right = rect->left;
905 if( flags & DT_SINGLELINE)
906 rect->bottom = rect->top + lh;
908 rect->bottom = rect->top;
914 if (GetGraphicsMode(hdc) == GM_COMPATIBLE)
916 SIZE window_ext, viewport_ext;
917 GetWindowExtEx(hdc, &window_ext);
918 GetViewportExtEx(hdc, &viewport_ext);
919 if ((window_ext.cy > 0) != (viewport_ext.cy > 0))
925 lmargin = dtp->iLeftMargin;
926 rmargin = dtp->iRightMargin;
927 if (!(flags & (DT_CENTER | DT_RIGHT)))
929 dtp->uiLengthDrawn = 0; /* This param RECEIVES number of chars processed */
932 if (flags & DT_EXPANDTABS)
934 int tabstop = ((flags & DT_TABSTOP) && dtp) ? dtp->iTabLength : 8;
935 tabwidth = tm.tmAveCharWidth * tabstop;
938 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
940 if (flags & DT_MODIFYSTRING)
942 size_retstr = (count + 4) * sizeof (WCHAR);
943 retstr = HeapAlloc(GetProcessHeap(), 0, size_retstr);
944 if (!retstr) return 0;
945 memcpy (retstr, str, size_retstr);
956 len = sizeof(line)/sizeof(line[0]);
958 last_line = !(flags & DT_NOCLIP) && y - ((flags & DT_EDITCONTROL) ? 2*lh-1 : lh) < rect->bottom;
960 last_line = !(flags & DT_NOCLIP) && y + ((flags & DT_EDITCONTROL) ? 2*lh-1 : lh) > rect->bottom;
961 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags, &size, last_line, &p_retstr, tabwidth, &prefix_offset, &ellip);
963 if (flags & DT_CENTER) x = (rect->left + rect->right -
965 else if (flags & DT_RIGHT) x = rect->right - size.cx;
967 if (flags & DT_SINGLELINE)
969 if (flags & DT_VCENTER) y = rect->top +
970 (rect->bottom - rect->top) / 2 - size.cy / 2;
971 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
974 if (!(flags & DT_CALCRECT))
976 const WCHAR *str = line;
982 if ((flags & DT_EXPANDTABS))
985 p = str; while (p < str+len && *p != TAB) p++;
987 if (len_seg != len && !GetTextExtentPointW(hdc, str, len_seg, &size))
993 if (!ExtTextOutW( hdc, xseg, y,
994 ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
995 ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
996 rect, str, len_seg, NULL )) return 0;
997 if (prefix_offset != -1 && prefix_offset < len_seg)
999 TEXT_DrawUnderscore (hdc, xseg, y + tm.tmAscent + 1, str, prefix_offset, (flags & DT_NOCLIP) ? NULL : rect);
1005 assert ((flags & DT_EXPANDTABS) && *str == TAB);
1007 xseg += ((size.cx/tabwidth)+1)*tabwidth;
1008 if (prefix_offset != -1)
1010 if (prefix_offset < len_seg)
1012 /* We have just drawn an underscore; we ought to
1013 * figure out where the next one is. I am going
1014 * to leave it for now until I have a better model
1015 * for the line, which will make reprefixing easier.
1016 * This is where ellip would be used.
1021 prefix_offset -= len_seg;
1026 else if (size.cx > max_width)
1027 max_width = size.cx;
1034 dtp->uiLengthDrawn += len;
1036 while (strPtr && !last_line);
1038 if (flags & DT_CALCRECT)
1040 rect->right = rect->left + max_width;
1043 rect->right += lmargin + rmargin;
1047 memcpy (str, retstr, size_retstr);
1048 HeapFree (GetProcessHeap(), 0, retstr);
1050 return y - rect->top;
1053 /***********************************************************************
1054 * DrawTextExA (USER32.@)
1056 * If DT_MODIFYSTRING is specified then there must be room for up to
1057 * 4 extra characters. We take great care about just how much modified
1060 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
1061 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
1072 if (!count) return 0;
1073 if (!str && count > 0) return 0;
1074 if( !str || ((count == -1) && !(count = strlen(str))))
1079 if (dtp && dtp->cbSize != sizeof(DRAWTEXTPARAMS))
1082 GetTextMetricsA(hdc, &tm);
1083 if (flags & DT_EXTERNALLEADING)
1084 lh = tm.tmHeight + tm.tmExternalLeading;
1088 if( flags & DT_CALCRECT)
1090 rect->right = rect->left;
1091 if( flags & DT_SINGLELINE)
1092 rect->bottom = rect->top + lh;
1094 rect->bottom = rect->top;
1098 cp = GdiGetCodePage( hdc );
1099 wcount = MultiByteToWideChar( cp, 0, str, count, NULL, 0 );
1102 if (flags & DT_MODIFYSTRING)
1107 wstr = HeapAlloc(GetProcessHeap(), 0, wmax * sizeof(WCHAR));
1110 MultiByteToWideChar( cp, 0, str, count, wstr, wcount );
1111 if (flags & DT_MODIFYSTRING)
1112 for (i=4, p=wstr+wcount; i--; p++) *p=0xFFFE;
1113 /* Initialise the extra characters so that we can see which ones
1114 * change. U+FFFE is guaranteed to be not a unicode character and
1115 * so will not be generated by DrawTextEx itself.
1117 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, dtp );
1118 if (flags & DT_MODIFYSTRING)
1120 /* Unfortunately the returned string may contain multiple \0s
1121 * and so we need to measure it ourselves.
1123 for (i=4, p=wstr+wcount; i-- && *p != 0xFFFE; p++) wcount++;
1124 WideCharToMultiByte( cp, 0, wstr, wcount, str, amax, NULL, NULL );
1126 HeapFree(GetProcessHeap(), 0, wstr);
1131 /***********************************************************************
1132 * DrawTextW (USER32.@)
1134 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
1138 memset (&dtp, 0, sizeof(dtp));
1139 dtp.cbSize = sizeof(dtp);
1140 if (flags & DT_TABSTOP)
1142 dtp.iTabLength = (flags >> 8) & 0xff;
1143 flags &= 0xffff00ff;
1145 return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, &dtp);
1148 /***********************************************************************
1149 * DrawTextA (USER32.@)
1151 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
1155 memset (&dtp, 0, sizeof(dtp));
1156 dtp.cbSize = sizeof(dtp);
1157 if (flags & DT_TABSTOP)
1159 dtp.iTabLength = (flags >> 8) & 0xff;
1160 flags &= 0xffff00ff;
1162 return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, &dtp );
1165 /***********************************************************************
1167 * GrayString functions
1170 /* callback for ASCII gray string proc */
1171 static BOOL CALLBACK gray_string_callbackA( HDC hdc, LPARAM param, INT len )
1173 return TextOutA( hdc, 0, 0, (LPCSTR)param, len );
1176 /* callback for Unicode gray string proc */
1177 static BOOL CALLBACK gray_string_callbackW( HDC hdc, LPARAM param, INT len )
1179 return TextOutW( hdc, 0, 0, (LPCWSTR)param, len );
1182 /***********************************************************************
1185 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
1186 INT x, INT y, INT cx, INT cy )
1188 HBITMAP hbm, hbmsave;
1196 if(!hdc) return FALSE;
1197 if (!(memdc = CreateCompatibleDC(hdc))) return FALSE;
1199 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1200 hbmsave = SelectObject(memdc, hbm);
1201 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
1202 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
1203 SelectObject( memdc, hbsave );
1204 SetTextColor(memdc, RGB(255, 255, 255));
1205 SetBkColor(memdc, RGB(0, 0, 0));
1206 hfsave = SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1208 retval = fn(memdc, lp, slen);
1209 SelectObject(memdc, hfsave);
1212 * Windows doc says that the bitmap isn't grayed when len == -1 and
1213 * the callback function returns FALSE. However, testing this on
1214 * win95 showed otherwise...
1216 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1217 if(retval || len != -1)
1220 hbsave = SelectObject(memdc, SYSCOLOR_55AABrush);
1221 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
1222 SelectObject(memdc, hbsave);
1225 if(hb) hbsave = SelectObject(hdc, hb);
1226 fg = SetTextColor(hdc, RGB(0, 0, 0));
1227 bg = SetBkColor(hdc, RGB(255, 255, 255));
1228 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
1229 SetTextColor(hdc, fg);
1230 SetBkColor(hdc, bg);
1231 if(hb) SelectObject(hdc, hbsave);
1233 SelectObject(memdc, hbmsave);
1240 /***********************************************************************
1241 * GrayStringA (USER32.@)
1243 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1244 LPARAM lParam, INT cch, INT x, INT y,
1247 if (!cch) cch = strlen( (LPCSTR)lParam );
1248 if ((cx == 0 || cy == 0) && cch != -1)
1251 GetTextExtentPoint32A( hdc, (LPCSTR)lParam, cch, &s );
1252 if (cx == 0) cx = s.cx;
1253 if (cy == 0) cy = s.cy;
1255 if (!gsprc) gsprc = gray_string_callbackA;
1256 return TEXT_GrayString( hdc, hbr, gsprc, lParam, cch, x, y, cx, cy );
1260 /***********************************************************************
1261 * GrayStringW (USER32.@)
1263 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1264 LPARAM lParam, INT cch, INT x, INT y,
1267 if (!cch) cch = strlenW( (LPCWSTR)lParam );
1268 if ((cx == 0 || cy == 0) && cch != -1)
1271 GetTextExtentPoint32W( hdc, (LPCWSTR)lParam, cch, &s );
1272 if (cx == 0) cx = s.cx;
1273 if (cy == 0) cy = s.cy;
1275 if (!gsprc) gsprc = gray_string_callbackW;
1276 return TEXT_GrayString( hdc, hbr, gsprc, lParam, cch, x, y, cx, cy );
1280 /***********************************************************************
1281 * TEXT_TabbedTextOut
1283 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1284 * Note: this doesn't work too well for text-alignment modes other
1285 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1287 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCWSTR lpstr,
1288 INT count, INT cTabStops, const INT *lpTabPos, INT nTabOrg,
1296 if (!lpstr || count == 0) return 0;
1303 defWidth = *lpTabPos;
1309 GetTextMetricsW( hdc, &tm );
1310 defWidth = 8 * tm.tmAveCharWidth;
1319 /* chop the string into substrings of 0 or more <tabs>
1320 * possibly followed by 1 or more normal characters */
1321 for (i = 0; i < count; i++)
1322 if (lpstr[i] != '\t') break;
1323 for (j = i; j < count; j++)
1324 if (lpstr[j] == '\t') break;
1325 /* get the extent of the normal character part */
1326 GetTextExtentPointW( hdc, lpstr + i, j - i , &extent );
1327 /* and if there is a <tab>, calculate its position */
1329 /* get x coordinate for the drawing of this string */
1330 for (; cTabStops >= i; lpTabPos++, cTabStops--)
1332 if( nTabOrg + abs( *lpTabPos) > x) {
1333 if( lpTabPos[ i - 1] >= 0) {
1334 /* a left aligned tab */
1335 x0 = nTabOrg + lpTabPos[i-1];
1341 /* if tab pos is negative then text is right-aligned
1342 * to tab stop meaning that the string extends to the
1343 * left, so we must subtract the width of the string */
1344 if (nTabOrg - lpTabPos[ i - 1] - extent.cx > x)
1346 x = nTabOrg - lpTabPos[ i - 1];
1353 /* if we have run out of tab stops and we have a valid default tab
1354 * stop width then round x up to that width */
1355 if ((cTabStops < i) && (defWidth > 0)) {
1356 x0 = nTabOrg + ((x - nTabOrg) / defWidth + i) * defWidth;
1358 } else if ((cTabStops < i) && (defWidth < 0)) {
1359 x = nTabOrg + ((x - nTabOrg + extent.cx) / -defWidth + i)
1370 r.bottom = y + extent.cy;
1371 ExtTextOutW( hdc, x0, y, GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
1372 &r, lpstr + i, j - i, NULL );
1377 return MAKELONG(x - start, extent.cy);
1381 /***********************************************************************
1382 * TabbedTextOutA (USER32.@)
1384 * See TabbedTextOutW.
1386 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
1387 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1390 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpstr, count, NULL, 0 );
1391 LPWSTR strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1392 if (!strW) return 0;
1393 MultiByteToWideChar( CP_ACP, 0, lpstr, count, strW, len );
1394 ret = TabbedTextOutW( hdc, x, y, strW, len, cTabStops, lpTabPos, nTabOrg );
1395 HeapFree( GetProcessHeap(), 0, strW );
1400 /***********************************************************************
1401 * TabbedTextOutW (USER32.@)
1403 * Draws tabbed text aligned using the specified tab stops.
1406 * hdc [I] Handle to device context to draw to.
1407 * x [I] X co-ordinate to start drawing the text at in logical units.
1408 * y [I] Y co-ordinate to start drawing the text at in logical units.
1409 * str [I] Pointer to the characters to draw.
1410 * count [I] Number of WCHARs pointed to by str.
1411 * cTabStops [I] Number of tab stops pointed to by lpTabPos.
1412 * lpTabPos [I] Tab stops in logical units. Should be sorted in ascending order.
1413 * nTabOrg [I] Starting position to expand tabs from in logical units.
1416 * The dimensions of the string drawn. The height is in the high-order word
1417 * and the width is in the low-order word.
1420 * The tabs stops can be negative, in which case the text is right aligned to
1421 * that tab stop and, despite what MSDN says, this is supported on
1425 * MSDN says that the TA_UPDATECP from GetTextAlign causes this function to
1426 * ignore the x and y co-ordinates, but this is unimplemented at the moment.
1428 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
1429 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1431 TRACE("%p %d,%d %s %d\n", hdc, x, y, debugstr_wn(str,count), count );
1432 return TEXT_TabbedTextOut( hdc, x, y, str, count, cTabStops, lpTabPos, nTabOrg, TRUE );
1436 /***********************************************************************
1437 * GetTabbedTextExtentA (USER32.@)
1439 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
1440 INT cTabStops, const INT *lpTabPos )
1443 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpstr, count, NULL, 0 );
1444 LPWSTR strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1445 if (!strW) return 0;
1446 MultiByteToWideChar( CP_ACP, 0, lpstr, count, strW, len );
1447 ret = GetTabbedTextExtentW( hdc, strW, len, cTabStops, lpTabPos );
1448 HeapFree( GetProcessHeap(), 0, strW );
1453 /***********************************************************************
1454 * GetTabbedTextExtentW (USER32.@)
1456 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
1457 INT cTabStops, const INT *lpTabPos )
1459 TRACE("%p %s %d\n", hdc, debugstr_wn(lpstr,count), count );
1460 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops, lpTabPos, 0, FALSE );