有时我们的应用程序需要使用ExtTextOut绘制非常长的字符串(例如6,000个字符)。有时ExtTextOut失败并返回零,GetLastError也返回零。
要重新创建该情境,请创建一个简单的MFC Single Document应用程序,然后将OnDraw设置为:
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=\"'Arial Unicode MS'\" 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't actually fail if we switch to Anti-Aliased Fonts. Not sure why. It also doesn'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't actually fail if we switch to Anti-Aliased Fonts. Not sure why. It also doesn'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 );
}
运行应用程序时,您应该会看到一行“但是这将在屏幕中间打印出来”。如果你设置lfQuality = ANTIALIASED_QUALITY然后它确实打印出一些东西,但看起来不正确。
我在Vista和XP上测试了这个。
有什么想法吗?
答案 0 :(得分:5)
我创建了一个简单的字体:
CFont font;
font.CreatePointFont(720, _T("Times New Roman"));
CFont * pold = pDC->SelectObject( &font );
然后初始化一个字符串,直到它无法打印。 761个字符有效,762个字符失败:
CString str('a', 761); // Works
CString str('a', 762); // Fails
我尝试了不同的字体,但是在更多的字符数下失败了。在我拿出每个字符串的大小之前没有意义:
CSize s = pDC->GetTextExtent(str);
两根琴弦的宽度均为~32700;在16位符号限制32767附近。
我认为16位坐标限制从NT开始被提升到32位,所以我不知道为什么这在XP或Vista上不起作用。我可以依稀记得关于这个主题的知识库文章,但我找不到它。
我尝试使用TextOut和DrawText并得到了相同的结果。
然后我尝试绘制几行以确保它们超出了16位的限制:
pDC->MoveTo(10,0);
pDC->LineTo(10,38000);
pDC->MoveTo(10,38000);
pDC->LineTo(100, 38000);
并且它工作正常,所以我猜测基于文本的GDI函数存在一个错误。
答案 1 :(得分:1)
进一步的实验表明,它看起来并没有特别缩小到线条的长度或产生的宽度,即一些具有更多字符和更宽宽度的线条会打印得很好(例如,一条线条有7,365个字符和40,360宽度印刷精细,而另一条线有6,572个字符和36,113宽度失败)。但是,通过更改线条的背景颜色等可以使那些相同的较长线条失败。
这让我相信这可能是由于文本行的复杂性而不仅仅是行的长度而且可能存在内部超时,即如果ExtTextOut发现它花了太长时间,那么它只是退出而不打印任何输出。
我们的解决方案是将每一行分成500个字符的块。因此,对于6,000个字符行而不是单个ExtTextOut,在最后一个末尾打印有12个ExtTextOut。这似乎非常适用于非常小的性能打击,并允许我们打印非常大的线(我在60,000个字符后停止测试)。
答案 2 :(得分:1)
根据这些观察以及其他人在此处所写的内容,我可以推断:
解决方案肯定不会使用问题中建议的其他字体。 (较低质量的字体使绘图更快,这再次批准了超时理论。)
我写了一个代码,最终解决了这个问题。功能高速优化。
// 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;
}