Is there a way to use a ODTTF font file in my WPF app?
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.
精彩评论