×

First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

UPDATE: The supported method for Silverlight 2 release is shown here: http://silverlight.net/learn/learnvideo.aspx?video=69800.  You basically have to make it an assembly resource.

Since the beginning of Silverlight you've been able to embed fonts within a Silverlight application.  The challenge in version 1.0 was that you essentially had to use a downloader and some SetFontSource methods on a TextBlock (for example) to do it.  I wrote about this a while back when using my own handwriting as a font within Silverlight. 

It looked something like this:

this.downloader = control.createObject("downloader");  
this.downloader.addEventListener("completed",
Silverlight.createDelegate(this, this.handleFontDownloaded));
this.downloader.open("GET", "timheuer.ttf");
this.downloader.send();

handleFontDownloaded: function(sender, eventArgs)
{
this.header.setFontSource(sender);
this.itemtext.setFontSource(sender);
this.header.fontFamily = "Tim Heuer Normal";
this.itemtext.fontFamily = "Tim Heuer Normal";
}

It isn't incredibly ideal for all situations.  It works, and in some scenarios might be valid and fine.

For most, I think we'll want an easier implementation and something that feels a bit more natural.  Well, in Silverlight 2, we now have it.  Let's take a look at the above sample and how we could do that for Silverlight 2:

<TextBlock x:Name="Header" FontFamily="timheuer.ttf#Tim Heuer Normal" /> <TextBlock x:Name="ItemText" FontFamily="timheuer.ttf#Tim Heuer Normal" />

Okay, so what is happening here?  What happened to the script?  There is none (obviously).  What is happening here is that Silverlight now does the lifting for you.  Let's break this down a bit more.

First, the FontFamily is set to "timheuer.ttf" in this example, which is my handwriting font in TrueType format.  This font is located next to the applications XAP file which is in ClientBin.  It could be located anywhere in the same application domain and you could use an absolute URL here as well.  For our purposes, we have a file on a web server.

When we set that in the FontFamily to a file, Silverlight essentially creates the downloader for us in an efficient manner.  The font file is requested based on the URI provided and downloaded via a GET request.  Once downloaded it parses out the second part (the "#") to look within that font file for the named font.  So essentially the format is:

<file>#<named-font>

where # is the delimiter in this format.  That's it, you are done.  No script needed.  If you choose to package several font assets within your application you can put them in a single archive file as well and the same syntax would apply:

<TextBlock x:Name="Header" FontFamily="timheuer.zip#Tim Heuer Normal" />

The same execution happens.  Silverlight gets the archive file and then looks at the font file contents in the archive to find the first named font to use.  The archive doesn't have to only have font files either...which is cool at times.

Hope this helps!


3/11/2008 12:44 AM | # re: Embedding fonts in Silverlight 2
Hiya Tim, good call. It should be nice to mention that I only got this to work when changing the build action for the font to "content".
3/11/2008 2:30 AM | # re: Embedding fonts in Silverlight 2
thanks for this, very useful. Do you know of any sources of fonts whose license would allow you to use them in this way? I have been looking for some more interesting fonts for my own Silverlight apps, and I've found lots of nice looking "free for personal use" ones, but I'm not sure about the licensing implications of using these in a Silverlight app
3/11/2008 11:21 AM | # re: Embedding fonts in Silverlight 2
That's awesome! Thanks for doing the research for us!

- www.JoeLevi.com
3/12/2008 1:22 PM | # re: Embedding fonts in Silverlight 2
Way to go, thanks for the info!
5/9/2008 11:23 AM | # re: Embedding fonts in Silverlight 2
How do you do this in VB.net
5/9/2008 1:40 PM | # re: Embedding fonts in Silverlight 2
@patrick: in Silverlight 2 there is no code needed. the code above is javascript using Silverlight 1.0. So no VB or C# code is needed to do this.
5/12/2008 7:38 AM | # re: Embedding fonts in Silverlight 2
Hey Tim,
I followed your instructions and embedded my font on my Silverlight application. I switched its type to content and made sure that the font exists in the *.xap file. I then changed the FontFamily property of a control to reference the new Font.
Although this works in the Blend 2.5 designer (I can see the selected font), I keep getting a Download exception when trying to run it.
Do you have any idea what's going wrong? Have you tried it?
Thanks

