Embedded Fonts
February 17th, 2007So I was digging through a bunch of test projects I had created a while ago and I stumbled across a project involving embedded fonts. I was about to delete the project when I saw that I had my app referencing fonts embedded in a library. This got me thinking about building applications where all of your visual information (colors, images, etc etc) could live in a library outside of your application. Then multiple apps could share the same visual resources or you could even set up your app to read in these library’s like skins.
Not that unique of an idea but with WPF’s Resource Dictionaries and Embedded Fonts this would be a very simple thing to do with some super hip rewards. Example: Let’s say after a year since you released your app you feel it could stand a visual redesign. Just throw together a new library, update the user’s “graphicslibrary.dll” and wham…you’re done. Or just push to the user as a new “skin file”. It’s almost to easy.
Now that I’ve decided every WPF app built from this day forward should have something like I just described, I’d better make a post so you know how to embed fonts.
Embedding and Referencing Fonts in your application and/or library is very simple.
First you’ll need to add your fonts to your project. In the image below you can see where I added the NEUROPOL.ttf font to my application and the VINQUE.ttf font to my library. NOTE: In your properties for the fonts make sure that “Build Action” is set to “Resource”. I believe that is the default value.

Now it’s just a matter of referencing the fonts in your xaml. (Normally I would make a reference to the FontFamily in a ResourceDictionary but for the sake of this example I just slapped it on the TextBoxes.)

The reference to the application’s embedded font on the first TextBox is fairly straight forward. It follows the format: /[PathToResource]/#[FontFamilyName]
The second reference to the library’s embedded font is a little trickier. It’s format is: /[LibraryAssemblyName];Component/[PathToResource]/#[FontFamilyName]
Oh…and don’t forget to add your library to your application’s References.
When it’s all said and done you get the following.

Yes I took screenshots of Visual Studio. It’s easier then hacking XAML into WordPress and I think it looks better. Since you can’t copy & paste here’s the SOURCE CODE.
Now get out there and start making skins for your apps. I’m going to spend the next couple of hours making a Star Wars Theme for Times Reader. Hmmm…or maybe a Mid-Evil Dragon Themed Times Reader. With Elfs…and Dwarfs…and Maidens…YAH…lots of Maidens…with swords…
March 13th, 2007 at 1:41 pm
Copy Source as HTML works with XAML. http://www.jtleigh.com/CopySourceAsHtml
March 14th, 2007 at 10:21 pm
That link gave me no love. I was able to find this though.
http://jasonhaley.com/blog/archive/2005/12/27/131246.aspx
July 24th, 2007 at 9:36 am
How can I dynamically add a font? I’m writing an application where I don’t know which of the 10,000 fonts we have are going to be used at that moment, and I need to copy it over and get it usable by the application.
I know how to move it into a specific directory and use a file reference, but that requires a lot of work we’d rather avoid. In the GDI+ world, we have PrivateFontCollection, but WPF doesn’t appear to have anything similar.
I’d love to HTTP the font across if I could somehow figure out the way to get the correct URI for it, but I’m a little confused on that as well.
Thanks,
R
October 17th, 2007 at 3:40 am
[...] Blog über das Einlagern von Fonts in einer Windows Presentation Foundation (WPF) Anwendung ("Embedding and Referencing Fonts in your application and/or library"). Der Artikel beschreibt in Kürze die einfache Funktionalität, doch dass es im Endeffekt [...]