C++ Direct2D:将文本转换为路径
我对Direct2D和DirectWrite非常陌生,仍然在研究这些API提供的可能性。对于一个潜在的图形应用程序,我想知道是否有可能将文本渲染为路径,以便可以像在矢量图形编辑器中一样修改各个点。是否可以直接在Direct2D和DirectWrite中执行类似操作,或者至少有方法检索必要的信息并手动构造类似文本的路径对象?您在评论中引用的文章是正确的。关键很简单。可以找到更多关于用法的信息。在CustomTextRenderer文章中,他们的示例代码中描述的函数如下(尽管很难看):C++ Direct2D:将文本转换为路径,c++,direct2d,directwrite,C++,Direct2d,Directwrite,我对Direct2D和DirectWrite非常陌生,仍然在研究这些API提供的可能性。对于一个潜在的图形应用程序,我想知道是否有可能将文本渲染为路径,以便可以像在矢量图形编辑器中一样修改各个点。是否可以直接在Direct2D和DirectWrite中执行类似操作,或者至少有方法检索必要的信息并手动构造类似文本的路径对象?您在评论中引用的文章是正确的。关键很简单。可以找到更多关于用法的信息。在CustomTextRenderer文章中,他们的示例代码中描述的函数如下(尽管很难看): //通过ID
//通过IDWriteFontFace::GetGlyphRunOutline获取GlyphRun轮廓
//然后使用Direct2D路径几何图形绘制和填充它们
IFACEMETHODIMP CustomTextRenderer::DrawGlyphRun(
_在_opt_uuvoid*clientDrawingContext中,
浮动基线原点,
浮动基线原始,
数据写入测量模式测量模式,
_在uwrite_GLYPH_RUN const*glyphRun中,
_在uwrite_GLYPH_RUN_DESCRIPTION const*glyphRunDescription中,
IUnknown*客户端绘图效果)
{
HRESULT hr=S_正常;
//创建路径几何图形。
Microsoft::WRL::ComPtr pathGeometry;
hr=D2DFactory->CreatePathGeometry(&pathGeometry);
//使用几何图形接收器写入路径几何图形。
微软::WRL::ComPtr接收器;
如果(成功(hr))
{
hr=路径几何体->打开(&S)接收器;
}
//从DirectWrite获取glyph运行轮廓几何图形
//并将其放置在几何体水槽中。
如果(成功(hr))
{
hr=glyphRun->fontFace->GetGlyphRunOutline(
glyphRun->fontEmSize,
glyphRun->glyphrindex,
glyphRun->glyphRun前进,
glyphRun->glyphroffset,
glyphRun->glyphrount,
glyphRun->isSideways,
glyphRun->bidiLevel%2,
sink.Get();
}
//关闭几何体水槽
如果(成功(hr))
{
hr=sink.Get()->Close();
}
//初始化矩阵以转换字形运行的原点。
D2D1::Matrix3x2F常量矩阵=D2D1::Matrix3x2F(
1.0f,0.0f,
0.0f,1.0f,
baselineOriginX,baselineOriginY);
//创建变换的几何体
微软::WRL::ComPtr转换几何;
如果(成功(hr))
{
hr=D2DFactory.Get()->CreateTransformedGeometry(
pathGeometry.Get(),
&矩阵,
&转换几何);
}
//绘制图示符运行的轮廓
D2DDeviceContext->DrawGeometry(
transformedGeometry.Get(),
Get());
//填充glyph run
D2DDeviceContext->FillGeometry(
transformedGeometry.Get(),
fillBrush.Get());
返回人力资源;
}
在该示例中,从文本创建路径几何图形后,它们只需移动到x和y参数中指定的位置,然后跟踪和填充几何图形。由于逻辑原因,我认为代码很难看,特别是当大纲调用失败时,接收器可能无法关闭。但这只是示例代码。祝你好运。这种性质的问题通常被认为是“过于宽泛”,或者是“主要基于观点”的答案。但这并不意味着这不是一个好问题。您是否确实尝试过Direct2D,以实现您的目标?我仍在学习使用Direct2D/DirectWrite,但我发现它很难使用,尽管它似乎是一个非常灵活的API。这个链接可能是我想要的,但是也许一个更有经验的开发人员可以对此发表评论。你可能想把你的问题贴在那里,在那个代码项目下。一旦你达到一个点,你可以实际向我们展示一些代码,我们可以在这里帮助你。而且,是的,Direct2D有点吓人:)。
// 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;
}