c++ - Direct2D:将文本转换为路径

标签 c++ direct2d directwrite

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

最佳答案

您在评论中引用的文章是正确的。关键就是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 参数中指定的位置,然后跟踪并填充几何图形。由于逻辑原因,我称代码丑陋,尤其是在大纲调用失败时接收器可能无法关闭的地方。但这只是示例代码。祝你好运。

关于c++ - Direct2D:将文本转换为路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28520082/

相关文章:

c++ - 如何在 DirectWrite 中使用默认 UI 字体绘制文本?

c# - 为什么 Direct2D 和 DirectWrite 不使用传统的 COM 对象?

c++ - boost::uuids::random_generator 和多线程的唯一性

c++ - QVTKOpenGLWidget 和 vtkGenericOpenGlRenderWindow 与 QTDesigner

c++ - 将 DirectShow 与 Direct2D 结合使用

c++ - 调用 ID2D1RenderTarget::DrawTextLayout() 不工作

c++ - 在调试或 Release模式下使用 DLL?

c++ - 传递对象还是创建新对象?

c++ - d2d1debug3.dll!DebugRenderTarget::EndDraw 访问冲突

delphi - 更改 DrawTextLayout 的 DWriteMeasuringMode