如何在.NET中创建子集的字体? [英] How to Create Subset Fonts in .NET?

查看:196
本文介绍了如何在.NET中创建子集的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要嵌入一些不那么常见的字体在Silverlight应用程序。这是非常简单,我把刚才复制在TTF / OTF和编译,与我的应用程序。然而,在许多情况下,实际使用只喜欢5-10的字符。在其他情况下,一些字体文件是令人难以置信的大(宋体统一code MS常规的是22.1 MB,作为一个例子)。我的应用程序的快速下载时间是非常重要的,所以优化使用的字体是至关重要的。

I have a Silverlight application that I need to embed some less-than-common fonts in. It's simple enough for me to just copy over the TTF/OTF and compile that with my app. However, in many cases, only like 5-10 of the characters are actually used. In other cases, some font files are incredibly large (Arial Unicode MS Regular is 22.1 MB, as an example). Fast download times of my app is really important, so optimizing the fonts used is paramount.

所以,我在想什么,是我见过的像前pression混合应用场合<雕文/> 用来创建一个只读的字体,你也可以只选择只嵌入特定字符。在其他情况下,我见过的人使用的字体,只有包含特定字符作为一个子集完整的字体的(而不是使用<雕文/> 在Silverlight的,而只是使用子集.TTF为<的FontFamily /> )这是什么样的我后,除了我没有使用防爆pressions。

So, what I was thinking is that I've seen in applications like Expression Blend where a <Glyph/> is used to create a read-only font and you can also just choose embed only certain characters. In other circumstances, I've seen people use fonts that only contained certain characters as a sub-set of the full font (and not use a <Glyph/> in Silverlight, but rather just use the sub-set .TTF as <FontFamily/>.) That's kind of what I'm after, except I'm not using Expressions.

我不找偷偷摸摸的解决方法,比如出口到XPS文件,并抢得了.odtff文件。

I'm not looking for sneaky workarounds, like exporting to an XPS file and grabbing the .odtff file.

有没有一种编程方式(.NET / GDI +),以创建一个子集的字体,只有某些字符并编译出一个.TTF / .OTF?此外,这将需要以及为.TTC文件的工作。

Is there a programmatic way (.NET/GDI+) to create a sub-set of a font with only certain characters and compile it out to a .TTF/.OTF? Also, this would need to work for .TTC files as well.

推荐答案

更​​改接受的答案这一个,因为它是纯.NET没有外部引用。使用.NET 4.0:

Changing the accepted answer to this one as it is pure .NET with no external references. Uses .NET 4.0:

Imports System.Windows.Media
Imports System.Text.Encoding
Imports System.Collections

Public Sub CreateSubSet(sourceText As String, fontURI As Uri)
    Dim gt As FontEmbeddingManager = New FontEmbeddingManager

    Dim glyphTypeface As GlyphTypeface = New GlyphTypeface(fontURI)
    Dim Index As Generic.ICollection(Of UShort)
    Index = New Generic.List(Of UShort)
    Dim sourceTextBytes As Byte() = Unicode.GetBytes(sourceText)
    Dim sourceTextChars As Char() = Unicode.GetChars(sourceTextBytes)
    Dim sourceTextCharVal As Integer
    Dim glyphIndex As Integer
    For sourceTextCharPos = 0 To UBound(sourceTextChars)
        sourceTextCharVal = AscW(sourceTextChars(sourceTextCharPos))
        glyphIndex = glyphTypeface.CharacterToGlyphMap(sourceTextCharVal)
        Index.Add(glyphIndex)
    Next
    Dim filebytes() As Byte = glyphTypeface.ComputeSubset(Index)
    Using fileStream As New System.IO.FileStream("C:\Users\Me\new-subset.ttf", System.IO.FileMode.Create)
        fileStream.Write(filebytes, 0, filebytes.Length)
    End Using
End Sub

这篇关于如何在.NET中创建子集的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