forked from reactos/reactos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.c
1387 lines (1289 loc) · 46.2 KB
/
text.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ReactOS kernel
* Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* PROJECT: ReactOS user32.dll
* FILE: win32ss/user/rtl/text.c
* PURPOSE: Draw Text
* PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
* UPDATE HISTORY:
* 09-05-2001 CSH Created
*/
/* INCLUDES ******************************************************************/
#ifdef _WIN32K_
#include <win32k.h>
DBG_DEFAULT_CHANNEL(UserMenu);
#else
#include <user32.h>
#include <wine/debug.h>
WINE_DEFAULT_DEBUG_CHANNEL(text);
#endif
/* FUNCTIONS *****************************************************************/
#ifndef NDEBUG
#ifdef assert
#undef assert
#endif
#define assert(e) ((e) ? (void)0 : _font_assert(#e, __FILE__, __LINE__))
#else
#include <assert.h>
#endif
void _font_assert(const char *msg, const char *file, int line)
{
/* Assertion failed at foo.c line 45: x<y */
DbgPrint("Assertion failed at %s line %d: %s\n", file, line, msg);
#ifdef _WIN32K_
ASSERT(FALSE);
#else
ExitProcess(3);
for(;;); /* eliminate warning by mingw */
#endif
}
/*********************************************************************
*
* DrawText functions
*
* Copied from Wine.
* Copyright 1993, 1994 Alexandre Julliard
* Copyright 2002 Bill Medland
*
* Design issues
* How many buffers to use
* While processing in DrawText there are potentially three different forms
* of the text that need to be held. How are they best held?
* 1. The original text is needed, of course, to see what to display.
* 2. The text that will be returned to the user if the DT_MODIFYSTRING is
* in effect.
* 3. The buffered text that is about to be displayed e.g. the current line.
* Typically this will exclude the ampersands used for prefixing etc.
*
* Complications.
* a. If the buffered text to be displayed includes the ampersands then
* we will need special measurement and draw functions that will ignore
* the ampersands (e.g. by copying to a buffer without the prefix and
* then using the normal forms). This may involve less space but may
* require more processing. e.g. since a line containing tabs may
* contain several underlined characters either we need to carry around
* a list of prefix locations or we may need to locate them several
* times.
* b. If we actually directly modify the "original text" as we go then we
* will need some special "caching" to handle the fact that when we
* ellipsify the text the ellipsis may modify the next line of text,
* which we have not yet processed. (e.g. ellipsification of a W at the
* end of a line will overwrite the W, the \n and the first character of
* the next line, and a \0 will overwrite the second. Try it!!)
*
* Option 1. Three separate storages. (To be implemented)
* If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
* the edited string in some form, either as the string itself or as some
* sort of "edit list" to be applied just before returning.
* Use a buffer that holds the ellipsified current line sans ampersands
* and accept the need occasionally to recalculate the prefixes (if
* DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
*/
#define TAB 9
#define LF 10
#define CR 13
#define SPACE 32
#define PREFIX 38
#define ALPHA_PREFIX 30 /* Win16: Alphabet prefix */
#define KANA_PREFIX 31 /* Win16: Katakana prefix */
#define FORWARD_SLASH '/'
#define BACK_SLASH '\\'
static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
typedef struct tag_ellipsis_data
{
int before;
int len;
int under;
int after;
} ellipsis_data;
/*********************************************************************
* TEXT_Ellipsify (static)
*
* Add an ellipsis to the end of the given string whilst ensuring it fits.
*
* If the ellipsis alone doesn't fit then it will be returned anyway.
*
* See Also TEXT_PathEllipsify
*
* Arguments
* hdc [in] The handle to the DC that defines the font.
* str [in/out] The string that needs to be modified.
* max_str [in] The dimension of str (number of WCHAR).
* len_str [in/out] The number of characters in str
* width [in] The maximum width permitted (in logical coordinates)
* size [out] The dimensions of the text
* modstr [out] The modified form of the string, to be returned to the
* calling program. It is assumed that the caller has
* made sufficient space available so we don't need to
* know the size of the space. This pointer may be NULL if
* the modified string is not required.
* len_before [out] The number of characters before the ellipsis.
* len_ellip [out] The number of characters in the ellipsis.
*
* See for example Microsoft article Q249678.
*
* For now we will simply use three dots rather than worrying about whether
* the font contains an explicit ellipsis character.
*/
static void TEXT_Ellipsify (HDC hdc, WCHAR *str, unsigned int max_len,
unsigned int *len_str, int width, SIZE *size,
WCHAR *modstr,
int *len_before, int *len_ellip)
{
unsigned int len_ellipsis;
unsigned int lo, mid, hi;
len_ellipsis = strlenW (ELLIPSISW);
if (len_ellipsis > max_len) len_ellipsis = max_len;
if (*len_str > max_len - len_ellipsis)
*len_str = max_len - len_ellipsis;
/* First do a quick binary search to get an upper bound for *len_str. */
if (*len_str > 0 &&
#ifdef _WIN32K_
GreGetTextExtentExW(hdc, str, *len_str, width, NULL, NULL, size, 0) &&
#else
GetTextExtentExPointW(hdc, str, *len_str, width, NULL, NULL, size) &&
#endif
size->cx > width)
{
for (lo = 0, hi = *len_str; lo < hi; )
{
mid = (lo + hi) / 2;
#ifdef _WIN32K_
if (!GreGetTextExtentExW(hdc, str, mid, width, NULL, NULL, size, 0))
#else
if (!GetTextExtentExPointW(hdc, str, mid, width, NULL, NULL, size))
#endif
break;
if (size->cx > width)
hi = mid;
else
lo = mid + 1;
}
*len_str = hi;
}
/* Now this should take only a couple iterations at most. */
for ( ; ; )
{
memcpy(str + *len_str, ELLIPSISW, len_ellipsis*sizeof(WCHAR));
#ifdef _WIN32K_
if (!GreGetTextExtentExW (hdc, str, *len_str + len_ellipsis, width,
NULL, NULL, size, 0)) break;
#else
if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
NULL, NULL, size)) break;
#endif
if (!*len_str || size->cx <= width) break;
(*len_str)--;
}
*len_ellip = len_ellipsis;
*len_before = *len_str;
*len_str += len_ellipsis;
if (modstr)
{
memcpy (modstr, str, *len_str * sizeof(WCHAR));
modstr[*len_str] = '\0';
}
}
/*********************************************************************
* TEXT_PathEllipsify (static)
*
* Add an ellipsis to the provided string in order to make it fit within
* the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
* flag.
*
* See Also TEXT_Ellipsify
*
* Arguments
* hdc [in] The handle to the DC that defines the font.
* str [in/out] The string that needs to be modified
* max_str [in] The dimension of str (number of WCHAR).
* len_str [in/out] The number of characters in str
* width [in] The maximum width permitted (in logical coordinates)
* size [out] The dimensions of the text
* modstr [out] The modified form of the string, to be returned to the
* calling program. It is assumed that the caller has
* made sufficient space available so we don't need to
* know the size of the space. This pointer may be NULL if
* the modified string is not required.
* pellip [out] The ellipsification results
*
* For now we will simply use three dots rather than worrying about whether
* the font contains an explicit ellipsis character.
*
* The following applies, I think to Win95. We will need to extend it for
* Win98 which can have both path and end ellipsis at the same time (e.g.
* C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
*
* The resulting string consists of as much as possible of the following:
* 1. The ellipsis itself
* 2. The last \ or / of the string (if any)
* 3. Everything after the last \ or / of the string (if any) or the whole
* string if there is no / or \. I believe that under Win95 this would
* include everything even though some might be clipped off the end whereas
* under Win98 that might be ellipsified too.
* Yet to be investigated is whether this would include wordbreaking if the
* filename is more than 1 word and splitting if DT_EDITCONTROL was in
* effect. (If DT_EDITCONTROL is in effect then on occasions text will be
* broken within words).
* 4. All the stuff before the / or \, which is placed before the ellipsis.
*/
static void TEXT_PathEllipsify (HDC hdc, WCHAR *str, unsigned int max_len,
unsigned int *len_str, int width, SIZE *size,
WCHAR *modstr, ellipsis_data *pellip)
{
int len_ellipsis;
int len_trailing;
int len_under;
WCHAR *lastBkSlash, *lastFwdSlash, *lastSlash;
len_ellipsis = strlenW (ELLIPSISW);
if (!max_len) return;
if (len_ellipsis >= max_len) len_ellipsis = max_len - 1;
if (*len_str + len_ellipsis >= max_len)
*len_str = max_len - len_ellipsis-1;
/* Hopefully this will never happen, otherwise it would probably lose
* the wrong character
*/
str[*len_str] = '\0'; /* to simplify things */
#ifdef _WIN32K_
lastBkSlash = wcsrchr (str, BACK_SLASH);
lastFwdSlash = wcsrchr (str, FORWARD_SLASH);
#else
lastBkSlash = strrchrW (str, BACK_SLASH);
lastFwdSlash = strrchrW (str, FORWARD_SLASH);
#endif
lastSlash = lastBkSlash > lastFwdSlash ? lastBkSlash : lastFwdSlash;
if (!lastSlash) lastSlash = str;
len_trailing = *len_str - (lastSlash - str);
/* overlap-safe movement to the right */
memmove (lastSlash+len_ellipsis, lastSlash, len_trailing * sizeof(WCHAR));
memcpy (lastSlash, ELLIPSISW, len_ellipsis*sizeof(WCHAR));
len_trailing += len_ellipsis;
/* From this point on lastSlash actually points to the ellipsis in front
* of the last slash and len_trailing includes the ellipsis
*/
len_under = 0;
for ( ; ; )
{
#ifdef _WIN32K_
if (!GreGetTextExtentExW (hdc, str, *len_str + len_ellipsis, width,
NULL, NULL, size, 0)) break;
#else
if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
NULL, NULL, size)) break;
#endif
if (lastSlash == str || size->cx <= width) break;
/* overlap-safe movement to the left */
memmove (lastSlash-1, lastSlash, len_trailing * sizeof(WCHAR));
lastSlash--;
len_under++;
assert (*len_str);
(*len_str)--;
}
pellip->before = lastSlash-str;
pellip->len = len_ellipsis;
pellip->under = len_under;
pellip->after = len_trailing - len_ellipsis;
*len_str += len_ellipsis;
if (modstr)
{
memcpy(modstr, str, *len_str * sizeof(WCHAR));
modstr[*len_str] = '\0';
}
}
/* Check the character is Chinese, Japanese, Korean and/or Thai */
inline BOOL IsCJKT(WCHAR wch)
{
if (0x0E00 <= wch && wch <= 0x0E7F)
return TRUE; /* Thai */
if (0x3000 <= wch && wch <= 0x9FFF)
return TRUE; /* CJK */
if (0xAC00 <= wch && wch <= 0xD7FF)
return TRUE; /* Korean */
if (0xFF00 <= wch && wch <= 0xFFEF)
return TRUE; /* CJK */
return FALSE;
}
/* See http://en.wikipedia.org/wiki/Kinsoku_shori */
static const WCHAR KinsokuClassA[] =
{
0x2010, 0x2013, 0x2019, 0x201D, 0x203C, 0x2047, 0x2048, 0x2049, 0x3001,
0x3002, 0x3005, 0x3009, 0x300B, 0x300D, 0x300F, 0x3011, 0x3015, 0x3017,
0x3019, 0x301C, 0x301F, 0x303B, 0x3041, 0x3043, 0x3045, 0x3047, 0x3049,
0x3063, 0x3083, 0x3085, 0x3087, 0x308E, 0x3095, 0x3096, 0x30A0, 0x30A1,
0x30A3, 0x30A5, 0x30A7, 0x30A9, 0x30C3, 0x30E3, 0x30E5, 0x30E7, 0x30EE,
0x30F5, 0x30F6, 0x30FB, 0x30FC, 0x30FD, 0x30FE, 0x31F0, 0x31F1, 0x31F2,
0x31F3, 0x31F4, 0x31F5, 0x31F6, 0x31F7, 0x31F8, 0x31F9, 0x31FA, 0x31FB,
0x31FC, 0x31FD, 0x31FE, 0x31FF, 0xFF01, 0xFF09, 0xFF0C, 0xFF0E, 0xFF1A,
0xFF1B, 0xFF1F, 0xFF3D, 0xFF5D, 0xFF60, 0
};
/*********************************************************************
* TEXT_WordBreak (static)
*
* Perform wordbreak processing on the given string
*
* Assumes that DT_WORDBREAK has been specified and not all the characters
* fit. Note that this function should even be called when the first character
* that doesn't fit is known to be a space or tab, so that it can swallow them.
*
* Note that the Windows processing has some strange properties.
* 1. If the text is left-justified and there is room for some of the spaces
* that follow the last word on the line then those that fit are included on
* the line.
* 2. If the text is centred or right-justified and there is room for some of
* the spaces that follow the last word on the line then all but one of those
* that fit are included on the line.
* 3. (Reasonable behaviour) If the word breaking causes a space to be the first
* character of a new line it will be skipped.
*
* Arguments
* hdc [in] The handle to the DC that defines the font.
* str [in/out] The string that needs to be broken.
* max_str [in] The dimension of str (number of WCHAR).
* len_str [in/out] The number of characters in str
* width [in] The maximum width permitted
* format [in] The format flags in effect
* chars_fit [in] The maximum number of characters of str that are already
* known to fit; chars_fit+1 is known not to fit.
* chars_used [out] The number of characters of str that have been "used" and
* do not need to be included in later text. For example this will
* include any spaces that have been discarded from the start of
* the next line.
* size [out] The size of the returned text in logical coordinates
*
* Pedantic assumption - Assumes that the text length is monotonically
* increasing with number of characters (i.e. no weird kernings)
*
* Algorithm
*
* Work back from the last character that did fit to either a space or the last
* character of a word, whichever is met first.
* If there was one or the first character didn't fit then
* If the text is centred or right justified and that one character was a
* space then break the line before that character
* Otherwise break the line after that character
* and if the next character is a space then discard it.
* Suppose there was none (and the first character did fit).
* If Break Within Word is permitted
* break the word after the last character that fits (there must be
* at least one; none is caught earlier).
* Otherwise
* discard any trailing space.
* include the whole word; it may be ellipsified later
*
* Break Within Word is permitted under a set of circumstances that are not
* totally clear yet. Currently our best guess is:
* If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
* DT_PATH_ELLIPSIS is
*/
static void TEXT_WordBreak (HDC hdc, WCHAR *str, unsigned int max_str,
unsigned int *len_str,
int width, int format, unsigned int chars_fit,
unsigned int *chars_used, SIZE *size)
{
WCHAR *p;
int word_fits;
assert (format & DT_WORDBREAK);
assert (chars_fit < *len_str);
/* Work back from the last character that did fit to either a space or the
* last character of a word, whichever is met first.
*/
p = str + chars_fit; /* The character that doesn't fit */
word_fits = TRUE;
if (!chars_fit)
word_fits = FALSE;
else if (*p == SPACE) /* chars_fit < *len_str so this is valid */
p--; /* the word just fitted */
else
{
while (p > str && *(--p) != SPACE && (!IsCJKT(p[1]) ||
p[1] == L'\0' || wcschr(KinsokuClassA, p[1]) != NULL))
;
word_fits = (p != str || *p == SPACE || IsCJKT(p[1]));
}
/* If there was one. */
if (word_fits)
{
int next_is_space;
/* break the line before/after that character */
if (!(format & (DT_RIGHT | DT_CENTER)) || *p != SPACE)
p++;
next_is_space = (unsigned int) (p - str) < *len_str && *p == SPACE;
*len_str = p - str;
/* and if the next character is a space then discard it. */
*chars_used = *len_str;
if (next_is_space)
(*chars_used)++;
}
/* Suppose there was none. */
else
{
if ((format & (DT_EDITCONTROL | DT_WORD_ELLIPSIS | DT_PATH_ELLIPSIS)) ==
DT_EDITCONTROL)
{
/* break the word after the last character that fits (there must be
* at least one). */
if (!chars_fit)
++chars_fit;
*len_str = chars_fit;
*chars_used = chars_fit;
/* FIXME - possible error. Since the next character is now removed
* this could make the text longer so that it no longer fits, and
* so we need a loop to test and shrink.
*/
}
/* Otherwise */
else
{
/* discard any trailing space. */
const WCHAR *e = str + *len_str;
p = str + chars_fit;
while (p < e && *p != SPACE)
p++;
*chars_used = p - str;
if (p < e) /* i.e. loop failed because *p == SPACE */
(*chars_used)++;
/* include the whole word; it may be ellipsified later */
*len_str = p - str;
/* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
* so that it will be too long
*/
}
}
/* Remeasure the string */
#ifdef _WIN32K_
GreGetTextExtentExW (hdc, str, *len_str, 0, NULL, NULL, size, 0);
#else
GetTextExtentExPointW (hdc, str, *len_str, 0, NULL, NULL, size);
#endif
}
/*********************************************************************
* TEXT_SkipChars
*
* Skip over the given number of characters, bearing in mind prefix
* substitution and the fact that a character may take more than one
* WCHAR (Unicode surrogates are two words long) (and there may have been
* a trailing &)
*
* Parameters
* new_count [out] The updated count
* new_str [out] The updated pointer
* start_count [in] The count of remaining characters corresponding to the
* start of the string
* start_str [in] The starting point of the string
* max [in] The number of characters actually in this segment of the
* string (the & counts)
* n [in] The number of characters to skip (if prefix then
* &c counts as one)
* prefix [in] Apply prefix substitution
*
* Return Values
* none
*
* Remarks
* There must be at least n characters in the string
* We need max because the "line" may have ended with a & followed by a tab
* or newline etc. which we don't want to swallow
*/
static void TEXT_SkipChars (int *new_count, const WCHAR **new_str,
int start_count, const WCHAR *start_str,
int max, int n, int prefix)
{
/* This is specific to wide characters, MSDN doesn't say anything much
* about Unicode surrogates yet and it isn't clear if _wcsinc will
* correctly handle them so we'll just do this the easy way for now
*/
if (prefix)
{
const WCHAR *str_on_entry = start_str;
assert (max >= n);
max -= n;
while (n--)
{
if ((*start_str == PREFIX || *start_str == ALPHA_PREFIX) && max--)
start_str++;
start_str++;
}
start_count -= (start_str - str_on_entry);
}
else
{
start_str += n;
start_count -= n;
}
*new_str = start_str;
*new_count = start_count;
}
/*********************************************************************
* TEXT_Reprefix
*
* Reanalyse the text to find the prefixed character. This is called when
* wordbreaking or ellipsification has shortened the string such that the
* previously noted prefixed character is no longer visible.
*
* Parameters
* str [in] The original string segment (including all characters)
* ns [in] The number of characters in str (including prefixes)
* pe [in] The ellipsification data
*
* Return Values
* The prefix offset within the new string segment (the one that contains the
* ellipses and does not contain the prefix characters) (-1 if none)
*/
static int TEXT_Reprefix (const WCHAR *str, unsigned int ns,
const ellipsis_data *pe)
{
int result = -1;
unsigned int i;
unsigned int n = pe->before + pe->under + pe->after;
assert (n <= ns);
for (i = 0; i < n; i++, str++)
{
if (i == (unsigned int) pe->before)
{
/* Reached the path ellipsis; jump over it */
if (ns < (unsigned int) pe->under) break;
str += pe->under;
ns -= pe->under;
i += pe->under;
if (!pe->after) break; /* Nothing after the path ellipsis */
}
if (!ns) break;
ns--;
if (*str++ == PREFIX || *str == ALPHA_PREFIX)
{
str++;
if (!ns) break;
if (*str != PREFIX)
result = (i < (unsigned int) pe->before || pe->under == 0) ? i : i - pe->under + pe->len;
/* pe->len may be non-zero while pe_under is zero */
ns--;
}
}
return result;
}
/*********************************************************************
* Returns true if and only if the remainder of the line is a single
* newline representation or nothing
*/
static int remainder_is_none_or_newline (int num_chars, const WCHAR *str)
{
if (!num_chars) return TRUE;
if (*str != LF && *str != CR) return FALSE;
if (!--num_chars) return TRUE;
if (*str == *(str+1)) return FALSE;
str++;
if (*str != CR && *str != LF) return FALSE;
if (--num_chars) return FALSE;
return TRUE;
}
/*********************************************************************
* Return next line of text from a string.
*
* hdc - handle to DC.
* str - string to parse into lines.
* count - length of str.
* dest - destination in which to return line.
* len - dest buffer size in chars on input, copied length into dest on output.
* width - maximum width of line in pixels.
* format - format type passed to DrawText.
* retsize - returned size of the line in pixels.
* last_line - TRUE if is the last line that will be processed
* p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
* the return string is built.
* tabwidth - The width of a tab in logical coordinates
* pprefix_offset - Here is where we return the offset within dest of the first
* prefixed (underlined) character. -1 is returned if there
* are none. Note that there may be more; the calling code
* will need to use TEXT_Reprefix to find any later ones.
* pellip - Here is where we return the information about any ellipsification
* that was carried out. Note that if tabs are being expanded then
* this data will correspond to the last text segment actually
* returned in dest; by definition there would not have been any
* ellipsification in earlier text segments of the line.
*
* Returns pointer to next char in str after end of the line
* or NULL if end of str reached.
*/
static const WCHAR *TEXT_NextLineW( HDC hdc, const WCHAR *str, int *count,
WCHAR *dest, int *len, int width, DWORD format,
SIZE *retsize, int last_line, WCHAR **p_retstr,
int tabwidth, int *pprefix_offset,
ellipsis_data *pellip)
{
int i = 0, j = 0;
int plen = 0;
SIZE size = {0, 0};
int maxl = *len;
int seg_i, seg_count, seg_j;
int max_seg_width;
int num_fit;
int word_broken;
int line_fits;
unsigned int j_in_seg;
int ellipsified;
*pprefix_offset = -1;
/* For each text segment in the line */
retsize->cy = 0;
while (*count)
{
/* Skip any leading tabs */
if (str[i] == TAB && (format & DT_EXPANDTABS))
{
plen = ((plen/tabwidth)+1)*tabwidth;
(*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
while (*count && str[i] == TAB)
{
plen += tabwidth;
(*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
}
}
/* Now copy as far as the next tab or cr/lf or eos */
seg_i = i;
seg_count = *count;
seg_j = j;
while (*count &&
(str[i] != TAB || !(format & DT_EXPANDTABS)) &&
((str[i] != CR && str[i] != LF) || (format & DT_SINGLELINE)))
{
if ((format & DT_NOPREFIX) || *count <= 1)
{
(*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
continue;
}
if (str[i] == PREFIX || str[i] == ALPHA_PREFIX) {
(*count)--, i++; /* Throw away the prefix itself */
if (str[i] == PREFIX)
{
/* Swallow it before we see it again */
(*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
}
else if (*pprefix_offset == -1 || *pprefix_offset >= seg_j)
{
*pprefix_offset = j;
}
/* else the previous prefix was in an earlier segment of the
* line; we will leave it to the drawing code to catch this
* one.
*/
}
else if (str[i] == KANA_PREFIX)
{
/* Throw away katakana access keys */
(*count)--, i++; /* skip the prefix */
(*count)--, i++; /* skip the letter */
}
else
{
(*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
}
}
/* Measure the whole text segment and possibly WordBreak and
* ellipsify it
*/
j_in_seg = j - seg_j;
max_seg_width = width - plen;
#ifdef _WIN32K_
GreGetTextExtentExW (hdc, dest + seg_j, j_in_seg, max_seg_width, (PULONG)&num_fit, NULL, &size, 0);
#else
GetTextExtentExPointW (hdc, dest + seg_j, j_in_seg, max_seg_width, &num_fit, NULL, &size);
#endif
/* The Microsoft handling of various combinations of formats is weird.
* The following may very easily be incorrect if several formats are
* combined, and may differ between versions (to say nothing of the
* several bugs in the Microsoft versions).
*/
word_broken = 0;
line_fits = (num_fit >= j_in_seg);
if (!line_fits && (format & DT_WORDBREAK))
{
const WCHAR *s;
unsigned int chars_used;
TEXT_WordBreak (hdc, dest+seg_j, maxl-seg_j, &j_in_seg,
max_seg_width, format, num_fit, &chars_used, &size);
line_fits = (size.cx <= max_seg_width);
/* and correct the counts */
TEXT_SkipChars (count, &s, seg_count, str+seg_i, i-seg_i,
chars_used, !(format & DT_NOPREFIX));
i = s - str;
word_broken = 1;
}
pellip->before = j_in_seg;
pellip->under = 0;
pellip->after = 0;
pellip->len = 0;
ellipsified = 0;
if (!line_fits && (format & DT_PATH_ELLIPSIS))
{
TEXT_PathEllipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
max_seg_width, &size, *p_retstr, pellip);
line_fits = (size.cx <= max_seg_width);
ellipsified = 1;
}
/* NB we may end up ellipsifying a word-broken or path_ellipsified
* string */
if ((!line_fits && (format & DT_WORD_ELLIPSIS)) ||
((format & DT_END_ELLIPSIS) &&
((last_line && *count) ||
(remainder_is_none_or_newline (*count, &str[i]) && !line_fits))))
{
int before, len_ellipsis;
TEXT_Ellipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
max_seg_width, &size, *p_retstr, &before, &len_ellipsis);
if (before > pellip->before)
{
/* We must have done a path ellipsis too */
pellip->after = before - pellip->before - pellip->len;
/* Leave the len as the length of the first ellipsis */
}
else
{
/* If we are here after a path ellipsification it must be
* because even the ellipsis itself didn't fit.
*/
assert (pellip->under == 0 && pellip->after == 0);
pellip->before = before;
pellip->len = len_ellipsis;
/* pellip->after remains as zero as does
* pellip->under
*/
}
line_fits = (size.cx <= max_seg_width);
ellipsified = 1;
}
/* As an optimisation if we have ellipsified and we are expanding
* tabs and we haven't reached the end of the line we can skip to it
* now rather than going around the loop again.
*/
if ((format & DT_EXPANDTABS) && ellipsified)
{
if (format & DT_SINGLELINE)
*count = 0;
else
{
while ((*count) && str[i] != CR && str[i] != LF)
{
(*count)--, i++;
}
}
}
j = seg_j + j_in_seg;
if (*pprefix_offset >= seg_j + pellip->before)
{
*pprefix_offset = TEXT_Reprefix (str + seg_i, i - seg_i, pellip);
if (*pprefix_offset != -1)
*pprefix_offset += seg_j;
}
plen += size.cx;
if (size.cy > retsize->cy)
retsize->cy = size.cy;
if (word_broken)
break;
else if (!*count)
break;
else if (str[i] == CR || str[i] == LF)
{
(*count)--, i++;
if (*count && (str[i] == CR || str[i] == LF) && str[i] != str[i-1])
{
(*count)--, i++;
}
break;
}
/* else it was a Tab and we go around again */
}
retsize->cx = plen;
*len = j;
if (*count)
return (&str[i]);
else
return NULL;
}
/***********************************************************************
* TEXT_DrawUnderscore
*
* Draw the underline under the prefixed character
*
* Parameters
* hdc [in] The handle of the DC for drawing
* x [in] The x location of the line segment (logical coordinates)
* y [in] The y location of where the underscore should appear
* (logical coordinates)
* str [in] The text of the line segment
* offset [in] The offset of the underscored character within str
* rect [in] Clipping rectangle (if not NULL)
*/
/* Synced with wine 1.1.32 */
static void TEXT_DrawUnderscore (HDC hdc, int x, int y, const WCHAR *str, int offset, const RECT *rect)
{
int prefix_x;
int prefix_end;
SIZE size;
HPEN hpen;
HPEN oldPen;
#ifdef _WIN32K_
GreGetTextExtentW (hdc, str, offset, &size, 0);
#else
GetTextExtentPointW (hdc, str, offset, &size);
#endif
prefix_x = x + size.cx;
#ifdef _WIN32K_
GreGetTextExtentW (hdc, str, offset+1, &size, 0);
#else
GetTextExtentPointW (hdc, str, offset+1, &size);
#endif
prefix_end = x + size.cx - 1;
/* The above method may eventually be slightly wrong due to kerning etc. */
/* Check for clipping */
if (rect)
{
if (prefix_x > rect->right || prefix_end < rect->left ||
y < rect->top || y > rect->bottom)
return; /* Completely outside */
/* Partially outside */
if (prefix_x < rect->left ) prefix_x = rect->left;
if (prefix_end > rect->right) prefix_end = rect->right;
}
#ifdef _WIN32K_
hpen = NtGdiCreatePen (PS_SOLID, 1, GreGetTextColor (hdc), NULL);
oldPen = NtGdiSelectPen (hdc, hpen);
GreMoveTo (hdc, prefix_x, y, NULL);
NtGdiLineTo (hdc, prefix_end, y);
NtGdiSelectPen (hdc, oldPen);
GreDeleteObject (hpen);
#else
hpen = CreatePen (PS_SOLID, 1, GetTextColor (hdc));
oldPen = SelectObject (hdc, hpen);
MoveToEx (hdc, prefix_x, y, NULL);
LineTo (hdc, prefix_end, y);
SelectObject (hdc, oldPen);
DeleteObject (hpen);
#endif
}
#ifdef _WIN32K_
BOOL UserExtTextOutW(HDC hdc,
INT x,
INT y,
UINT flags,
PRECTL lprc,
LPCWSTR lpString,
UINT count)
{
PVOID ResultPointer;
ULONG ResultLength;
ULONG ArgumentLength;
ULONG_PTR pStringBuffer = 0;
NTSTATUS Status;
PLPK_CALLBACK_ARGUMENTS Argument;
BOOL bResult;
ArgumentLength = sizeof(LPK_CALLBACK_ARGUMENTS);
pStringBuffer = ArgumentLength;
ArgumentLength += sizeof(WCHAR) * (count + 2);
Argument = IntCbAllocateMemory(ArgumentLength);
if (!Argument)
{
goto fallback;
}
/* Initialize struct members */
Argument->hdc = hdc;
Argument->x = x;
Argument->y = y;
Argument->flags = flags;
Argument->count = count;
if (lprc)
{
Argument->rect = *lprc;
Argument->bRect = TRUE;
}
else
{
RtlZeroMemory(&Argument->rect, sizeof(RECT));
Argument->bRect = FALSE;
}
/* Align lpString
mimicks code from co_IntClientLoadLibrary */
Argument->lpString = (LPWSTR)pStringBuffer;
pStringBuffer += (ULONG_PTR)Argument;
RtlStringCchCopyNW((LPWSTR)pStringBuffer, count + 1, lpString, count);
UserLeaveCo();
Status = KeUserModeCallback(USER32_CALLBACK_LPK,
Argument,
ArgumentLength,
&ResultPointer,