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

- 23,067
- 22
- 97
- 166
-
This could be a bug: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=507456 – Kredns Nov 03 '09 at 08:15
-
1I posted that bug, but thought it would be prudent to ask here, just in case I'm doing something wrong. – Dmitri Nesteruk Nov 03 '09 at 08:41
-
1Your bug report got closed today due to lack of response concerning their request for a reproduction example- you might want to reopen it, providing the example they need... – Henrik Opel Nov 12 '09 at 19:43
-
A code example would be great. Same on connect.microsoft.com. Can you update your q. pls? – Abel Nov 13 '09 at 11:31
-
Did you call it with the DWRITE_LINE_SPACING_METHOD_DEFAULT? Per the MSDN documentation, this mode relies solely on the content (the fonts being used), regardless of the two parameters. It is the DWRITE_LINE_SPACING_METHOD_UNIFORM which utilize the lineSpacing and baseline parameters. Need code snippet... – Dwayne Robinson Sep 20 '14 at 10:20
6 Answers
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.

- 12,884
- 8
- 53
- 83
-
I don't know if it really didn't work in 2009, but at least now calling `SetLineSpacing()` works as expected. The problem is that when specifying `DWRITE_LINE_SPACING_METHOD_DEFAULT`, the two float values are ignored, as they don't make sense in this context. Default line spacing means "derive line spacing from the used fonts and inline objects". See my answer below for more information. – Pepor Apr 01 '16 at 11:29
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.

- 1,042
- 1
- 8
- 27
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.

- 21
- 1
Am i missing something?
Neither IDWriteTextFormat nor IDWriteTextLayout has a SetLineHeight function ...

- 61,365
- 24
- 124
- 204
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.

- 5,015
- 3
- 37
- 36
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.

- 4,882
- 13
- 53
- 74
-
The fact that I'm running it would imply that I am, in fact, using a supported operating system, in this case 2008R2. – Dmitri Nesteruk Nov 13 '09 at 12:39