Search code examples
direct2ddirectwrite

DirectWrite + Direct2D custom text rendering is hairy


I'm evaluating Direct2D for research purposes, and while I was at it, I decided to render my usual help text with a DirectWrite custom text renderer, which converts the text to a path geometry in order to add outline (as demonstrated in the DWriteHelloWorld sample on MSDN).

However, some letters have weird "hairs" or "horns" on them (picture: stroke width of 3 and 5).

text outline with different stroke widths

Also tried with other fonts (f.e. Consolas), the effect is the same.

Source code (VS 2015): https://www.dropbox.com/s/v3204h0ww2cp0yk/FOR_STACKOVERFLOW_12.zip?dl=0


Solution

  • The solution is as easy as I hoped. The "hairs" are actually caused by the line joints which D2D generates. Therefore the solution is to create an ID2D1StrokeStyle object as the following:

    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);
    

    With this solution, the text is rendered as expected (perhaps a little more roundish at the joints).