General tidy up of the MetaFile driver - make sure that everything
[wine] / objects / text.c
1 /*
2  * text functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  *
6  */
7
8 #include <string.h>
9 #include "winbase.h"
10 #include "winuser.h"
11 #include "dc.h"
12 #include "gdi.h"
13 #include "heap.h"
14 #include "debug.h"
15 #include "cache.h"
16 #include "debugstr.h"
17
18 DEFAULT_DEBUG_CHANNEL(text)
19
20 #define TAB     9
21 #define LF     10
22 #define CR     13
23 #define SPACE  32
24 #define PREFIX 38
25
26 #define SWAP_INT(a,b)  { int t = a; a = b; b = t; }
27
28 static int tabstop = 8;
29 static int tabwidth;
30 static int spacewidth;
31 static int prefix_offset;
32
33 static const char *TEXT_NextLine( HDC16 hdc, const char *str, int *count,
34                                   char *dest, int *len, int width, WORD format)
35 {
36     /* Return next line of text from a string.
37      * 
38      * hdc - handle to DC.
39      * str - string to parse into lines.
40      * count - length of str.
41      * dest - destination in which to return line.
42      * len - length of resultant line in dest in chars.
43      * width - maximum width of line in pixels.
44      * format - format type passed to DrawText.
45      *
46      * Returns pointer to next char in str after end of the line
47      * or NULL if end of str reached.
48      */
49
50     int i = 0, j = 0, k;
51     int plen = 0;
52     int numspaces;
53     SIZE16 size;
54     int lasttab = 0;
55     int wb_i = 0, wb_j = 0, wb_count = 0;
56
57     while (*count)
58     {
59         switch (str[i])
60         {
61         case CR:
62         case LF:
63             if (!(format & DT_SINGLELINE))
64             {
65                 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
66                 {
67                     (*count)--;
68                     i++;
69                 }
70                 i++;
71                 *len = j;
72                 (*count)--;
73                 return (&str[i]);
74             }
75             dest[j++] = str[i++];
76             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
77                 (format & DT_WORDBREAK))
78             {
79                 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
80                     return NULL;
81                 plen += size.cx;
82             }
83             break;
84             
85         case PREFIX:
86             if (!(format & DT_NOPREFIX) && *count > 1)
87                 {
88                 if (str[++i] == PREFIX)
89                     (*count)--;
90                 else {
91                     prefix_offset = j;
92                     break;
93                 }
94             }
95             dest[j++] = str[i++];
96             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
97                 (format & DT_WORDBREAK))
98             {
99                 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
100                     return NULL;
101                 plen += size.cx;
102             }
103             break;
104             
105         case TAB:
106             if (format & DT_EXPANDTABS)
107             {
108                 wb_i = ++i;
109                 wb_j = j;
110                 wb_count = *count;
111
112                 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
113                                                                  &size))
114                     return NULL;
115
116                 numspaces = (tabwidth - size.cx) / spacewidth;
117                 for (k = 0; k < numspaces; k++)
118                     dest[j++] = SPACE;
119                 plen += tabwidth - size.cx;
120                 lasttab = wb_j + numspaces;
121             }
122             else
123             {
124                 dest[j++] = str[i++];
125                 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
126                     (format & DT_WORDBREAK))
127                 {
128                     if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
129                         return NULL;
130                     plen += size.cx;
131                 }
132             }
133             break;
134
135         case SPACE:
136             dest[j++] = str[i++];
137             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
138                 (format & DT_WORDBREAK))
139             {
140                 wb_i = i;
141                 wb_j = j - 1;
142                 wb_count = *count;
143                 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
144                     return NULL;
145                 plen += size.cx;
146             }
147             break;
148
149         default:
150             dest[j++] = str[i++];
151             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
152                 (format & DT_WORDBREAK))
153             {
154                 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
155                     return NULL;
156                 plen += size.cx;
157             }
158         }
159
160         (*count)--;
161         if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
162         {
163             if (plen > width)
164             {
165                 if (format & DT_WORDBREAK)
166                 {
167                     if (wb_j)
168                     {
169                         *len = wb_j;
170                         *count = wb_count - 1;
171                         return (&str[wb_i]);
172                     }
173                 }
174                 else
175                 {
176                     *len = j;
177                     return (&str[i]);
178                 }
179             }
180         }
181     }
182     
183     *len = j;
184     return NULL;
185 }
186
187
188 /***********************************************************************
189  *           DrawText16    (USER.85)
190  */
191 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
192                          LPRECT16 rect, UINT16 flags )
193 {
194     SIZE16 size;
195     const char *strPtr;
196     static char line[1024];
197     int len, lh, count=i_count;
198     int prefix_x = 0;
199     int prefix_end = 0;
200     TEXTMETRIC16 tm;
201     int x = rect->left, y = rect->top;
202     int width = rect->right - rect->left;
203     int max_width = 0;
204
205     TRACE(text,"%s, %d , [(%d,%d),(%d,%d)]\n",
206           debugstr_an (str, count), count,
207           rect->left, rect->top, rect->right, rect->bottom);
208     
209     if (count == -1) count = strlen(str);
210     strPtr = str;
211
212     GetTextMetrics16(hdc, &tm);
213     if (flags & DT_EXTERNALLEADING)
214         lh = tm.tmHeight + tm.tmExternalLeading;
215     else
216         lh = tm.tmHeight;
217
218     if (flags & DT_TABSTOP)
219         tabstop = flags >> 8;
220
221     if (flags & DT_EXPANDTABS)
222     {
223         GetTextExtentPoint16(hdc, " ", 1, &size);
224         spacewidth = size.cx;
225         GetTextExtentPoint16(hdc, "o", 1, &size);
226         tabwidth = size.cx * tabstop;
227     }
228
229     if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
230
231     do
232     {
233         prefix_offset = -1;
234         strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
235
236         if (prefix_offset != -1)
237         {
238             GetTextExtentPoint16(hdc, line, prefix_offset, &size);
239             prefix_x = size.cx;
240             GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
241             prefix_end = size.cx - 1;
242         }
243
244         if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
245         if (flags & DT_CENTER) x = (rect->left + rect->right -
246                                     size.cx) / 2;
247         else if (flags & DT_RIGHT) x = rect->right - size.cx;
248
249         if (flags & DT_SINGLELINE)
250         {
251             if (flags & DT_VCENTER) y = rect->top + 
252                 (rect->bottom - rect->top) / 2 - size.cy / 2;
253             else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
254         }
255         if (!(flags & DT_CALCRECT))
256         {
257             if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
258                               rect, line, len, NULL )) return 0;
259             if (prefix_offset != -1)
260             {
261                 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
262                 HPEN oldPen = SelectObject( hdc, hpen );
263                 MoveTo16(hdc, x + prefix_x, y + tm.tmAscent + 1 );
264                 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
265                 SelectObject( hdc, oldPen );
266                 DeleteObject( hpen );
267             }
268         }
269         else if (size.cx > max_width)
270             max_width = size.cx;
271
272         y += lh;
273         if (strPtr)
274         {
275             if (!(flags & DT_NOCLIP))
276             {
277                 if (y > rect->bottom - lh)
278                     break;
279             }
280         }
281     }
282     while (strPtr);
283     if (flags & DT_CALCRECT)
284     {
285         rect->right = rect->left + max_width;
286         rect->bottom = y;
287     }
288     return y - rect->top;
289 }
290
291
292 /***********************************************************************
293  *           DrawText32A    (USER32.164)
294  */
295 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count,
296                           LPRECT rect, UINT flags )
297 {
298     RECT16 rect16;
299     INT16 ret;
300
301     if (!rect)
302         return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
303     CONV_RECT32TO16( rect, &rect16 );
304     ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
305     CONV_RECT16TO32( &rect16, rect );
306     return ret;
307 }
308
309
310 /***********************************************************************
311  *           DrawText32W    (USER32.167)
312  */
313 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count,
314                           LPRECT rect, UINT flags )
315 {
316     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
317     INT ret = DrawTextA( hdc, p, count, rect, flags );
318     HeapFree( GetProcessHeap(), 0, p );
319     return ret;
320 }
321
322 /***********************************************************************
323  *           DrawTextEx32A    (USER32.165)
324  */
325 INT WINAPI DrawTextExA( HDC hdc, LPCSTR str, INT count,
326                      LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
327 {
328     TRACE(text,"(%d,'%s',%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
329     if(dtp) {
330         FIXME(text,"Ignores params:%d,%d,%d,%d,%d\n",dtp->cbSize,
331                    dtp->iTabLength,dtp->iLeftMargin,dtp->iRightMargin,
332                    dtp->uiLengthDrawn);
333     }
334     return DrawTextA(hdc,str,count,rect,flags);
335 }
336
337 /***********************************************************************
338  *           DrawTextEx32W    (USER32.166)
339  */
340 INT WINAPI DrawTextExW( HDC hdc, LPCWSTR str, INT count,
341                      LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
342 {
343     TRACE(text,"(%d,%p,%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
344     FIXME(text,"ignores extended functionality\n");
345     return DrawTextW(hdc,str,count,rect,flags);
346 }
347
348 /***********************************************************************
349  *           ExtTextOut16    (GDI.351)
350  */
351 BOOL16 WINAPI ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
352                             const RECT16 *lprect, LPCSTR str, UINT16 count,
353                             const INT16 *lpDx )
354 {
355     BOOL        ret;
356     int         i;
357     RECT        rect32;
358     LPINT       lpdx32 = NULL;
359
360     if (lpDx) lpdx32 = (LPINT)HEAP_xalloc( GetProcessHeap(), 0,
361                                              sizeof(INT)*count );
362     if (lprect) CONV_RECT16TO32(lprect,&rect32);
363     if (lpdx32) for (i=count;i--;) lpdx32[i]=lpDx[i];
364     ret = ExtTextOutA(hdc,x,y,flags,lprect?&rect32:NULL,str,count,lpdx32);
365     if (lpdx32) HeapFree( GetProcessHeap(), 0, lpdx32 );
366     return ret;
367
368
369 }
370
371
372 /***********************************************************************
373  *           ExtTextOut32A    (GDI32.98)
374  */
375 BOOL WINAPI ExtTextOutA( HDC hdc, INT x, INT y, UINT flags,
376                              const RECT *lprect, LPCSTR str, UINT count,
377                              const INT *lpDx )
378 {
379     DC * dc = DC_GetDCPtr( hdc );
380     return dc && dc->funcs->pExtTextOut && 
381            dc->funcs->pExtTextOut(dc,x,y,flags,lprect,str,count,lpDx);
382 }
383
384
385 /***********************************************************************
386  *           ExtTextOut32W    (GDI32.99)
387  */
388 BOOL WINAPI ExtTextOutW( HDC hdc, INT x, INT y, UINT flags,
389                              const RECT *lprect, LPCWSTR str, UINT count,
390                              const INT *lpDx )
391 {
392     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
393     INT ret = ExtTextOutA( hdc, x, y, flags, lprect, p, count, lpDx );
394     HeapFree( GetProcessHeap(), 0, p );
395     return ret;
396 }
397
398
399 /***********************************************************************
400  *           TextOut16    (GDI.33)
401  */
402 BOOL16 WINAPI TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
403 {
404     return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
405 }
406
407
408 /***********************************************************************
409  *           TextOut32A    (GDI32.355)
410  */
411 BOOL WINAPI TextOutA( HDC hdc, INT x, INT y, LPCSTR str, INT count )
412 {
413     return ExtTextOutA( hdc, x, y, 0, NULL, str, count, NULL );
414 }
415
416
417 /***********************************************************************
418  *           TextOut32W    (GDI32.356)
419  */
420 BOOL WINAPI TextOutW(HDC hdc, INT x, INT y, LPCWSTR str, INT count)
421 {
422     return ExtTextOutW( hdc, x, y, 0, NULL, str, count, NULL );
423 }
424
425
426 /***********************************************************************
427  *           TEXT_GrayString
428  *
429  * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
430  * heap and we can guarantee that the handles fit in an INT16. We have to
431  * rethink the strategy once the migration to NT handles is complete.
432  * We are going to get a lot of code-duplication once this migration is
433  * completed...
434  * 
435  */
436 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, 
437                               GRAYSTRINGPROC fn, LPARAM lp, INT len,
438                               INT x, INT y, INT cx, INT cy, 
439                               BOOL unicode, BOOL _32bit)
440 {
441     HBITMAP hbm, hbmsave;
442     HBRUSH hbsave;
443     HFONT hfsave;
444     HDC memdc = CreateCompatibleDC(hdc);
445     int slen = len;
446     BOOL retval = TRUE;
447     RECT r;
448     COLORREF fg, bg;
449
450     if(!hdc) return FALSE;
451     
452     if(len == 0)
453     {
454         if(unicode)
455             slen = lstrlenW((LPCWSTR)lp);
456         else if(_32bit)
457             slen = lstrlenA((LPCSTR)lp);
458         else
459             slen = lstrlenA((LPCSTR)PTR_SEG_TO_LIN(lp));
460     }
461
462     if((cx == 0 || cy == 0) && slen != -1)
463     {
464         SIZE s;
465         if(unicode)
466             GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
467         else if(_32bit)
468             GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
469         else
470             GetTextExtentPoint32A(hdc, (LPCSTR)PTR_SEG_TO_LIN(lp), slen, &s);
471         if(cx == 0) cx = s.cx;
472         if(cy == 0) cy = s.cy;
473     }
474
475     r.left = r.top = 0;
476     r.right = cx;
477     r.bottom = cy;
478
479     hbm = CreateBitmap(cx, cy, 1, 1, NULL);
480     hbmsave = (HBITMAP)SelectObject(memdc, hbm);
481     FillRect(memdc, &r, (HBRUSH)GetStockObject(BLACK_BRUSH));
482     SetTextColor(memdc, RGB(255, 255, 255));
483     SetBkColor(memdc, RGB(0, 0, 0));
484     hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
485             
486     if(fn)
487         if(_32bit)
488             retval = fn(memdc, lp, slen);
489         else
490             retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
491     else
492         if(unicode)
493             TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
494         else if(_32bit)
495             TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
496         else
497             TextOutA(memdc, 0, 0, (LPCSTR)PTR_SEG_TO_LIN(lp), slen);
498
499     SelectObject(memdc, hfsave);
500
501 /*
502  * Windows doc says that the bitmap isn't grayed when len == -1 and
503  * the callback function returns FALSE. However, testing this on
504  * win95 showed otherwise...
505 */
506 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
507     if(retval || len != -1)
508 #endif
509     {
510         hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
511         PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
512         SelectObject(memdc, hbsave);
513     }
514
515     if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
516     fg = SetTextColor(hdc, RGB(0, 0, 0));
517     bg = SetBkColor(hdc, RGB(255, 255, 255));
518     BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
519     SetTextColor(hdc, fg);
520     SetBkColor(hdc, bg);
521     if(hb) SelectObject(hdc, hbsave);
522
523     SelectObject(memdc, hbmsave);
524     DeleteObject(hbm);
525     DeleteDC(memdc);
526     return retval;
527 }
528
529
530 /***********************************************************************
531  *           GrayString16   (USER.185)
532  */
533 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
534                             LPARAM lParam, INT16 cch, INT16 x, INT16 y,
535                             INT16 cx, INT16 cy )
536 {
537     return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch, x, y, cx, cy, FALSE, FALSE);
538 }
539
540
541 /***********************************************************************
542  *           GrayString32A   (USER32.315)
543  */
544 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
545                              LPARAM lParam, INT cch, INT x, INT y,
546                              INT cx, INT cy )
547 {
548     return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, FALSE, TRUE);
549 }
550
551
552 /***********************************************************************
553  *           GrayString32W   (USER32.316)
554  */
555 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
556                              LPARAM lParam, INT cch, INT x, INT y,
557                              INT cx, INT cy )
558 {
559     return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, TRUE, TRUE);
560 }
561
562
563 /***********************************************************************
564  *           TEXT_TabbedTextOut
565  *
566  * Helper function for TabbedTextOut() and GetTabbedTextExtent().
567  * Note: this doesn't work too well for text-alignment modes other
568  *       than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
569  */
570 LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
571                          INT count, INT cTabStops, const INT16 *lpTabPos16,
572                          const INT *lpTabPos32, INT nTabOrg,
573                          BOOL fDisplayText )
574 {
575     INT defWidth;
576     DWORD extent = 0;
577     int i, tabPos = x;
578     int start = x;
579
580     if (cTabStops == 1)
581     {
582         defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
583         cTabStops = 0;
584     }
585     else
586     {
587         TEXTMETRIC16 tm;
588         GetTextMetrics16( hdc, &tm );
589         defWidth = 8 * tm.tmAveCharWidth;
590     }
591     
592     while (count > 0)
593     {
594         for (i = 0; i < count; i++)
595             if (lpstr[i] == '\t') break;
596         extent = GetTextExtent16( hdc, lpstr, i );
597         if (lpTabPos32)
598         {
599             while ((cTabStops > 0) &&
600                    (nTabOrg + *lpTabPos32 <= x + LOWORD(extent)))
601             {
602                 lpTabPos32++;
603                 cTabStops--;
604             }
605         }
606         else
607         {
608             while ((cTabStops > 0) &&
609                    (nTabOrg + *lpTabPos16 <= x + LOWORD(extent)))
610             {
611                 lpTabPos16++;
612                 cTabStops--;
613             }
614         }
615         if (i == count)
616             tabPos = x + LOWORD(extent);
617         else if (cTabStops > 0)
618             tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
619         else
620             tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
621         if (fDisplayText)
622         {
623             RECT r;
624             SetRect( &r, x, y, tabPos, y+HIWORD(extent) );
625             ExtTextOutA( hdc, x, y,
626                            GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
627                            &r, lpstr, i, NULL );
628         }
629         x = tabPos;
630         count -= i+1;
631         lpstr += i+1;
632     }
633     return MAKELONG(tabPos - start, HIWORD(extent));
634 }
635
636
637 /***********************************************************************
638  *           TabbedTextOut16    (USER.196)
639  */
640 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
641                              INT16 count, INT16 cTabStops,
642                              const INT16 *lpTabPos, INT16 nTabOrg )
643 {
644     TRACE(text, "%04x %d,%d '%.*s' %d\n",
645                   hdc, x, y, count, lpstr, count );
646     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
647                                lpTabPos, NULL, nTabOrg, TRUE );
648 }
649
650
651 /***********************************************************************
652  *           TabbedTextOut32A    (USER32.542)
653  */
654 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr,
655                               INT count, INT cTabStops,
656                               const INT *lpTabPos, INT nTabOrg )
657 {
658     TRACE(text, "%04x %d,%d '%.*s' %d\n",
659                   hdc, x, y, count, lpstr, count );
660     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
661                                NULL, lpTabPos, nTabOrg, TRUE );
662 }
663
664
665 /***********************************************************************
666  *           TabbedTextOut32W    (USER32.543)
667  */
668 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str,
669                               INT count, INT cTabStops,
670                               const INT *lpTabPos, INT nTabOrg )
671 {
672     LONG ret;
673     LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
674     lstrcpynWtoA( p, str, count + 1 );
675     ret = TabbedTextOutA( hdc, x, y, p, count, cTabStops,
676                             lpTabPos, nTabOrg );
677     HeapFree( GetProcessHeap(), 0, p );
678     return ret;
679 }
680
681
682 /***********************************************************************
683  *           GetTabbedTextExtent16    (USER.197)
684  */
685 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count, 
686                                     INT16 cTabStops, const INT16 *lpTabPos )
687 {
688     TRACE(text, "%04x '%.*s' %d\n",
689                   hdc, count, lpstr, count );
690     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
691                                lpTabPos, NULL, 0, FALSE );
692 }
693
694
695 /***********************************************************************
696  *           GetTabbedTextExtent32A    (USER32.293)
697  */
698 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count, 
699                                      INT cTabStops, const INT *lpTabPos )
700 {
701     TRACE(text, "%04x '%.*s' %d\n",
702                   hdc, count, lpstr, count );
703     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
704                                NULL, lpTabPos, 0, FALSE );
705 }
706
707
708 /***********************************************************************
709  *           GetTabbedTextExtent32W    (USER32.294)
710  */
711 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count, 
712                                      INT cTabStops, const INT *lpTabPos )
713 {
714     LONG ret;
715     LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
716     lstrcpynWtoA( p, lpstr, count + 1 );
717     ret = GetTabbedTextExtentA( hdc, p, count, cTabStops, lpTabPos );
718     HeapFree( GetProcessHeap(), 0, p );
719     return ret;
720 }
721
722 /***********************************************************************
723  * GetTextCharset32 [GDI32.226]  Gets character set for font in DC
724  *
725  * NOTES
726  *    Should it return a UINT32 instead of an INT32?
727  *    => YES, as GetTextCharsetInfo returns UINT32
728  *
729  * RETURNS
730  *    Success: Character set identifier
731  *    Failure: DEFAULT_CHARSET
732  */
733 UINT WINAPI GetTextCharset(
734     HDC hdc) /* [in] Handle to device context */
735 {
736     /* MSDN docs say this is equivalent */
737     return GetTextCharsetInfo(hdc, NULL, 0);
738 }
739
740 /***********************************************************************
741  * GetTextCharset16 [GDI.612]
742  */
743 UINT16 WINAPI GetTextCharset16(HDC16 hdc)
744 {
745     return (UINT16)GetTextCharset(hdc);
746 }
747
748 /***********************************************************************
749  * GetTextCharsetInfo [GDI32.381]  Gets character set for font
750  *
751  * NOTES
752  *    Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
753  *    Should it return a UINT32 instead of an INT32?
754  *    => YES and YES, from win32.hlp from Borland
755  *
756  * RETURNS
757  *    Success: Character set identifier
758  *    Failure: DEFAULT_CHARSET
759  */
760 UINT WINAPI GetTextCharsetInfo(
761     HDC hdc,          /* [in]  Handle to device context */
762     LPFONTSIGNATURE fs, /* [out] Pointer to struct to receive data */
763     DWORD flags)        /* [in]  Reserved - must be 0 */
764 {
765     HGDIOBJ hFont;
766     UINT charSet = DEFAULT_CHARSET;
767     LOGFONTW lf;
768     CHARSETINFO csinfo;
769
770     hFont = GetCurrentObject(hdc, OBJ_FONT);
771     if (hFont == 0)
772         return(DEFAULT_CHARSET);
773     if ( GetObjectW(hFont, sizeof(LOGFONTW), &lf) != 0 )
774         charSet = lf.lfCharSet;
775
776     if (fs != NULL) {
777       if (!TranslateCharsetInfo((LPDWORD)charSet, &csinfo, TCI_SRCCHARSET))
778            return  (DEFAULT_CHARSET);
779       memcpy(fs, &csinfo.fs, sizeof(FONTSIGNATURE));
780     }
781     return charSet;
782 }