ExtTextOut doesn't work on very long lines unless a lower font quality is specified

Sometimes our application needs to draw very long lines (for example 6000 characters) using ExtTextOut. Sometimes ExtTextOut fails and returns zero, and GetLastError also returns zero.

To recreate the situation, create a simple MFC Single Document application and then set OnDraw as:

void CTestExtTextView::OnDraw(CDC* pDC)
{
 CTestExtTextDoc* pDoc = GetDocument();
 ASSERT_VALID(pDoc);
 if (!pDoc)
  return;

 LOGFONT lfDetail = {0};
 lfDetail.lfHeight = -(::MulDiv(100, pDC->GetDeviceCaps(LOGPIXELSY), 720));
 lfDetail.lfCharSet = ANSI_CHARSET;
 lfDetail.lfOutPrecision = OUT_DEFAULT_PRECIS;
 lfDetail.lfQuality = CLEARTYPE_QUALITY;
 lfDetail.lfWeight = 400;
 _tcscpy_s(lfDetail.lfFaceName, LF_FACESIZE, _T("Arial"));

 CFont font;
 font.CreateFontIndirectW( &lfDetail );

 CFont * pold = pDC->SelectObject( &font );

 CString str = L"2 <office:document-content xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\" xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\" xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\" xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\" xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\" xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\" xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\" xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\" xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\" xmlns:math=\"http://www.w3.org/1998/Math/MathML\" xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\" xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\" xmlns:ooo=\"http://openoffice.org/2004/office\" xmlns:ooow=\"http://openoffice.org/2004/writer\" xmlns:oooc=\"http://openoffice.org/2004/calc\" xmlns:dom=\"http://www.w3.org/2001/xml-events\" xmlns:xforms=\"http://www.w3.org/2002/xforms\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" office:version=\"1.1\"><office:scripts/><office:font-face-decls><style:font-face style:name=\"Arial\" svg:font-family=\"Arial\" style:font-family-generic=\"swiss\" style:font-pitch=\"variable\"/><style:font-face style:name=\"Arial Unicode MS\" svg:font-family=\"&apos;Arial Unicode MS&apos;\" style:font-family-generic=\"system\" style:font-pitch=\"variable\"/><style:font-face style:name=\"Tahoma\" svg:font-family=\"Tahoma\" style:font-family-generic=\"system\" style:font-pitch=\"variable\"/></office:font-face-decls><office:automatic-styles><style:style style:name=\"co1\" style:family=\"table-column\"><style:table-column-properties fo:break-before=\"auto\" style:column-width=\"6.659cm\"/></style:style><style:style style:name=\"co2\" style:family=\"table-column\"><style:table-column-properties fo:break-before=\"auto\" style:column-width=\"7.408cm\"/></style:style><style:style style:name=\"co3\" style:family=\"table-column\"><style:table-column-properties fo:break-before=\"auto\" style:column-width=\"7.02cm\"/></style:style><style:style style:name=\"co4\" style:family=\"table-column\"><style:table-column-properties fo:break-before=\"auto\" style:column-width=\"7.214cm\"/></style:style><style:style style:name=\"co5\" style:family=\"table-column\"><style:table-column-properties fo:break-before=\"auto\" style:column-width=\"2.267cm\"/></style:style><style:style style:name=\"ro1\" style:family=\"table-row\"><style:table-row-properties style:row-height=\"0.473cm\" fo:break-before=\"auto\" style:use-optimal-row-height=\"true\"/></style:style><style:style style:name=\"ro2\" style:family=\"table-row\"><style:table-row-properties style:row-height=\"0.453cm\" fo:break-before=\"auto\" style:use-optimal-row-height=\"true\"/></style:style><style:style style:name=\"ta1\" style:family=\"table\" style:master-page-name=\"Default\"><style:table-properties table:display=\"true\" style:writing-mode=\"lr-tb\"/></style:style><style:style style:name=\"T1\" style:family=\"text\"><style:text-properties style:text-position=\"super 58%\"/></style:style></office:automatic-styles><office:body><office:spreadsheet><table:table table:name=\"Sheet1\" table:style-name=\"ta1\" table:print=\"false\"><office:forms form:automatic-focus=\"false\" form:apply-design-mode=\"false\"/><table:table-column table:style-name=\"co1\" table:default-cell-style-name=\"Default\"/><table:table-column table:style-name=\"co2\" table:default-cell-style-name=\"Default\"/><table:table-column table:style-name=\"co3\" table:default-cell-style-name=\"Default\"/><table:table-column table:style-name=\"co4\" table:default-cell-style-name=\"Default\"/><table:table-row table:style-name=\"ro1\"><table:table-cell office:value-type=\"string\"><text:p>Sample text for ExtTextOut</text:p></table:table-cell><table:table-cell office:value-type=\"string\"><text:p>Second Column Sample Text</text:p></table:table-cell><table:table-cell office:value-type=\"string\"><text:p>3<text:span text:style-name=\"T1\">rd</text:span> Column</text:p></table:table-cell><table:table-cell office:value-type=\"string\"><text:p>A More Lengthly bit of text just to make sure we increase the size of the line to something that might make ExtTextOut fail. It doesn&apos;t actually fail if we switch to Anti-Aliased Fonts. Not sure why. It also doesn&apos;t fail if we make the text shorter.</text:p></table:table-cell></table:table-row><table:table-row table:style-name=\"ro1\"><table:table-cell office:value-type=\"string\"><text:p>Sample text for ExtTextOut</text:p></table:table-cell><table:table-cell office:value-type=\"string\"><text:p>Second Column Sample Text</text:p></table:table-cell><table:table-cell office:value-type=\"string\"><text:p>3<text:span text:style-name=\"T1\">rd</text:span> Column</text:p></table:table-cell><table:table-cell office:value-type=\"string\"><text:p>A More Lengthly bit of text just to make sure we increase the size of the line to something that might make ExtTextOut fail. It doesn&apos;t actually fail if we switch to Anti-Aliased Fonts. Not sure why. It also doesn&apos;t fail if we make the text shorter.</text:p></table:table-cell></table:table-row><table:table-row table:style-name=\"ro1\"><table:table-cell office:value-type=\"string\"><text:p>Sample text for ExtTextOut</text:p></table:table-cell><table:table-cell office:value-type=\"string\"><text:p>Second Column Sample Text</text:p></table:table-cell><table:table-cell office:value-type=\"string\">";
 pDC->ExtTextOutW( 0,0, NULL, NULL, str, NULL );
 pDC->ExtTextOutW( 0,50, NULL, NULL, CString(L"But this will print out"), NULL );

 pDC->SelectObject( pold );
}

      

