8
votes

I cannot figure out how to determine how to render a specific OpenType font correctly in WPF (.NET 4.5 / VS2012 / Windows 8). I have a font installed on my computer called "DINPro-BlackItalic".

The file name is: DINPro-BlackItalic.otf

Windows reports the font name is: DINPro-BlackItalic

Windows Font preview for DINPro-BlackItalic

In WPF, I do something simple, like this:

<TextBlock Text="This is a sample sentence in DINPro-BlackItalic"
               FontFamily="DinPro-BlackItalic" FontSize="24" />

or FontFamily="#DinPro-BlackItalic"

This doesn't work (clearly not italic) and it falls back to the default font. I've tried lots of other variations which do work for other fonts in the font directory.

For example, Comic Sans works: FontFamily="Comic Sans MS" (awesome...)

Now, this is why I am really confused: If I just guess at a different way to punctuate the font name, it renders correctly!

This works: FontFamily="Din Pro Black Italic"

In summary, here is a screen shot of different FontFamily settings in WPF: Several FontFamily settings in WPF

Two questions:

1) How am I supposed to guess at the right way to tell WPF to render this font? It must have a canonical name registered somewhere but it isn't visible in the filename, file properties, or the Font Name that Windows reports after it has parsed the file.

2) What's worse is that if I'm attempting to render the same font in a desktop application and on the Web, browsers like Chrome on Windows and even IE expect "DINPro-BlackItalic" and will not render the spaced out version of the name. It seems that WPF and windows browsers make mutually exclusive decisions about which Font Name to use to index fonts. How do I work around this for arbitrary fonts?

DINPro Font rendering in Chrome and IE

4

4 Answers

7
votes

You can pack the font with the Application, and you can refer it from the project directory. Say if you place the font inside Fonts Directory in the Project.

You can add as follows,

<TextBlock FontFamily=”/Fonts/DINPro-BlackItalic.otf#DinPro-BlackItalic”/>

So you can be sure the, font gets deployed with the application.

2
votes

Okay so I think i found the solution..

<TextBlock FontFamily=”/Fonts/DINPro-BlackItalic.otf#Din Pro”/>

or

<TextBlock FontFamily=”/Fonts/DINPro-BlackItalic.otf#Din Pro Black Italic”/>

Havn't tried your code, but This seemed to work for me.. Referencing the path of the file+fontname

Opentype files have a commen name they share. And this is where it gets tricky, because you can only see this name if you install the font on your pc, and check it in the familytree. Like you showed before in your question:

FontFamily="Din Pro Black Italic"

This is my experience, not 100% sure that i'm correct, only that it works for me :)

0
votes

For WPF, Better to try it with designer for selecting the font for Textblock

For Browsers, It can be tried with Label or any other UI controls to display.

0
votes

The syntax that works for me is:

/{Application Name};component/{Font Folder in Project}/#{Font Name (not filename)}

Example:

/ApplicationName;component/Fonts/#Proxima Nova Lt