From ec23fb3a925058a47323c6f402d7db2918783de8 Mon Sep 17 00:00:00 2001 From: Rein Klazes Date: Fri, 27 Jan 2006 15:47:15 +0100 Subject: [PATCH] user: Fix DrawTextExA/W on empty strings. DrawTextExA/W should calculate a rectangle of zero height and width in some cases of null or empty supplied strings. With conformance tests. --- dlls/user/tests/text.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ dlls/user/text.c | 28 +++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/dlls/user/tests/text.c b/dlls/user/tests/text.c index 15188f53664..8d079ae7c42 100644 --- a/dlls/user/tests/text.c +++ b/dlls/user/tests/text.c @@ -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()); diff --git a/dlls/user/text.c b/dlls/user/text.c index 2c3f265e7ff..db9437e790f 100644 --- a/dlls/user/text.c +++ b/dlls/user/text.c @@ -864,9 +864,20 @@ INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count, 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 +1031,16 @@ INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count, 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; -- 2.11.4.GIT