2

アプリケーションでExtTextOutを使用して非常に長い文字列(6,000文字など)を描画する必要がある場合があります。ExtTextOutが失敗してゼロを返し、GetLastErrorもゼロを返すことがあります。

シチュエーションを再作成するには、単純なMFCシングルドキュメントアプリケーションを作成してから、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=\"&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 );
}

アプリケーションを実行すると、「しかし、これは画面の途中まで印刷されます」という1行が表示されます。lfQuality = ANTIALIASED_QUALITYを設定すると、実際には何かが出力されますが、正しく表示されません。

私はこれをVistaとXPでテストしました。

何か案は?

4

3 に答える 3

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でした。32767の16ビット符号付き制限のすぐ近く。

NTから16ビット座標の制限が32ビットに引き上げられたという印象を受けたので、これがXPまたはVistaで機能しない理由がわかりません。この件に関するKB記事を漠然と覚えていますが、見つかりません。

TextOutとDrawTextを使ってみたところ、同じ結果が得られました。

次に、16ビットの制限を超えて機能することを確認するために、いくつかの線を描画してみました。

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

正常に動作したので、テキストベースのGDI関数にバグがあると思います。

于 2009-09-02T21:41:27.130 に答える
1

さらに実験を行った結果、行の長さや結果の幅まで具体的には見えないことがわかりました。つまり、文字数が多く幅が広い行の中には、うまく印刷されるものもあります(たとえば、7,365文字で幅が40,360の行はうまく印刷されます)。 6,572文字で36,113幅の別の行が失敗しました)。ただし、線の背景色などを変更することで、同じ長い線を失敗させることができます。

これは、行の長さだけでなくテキスト行の複雑さが原因である可能性があり、内部タイムアウトが発生する可能性があると私は信じています。つまり、ExtTextOutが時間がかかりすぎると判断した場合は、出力を印刷します。

私たちの解決策は、各行を500文字のチャンクに分割することでした。したがって、6,000文字の行に対する単一のExtTextOutの代わりに、最後の最後にそれぞれ12個のExtTextOutが印刷されます。これは、パフォーマンスへの影響がほとんどなく、非常に大きな行を印刷できるようになります(60,000文字の後でテストを停止しました)。

于 2009-09-03T10:29:55.500 に答える
1
  1. 私はWindows10の4000文字で同じ問題を観察します(したがって、この非常に古い質問は2016年でもまだトピックです)
  2. 私はWindows7で問題を観察しましたが、Windows 7を搭載した別のコンピューターで動作している間、1台のコンピューターでのみ問題が発生します。
  3. ExtTextOutが失敗すると、FALSEが返されます。したがって、これはバグではないようです。関数は、何かがうまくいかなかったことをすでに認識しているからです。

これらの観察と他の人がここに書いたことから、私は推測することができます:

  1. 関数に16ビットの制限があるというFatElvisの結論は確かに間違っています。そうでないと、すべてのWindows7マシンで失敗します。
  2. 遅いWindows7マシンでは問題が観察されますが、速いWindows 7マシンでは同じ文字列が正しく描画されるため、タイムアウトが役割を果たすというスノーデュードの理論は非常に理にかなっています。おそらくグラフィックスドライバーには、文字を描画しなければならない時間制限があります。さらに、MSDNは、文字列は8192文字を超えてはならないと述べています。そのため、Microsoftは、文字列が長すぎると問題が発生する可能性があるとすでに述べています。

解決策は、質問で提案されているように別のフォントを使用しないことです。(フォントの品質が低いと描画が速くなり、タイムアウトの理論が再び承認されます。)

私は最終的に問題を解決するコードを書きました。機能は高度に速度が最適化されています。

// 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;
}
于 2016-01-28T02:41:32.413 に答える