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

别来无恙 提交于 2019-12-05 08:29:14

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.

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