5/15/2008 10:35 AM | # re: Embedding fonts in Silverlight 2
@kpantos: what is the exception being shown? have you looked at the http traffic to see if there is any bad request?
6/25/2008 12:34 AM | # re: Embedding fonts in Silverlight 2
Hey Tim, gorgeous!
Just what I was looking for. Take care,
Sylvie
6/26/2008 1:42 AM | # re: Embedding fonts in Silverlight 2
Hey Tim, it´s Sylvie again. You said: "The archive doesn't have to only have font files either...which is cool at times." And I am now trying to also put in the CliendBin folder more kind of files than just fonts. I then embedded a XML file in the ClientBin folder. Could you tell me how I can read it? I tried the StreamResourceInfo with System.XML.Linq but it doesn´t work. Any idea? When you also know how this works with Images, just send me the code. It will be more than appreciated.
Many thanks,

Sylvie
6/26/2008 8:52 AM | # re: Embedding fonts in Silverlight 2
@Sylvie: you can just use a WebClient object to request the URI to the endpoints of where your XML/images are. Assuming they are in the same location as the XAP (same domain I mean) you should be fine. Once you get your XML you can load it into an XDocument or with your Image to a Stream.
7/8/2008 6:54 AM | # re: Embedding fonts in Silverlight 2
How do I change the the build action? I can't find any option or setting for this....
7/9/2008 2:20 AM | # re: Embedding fonts in Silverlight 2
Answered offline. Thanks, Tim!
7/30/2008 10:57 PM | # re: Embedding fonts in Silverlight 2
Thanx You.. Perfect Docs
8/6/2008 4:18 AM | # re: Embedding fonts in Silverlight 2
Hey man,

Welli tried to use your code with Silverlight 2 beta, but I cant find the downloader, am i missing a reference?

I tried this too: Downloader myDownloader = new Downloader()...

My VS2008 cant find the Downloader...

Please answer me man.
8/6/2008 4:45 AM | # Cant find the Downloader
Hey man,

Welli tried to use your code with Silverlight 2 beta, but I cant find the downloader, am i missing a reference?

I tried this too: Downloader myDownloader = new Downloader()...

My VS2008 cant find the Downloader...

Please answer me man.
8/6/2008 7:57 AM | # re: Embedding fonts in Silverlight 2
Andres: See http://www.timheuer.com/blog/archive/2008/07/10/embed-fonts-and-file-upload-in-silverlight-2.aspx for a link to a video describing this process and how to do it in a supported manner. The Downloader object is for use in Javascript.
8/10/2008 11:33 AM | # re: Embedding fonts in Silverlight 2
Answered offline. Thanks, Tim!
8/15/2008 10:13 AM | # re: Embedding fonts in Silverlight 2
thanks you , perfect Post.
8/17/2008 11:19 PM | # re: Embedding fonts in Silverlight 2
Hey!
Your article is really nice one But i am still hungry to knoe much about the fonts in Silverlight.Actually I want to change "foreground property" of textblock on mouse enter and mouseleave event as well.I gt it correctly for changing fontsize but nt getting for foreground.Plz help me come out of this.
8/18/2008 9:27 PM | # re: Embedding fonts in Silverlight 2
Hey Tim Thanx Again
I got the solution.Although I was nt using storyboard.I just made a instance(obj1) of class SolidColorBrush().and then assign value to textbox1.foreground = obj1.color.Anyways thanks for your quick reply.I want one more favour if you can plz help me.I want to create whole web app. in silverlight.I had created the homepage.Now i dont knoe how to add another aspx page into it.Hope you wud comment on it.
8/19/2008 6:51 AM | # re: Embedding fonts in Silverlight 2
Nirav: you wouldn't add an ASPX page into a Silverlight application, you'd continue to build your XAML or XAML user controls into your Silverlight appication.
8/19/2008 9:37 PM | # re: Embedding fonts in Silverlight 2
HI
Thanks for that now i had added xaml pages to it but m getting trouble in navigation.I got some source thru net for that but all are in c#.I am trying to convert that in VB but its not working.I am stuck up here.
8/19/2008 9:44 PM | # re: Embedding fonts in Silverlight 2
Nirav: see silverlight.net/learn/learnvideo.aspx?video=69800 for a supported method for Silverlight 2
8/19/2008 11:22 PM | # re: Embedding fonts in Silverlight 2
Hey Thanks man
You are just great.You wud think tht I am gettin free tution from you but I could not stop myself asking questions.Now I am trying to have "include file(that we use in aspx page for repeated html)" in a Xaml file.Is it possible in Silverlight or will have to find some alternate.
Thanks again.Have a nice time there
8/20/2008 8:00 AM | # re: Embedding fonts in Silverlight 2
Nirav: you don't use the same model as ASPNET, so there isn't a concept of "includes" -- but you can create your own XAML user controls and add logic in there.
8/21/2008 12:36 AM | # re: Embedding fonts in Silverlight 2
Thanks Tim
It was so kind of you to guide me.Actually I am new to Sl and gt the wrok load so I had to find shortcuts,any ways I succeded finding substitute for "include files".Now Design is almost over,now I am trying to add one class,in which I want to write each and every functions used thru out application.But while importing some of namespaces I am getting error.Can u tell me y it is so.Below are some

