私は Direct2D と DirectWrite に非常に慣れていないので、これらの API が提供する可能性をまだ調べています。潜在的なグラフィック アプリケーションについて、ベクトル グラフィック エディタのように個々のポイントを変更できるように、テキストをパスとしてレンダリングできるかどうか疑問に思っていました。Direct2D と DirectWrite で直接そのようなことを行うことは可能ですか、または少なくとも必要な情報を取得して、テキストに似たパス オブジェクトを手動で構築する方法はありますか?
1491 次
1 に答える
2
コメントで参照している記事は正しいです。キーは単にIDWriteFontFace::GetGlyphRunOutlineです。使用方法については、こちらを参照してください。そして、サンプル コードからの 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 パラメーターで指定された場所に移動し、ジオメトリをトレースして塗りつぶします。特にアウトライン呼び出しが失敗した場合にシンクが閉じられない可能性があるロジックのために、私はコードを醜いと呼びます。しかし、それは単なるサンプルコードです。幸運を。
于 2015-06-05T13:21:18.337 に答える