user: fix DrawTextExA/W on empty strings. take 2

Rein Klazes wijn at wanadoo.nl
Thu Jan 26 04:22:51 CST 2006


Hi,

Take 2:  make some code a bit more obvious. 

Changelog:
dlls/user		: text.c
dlls/user/tests	: text.c
DrawTextExA/W should calculate a rectangle of zero height and width in
some cases of null or empty supplied strings. With conformance tests.

Rein.
-------------- next part --------------
--- wine/dlls/user/text.c	2005-11-03 09:43:05.000000000 +0100
+++ mywine/dlls/user/text.c	2006-01-25 19:50:18.000000000 +0100
@@ -864,9 +864,17 @@ INT WINAPI DrawTextExW( HDC hdc, LPWSTR 
    if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
           dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
 
-    if (!str) return 0;
-    if (count == -1) count = strlenW(str);
-    if (count == 0) return 0;
+    if (!str || count == 0) return 0;
+    if (count == -1) {
+        count = strlenW(str);
+        if (count == 0) {
+            if( flags & DT_CALCRECT) {
+                rect->right = rect->left;
+                rect->bottom = rect->top;
+            }
+            return 0;
+        }
+    }
     strPtr = str;
 
     if (flags & DT_SINGLELINE)
@@ -1020,9 +1028,14 @@ INT WINAPI DrawTextExA( HDC hdc, LPSTR s
    DWORD wmax;
    DWORD amax;
 
-   if (!str) return 0;
-   if (count == -1) count = strlen(str);
    if (!count) return 0;
+   if( !str || ((count == -1) &&  !(count = strlen(str)))) {
+        if( flags & DT_CALCRECT) {
+            rect->right = rect->left;
+            rect->bottom = rect->top;
+        }
+        return 0;
+   }
    wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
    wmax = wcount;
    amax = count;
--- wine/dlls/user/tests/text.c	2005-11-03 09:43:08.000000000 +0100
+++ mywine/dlls/user/tests/text.c	2006-01-25 13:41:55.000000000 +0100
@@ -36,6 +36,9 @@ static void test_DrawTextCalcRect(void)
     LOGFONTA lf;
     const char text[] = "Example text for testing DrawText in "
       "MM_HIENGLISH mode";
+    const WCHAR textW[] = {'W','i','d','e',' ','c','h','a','r',' ',
+        's','t','r','i','n','g','\0'};
+    const WCHAR emptystringW[] = { 0 };
     INT textlen,textheight;
     RECT rect = { 0, 0, 100, 0 };
     BOOL ret;
@@ -100,6 +103,49 @@ static void test_DrawTextCalcRect(void)
        "should return a positive rectangle bottom. (bot=%ld)\n",
        rect.bottom);
 
+    /* empty or null text should in some cases calc an empty rectangle */
+    /* note: testing the function's return value is useless, it differs
+     * ( 0 or 1) on every Windows version I tried */
+    SetRect( &rect, 10,10, 100, 100);
+    textheight = DrawTextExA(hdc, (char *) text, 0, &rect, DT_CALCRECT, NULL );
+    ok( !(rect.left == rect.right && rect.bottom == rect.top),
+            "rectangle should NOT be empty.\n");
+    SetRect( &rect, 10,10, 100, 100);
+    SetLastError( 0);
+    textheight = DrawTextExA(hdc, "", -1, &rect, DT_CALCRECT, NULL );
+    ok( (rect.left == rect.right && rect.bottom == rect.top),
+            "rectangle should be empty.\n");
+    SetRect( &rect, 10,10, 100, 100);
+    SetLastError( 0);
+    textheight = DrawTextExA(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
+    ok( (rect.left == rect.right && rect.bottom == rect.top),
+            "rectangle should be empty.\n");
+    SetRect( &rect, 10,10, 100, 100);
+    textheight = DrawTextExA(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
+    ok( !(rect.left == rect.right && rect.bottom == rect.top),
+            "rectangle should NOT be empty.\n");
+
+    /* Wide char versions */
+    SetRect( &rect, 10,10, 100, 100);
+    SetLastError( 0);
+    textheight = DrawTextExW(hdc, (WCHAR *) textW, 0, &rect, DT_CALCRECT, NULL );
+    if( GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) {
+        ok( !(rect.left == rect.right && rect.bottom == rect.top),
+                "rectangle should NOT be empty.\n");
+        SetRect( &rect, 10,10, 100, 100);
+        textheight = DrawTextExW(hdc, (WCHAR *) emptystringW, -1, &rect, DT_CALCRECT, NULL );
+        ok( (rect.left == rect.right && rect.bottom == rect.top),
+                "rectangle should be empty.\n");
+        SetRect( &rect, 10,10, 100, 100);
+        textheight = DrawTextExW(hdc, NULL, -1, &rect, DT_CALCRECT, NULL );
+        ok( !(rect.left == rect.right && rect.bottom == rect.top),
+                "rectangle should NOT be empty.\n");
+        SetRect( &rect, 10,10, 100, 100);
+        textheight = DrawTextExW(hdc, NULL, 0, &rect, DT_CALCRECT, NULL );
+        ok( !(rect.left == rect.right && rect.bottom == rect.top),
+                "rectangle should NOT be empty.\n");
+    }
+   
     SelectObject(hdc, hOldFont);
     ret = DeleteObject(hFont);
     ok( ret, "DeleteObject error %lu\n", GetLastError());


More information about the wine-patches mailing list