【问题标题】:Direct2D: Convert text to pathDirect2D:将文本转换为路径
【发布时间】:2015-04-15 16:53:46
【问题描述】:

我对 Direct2D 和 DirectWrite 非常陌生,并且仍在研究这些 API 提供的可能性。对于潜在的图形应用程序,我想知道是否可以将文本渲染为路径,以便可以像在矢量图形编辑器中一样修改各个点。是否可以直接在 Direct2D 和 DirectWrite 中执行类似的操作,或者至少有办法检索必要的信息并手动构造类似于文本的路径对象?

【问题讨论】:

  • 这种性质的问题通常不被鼓励,因为在 SO 上“太宽泛”,或者产生的答案将“主要基于意见”。但这并不意味着这不是一个好问题。您是否真的尝试过 Direct2D 来实现您想要完成的目标?
  • 好吧,我仍在学习使用 Direct2D/DirectWrite,但我发现它很难使用,尽管它似乎是一个非常灵活的 API。这个链接可能是我正在寻找的,但也许更有经验的开发人员可以对此发表评论。 codeproject.com/Articles/376597/Outline-Text-With-DirectWrite
  • 您可能想在该代码项目下发布您的问题。一旦您达到可以实际向我们展示一些代码的程度,我们可以在这里为您提供帮助。而且,是的,Direct2D 有点令人生畏:)。

标签: c++ direct2d directwrite


【解决方案1】:

您在评论中引用的文章是正确的。关键是IDWriteFontFace::GetGlyphRunOutline。更多关于用法的信息可以在here 找到。他们的示例代码中的 CustomTextRenderer 文章中描述的功能如下(虽然丑陋):

//  Gets GlyphRun outlines via IDWriteFontFace::GetGlyphRunOutline
//  and then draws and fills them using Direct2D path geometries
IFACEMETHODIMP CustomTextRenderer::DrawGlyphRun(
    _In_opt_ void* clientDrawingContext,
    FLOAT baselineOriginX,
    FLOAT baselineOriginY,
    DWRITE_MEASURING_MODE measuringMode,
    _In_ DWRITE_GLYPH_RUN const* glyphRun,
    _In_ DWRITE_GLYPH_RUN_DESCRIPTION const* glyphRunDescription,
    IUnknown* clientDrawingEffect)
{
    HRESULT hr = S_OK;

    // Create the path geometry.
    Microsoft::WRL::ComPtr<ID2D1PathGeometry> pathGeometry;
    hr = D2DFactory->CreatePathGeometry(&pathGeometry);

    // Write to the path geometry using the geometry sink.
    Microsoft::WRL::ComPtr<ID2D1GeometrySink> sink;
    if (SUCCEEDED(hr))
    {
        hr = pathGeometry->Open(&sink);
    }

    // Get the glyph run outline geometries back from DirectWrite 
    // and place them within the geometry sink.
    if (SUCCEEDED(hr))
    {
        hr = glyphRun->fontFace->GetGlyphRunOutline(
            glyphRun->fontEmSize,
            glyphRun->glyphIndices,
            glyphRun->glyphAdvances,
            glyphRun->glyphOffsets,
            glyphRun->glyphCount,
            glyphRun->isSideways,
            glyphRun->bidiLevel % 2,
            sink.Get());
    }

    // Close the geometry sink
    if (SUCCEEDED(hr))
    {
        hr = sink.Get()->Close();
    }

    // Initialize a matrix to translate the origin of the glyph run.
    D2D1::Matrix3x2F const matrix = D2D1::Matrix3x2F(
        1.0f, 0.0f,
        0.0f, 1.0f,
        baselineOriginX, baselineOriginY);

    // Create the transformed geometry
    Microsoft::WRL::ComPtr<ID2D1TransformedGeometry> transformedGeometry;
    if (SUCCEEDED(hr))
    {
        hr = D2DFactory.Get()->CreateTransformedGeometry(
            pathGeometry.Get(),
            &matrix,
            &transformedGeometry);
    }

    // Draw the outline of the glyph run
    D2DDeviceContext->DrawGeometry(
        transformedGeometry.Get(),
        outlineBrush.Get());

    // Fill in the glyph run
    D2DDeviceContext->FillGeometry(
        transformedGeometry.Get(),
        fillBrush.Get());

    return hr;
}

在该示例中,从文本创建路径几何图形后,它们只是移动到 x 和 y 参数中指定的位置,然后跟踪和填充几何图形。由于逻辑原因,我称代码丑陋,特别是如果大纲调用失败,接收器可能不会关闭。但这只是示例代码。祝你好运。

【讨论】:

    猜你喜欢
    • 2013-12-13
    • 2012-03-30
    • 1970-01-01
    • 2011-12-06
    • 2011-10-20
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多