SetFontsSources

SetFontsSources(FontSourceBase[])

设置 Aspose.Words 在渲染文档或嵌入字体时查找 TrueType 字体的来源。

public void SetFontsSources(FontSourceBase[] sources)
范围类型描述
sourcesFontSourceBase[]包含 TrueType 字体的源数组。

评论

默认情况下,Aspose.Words 会查找系统中安装的字体。

设置此属性将重置所有先前加载的字体的缓存。

例子

展示如何将字体源添加到我们现有的字体源。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Font.Name = "Arial";
builder.Writeln("Hello world!");
builder.Font.Name = "Amethysta";
builder.Writeln("The quick brown fox jumps over the lazy dog.");
builder.Font.Name = "Junction Light";
builder.Writeln("The quick brown fox jumps over the lazy dog.");

FontSourceBase[] originalFontSources = FontSettings.DefaultInstance.GetFontsSources();

Assert.AreEqual(1, originalFontSources.Length);

Assert.True(originalFontSources[0].GetAvailableFonts().Any(f => f.FullFontName == "Arial"));

// 默认字体源缺少我们在文档中使用的两种字体。
// 当我们保存此文档时,Aspose.Words 将对所有使用无法访问的字体格式化的文本应用后备字体。
Assert.False(originalFontSources[0].GetAvailableFonts().Any(f => f.FullFontName == "Amethysta"));
Assert.False(originalFontSources[0].GetAvailableFonts().Any(f => f.FullFontName == "Junction Light"));

// 从包含字体的文件夹创建字体源。
FolderFontSource folderFontSource = new FolderFontSource(FontsDir, true);

// 应用一个包含原始字体源以及自定义字体的新字体源数组。
FontSourceBase[] updatedFontSources = {originalFontSources[0], folderFontSource};
FontSettings.DefaultInstance.SetFontsSources(updatedFontSources);

// 在将文档呈现为 PDF 之前,验证 Aspose.Words 是否可以访问所有必需的字体。
updatedFontSources = FontSettings.DefaultInstance.GetFontsSources();

Assert.True(updatedFontSources[0].GetAvailableFonts().Any(f => f.FullFontName == "Arial"));
Assert.True(updatedFontSources[1].GetAvailableFonts().Any(f => f.FullFontName == "Amethysta"));
Assert.True(updatedFontSources[1].GetAvailableFonts().Any(f => f.FullFontName == "Junction Light"));

doc.Save(ArtifactsDir + "FontSettings.AddFontSource.pdf");

// 恢复原始字体源。
FontSettings.DefaultInstance.SetFontsSources(originalFontSources);

也可以看看


SetFontsSources(FontSourceBase[], Stream)

设置 Aspose.Words 查找 TrueType 字体的来源,并额外加载以前保存的 字体搜索缓存。

public void SetFontsSources(FontSourceBase[] sources, Stream cacheInputStream)
范围类型描述
sourcesFontSourceBase[]包含 TrueType 字体的源数组。
cacheInputStreamStream带有已保存字体搜索缓存的输入流。

评论

加载之前保存的字体搜索缓存将加快字体缓存的初始化过程。当访问字体源比较复杂时(例如通过网络加载字体),此功能尤其有用。

保存和加载字体搜索缓存时,通过缓存键识别所提供源中的字体。 对于SystemFontSourceFolderFontSource缓存键是字体文件的 path 。例如MemoryFontSourceStreamFontSource缓存键的定义 在CacheKeyCacheKeyproperties 。对于FileFontSource缓存键是CacheKey 属性或文件路径(如果CacheKey无效的

强烈建议在加载缓存时提供与保存缓存时相同的字体源。 字体源中的任何更改(例如添加新字体、移动字体文件或更改缓存键)都可能导致 Aspose.Words 解析字体不准确。

例子

展示如何加速字体缓存初始化过程。

public void LoadFontSearchCache()
{
    const string cacheKey1 = "Arvo";
    const string cacheKey2 = "Arvo-Bold";
    FontSettings parsedFonts = new FontSettings();
    FontSettings loadedCache = new FontSettings();

    parsedFonts.SetFontsSources(new FontSourceBase[]
    {
        new FileFontSource(FontsDir + "Arvo-Regular.ttf", 0, cacheKey1),
        new FileFontSource(FontsDir + "Arvo-Bold.ttf", 0, cacheKey2)
    });

    using (MemoryStream cacheStream = new MemoryStream())
    {
        parsedFonts.SaveSearchCache(cacheStream);
        loadedCache.SetFontsSources(new FontSourceBase[]
        {
            new SearchCacheStream(cacheKey1),
            new MemoryFontSource(File.ReadAllBytes(FontsDir + "Arvo-Bold.ttf"), 0, cacheKey2)
        }, cacheStream);
    }

    Assert.AreEqual(parsedFonts.GetFontsSources().Length, loadedCache.GetFontsSources().Length);
}

/// <summary>
/// 仅在需要时加载字体数据,而不是将其存储在内存中
/// 在“FontSettings”对象的整个生命周期内。
/// </summary>
private class SearchCacheStream : StreamFontSource
{
    public SearchCacheStream(string cacheKey):base(0, cacheKey)
    {
    }

    public override Stream OpenFontDataStream()
    {
        return File.OpenRead(FontsDir + "Arvo-Regular.ttf");
    }
}

也可以看看