tags:

views:

63

answers:

1

I am mucking about with WPF glyphs and documents and have run into a null reference exception in the .Net 4 framework.

I extract and save true-type fonts to disk as .ttf files, then try to create Glyphs based on the fonts. The first time I save a font to disk and instantiate a GlyphTypeface based on the font after creating a GlyphTypeface from another font in the same folder I get a null reference exception.

Say I have fonts A and B. B has not been saved to disk (A may or may not have been saved to disk; that doesn't seem to matter):

1) save B to disk in the same folder as A,
2) create GlyphTypeface using font A,
3) create GlyphTypeface using font B = exception.

Null reference exception at:
at MS.Internal.FontCache.FontFaceLayoutInfo.IntMap.TryGetValue(Int32 key, UInt16& value)
at MS.Internal.FontCache.FontFaceLayoutInfo..ctor(Font font)
at System.Windows.Media.GlyphTypeface.Initialize(Uri typefaceSource, StyleSimulations styleSimulations)
at System.Windows.Media.GlyphTypeface..ctor(Uri typefaceSource)

If I restart my app and run it again (with font B already on disk), step 3 doesn't throw an exception.

The code to save a font to disk is simply writing a section from a binary stream and letting go of the file:

if (!File.Exists(filename))
{
    using (FileStream fs = File.Create(filename, length))
    {
        fs.Write(m_data, m_index, length);
        fs.Close();
    }
}

Any ideas? I don't want to have to put every font in its own folder...

Thanks for your time.

A: 

I ended up using the workaround of saving each font to its own folder (using the font name for the folder name). The exception went away, so I guess we can chalk this up to a bug in .Net.

AndrewS
The font-per-folder workaround isn't working for me, did you ever find any more info on this problem?
Cheetah
Nope, sorry. The workaround is still working for me. I am using .Net 4.0 (and I think it wasn't a problem in .Net 3.5 SP1, but I'm not positive). I check if a font exists in a folder before I create it too; I suspect that if you create a font in its own folder and then try to delete/overwrite it (without restarting your app) it will throw the exception.
AndrewS