Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to use Google Web Font in a WPF desktop Application

Tags:

wpf

webfonts

I'm creating a WPF application and trying to match the style of the user interface to an existing web site. The site is using Lato as its default font, which is not available by default on Windows. I'd like to use Lato, but I can't require users to install the font. Is there any way to get WPF to use the font definition that is available through the Google Web Font service, or otherwise set this up so that a manual install of the font is not necessary?

like image 472
Paul Keister Avatar asked Nov 28 '12 19:11

Paul Keister


1 Answers

Sure you can, the easy way is as a font resource you compile into your application:

First, download the TTF from google web fonts. From your example link find the link "Open Lato in Google Web Fonts" at the bottom of the page. This adds the font to your "collection". In the resulting page, up near the top is a smaller link titled "Download Your Collection" which will provide you with the the TTF font files in a zip file.

Next, embed the fonts as a resource in your WPF application. Add the files to your project, and make sure you select "Resource" for the compile action. Then, you can use your font in XAML simply by changing the FontFamily property to the resource name rather than the font name: <TextBlock FontFamily="/Resources/#Lato">Lato Text Here</TextBlock>. See the MSDN article on embedding fonts for more information.

You could also download the font on demand in code by setting the FontFamily property of whatever element or container you want to a new FontFamily object - use the constructor that takes a URI and use the URI of the TTF. I don't believe you can use the Google urls to do this directly, as the Google API assumes web page access and provides script and/or CSS as well as a font. Also note that this may require some additional permissions from what your app already demands.

like image 61
Philip Rieck Avatar answered Nov 12 '22 18:11

Philip Rieck