3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: font.c,v 1.7 2003/07/10 21:04:31 chorns Exp $
21 * PROJECT: ReactOS user32.dll
22 * FILE: lib/user32/windows/input.c
24 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
26 * 09-05-2001 CSH Created
29 /* INCLUDES ******************************************************************/
36 /* FUNCTIONS *****************************************************************/
48 CONST LPINT lpnTabStopPositions
)
65 CONST LPINT lpnTabStopPositions
)
71 /*********************************************************************
76 * Copyright 1993, 1994 Alexandre Julliard
77 * Copyright 2002 Bill Medland
80 * How many buffers to use
81 * While processing in DrawText there are potentially three different forms
82 * of the text that need to be held. How are they best held?
83 * 1. The original text is needed, of course, to see what to display.
84 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
86 * 3. The buffered text that is about to be displayed e.g. the current line.
87 * Typically this will exclude the ampersands used for prefixing etc.
90 * a. If the buffered text to be displayed includes the ampersands then
91 * we will need special measurement and draw functions that will ignore
92 * the ampersands (e.g. by copying to a buffer without the prefix and
93 * then using the normal forms). This may involve less space but may
94 * require more processing. e.g. since a line containing tabs may
95 * contain several underlined characters either we need to carry around
96 * a list of prefix locations or we may need to locate them several
98 * b. If we actually directly modify the "original text" as we go then we
99 * will need some special "caching" to handle the fact that when we
100 * ellipsify the text the ellipsis may modify the next line of text,
101 * which we have not yet processed. (e.g. ellipsification of a W at the
102 * end of a line will overwrite the W, the \n and the first character of
103 * the next line, and a \0 will overwrite the second. Try it!!)
105 * Option 1. Three separate storages. (To be implemented)
106 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
107 * the edited string in some form, either as the string itself or as some
108 * sort of "edit list" to be applied just before returning.
109 * Use a buffer that holds the ellipsified current line sans ampersands
110 * and accept the need occasionally to recalculate the prefixes (if
111 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
120 #define FORWARD_SLASH '/'
121 #define BACK_SLASH '\\'
123 static const WCHAR ELLIPSISW
[] = {'.','.','.', 0};
125 typedef struct tag_ellipsis_data
133 /*********************************************************************
134 * TEXT_Ellipsify (static)
136 * Add an ellipsis to the end of the given string whilst ensuring it fits.
138 * If the ellipsis alone doesn't fit then it will be returned anyway.
140 * See Also TEXT_PathEllipsify
143 * hdc [in] The handle to the DC that defines the font.
144 * str [in/out] The string that needs to be modified.
145 * max_str [in] The dimension of str (number of WCHAR).
146 * len_str [in/out] The number of characters in str
147 * width [in] The maximum width permitted (in logical coordinates)
148 * size [out] The dimensions of the text
149 * modstr [out] The modified form of the string, to be returned to the
150 * calling program. It is assumed that the caller has
151 * made sufficient space available so we don't need to
152 * know the size of the space. This pointer may be NULL if
153 * the modified string is not required.
154 * len_before [out] The number of characters before the ellipsis.
155 * len_ellip [out] The number of characters in the ellipsis.
157 * See for example Microsoft article Q249678.
159 * For now we will simply use three dots rather than worrying about whether
160 * the font contains an explicit ellipsis character.
162 static void TEXT_Ellipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
163 unsigned int *len_str
, int width
, SIZE
*size
,
165 int *len_before
, int *len_ellip
)
167 unsigned int len_ellipsis
;
168 unsigned int lo
, mid
, hi
;
170 len_ellipsis
= wcslen (ELLIPSISW
);
171 if (len_ellipsis
> max_len
) len_ellipsis
= max_len
;
172 if (*len_str
> max_len
- len_ellipsis
)
173 *len_str
= max_len
- len_ellipsis
;
175 /* First do a quick binary search to get an upper bound for *len_str. */
177 GetTextExtentExPointW(hdc
, str
, *len_str
, width
, NULL
, NULL
, size
) &&
180 for (lo
= 0, hi
= *len_str
; lo
< hi
; )
183 if (!GetTextExtentExPointW(hdc
, str
, mid
, width
, NULL
, NULL
, size
))
185 if (size
->cx
> width
)
192 /* Now this should take only a couple iterations at most. */
195 wcsncpy (str
+ *len_str
, ELLIPSISW
, len_ellipsis
);
197 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
198 NULL
, NULL
, size
)) break;
200 if (!*len_str
|| size
->cx
<= width
) break;
204 *len_ellip
= len_ellipsis
;
205 *len_before
= *len_str
;
206 *len_str
+= len_ellipsis
;
210 wcsncpy (modstr
, str
, *len_str
);
211 *(str
+*len_str
) = '\0';
215 /*********************************************************************
216 * TEXT_PathEllipsify (static)
218 * Add an ellipsis to the provided string in order to make it fit within
219 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
222 * See Also TEXT_Ellipsify
225 * hdc [in] The handle to the DC that defines the font.
226 * str [in/out] The string that needs to be modified
227 * max_str [in] The dimension of str (number of WCHAR).
228 * len_str [in/out] The number of characters in str
229 * width [in] The maximum width permitted (in logical coordinates)
230 * size [out] The dimensions of the text
231 * modstr [out] The modified form of the string, to be returned to the
232 * calling program. It is assumed that the caller has
233 * made sufficient space available so we don't need to
234 * know the size of the space. This pointer may be NULL if
235 * the modified string is not required.
236 * pellip [out] The ellipsification results
238 * For now we will simply use three dots rather than worrying about whether
239 * the font contains an explicit ellipsis character.
241 * The following applies, I think to Win95. We will need to extend it for
242 * Win98 which can have both path and end ellipsis at the same time (e.g.
243 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
245 * The resulting string consists of as much as possible of the following:
246 * 1. The ellipsis itself
247 * 2. The last \ or / of the string (if any)
248 * 3. Everything after the last \ or / of the string (if any) or the whole
249 * string if there is no / or \. I believe that under Win95 this would
250 * include everything even though some might be clipped off the end whereas
251 * under Win98 that might be ellipsified too.
252 * Yet to be investigated is whether this would include wordbreaking if the
253 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
254 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
255 * broken within words).
256 * 4. All the stuff before the / or \, which is placed before the ellipsis.
258 static void TEXT_PathEllipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
259 unsigned int *len_str
, int width
, SIZE
*size
,
260 WCHAR
*modstr
, ellipsis_data
*pellip
)
262 unsigned int len_ellipsis
;
265 WCHAR
*lastBkSlash
, *lastFwdSlash
, *lastSlash
;
267 len_ellipsis
= wcslen (ELLIPSISW
);
268 if (!max_len
) return;
269 if (len_ellipsis
>= max_len
) len_ellipsis
= max_len
- 1;
270 if (*len_str
+ len_ellipsis
>= max_len
)
271 *len_str
= max_len
- len_ellipsis
-1;
272 /* Hopefully this will never happen, otherwise it would probably lose
273 * the wrong character
275 str
[*len_str
] = '\0'; /* to simplify things */
277 lastBkSlash
= wcsrchr (str
, BACK_SLASH
);
278 lastFwdSlash
= wcsrchr (str
, FORWARD_SLASH
);
279 lastSlash
= lastBkSlash
> lastFwdSlash
? lastBkSlash
: lastFwdSlash
;
280 if (!lastSlash
) lastSlash
= str
;
281 len_trailing
= *len_str
- (lastSlash
- str
);
283 /* overlap-safe movement to the right */
284 memmove (lastSlash
+len_ellipsis
, lastSlash
, len_trailing
* sizeof(WCHAR
));
285 wcsncpy (lastSlash
, ELLIPSISW
, len_ellipsis
);
286 len_trailing
+= len_ellipsis
;
287 /* From this point on lastSlash actually points to the ellipsis in front
288 * of the last slash and len_trailing includes the ellipsis
294 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
295 NULL
, NULL
, size
)) break;
297 if (lastSlash
== str
|| size
->cx
<= width
) break;
299 /* overlap-safe movement to the left */
300 memmove (lastSlash
-1, lastSlash
, len_trailing
* sizeof(WCHAR
));
307 pellip
->before
= lastSlash
-str
;
308 pellip
->len
= len_ellipsis
;
309 pellip
->under
= len_under
;
310 pellip
->after
= len_trailing
- len_ellipsis
;
311 *len_str
+= len_ellipsis
;
315 wcsncpy (modstr
, str
, *len_str
);
316 *(str
+*len_str
) = '\0';
320 /*********************************************************************
321 * TEXT_WordBreak (static)
323 * Perform wordbreak processing on the given string
325 * Assumes that DT_WORDBREAK has been specified and not all the characters
326 * fit. Note that this function should even be called when the first character
327 * that doesn't fit is known to be a space or tab, so that it can swallow them.
329 * Note that the Windows processing has some strange properties.
330 * 1. If the text is left-justified and there is room for some of the spaces
331 * that follow the last word on the line then those that fit are included on
333 * 2. If the text is centred or right-justified and there is room for some of
334 * the spaces that follow the last word on the line then all but one of those
335 * that fit are included on the line.
336 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
337 * character of a new line it will be skipped.
340 * hdc [in] The handle to the DC that defines the font.
341 * str [in/out] The string that needs to be broken.
342 * max_str [in] The dimension of str (number of WCHAR).
343 * len_str [in/out] The number of characters in str
344 * width [in] The maximum width permitted
345 * format [in] The format flags in effect
346 * chars_fit [in] The maximum number of characters of str that are already
347 * known to fit; chars_fit+1 is known not to fit.
348 * chars_used [out] The number of characters of str that have been "used" and
349 * do not need to be included in later text. For example this will
350 * include any spaces that have been discarded from the start of
352 * size [out] The size of the returned text in logical coordinates
354 * Pedantic assumption - Assumes that the text length is monotonically
355 * increasing with number of characters (i.e. no weird kernings)
359 * Work back from the last character that did fit to either a space or the last
360 * character of a word, whichever is met first.
361 * If there was one or the first character didn't fit then
362 * If the text is centred or right justified and that one character was a
363 * space then break the line before that character
364 * Otherwise break the line after that character
365 * and if the next character is a space then discard it.
366 * Suppose there was none (and the first character did fit).
367 * If Break Within Word is permitted
368 * break the word after the last character that fits (there must be
369 * at least one; none is caught earlier).
371 * discard any trailing space.
372 * include the whole word; it may be ellipsified later
374 * Break Within Word is permitted under a set of circumstances that are not
375 * totally clear yet. Currently our best guess is:
376 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
377 * DT_PATH_ELLIPSIS is
380 static void TEXT_WordBreak (HDC hdc
, WCHAR
*str
, unsigned int max_str
,
381 unsigned int *len_str
,
382 int width
, int format
, unsigned int chars_fit
,
383 unsigned int *chars_used
, SIZE
*size
)
387 assert (format
& DT_WORDBREAK
);
388 assert (chars_fit
< *len_str
);
390 /* Work back from the last character that did fit to either a space or the
391 * last character of a word, whichever is met first.
393 p
= str
+ chars_fit
; /* The character that doesn't fit */
396 ; /* we pretend that it fits anyway */
397 else if (*p
== SPACE
) /* chars_fit < *len_str so this is valid */
398 p
--; /* the word just fitted */
401 while (p
> str
&& *(--p
) != SPACE
)
403 word_fits
= (p
!= str
|| *p
== SPACE
);
405 /* If there was one or the first character didn't fit then */
409 /* break the line before/after that character */
410 if (!(format
& (DT_RIGHT
| DT_CENTER
)) || *p
!= SPACE
)
412 next_is_space
= (unsigned int) (p
- str
) < *len_str
&& *p
== SPACE
;
414 /* and if the next character is a space then discard it. */
415 *chars_used
= *len_str
;
419 /* Suppose there was none. */
422 if ((format
& (DT_EDITCONTROL
| DT_WORD_ELLIPSIS
| DT_PATH_ELLIPSIS
)) ==
425 /* break the word after the last character that fits (there must be
426 * at least one; none is caught earlier).
428 *len_str
= chars_fit
;
429 *chars_used
= chars_fit
;
431 /* FIXME - possible error. Since the next character is now removed
432 * this could make the text longer so that it no longer fits, and
433 * so we need a loop to test and shrink.
439 /* discard any trailing space. */
440 const WCHAR
*e
= str
+ *len_str
;
442 while (p
< e
&& *p
!= SPACE
)
444 *chars_used
= p
- str
;
445 if (p
< e
) /* i.e. loop failed because *p == SPACE */
448 /* include the whole word; it may be ellipsified later */
450 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
451 * so that it will be too long
455 /* Remeasure the string */
456 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
459 /*********************************************************************
462 * Skip over the given number of characters, bearing in mind prefix
463 * substitution and the fact that a character may take more than one
464 * WCHAR (Unicode surrogates are two words long) (and there may have been
468 * new_count [out] The updated count
469 * new_str [out] The updated pointer
470 * start_count [in] The count of remaining characters corresponding to the
471 * start of the string
472 * start_str [in] The starting point of the string
473 * max [in] The number of characters actually in this segment of the
474 * string (the & counts)
475 * n [in] The number of characters to skip (if prefix then
477 * prefix [in] Apply prefix substitution
483 * There must be at least n characters in the string
484 * We need max because the "line" may have ended with a & followed by a tab
485 * or newline etc. which we don't want to swallow
488 static void TEXT_SkipChars (int *new_count
, const WCHAR
**new_str
,
489 int start_count
, const WCHAR
*start_str
,
490 int max
, int n
, int prefix
)
492 /* This is specific to wide characters, MSDN doesn't say anything much
493 * about Unicode surrogates yet and it isn't clear if _wcsinc will
494 * correctly handle them so we'll just do this the easy way for now
499 const WCHAR
*str_on_entry
= start_str
;
503 if (*start_str
++ == PREFIX
&& max
--)
506 start_count
-= (start_str
- str_on_entry
);
513 *new_str
= start_str
;
514 *new_count
= start_count
;
517 /*********************************************************************
520 * Reanalyse the text to find the prefixed character. This is called when
521 * wordbreaking or ellipsification has shortened the string such that the
522 * previously noted prefixed character is no longer visible.
525 * str [in] The original string segment (including all characters)
526 * ns [in] The number of characters in str (including prefixes)
527 * pe [in] The ellipsification data
530 * The prefix offset within the new string segment (the one that contains the
531 * ellipses and does not contain the prefix characters) (-1 if none)
534 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
535 const ellipsis_data
*pe
)
539 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
543 if (i
== (unsigned int) pe
->before
)
545 /* Reached the path ellipsis; jump over it */
546 if (ns
< (unsigned int) pe
->under
) break;
550 if (!pe
->after
) break; /* Nothing after the path ellipsis */
554 if (*str
++ == PREFIX
)
558 result
= (i
< (unsigned int) pe
->before
|| pe
->under
== 0) ? i
: i
- pe
->under
+ pe
->len
;
559 /* pe->len may be non-zero while pe_under is zero */
569 /*********************************************************************
570 * Returns true if and only if the remainder of the line is a single
571 * newline representation or nothing
574 static int remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
576 if (!num_chars
) return TRUE
;
577 if (*str
!= LF
&& *str
!= CR
) return FALSE
;
578 if (!--num_chars
) return TRUE
;
579 if (*str
== *(str
+1)) return FALSE
;
581 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
582 if (--num_chars
) return FALSE
;
586 /*********************************************************************
587 * Return next line of text from a string.
589 * hdc - handle to DC.
590 * str - string to parse into lines.
591 * count - length of str.
592 * dest - destination in which to return line.
593 * len - dest buffer size in chars on input, copied length into dest on output.
594 * width - maximum width of line in pixels.
595 * format - format type passed to DrawText.
596 * retsize - returned size of the line in pixels.
597 * last_line - TRUE if is the last line that will be processed
598 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
599 * the return string is built.
600 * tabwidth - The width of a tab in logical coordinates
601 * pprefix_offset - Here is where we return the offset within dest of the first
602 * prefixed (underlined) character. -1 is returned if there
603 * are none. Note that there may be more; the calling code
604 * will need to use TEXT_Reprefix to find any later ones.
605 * pellip - Here is where we return the information about any ellipsification
606 * that was carried out. Note that if tabs are being expanded then
607 * this data will correspond to the last text segment actually
608 * returned in dest; by definition there would not have been any
609 * ellipsification in earlier text segments of the line.
611 * Returns pointer to next char in str after end of the line
612 * or NULL if end of str reached.
614 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
615 WCHAR
*dest
, int *len
, int width
, DWORD format
,
616 SIZE
*retsize
, int last_line
, WCHAR
**p_retstr
,
617 int tabwidth
, int *pprefix_offset
,
618 ellipsis_data
*pellip
)
624 int seg_i
, seg_count
, seg_j
;
631 *pprefix_offset
= -1;
633 /* For each text segment in the line */
639 /* Skip any leading tabs */
641 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
643 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
644 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
645 while (*count
&& str
[i
] == TAB
)
648 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
653 /* Now copy as far as the next tab or cr/lf or eos */
660 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
661 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
663 if (str
[i
] == PREFIX
&& !(format
& DT_NOPREFIX
) && *count
> 1)
665 (*count
)--, i
++; /* Throw away the prefix itself */
666 if (str
[i
] == PREFIX
)
668 /* Swallow it before we see it again */
669 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
671 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
675 /* else the previous prefix was in an earlier segment of the
676 * line; we will leave it to the drawing code to catch this
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
))
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
755 line_fits
= (size
.cx
<= max_seg_width
);
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
826 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
)
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 hpen
= CreatePen (PS_SOLID
, 1, GetTextColor (hdc
));
841 oldPen
= SelectObject (hdc
, hpen
);
842 MoveToEx (hdc
, prefix_x
, y
, NULL
);
843 LineTo (hdc
, prefix_end
, y
);
844 SelectObject (hdc
, oldPen
);
848 /***********************************************************************
849 * DrawTextExW (USER32.@)
851 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
852 * is not quite complete, especially with regard to \0. We will assume that
853 * the returned string could have a length of up to i_count+3 and also have
854 * a trailing \0 (which would be 4 more than a not-null-terminated string but
855 * 3 more than a null-terminated string). If this is not so then increase
856 * the allowance in DrawTextExA.
858 #define MAX_STATIC_BUFFER 1024
863 DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
864 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
868 WCHAR
*retstr
, *p_retstr
;
870 static WCHAR line
[MAX_STATIC_BUFFER
];
871 int len
, lh
, count
=i_count
;
873 int lmargin
= 0, rmargin
= 0;
874 int x
= rect
->left
, y
= rect
->top
;
875 int width
= rect
->right
- rect
->left
;
878 int tabwidth
/* to keep gcc happy */ = 0;
883 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str
, count
), count
,
884 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
886 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
887 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
891 if (count
== -1) count
= wcslen(str
);
892 if (count
== 0) return 0;
895 if (flags
& DT_SINGLELINE
)
896 flags
&= ~DT_WORDBREAK
;
898 GetTextMetricsW(hdc
, &tm
);
899 if (flags
& DT_EXTERNALLEADING
)
900 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
906 lmargin
= dtp
->iLeftMargin
* tm
.tmAveCharWidth
;
907 rmargin
= dtp
->iRightMargin
* tm
.tmAveCharWidth
;
908 if (!(flags
& (DT_CENTER
| DT_RIGHT
)))
910 dtp
->uiLengthDrawn
= 0; /* This param RECEIVES number of chars processed */
913 if (flags
& DT_EXPANDTABS
)
915 int tabstop
= ((flags
& DT_TABSTOP
) && dtp
) ? dtp
->iTabLength
: 8;
916 tabwidth
= tm
.tmAveCharWidth
* tabstop
;
919 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
921 if (flags
& DT_MODIFYSTRING
)
923 size_retstr
= (count
+ 4) * sizeof (WCHAR
);
924 retstr
= HeapAlloc(GetProcessHeap(), 0, size_retstr
);
925 if (!retstr
) return 0;
926 memcpy (retstr
, str
, size_retstr
);
937 len
= MAX_STATIC_BUFFER
;
938 last_line
= !(flags
& DT_NOCLIP
) && y
+ ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) > rect
->bottom
;
939 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
, &size
, last_line
, &p_retstr
, tabwidth
, &prefix_offset
, &ellip
);
941 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
943 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
945 if (flags
& DT_SINGLELINE
)
947 if (flags
& DT_VCENTER
) y
= rect
->top
+
948 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
949 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
952 if (!(flags
& DT_CALCRECT
))
954 const WCHAR
*str
= line
;
960 if ((flags
& DT_EXPANDTABS
))
963 p
= str
; while (p
< str
+len
&& *p
!= TAB
) p
++;
965 if (len_seg
!= len
&& !GetTextExtentPointW(hdc
, str
, len_seg
, &size
))
971 if (!ExtTextOutW( hdc
, xseg
, y
,
972 ((flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
) |
973 ((flags
& DT_RTLREADING
) ? ETO_RTLREADING
: 0),
974 rect
, str
, len_seg
, NULL
)) return 0;
975 if (prefix_offset
!= -1 && prefix_offset
< len_seg
)
977 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
);
983 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
985 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
986 if (prefix_offset
!= -1)
988 if (prefix_offset
< len_seg
)
990 /* We have just drawn an underscore; we ought to
991 * figure out where the next one is. I am going
992 * to leave it for now until I have a better model
993 * for the line, which will make reprefixing easier.
994 * This is where ellip would be used.
999 prefix_offset
-= len_seg
;
1004 else if (size
.cx
> max_width
)
1005 max_width
= size
.cx
;
1009 dtp
->uiLengthDrawn
+= len
;
1011 while (strPtr
&& !last_line
);
1013 if (flags
& DT_CALCRECT
)
1015 rect
->right
= rect
->left
+ max_width
;
1018 rect
->right
+= lmargin
+ rmargin
;
1022 memcpy (str
, retstr
, size_retstr
);
1023 HeapFree (GetProcessHeap(), 0, retstr
);
1025 return y
- rect
->top
;
1028 /***********************************************************************
1029 * DrawTextExA (USER32.@)
1031 * If DT_MODIFYSTRING is specified then there must be room for up to
1032 * 4 extra characters. We take great care about just how much modified
1038 DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
1039 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
1050 if (count
== -1) count
= strlen(str
);
1051 if (!count
) return 0;
1052 wcount
= MultiByteToWideChar( CP_ACP
, 0, str
, count
, NULL
, 0 );
1055 if (flags
& DT_MODIFYSTRING
)
1060 wstr
= HeapAlloc(GetProcessHeap(), 0, wmax
* sizeof(WCHAR
));
1063 MultiByteToWideChar( CP_ACP
, 0, str
, count
, wstr
, wcount
);
1064 if (flags
& DT_MODIFYSTRING
)
1065 for (i
=4, p
=wstr
+wcount
; i
--; p
++) *p
=0xFFFE;
1066 /* Initialise the extra characters so that we can see which ones
1067 * change. U+FFFE is guaranteed to be not a unicode character and
1068 * so will not be generated by DrawTextEx itself.
1070 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, NULL
);
1071 if (flags
& DT_MODIFYSTRING
)
1073 /* Unfortunately the returned string may contain multiple \0s
1074 * and so we need to measure it ourselves.
1076 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1077 WideCharToMultiByte( CP_ACP
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1079 HeapFree(GetProcessHeap(), 0, wstr
);
1084 /***********************************************************************
1085 * DrawTextW (USER32.@)
1090 DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1094 memset (&dtp
, 0, sizeof(dtp
));
1095 if (flags
& DT_TABSTOP
)
1097 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1098 flags
&= 0xffff00ff;
1100 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1103 /***********************************************************************
1104 * DrawTextA (USER32.@)
1109 DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1113 memset (&dtp
, 0, sizeof(dtp
));
1114 if (flags
& DT_TABSTOP
)
1116 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1117 flags
&= 0xffff00ff;
1119 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);