"Imports System.Web.HttpContext
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Image
Imports System.Drawing.Imaging
Imports System.Net.Mail"
8/21/2008 11:17 AM | # re: Embedding fonts in Silverlight 2
Nirav: the classes you are trying to use are not a part of the Silverlight managed runtime. Silverlight has a subset of the base class libraries. For instance, System.Data.SqlClient and System.Net.Mail are not available for sure.
8/22/2008 2:36 AM | # re: Embedding fonts in Silverlight 2
Thanks Tim.
8/25/2008 9:31 PM | # re: Embedding fonts in Silverlight 2
Hi Tim
Is it possible to find any alternative for such namespaces other then "webservices".or this is the only way
8/25/2008 9:37 PM | # re: Embedding fonts in Silverlight 2
Nirav: if you want to access data from a client in a sandbox in a browser (aka Silverlight), you're safest route is to use a service architecture. Anything otherwise (i.e., direct access to your data source) would be increasing your attack surface of your application. This would be true of any rich client platform.
8/26/2008 12:38 AM | # re: Embedding fonts in Silverlight 2
Thanks Tim
This means I dont have any option but learn building webservices or WCF.
Its ok let me try this .
8/27/2008 12:19 AM | # re: Embedding fonts in Silverlight 2
This method downloads the TTF file on to the person's computer, right? Can this be somehow done by embedding TTF into a binary DLL or somehow that would make reverse-engineering / font-extraction IMPOSSIBLE? Or at least really really hard?
9/2/2008 3:24 AM | # Embedding fonts in Silverlight 2
Hello TH
Useful article .Thanks for that.Still more to learn,I am confused about validating the forms(aa...h we dont have forms in SLB2) So how to deal with it any comments.
9/26/2008 9:40 PM | # re: Embedding fonts in Silverlight 2
Hello!
Thank you so much for the tip, it was very useful! This said, what I found pretty interesting is the "#" use which could turn out to be really handful for loading images within a package just with XAML!

Let's say that I have an image (say "image1.png") that is in the root of a package (say "images.zip") on the file system repertory "C:"
My question is: Is it possible to load the image with XAML in an Image control by using the "#" syntax?
i.e: I would have
10/23/2008 8:08 AM | # re: Embedding fonts in Silverlight 2
My favorite megaupload search engine is megauploadfiles.com it’s the most powerful an easy to use. megauploadfiles.com has incredible speed of searching rapidshare links in the internet.
megauploadfiles.com database include all rapidshare links.
2/14/2009 3:02 PM | # re: Embedding fonts in Silverlight 2
Hi Tim-

