DirectWrite + Direct2D 自定义文本渲染很毛茸茸

DirectWrite + Direct2D custom text rendering is hairy

我正在评估 Direct2D 以用于研究目的,在评估过程中,我决定使用 DirectWrite 自定义文本呈现器来呈现我常用的帮助文本,它将文本转换为路径几何图形以添加轮廓 (如 MSDN 上的 DWriteHelloWorld 示例所示。

不过,有些字母上面有奇怪的“毛”或“角”(图片:笔划宽度为3和5)。

也试过其他字体(f.e.Consolas),效果一样。

源代码(VS 2015): https://www.dropbox.com/s/v3204h0ww2cp0yk/FOR_Whosebug_12.zip?dl=0

我怀疑这样做的原因是 D2D 中的默认展平公差对于以足够小的尺寸呈现字形轮廓的目的来说效果不佳。根据 GetRecommendedRenderingMode(),通常您会为小尺寸使用位图渲染,为大尺寸使用轮廓渲染。如果您将字体大小增加 10 倍,您是否有相同的伪像?

解决方法和我希望的一样简单。 “毛发”实际上是由 D2D 生成的线接头引起的。因此,解决方案是创建一个 ID2D1StrokeStyle 对象,如下所示:

ID2D1StrokeStyle* strokestyle = nullptr;
D2D1_STROKE_STYLE_PROPERTIES strokeprops = D2D1::StrokeStyleProperties();

strokeprops.lineJoin = D2D1_LINE_JOIN_ROUND;

d2dfactory->CreateStrokeStyle(strokeprops, NULL, 0, &strokestyle);

// draw
rendertarget->DrawGeometry(transformedgeometry, blackbrush, 3.0f, strokestyle);

使用此解决方案,文本按预期呈现(可能在关节处更圆一点)。