【问题标题】:RTL text width in DirectWriteDirectWrite 中的 RTL 文本宽度
【发布时间】:2014-03-20 06:43:18
【问题描述】:

我使用IDWriteTextAnalysisSink / AnalyzeScript 在 DirectWrite (C++) 中显示混合的 LTR(英语)和 RTL(希伯来语)文本,并计算文本宽度:

textwidth = 0;
for (UINT glyph=0; glyph<actualGlyphCount; glyph++) 
  textwidth += glyphAdvances[glyph];

glyphAdvancesGetGlyphPlacements 返回。

但是对于从右到左的文本,这通常是不准确的,导致文本重叠等。这是正确的方法吗?

谢谢。

【问题讨论】:

    标签: c++ directwrite


    【解决方案1】:

    您可能不需要对 IDWriteTextAnalysisSink / AnalyzeScript 做太多工作。

        HRESULT hr = S_OK;
        ComPtr<IDWriteTextFormat> textFormat;
        ComPtr<IDWriteTextLayout> textLayout;
    
        // Error checking omitted for brevity
        hr = textFactory->CreateTextFormat(L"Arial", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 30.0f, L"", textFormat.GetAddressOf());
        hr = textFormat->SetReadingDirection(DWRITE_READING_DIRECTION_RIGHT_TO_LEFT);
        hr = textFactory->CreateTextLayout(L"[HEBREW TEXT HERE]", textsize, textFormat.Get(), 0.0f, 0.0f, textLayout.GetAddressOf());
    

    渲染代码:

        renderTarget->Clear(BG_COLOR);
        auto size = renderTarget->GetSize();
    
        auto margin = 50.0f;
        size.width -= margin * 2.0f;
        size.height -= margin * 2.0f;
    
        if (S_OK == textLayout->SetMaxWidth(size.width) &&
            S_OK == textLayout->SetMaxHeight(size.height))
        {
            renderTarget->DrawTextLayout(Point2F(margin, margin), textLayout.Get(), brush.Get(), D2D1_DRAW_TEXT_OPTIONS_NONE);
        }
    

    读取希伯来文文件的截图:

    (注意:我的解决方案基于 Kenny Kerr 的示例) 我意识到您正在混合 LTR 和 RTL,但是我不确定这是否会增加 IDWriteTextAnalysisSink / AnalyzeScript 的复杂性。

    【讨论】:

    • 太好了,我已将部分代码转换为使用 IDWriteTextFormat/IDWriteTextLayout,其余部分稍后再做,我没有注意到 GetLineMetrics 和 GetClusterMetrics 函数提供了所有必要的测量信息,所以在我的毕竟没有必要实现 IDWriteTextAnalysisSink 。谢谢。
    • 是的,如果您正在绘制 IDWriteTextLayout,那么最好也使用一个进行测量,以便两者保持一致,而不是使用实际用于创建布局本身的低级函数。否则,您还必须复制一大堆内容,这些内容布局透明地为您执行,例如双向分析和字体回退,其中缺少字体回退可能是您得到希伯来语结果不正确的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 2015-04-11
    • 1970-01-01
    • 2012-06-13
    • 2014-10-26
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多