Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF does not render Windows OpenType font name

Tags:

wpf

fonts

render

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

like image 725
chris.keitel Avatar asked Aug 15 '13 14:08

chris.keitel


2 Answers

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.

like image 66
Arun Selva Kumar Avatar answered Nov 03 '22 01:11

Arun Selva Kumar


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 :)

like image 33
Max Mazur Avatar answered Nov 03 '22 02:11

Max Mazur