When you launch the application, you should see one line "But this will print" halfway down the screen. If you set lfQuality = ANTIALIASED_QUALITY then it does print something, but it just doesn't look right.

I've tested this on Vista and XP.

Any ideas?

+2


source to share


3 answers


Further experimentation has shown that it does not appear to be specific to line length or resulting width, that is, some lines with more characters and wider width will print just fine (for example, a line with 7,365 characters and a width of 40,360 while while another string with 6,572 characters and 36113 widths failed). However, those same longer lines can be made unsuccessful by changing something like the background color of the line.

This leads me to believe that it might be due to the complexity of the text string, not just the length of the line, and perhaps there is an internal timeout, that is, if ExtTextOut sees that it is taking too long, it just exits without printing any output.



Our solution was to slice each line by 500 characters. So instead of one ExtTextOut for the 6000 character line, there are 12 ExtTextOuts, each one printed at the end of the last one. This seems to work fine with very few hits and allows very large lines to be printed (I stopped testing after 60,000 characters).

+1


source


I created a simple font:

CFont font;
font.CreatePointFont(720, _T("Times New Roman"));
CFont * pold = pDC->SelectObject( &font );

      

Then the string is initialized until it is printed. 761 characters worked, 762 failed:

CString str('a', 761); // Works
CString str('a', 762); // Fails

      