So, I'm working in Blend and i embedded this font, called Berlin.
I've loaded it up from VS and apparently the font shows up everywhere I wanted, except in the hyperlinks i have.
It shows the fonts correctly in the links except for: any #s, and the cap letters B,P,Q & X. This is totally weird to me.

Got any answers? You've had plenty for me so far so, let's see if you can solve this puzzle.

thanks a bunch-
Karen
2/14/2009 3:19 PM | # re: Embedding fonts in Silverlight 2
Nevermind. I figured it out. So, if anyone ever has this problem, the key is to expand the font palette, open the font manager and make sure that the numbers, caps, etc. are checked for that embedded font.

So, there you go. :)
Love all the tips though, Tim.

Thanks a bunch-
Karen
2/24/2009 1:15 AM | # re: Embedding fonts in Silverlight 2
Has somebody managed to store a font in isolated storage?
Because fonts never change, they would be fine there, but how do I get them back into the picture?
2/24/2009 1:58 PM | # re: Embedding fonts in Silverlight 2
I think that I figured out how to get a font from isolated storage, with new FontSource(stream), cool
3/17/2009 8:42 PM | # re: Embedding fonts in Silverlight 2
I am having some erratic behaviour from embedded fonts. My application would sometime fail to render the embeded fonts, but will reappear if I refresh a couple of times... Anyone know what is the reason for this?
4/18/2009 2:00 AM | # re: Embedding fonts in Silverlight 2
When I use blend to embed a font into my project,I got those error messages
Can you give me some clue why this happens? Thank you

Error 1 The "SubsetFontsSilverlight" task failed unexpectedly. System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path) at System.IO.Path.Combine(String path1, String path2) at Microsoft.Expression.SubsetFontTask.SubsetFonts.GetTypeface() at Microsoft.Expression.SubsetFontTask.SubsetFonts.ProcessXaml(XmlNode node, Boolean autoFillOnly) at Microsoft.Expression.SubsetFontTask.SubsetFonts.ProcessXaml(XmlNode node, Boolean autoFillOnly) at Microsoft.Expression.SubsetFontTask.SubsetFonts.ProcessXaml(XmlNode node, Boolean autoFillOnly) at Microsoft.Expression.SubsetFontTask.SubsetFonts.ProcessXaml(XmlNode node, Boolean autoFillOnly) at Microsoft.Expression.SubsetFontTask.SubsetFonts.ProcessXaml(ITaskItem xaml) at Microsoft.Expression.SubsetFontTask.SubsetFonts.Execute() at Microsoft.Build.BuildEngine.TaskEngine.ExecuteInstantiatedTask(EngineProxy engineProxy, ItemBucket bucket, TaskExecutionMode howToExecuteTask, ITask task, Boolean& taskResult) Codoc
4/20/2009 12:06 AM | # re: Embedding fonts in Silverlight 2
Hi,Friend..I met the same problem with you
I suggest you to use uri like this kind
"/Your assembly name;component/Fonts/Fonts.zip#Your font name"
eg:
"/Codoc;component/Fonts/Fonts.zip#Myriad Pro"

To force Silverlight to load the resource from the assembly
It works pretty well for me,Hope it will help ^_^
4/28/2009 2:37 PM | # re: Embedding fonts in Silverlight 2
I have a Silverlight web application setup with multiple class libraries. I have added the font.ttf file to the ClientBin and changed the "Build Action" property to "Resource".

My xaml files for the main application as well as the libraries include the following line for TextBlocks:
<TextBlock Text="Testing Font" FontFamily="font.ttf#Font Name"/>

The font does not get applied to any of the textblocks, whether in the main application or the class libraries. I have also attempted to add the font.ttf file to the main application without changing the xaml, and this works.

I also used the Downloader object to download the font and set the source with the font.ttf file in the ClientBin, which worked in both the main application as well as the class libraries:
private void fontDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
TextBlock textblock = new TextBlock();
textblock.FontSource = new FontSource(e.Result);
textblock.SetValue(Grid.RowProperty, 0);
textblock.FontFamily = new FontFamily("Font Name");
textblock.FontSize = 14;
textblock.Text = "Testing Font";
this.LayoutRoot.Children.Add(textblock);
}

