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

When an XPS file is created, it multiplies and obfuscates the original document fonts as ODTTF font files and links them in an XPS file (which is just a zip file, so they are easy to extract.)

I extracted one of these ODTTF files and included it as a resource in my WPF application.

Now I'm trying to use it as FontFamily in 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, not ODTTF)

Is there any way to do this? I found evidence in several Google search processes that people can do this!

+5
source share
1 answer

ODTTF obfuscation files. To use them as TTF, you must render them harmless. 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);
        }
    }
}

After writing in the .TTF file this way you can use the font. Note that fonts in XPS files are subsets containing only those characters that are actually used in the XPS file, so they will not be useful for use, say, in MS-Word as a font.

+9
source

All Articles