I tried another font, it failed with a lot of characters. It didn't make sense until I took the size of each line:

CSize s = pDC->GetTextExtent(str);

      



The width of both lines was ~ 32700; near the 16-bit subscription limit of 32767.

I was under the impression that the 16-bit coordinated values ​​limit was imposed on 32 bits since NT, so I don't know why this won't work on XP or Vista. I can vaguely remember the KB article on this subject, but I cannot find it.

I tried using TextOut and DrawText and got the same results.

Then I tried to draw a couple of lines to make sure they work outside of the 16-bit limit:

pDC->MoveTo(10,0);
pDC->LineTo(10,38000);
pDC->MoveTo(10,38000);
pDC->LineTo(100, 38000);

      

and it worked fine, so I guess there is a bug in the GDI text functions.

+5


source


  • I'm seeing the same 4000 character issue on Windows 10 (so this rather old question is still a topic in 2016).
  • I am seeing this issue on Windows 7, but only on one computer when it is running on another Windows 7 computer.
  • If ExtTextOut fails, it returns FALSE. So it doesn't seem to be a bug, because the function has already noticed that something has gone wrong.

From these observations and from what other authors have written here, I can deduce:

  • Fat Elvis's conclusion that the 16-bit limit in funcion is definitely wrong, otherwise it will fail on all Windows 7 machines.
  • The snowfall theory that timeout plays a role is of great importance because I am seeing the problem on a slow Windows 7 machine and on a faster Windows 7 machine it draws the same line correctly. The graphics driver probably has a time limit at which it must draw characters. Also, MSDN says the string should be no more than 8192 characters. Therefore, Microsoft already states that there may be problems with too long lines.

The solution is definitely not to use a different font as suggested in the question. (A lower quality font renders the drawing faster, which again confirms the timeout theory.)

I wrote some code that finally solves the problem. The function is optimized at high speed.

// ATTENTION:
// The function returns FALSE on error but you cannot use GetLastError()!
BOOL ExtTextOutChunks(HDC h_Dc, int X, int Y, UINT u32_Flags, const RECT* pk_Rect, 
                      const WCHAR* u16_String, UINT u32_StrLen, const int* ps32_DX)
{
    // The maximum amount of characters that are printed at once.
    // The slower the computer the lower the value must be.
    const UINT CHUNK_SIZE = 500;

    // Speed optimization
    if (u32_StrLen <= CHUNK_SIZE)
        return ExtTextOut(h_Dc, X, Y, u32_Flags, pk_Rect, u16_String, u32_StrLen, ps32_DX);

    BOOL b_Return = TRUE;
    UINT u32_TxtAlign = GetTextAlign(h_Dc);
    BOOL b_SetFlag    = (u32_TxtAlign & TA_UPDATECP) == 0;

    // Set TA_UPDATECP to move the drawing position automagically after each drawing.
    // This is much faster than calling GetTextExtentPoint32() each time.
    if (b_SetFlag)
    {
        SetTextAlign(h_Dc, u32_TxtAlign | TA_UPDATECP);
        MoveToEx(h_Dc, X, Y, NULL);
    }

    while (u32_StrLen > 0)
    {
        UINT u32_Count = min(u32_StrLen, CHUNK_SIZE);

        if (!ExtTextOut(h_Dc, 0, 0, u32_Flags, pk_Rect, u16_String, u32_Count, ps32_DX))
        {
            b_Return = FALSE;
            break;
        }

        u32_StrLen -= u32_Count;
        u16_String += u32_Count;

        if (ps32_DX) ps32_DX += u32_Count;
    }

    // Reset the flag if it was not set before (ALWAYS!)
    if (b_SetFlag)
        SetTextAlign(h_Dc, u32_TxtAlign);

    assert(b_Return);
    return b_Return;
}

      

+1


source







All Articles