有没有办法在我的WPF应用程序中使用ODTTF字体文件? [英] Is there a way to use a ODTTF font file in my WPF app?

查看:458
本文介绍了有没有办法在我的WPF应用程序中使用ODTTF字体文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建XPS文件时,它将原始文档的字体作为ODTTF字体文件进行子集和模糊处理,并将其捆绑到XPS文件中(这只是一个zip文件,因此可以轻松提取)。 b
$ b

我已经提取了其中一个ODTTF文件,并将其作为资源包含在我的WPF应用程序中。



试图用它作为TextBlock的FontFamily。我尝试了各种URI字符串来引用我的XAML中的ODTTF字体,但是我根本无法得到它。 (我可以得到一个正常的TTF文件工作,只是不是一个ODTTF)

有没有办法做到这一点?我发现了一些谷歌搜索的证据,人们正在这样做!

解决方案

ODTTF文件被混淆了。要将它们用作TTF,您必须对其进行反混淆处理。你可以使用下面的代码:

$ $ $ $ $ $ $ $ $ $ $ $ $(
$($) 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文件,一个文件就会被写入一个.TTF文件中文件这种方式可以使用字体。请注意,XPS文件中的字体是子集,只包含XPS文件中实际使用的字符,所以它们在MS-Word中用作字体是没有用的。


When an XPS file is created, it subsets and obfuscates the original document's fonts as ODTTF font files, and bundles them in the XPS file (which is just a zip file, so they are easily extracted.)

I've extracted one of these ODTTF files, and included it as a Resource in my WPF app.

I'm now trying to use it as the FontFamily of a TextBlock. I tried various URI strings to reference the ODTTF font in my XAML, but I can't get it to work at all. (I can get a regular TTF file to work, just not an ODTTF)

Is there a way to do this? I've found evidence in a few Google searches that people are managing to do this!

解决方案

ODTTF files are obfuscated. To use them as TTF you must deobfuscate them. You can use this code:

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);
        }
    }
}

Once written to a .TTF file this way you can use the font. Note that the fonts in XPS files are subsets, only containing those characters actually used in the XPS file, so they won't be useful to use in, say, MS-Word as a font.

这篇关于有没有办法在我的WPF应用程序中使用ODTTF字体文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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