SetFontSource Method

Adds font files in the downloaded content to the object's collection of type faces.

XAML
Cannot use methods in XAML.
Scripting
object.SetFontSource(downloader)

Parameters

downloader

Downloader

The object containing the downloaded content. Set downloader to null to restore the default font for the object.

Remarks

The SetFontSource method can be used as a way of adding downloaded font content to the existing collection of type faces for a TextBlock object. The downloader parameter identifies the Downloader object representing the downloaded content. The downloaded content can either be an individual font file or packaged content, such as a Zip file, that contains one or more font files. This particular downloader API does not have a part parameter, because fonts are extracted from all parts in a downloaded package automatically.

Downloader only supports downloading fonts that come from the same domain as does the HTML page that contains the Silverlight plug-in; for details, see Using a Downloader Object. Also, backslashes (\) in ImageSource URIs are not permitted; always use forward slashes (/).

Note   Font files used with the SetFontSource method must be OpenType or TrueType fonts.

Note   As with most types of software, font files are licensed, rather than sold, and licenses that govern the use of fonts vary from vendor to vendor. As a developer it is your responsibility to ensure that you have the required license rights for any font you embed within a document or application, or otherwise redistribute.

Examples

The following JavaScript example shows how to use the Downloader object to download an individual font file.

JavaScript
// Event handler for initializing and executing a font file download request.
function onMouseLeftButtonUp(sender, eventArgs)
{
    // Retrieve a reference to the plug-in.
    var plugin = sender.getHost();
    // Create a Downloader object.
    var downloader = plugin.createObject("downloader");
    // Add Completed event.
    downloader.addEventListener("Completed", onCompleted);
    // Initialize the Downloader request.
    downloader.open("GET", "SHOWG.TTF");
    // Execute the Downloader request.
    // NOTE: downloader APIs disallow file:\\ scheme
    // you must run this sample over localhost: or off a server or the following call will fail
    downloader.send();
}

When the font file has been downloaded, it needs to be added to the TextBlock's collection of type faces, which is the purpose of SetFontSource. Once it has been added to the collection, it can then be selected using the FontFamily property. The following JavaScript example shows how to use the SetFontSource method to add the the font file to the type face collection, and then set the FontFamily property to display the TextBlock with the downloaded font:

JavaScript
// 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 = "Showcard Gothic";
    myTextBlock.text = "Showcard Gothic";
}

You can also download font files contained in a packaged format, such as a Zip file. The Zip file can contain other content files, such as image files, and those files are ignored for purposes of what gets added to the font collection.

Images in a ZIP file

Images in a ZIP file

The following JavaScript example shows how to use the Downloader object to download a Zip file containing an image file and multiple font files.

JavaScript
// Initialize the Downloader request. Zip file contains: Coco.png, Britanic.ttf, Erasbd.ttf, Showg.ttf
downloader.open("GET", "myMediaAssets.zip");

When the Zip file has been downloaded, the font files it contains must be added to the TextBlock's collection of type faces. Once the font files have been added to the collection, they can then be selected using the FontFamily property. The following JavaScript example shows how to use the SetSource and SetFontSource methods to use the downloaded content.

JavaScript
// Event handler for the Completed event.
function onCompleted(sender, eventArgs)
{
    // Retrieve the Image object.
    var myImage = sender.findName("myImage");
    // Set the Source property of the Image object to the specific image
    // within the downloaded Zip package file.
    myImage.setSource(sender, "Coco.png");
    // Retrieve the TextBlock object.
    var myTextBlock = sender.findName("myTextBlock");
    // Add the font files in the downloaded package object to the TextBlock's type face collection.
    myTextBlock.setFontSource(sender);
    // Set the FontFamily property to the friendly name of the font.
    myTextBlock.fontFamily = "Showcard Gothic";
}

The following JavaScript example shows how to define a Silverlight keyboard event by using the AddEventListener method:

JavaScript
function onLoaded(sender, eventArgs)
{
    // Set the root Canvas object to a KeyDown event handler function.
    var token = sender.addEventListener("keyDown", onKeyDown);
}
function onKeyDown(sender, keyEventArgs)
{
    // Determine whether the keystroke combination CTRL+V was detected.
    if ((keyEventArgs.key == 51) && (keyEventArgs.ctrl == true))
    {
        // Retrieve a reference to the plug-in.
        var slPlugin = sender.getHost();
        // Display the current version of the plug-in.
        alert("Silverlight version: " + slPlugin.isVersionSupported("1.0"));
    }
}

To return to the default font used to display the TextBlock, set the downloader parameter of the SetFontSource method to null.

The following JavaScript example shows how to use the SetFontSource method with a null parameter value.

JavaScript
// Retrieve the TextBlock object.
var myTextBlock = sender.findName("myTextBlock");
// Remove the custom font setting.
myTextBlock.setFontSource(null);

Applies To

TextBlock

See Also

Using a Downloader Object
Downloader