Silverlight TextBlock and Asian Fonts

 In addition to using the Glyph to render Asian Fonts in your Silverlight Applications, you can also use the TextBlock if you download and set the font using a downloader object. Please note that you'll need permission to distribute any font you use with your application.

Here's an example of XAML containing a TextBlock with Chinese Text:

<Canvas
xmlns="https://schemas.microsoft.com/client/2007"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Width="640" Height="480"
Background="White">
<TextBlock x:Name="myTextBlock" Width="152" Height="64"  

        Canvas.Left="184" Canvas.Top="56" Text="你好, 你好吗?"

        TextWrapping="Wrap" MouseLeftButtonDown="HandleIt" />
</Canvas>

 

When you run this, you'll see square blocks on the screen instead of the Chinese text. It will look something like this:

 

You'll see I have a 'MouseLeftButtonDown' event wired up to the TextBlock. This will call the following JavaScript event handler

 

// Event handler for initializing and executing a font file download request.
function HandleIt(sender, eventArgs)
{
// Retrieve a reference to the control.
var control = sender.getHost();

// Create a Downloader object.
var downloader = control.createObject("downloader");

// Add Completed event.
downloader.addEventListener("Completed", "onCompleted");

// Initialize the Downloader request.
downloader.open("GET", "SIMHEI.TTF", true);

// Execute the Downloader request.
downloader.send();
}

 

This creates a downloader object that downloads the SIMHEI.TTF font from the server. Note that this font is not freely distributable. For your applications you must use a font that you can legally distribute to your clients.

It sets up a callback called 'Completed' to call when the download is complete:

 

// Event handler for the Completed event.
function onCompleted(sender, eventArgs)
{
// Retrieve the TextBlock object.
var myTextBlock = sender.findName("myTextBlock");

// Add the font files in the downloaded object to the TextBlock's type face collection.
myTextBlock.setFontSource(sender);

// Set the FontFamily property to the friendly name of the font.
myTextBlock.fontFamily = "Simhei" ;

}

Now that we have the font file, we can set its font source to the downloader object, and pull the SimHei font family out of it. The text will then be correctly rendered.

And as you can see the font is now correctly rendered.

Depending on the language that you use, there are a number of fonts that are potentially freely redistributably. Check with your font vendors, but using this (or the Glyphs technique) can internationalize your Silverlight applications.