Search code examples
directwrite

How to get correct text width for oblique font in DirectWrite?


I create a IDWriteTextLayout object by the following code,

    hr = g_pDWriteFactory->CreateTextLayout(text, textLength, *g_pTextFormat, 200000000, 200000000, g_pTextLayout);

then get the text width by text metric,

    DWRITE_TEXT_METRICS dtm;
    pTextLayout->GetMetrics(&dtm);
    float minHeight = dtm.height;
    float minWidth = dtm.widthIncludingTrailingWhitespace;

what confuse me is that whether the style of the font is DWRITE_FONT_STYLE_OBLIQUE or DWRITE_FONT_STYLE_NORMAL, the width of the same string is the same value. Why? I expect that when the font style is DWRITE_FONT_STYLE_OBLIQUE, the width should be bigger. how can i get the correct width for oblique text?

Thanks.


Solution

  • Layout metrics are based on font design metrics, either hinted or not, and not on actual ink box for each glyph. If you want metrics that closer match resulting rendered bitmap size, you can adjust returned layout metrics with data GetOverhangMetrics() returns. But at the end the only way to know exact pixel size of resulting layout is to render it.

    So answering your question, returned layout metrics for oblique font are already correct, they just mean slightly different thing than you expect them to.