Question

I'm rendering text in Direct2D/DirectWrite, but calling SetLineSpacing() on either TextFormat or TextLayout seems to have no effect. Does anyone know why?

Was it helpful?

Solution

I'm 99% sure that this is a bug. I have done a little playing around with Direct2D lately and also had a problem with SetLineSpacing() on TextLayout, think it is the same as what you are describing, in that case I can confirm that it's not just you. Reopen your bug report on MS Connect, it has been closed.

OTHER TIPS

I even have the same problem as Dmitri Nesteruk said.

However, I find out that if you set the lineSpacing "after CreateTextLayout", SetLineSpacing cannot work.

Otherwise, if you set LineSpacing before CreateTextLayout, it can work now.

Maybe you can try this soluton.

PS: My env. is in Window Vista SP2.

Many thanks.

Unfortunately you don't provide code showing what you're trying to do. I'm assuming that you're trying to set the line spacing like this:

pTextLayout->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, lineSpacing, baseline);

The documentation on MSDN is not terribly clear about the different line spacing methods. The default method causes the spacing for each line to be derived from the used font and inline objects, if any, so each line is the height that it needs to be according to its content. In this case, the two parameters, lineSpacing and baseline, are ignored.

If you have a text format that uses a single font (and font size) and no inline objects (or inline objects that are no higher than the text), you can specify uniform line spacing. This is what the DWRITE_LINE_SPACING_METHOD_UNIFORM is for. I've just tried it and calling SetLineSpacing() with this method results in uniform line spacing according to the passed arguments.

Starting with Windows 10, there is a third line spacing method, DWRITE_LINE_SPACING_METHOD_PROPORTIONAL, that can be used to achieve line spacing that is relative to what the font sizes and inline objects specify. However, this is only supported on Windows 10.

The most misleading part, in my experience, is that calling SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, x, y) succeeds without an error, and retrieving the line spacing parameters afterwards, using the GetLineSpacing() method, returns the values that were provided (while they still don't achieve anything). The API is internally consistent in this regard, not discarding the specified values even though they don't achieve anything useful. Also, when you call GetLineSpacing() on a newly-created IDWriteTextFormat or IDWriteTextLayout instance, the values are returned as zero (which is correct because the line spacing method is DWRITE_LINE_SPACING_METHOD_DEFAULT). In order to be able to do anything useful, you have to determine the default line spacing for the font you're using. You can achieve this by calling IDWriteFont::GetMetrics() on the font you're using. The default line spacing is the sum of the ascent, descent and lineGap values.

Charles Petzold wrote about this in an article about Pagination With DirectWrite.

Am i missing something?

Neither IDWriteTextFormat nor IDWriteTextLayout has a SetLineHeight function ...

Have you checked the HRESULT error code returned by SetLineSpacing()?

Hint: If you're using Visual Studio just type eax,hr in one of the debug Watch windows to see any possible error code right after the call.

Source

minimum supported client: Windows 7, Windows Vista with SP2 and Platform Update for Windows Vista.

Do you run any of the above stated versions of windows? I believe the reason why you aren't seeing any changes is because any version below the ones states above doesn't support the SetLineSpacing() in DirectWrite.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top