Anyone know how I can use the font file in ClientBin for class libraries within an application through xaml without having to use the Downloader object? I followed Tim's video for best practices, but that does not seem to work in my case...

Thanks for any response!
4/28/2009 3:12 PM | # re: Embedding fonts in Silverlight 2
nushi123 - if your fonts are in a different assembly than your application assembly then your FontFamily will have to use the full assembly namespace: AssemblyShortName;component/assemblyresourcefile.ttf#fontfamilyname
4/29/2009 8:36 AM | # re: Embedding fonts in Silverlight 2
Thanks Tim! That worked perfectly!
4/30/2009 6:29 AM | # re: Embedding fonts in Silverlight 2
HI Silve you are so prtty sase seli
6/15/2009 10:00 AM | # re: Embedding fonts in Silverlight 2
I did a lot of research trying to figure out how to legally obfuscate and include fonts in a Silverlight project. Because I was unable to find a step by step guide in one place I am posting it here.

1. Create Microsoft Word Document
2. Change the font to the desired font type
3. Print to XPS file
4. Change the XPS file extension to .zip
5. Open zip file and navigate to Fonts directory (eg: somezipfile.zip\Documents\1\Resources\Fonts)
6. Copy the ODTTF file (***DO NOT CHANGE THE NAME OF THE ODTTF FILE***)
7. If more than one font is required you can add several to a zip file
8. In the Silverlight project (not the associated web project) include the ODTTF, or zip file using "Add" -> "Existing Item"
9. Click on the newly added file and go to Properties
10. Change the "Build Action" to Resource
11. In the Xaml file you can now add controls with the FontFamily set, see the two examples below
12. **Compile**
13. You may need to launch the site in the debugger to see the fonts for the first time, but after that they should show up in the designer.

Code examples:
FontFamily="DA09FD01-347E-4FF6-9877-23AED4599FCE.odttf#Segoe UI"
FontFamily="fonts.zip#Segoe UI"
6/15/2009 11:14 AM | # re: Embedding fonts in Silverlight 2
JWSilverlighter - unfortunately the issue is not about 'legally obfuscating' -- but rather honoring the license of the font foundry. Even if you can obfuscate, most fonts do not allow embedding, obfuscated or not. I encourage anyone using font embedding to understand the license of the font they are using before making that decision.
9/8/2009 11:46 AM | # re: Embedding fonts in Silverlight 2
Rockin thanks :)
9/9/2009 11:41 AM | # re: Embedding fonts in Silverlight 2
Hallo Tim,

I am new to Silverlight (came from Flash).
I can not understand why you prefer the second method to use custom fonts!?
If you have some xap files and in every xap file you use the the second method for embeding fonts, then the filesize will increase because you embed the font in every file...

Can I build a xap file which holds the fonts I need to use and load it in runtime to the Main file?

Or basically, can I load xap files into an other and do I have access to them?

It would be great if could answer me...

bye
Markus
Gravatar
12/22/2009 12:39 PM | # re: Embedding fonts in Silverlight 2
Hello,

(1) What kind of font formats/types (en.wikipedia.org/wiki/Computer_font#Font_formats, en.wikipedia.org/.../Compact_Font_Format#Font_type ) are supported by silverlight?
(2) Is sub-setting supported with all above types?
(3) Could you point good example or recommend any further readings to get answers of above questions and to understand the complete Text model of Silverlight?

Thanks
1/11/2010 9:15 AM | # re: Embedding fonts in Silverlight 2
Your post is a great source of knowledge.Realy nice job.
Gravatar
8/30/2010 10:00 AM | # re: Embedding fonts in Silverlight 2
Hi Tim,

We have a special type of Font that is included/embeded in silverlight application, which works fine. We also have a crystal report that is outside the silverlight application but under the same session of silverlight application.

My questions is how can the crystal report use the that special type of font that is embeded inside silverlight application?

Or how can I automatically download/install that special type of font into user's computer when silverlight application run in the first time?

Thanks

 
Please add 7 and 1 and type the answer here:

DISCLAIMER:

The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.