For DirectWrite, how we can placea text by specifying the position of the baseline? RRS feed

  • Question

  • Hi,

    I want to know, for DirectWrite, how I can place text by specifying the position of the baseline.

    I see both ID2D1RenderTarget::DrawText() and ID2D1RenderTarget::DrawTextLayout() calculate the position of the text to fit into the bounding box given by the parameter by default. It looks IDWriteTextFormat::SetLineSpacing() can modify the behavior but I couldn't figure out how I actualy can use it for my purpose.

    We could do this by specifying TA_BASELINE to SetTextAlign() for TextOut(). I'm looking for the something similar.


    Thursday, November 29, 2012 9:05 AM

All replies

  • Takuji,

    I'm looking at the same issue. Did you ever figure this out and would you mind sharing?

    Many thanks

    Thursday, December 20, 2012 4:27 PM
  • Hi GSP_T,

    My solution is bit redundant but the solution that I got is:


    //calculate font metrics DWRITE_FONT_METRICS fontMetrics; font->GetMetrics(&fontMetrics); //font is the object of IDWriteFont uses for the text rendering. ascent = MulDiv( fontMetrics.ascent, fontSize,fontMetrics.designUnitsPerEm); descent = MulDiv(fontMetrics.descent, fontSize,fontMetrics.designUnitsPerEm); ... // Specify the baseline of the text explicitly textFormat->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, ascent + descent, ascent); // textFotmat is the object of IDWriteTextFormat used for text rendering. ... // Calculate layoutY from the baseLineY layoutY = baseLineY - ascent; // Render the text using layoutY ...

    The real code that I have caches the metrics of the font and bit more complecated but the code above explains the idea enough I think.



    • Edited by tkawata5 Tuesday, January 15, 2013 7:51 AM
    Tuesday, January 15, 2013 7:50 AM
  • Thanks, I'll have to look at this later. I'm working on some other problems and will get back to this at some point.

    Again, thanks

    Tuesday, January 15, 2013 12:59 PM