Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextOptions.TextFormattingMode affecting text with bold font weight

Tags:

wpf

c#-4.0

xaml

I've had a problem where making the font weight of some text Bold made it smaller than the Normal font weighted text. I've worked out that this is because I've got the TextFormattingMode set to "Display"; the problem doesn't occur if the TextFormattingMode is set to "Ideal".

For example, the following code:

 <Label FontFamily="Calibri" FontSize="12" FontWeight="Bold" Content="This is some test text" TextOptions.TextFormattingMode="Ideal"/>
 <Label FontFamily="Calibri" FontSize="12" Content="This is some test text" TextOptions.TextFormattingMode="Ideal"/>
 <Label></Label>
 <Label FontFamily="Calibri" FontSize="12" FontWeight="Bold" Content="This is some test text" TextOptions.TextFormattingMode="Display"/>
 <Label FontFamily="Calibri" FontSize="12" Content="This is some test text" TextOptions.TextFormattingMode="Display"/>

Produces the following:

Results of XAML when running

If I up the font size to about 14 for the last two labels the bold text becomes larger than the normal weighted text.

My question is: is there a setting I can use to have 12 point bold text with the TextFormattingMode set to "Display" that is the same size / slightly larger than than the normal weighted text?

like image 474
GrandMasterFlush Avatar asked Dec 26 '22 15:12

GrandMasterFlush


1 Answers

The problem is not that the bold text is too short, it is that the normal text is too long.

There's history behind this, WPF originally shipped at .NET 3.0 supporting only the "Ideal" mode for scaling text. This mode supports true resolution independent text scaling, a line of text will have a predictable length in inches on various display devices with different dots-per-inch resolution. This was not received well, it caused massive complaints from WPF programmers that didn't like the blurry text that this produces. This is visible in your screen-shot. Note how the left stem of the bold letter m is too fat in Ideal mode but not in Display mode.

At .NET 4.0, the WPF team supported a new way to render text, called "Display". Which renders text the way GDI does it, applying font hinting rules to tweak the letter shape so it coincides better with the pixel grid of the monitor. This tends to stretch the letters, especially when their stem only has a single pixel. The smaller the point size, the more pronounced this becomes. The text is highly readable because of it, but true resolution independent rendering is lost.

Winforms also went through a similar evolution, from Graphics.DrawString() to TextRenderer.DrawText().

This blog post from the WPF team has the details.

The answer to your question is thus No.

like image 52
Hans Passant Avatar answered Mar 08 '23 23:03

Hans Passant