Condividi tramite


SetFontSource

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

object.SetFontSource(downloader)

Arguments

downloader

Downloader

The object that contains the downloaded content.

Managed Equivalent

FontSource

Remarks

The SetFontSource method can be used as a way to add downloaded font content to the existing collection of fonts for a TextBlock object. The downloader parameter identifies the Downloader object that represents 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 downloader method does not have a part parameter, because fonts are extracted from all parts of a downloaded package automatically.

If you previously set the font source explicitly and want to restore the default font, call SetFontSource again and pass a downloader value of null. This will restore the default font for the object.

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

NoteNote:

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

NoteNote:

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.

Example

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

// 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 a Completed event.
    downloader.addEventListener("Completed", onCompleted);

    // Initialize the Downloader request. Note that the font name referenced in this sample is fictitious.
    downloader.open("GET", "contoso.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 has to be added to the TextBlock's collection of fonts, which is the purpose of SetFontSource. When it has been added to the collection, it can be selected by using the FontFamily property. The following JavaScript example shows how to use the SetFontSource method to add the font file to the font collection, and then set the FontFamily property to display the TextBlock with the downloaded font.

// 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 font collection.
    myTextBlock.setFontSource(sender);

    // Set the FontFamily property to the friendly name of the font.
    // This sample uses a fictitious font called "Contoso Font".
    myTextBlock.fontFamily = "Contoso Font";

    myTextBlock.text = "This is my new font!";
}

You can also download font files 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 when adding fonts to the font collection.

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

// Initialize the Downloader request.
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 fonts. When the font files have been added to the collection, they can be selected by using the FontFamily property. The following JavaScript example shows how to use the SetSource and SetFontSource methods to use the downloaded content.

// 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, "Cat.png");

    // Retrieve the TextBlock object.
    var myTextBlock = sender.findName("myTextBlock");

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

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

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

// Retrieve the TextBlock object.
var myTextBlock = sender.findName("myTextBlock");

// Remove the custom font setting.
myTextBlock.setFontSource(null);

Applies To

TextBlock

See Also

Reference