Added a few more Unicode digits from Unicode version 4.1.
[wine] / dlls / user / tests / text.c
1 /*
2  * DrawText tests
3  *
4  * Copyright (c) 2004 Zach Gorman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19  * USA
20  */
21
22 #include <assert.h>
23
24 #include "wine/test.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "winerror.h"
29
30
31 static void test_DrawTextCalcRect(void)
32 {
33     HWND hwnd;
34     HDC hdc;
35     HFONT hFont, hOldFont;
36     LOGFONTA lf;
37     const char text[] = "Example text for testing DrawText in "
38       "MM_HIENGLISH mode";
39     INT textlen,textheight;
40     RECT rect = { 0, 0, 100, 0 };
41     BOOL ret;
42
43     /* Initialization */
44     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
45                            0, 0, 200, 200, 0, 0, 0, NULL);
46     ok(hwnd != 0, "CreateWindowExA error %lu\n", GetLastError());
47     hdc = GetDC(hwnd);
48     ok(hdc != 0, "GetDC error %lu\n", GetLastError());
49     trace("hdc %p\n", hdc);
50     textlen = lstrlenA(text);
51
52     /* LOGFONT initialization */
53     memset(&lf, 0, sizeof(lf));
54     lf.lfCharSet = ANSI_CHARSET;
55     lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
56     lf.lfWeight = FW_DONTCARE;
57     lf.lfHeight = 0; /* mapping mode dependent */
58     lf.lfQuality = DEFAULT_QUALITY;
59     lstrcpyA(lf.lfFaceName, "Arial");
60
61     /* DrawText in MM_HIENGLISH with DT_CALCRECT */
62     SetMapMode(hdc, MM_HIENGLISH);
63     lf.lfHeight = 100 * 9 / 72; /* 9 point */
64     hFont = CreateFontIndirectA(&lf);
65     ok(hFont != 0, "CreateFontIndirectA error %lu\n",
66        GetLastError());
67     hOldFont = SelectObject(hdc, hFont);
68
69     textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
70        DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
71        DT_NOPREFIX);
72     ok( textheight, "DrawTextA error %lu\n", GetLastError());
73
74     trace("MM_HIENGLISH rect.bottom %ld\n", rect.bottom);
75     todo_wine ok(rect.bottom < 0, "In MM_HIENGLISH, DrawText with "
76        "DT_CALCRECT should return a negative rectangle bottom. "
77        "(bot=%ld)\n", rect.bottom);
78
79     SelectObject(hdc, hOldFont);
80     ret = DeleteObject(hFont);
81     ok( ret, "DeleteObject error %lu\n", GetLastError());
82
83
84     /* DrawText in MM_TEXT with DT_CALCRECT */
85     SetMapMode(hdc, MM_TEXT);
86     lf.lfHeight = -MulDiv(9, GetDeviceCaps(hdc,
87        LOGPIXELSY), 72); /* 9 point */
88     hFont = CreateFontIndirectA(&lf);
89     ok(hFont != 0, "CreateFontIndirectA error %lu\n",
90        GetLastError());
91     hOldFont = SelectObject(hdc, hFont);
92
93     textheight = DrawTextA(hdc, text, textlen, &rect, DT_CALCRECT |
94        DT_EXTERNALLEADING | DT_WORDBREAK | DT_NOCLIP | DT_LEFT |
95        DT_NOPREFIX);
96     ok( textheight, "DrawTextA error %lu\n", GetLastError());
97
98     trace("MM_TEXT rect.bottom %ld\n", rect.bottom);
99     ok(rect.bottom > 0, "In MM_TEXT, DrawText with DT_CALCRECT "
100        "should return a positive rectangle bottom. (bot=%ld)\n",
101        rect.bottom);
102
103     SelectObject(hdc, hOldFont);
104     ret = DeleteObject(hFont);
105     ok( ret, "DeleteObject error %lu\n", GetLastError());
106
107     /* Clean up */
108     ret = ReleaseDC(hwnd, hdc);
109     ok( ret, "ReleaseDC error %lu\n", GetLastError());
110     ret = DestroyWindow(hwnd);
111     ok( ret, "DestroyWindow error %lu\n", GetLastError());
112 }
113
114 /* replace tabs by \t */
115 static void strfmt( char *str, char *strout)
116 {
117     unsigned int i,j ;
118     for(i=0,j=0;i<=strlen(str);i++,j++)
119         if((strout[j]=str[i])=='\t') {
120             strout[j++]='\\';
121             strout[j]='t';
122         }
123 }
124   
125
126 #define TABTEST( tabval, tabcount, string, _exp) \
127 { int i,x_act, x_exp; char strdisp[64];\
128     for(i=0;i<8;i++) tabs[i]=(i+1)*(tabval); \
129     extent = GetTabbedTextExtentA( hdc, string, strlen( string), (tabcount), tabs); \
130     strfmt( string, strdisp); \
131  /*   trace( "Extent is %08lx\n", extent); */\
132     x_act = LOWORD( extent); \
133     x_exp = (_exp); \
134     ok( x_act == x_exp, "Test case \"%s\". Text extent is %d, expected %d tab %d tabcount %d\n", \
135         strdisp, x_act, x_exp, tabval, tabcount); \
136 } \
137
138
139 static void test_TabbedText()
140 {
141     HWND hwnd;
142     HDC hdc;
143     BOOL ret;
144     TEXTMETRICA tm;
145     DWORD extent;
146     INT tabs[8], cx, cy, tab, tabcount,t,align;
147
148     /* Initialization */
149     hwnd = CreateWindowExA(0, "static", NULL, WS_POPUP,
150                            0, 0, 200, 200, 0, 0, 0, NULL);
151     ok(hwnd != 0, "CreateWindowExA error %lu\n", GetLastError());
152     hdc = GetDC(hwnd);
153     ok(hdc != 0, "GetDC error %lu\n", GetLastError());
154
155     ret = GetTextMetricsA( hdc, &tm);
156     ok( ret, "GetTextMetrics error %lu\n", GetLastError());
157
158     extent = GetTabbedTextExtentA( hdc, "x", 1, 1, tabs);
159     cx = LOWORD( extent);
160     cy = HIWORD( extent);
161     trace( "cx is %d cy is %d\n", cx, cy);
162
163     align=1;
164     for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to 
165                                catch the one off errors */
166         tab =  (cx *4 + t);
167         /* test the special case tabcount =1 and the general array (80 of tabs */
168         for( tabcount = 1; tabcount <= 8; tabcount +=7) { 
169             TABTEST( align * tab, tabcount, "\t", tab)
170             TABTEST( align * tab, tabcount, "xxx\t", tab)
171             TABTEST( align * tab, tabcount, "\tx", tab+cx)
172             TABTEST( align * tab, tabcount, "\t\t", tab*2)
173             TABTEST( align * tab, tabcount, "\tx\t", tab*2)
174             TABTEST( align * tab, tabcount, "x\tx", tab+cx)
175             TABTEST( align * tab, tabcount, "xx\tx", tab+cx)
176             TABTEST( align * tab, tabcount, "xxx\tx", tab+cx)
177             TABTEST( align * tab, tabcount, "xxxx\tx", t>0 ? tab + cx : 2*tab+cx)
178             TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab+cx)
179         }
180     }
181     align=-1;
182     for( t=-1; t<=1; t++) { /* slightly adjust the 4 char tabstop, to 
183                                catch the one off errors */
184         tab =  (cx *4 + t);
185         /* test the special case tabcount =1 and the general array (8) of tabs */
186         for( tabcount = 1; tabcount <= 8; tabcount +=7) { 
187             TABTEST( align * tab, tabcount, "\t", tab)
188             TABTEST( align * tab, tabcount, "xxx\t", tab)
189             TABTEST( align * tab, tabcount, "\tx", tab)
190             TABTEST( align * tab, tabcount, "\t\t", tab*2)
191             TABTEST( align * tab, tabcount, "\tx\t", tab*2)
192             TABTEST( align * tab, tabcount, "x\tx", tab)
193             TABTEST( align * tab, tabcount, "xx\tx", tab)
194             TABTEST( align * tab, tabcount, "xxx\tx", 4 * cx >= tab ? 2*tab :tab)
195             TABTEST( align * tab, tabcount, "xxxx\tx", 2*tab)
196             TABTEST( align * tab, tabcount, "xxxxx\tx", 2*tab)
197         }
198     }
199
200
201 }
202
203 START_TEST(text)
204 {
205     test_TabbedText();
206     test_DrawTextCalcRect();
207 }