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