Is there a way to use a ODTTF font file in my WPF app?

情到浓时终转凉″ 提交于 2019-12-07 06:30:23

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/7520182/is-there-a-way-to-use-a-odttf-font-file-in-my-wpf-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!