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

问题描述

我正在评估 Direct2D 以用于研究目的,当我在做时,我决定使用 DirectWrite 自定义文本渲染器渲染我常用的帮助文本,该渲染器将文本转换为路径几何图形以添加轮廓(如图所示)在 MSDN 上的 DWriteHelloWorld 示例中)。

但是,有些字母上有奇怪的“毛”或“角”(图片:笔画宽度为 3 和 5)。

text outline with different stroke widths

也试过其他字体(例如Consolas),效果一样。

代码(VS 2015): https://www.dropbox.com/s/v3204h0ww2cp0yk/FOR_STACKOVERFLOW_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,&strokestyle);

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

使用此解决方案,文本按预期呈现(可能在接缝处更圆润)。