创建 XPS 文件时,它会将原始文档的字体子集化并混淆为 ODTTF 字体文件,并将它们捆绑在 XPS 文件中(这只是一个 zip 文件,因此很容易提取它们。)
我已提取其中一个 ODTTF 文件,并将其作为资源包含在我的 WPF 应用程序中。
我现在尝试将它用作 TextBlock 的 FontFamily。我尝试了各种 URI 字符串来引用 XAML 中的 ODTTF 字体,但我根本无法让它工作。 (我可以使用常规 TTF 文件,但不能使用 ODTTF)
有办法做到这一点吗?我在一些 Google 搜索中发现了人们正在设法做到这一点的证据!
最佳答案
ODTTF 文件已被混淆。要将它们用作 TTF,您必须对它们进行反混淆。您可以使用此代码:
void DeobfuscateFont(XpsFont font, string outname)
{
using (Stream stm = font.GetStream())
{
using (FileStream fs = new FileStream(outname, FileMode.Create))
{
byte[] dta = new byte[stm.Length];
stm.Read(dta, 0, dta.Length);
if (font.IsObfuscated)
{
string guid = new Guid(font.Uri.GetFileName().Split('.')[0]).ToString("N");
byte[] guidBytes = new byte[16];
for (int i = 0; i < guidBytes.Length; i++)
guidBytes[i] = Convert.ToByte(guid.Substring(i * 2, 2), 16);
for (int i = 0; i < 32; i++)
{
int gi = guidBytes.Length - (i % guidBytes.Length) - 1;
dta[i] ^= guidBytes[gi];
}
}
fs.Write(dta, 0, dta.Length);
}
}
}
以这种方式写入 .TTF 文件后,您就可以使用该字体。请注意,XPS 文件中的字体是子集,仅包含 XPS 文件中实际使用的那些字符,因此它们无法在 MS-Word 等中用作字体。
关于wpf - 有没有办法在我的 WPF 应用程序中使用 ODTTF 字体文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7520182/