WPF - 将字体保存到磁盘,然后为字体抛出异常实例化GlyphTypeface。为什么? [英] WPF - Saving font to disk, then instantiating GlyphTypeface for font throws exception. Why?

查看:396
本文介绍了WPF - 将字体保存到磁盘,然后为字体抛出异常实例化GlyphTypeface。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WPF字形和文档,并且在.Net 4框架中遇到空引用异常。

提取并保存真正的字体磁盘作为.ttf文件,然后尝试基于字体创建字形。我第一次将字体保存到磁盘,然后基于字体实例化GlyphTypeface,然后在同一个文件夹中从另一个字体创建GlyphTypeface,我得到一个空引用异常。



说我有字体A和B. B还没有被保存到磁盘(A可能已经被保存到磁盘,这似乎并不重要):

1)将B保存到与A相同的文件夹中,b $ b 2)使用字体A创建字形字体,

3)使用字体B创建字形字体=例外。

 空引用异常:
at MS.Internal.FontCache.FontFaceLayoutInfo.IntMap.TryGetValue(Int32 key,UInt16& amp; amp; ;值)
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)

如果我重新启动我的应用程序并再次运行它(字体B已经在磁盘上),第3步不会抛出异常。



将字体保存到磁盘的代码只是从二进制流并放开文件:

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


$ / code $ / pre
$ b $ p

任何想法?我不想把每一个字体都放在自己的文件夹中...

感谢您的时间。

解决方案

我最终使用了将每种字体保存到自己的文件夹(使用文件夹名称的字体名称)的解决方法。例外消失了,所以我想我们可以把这个问题记录在.Net中的一个bug中。


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.

解决方案

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.

这篇关于WPF - 将字体保存到磁盘,然后为字体抛出异常实例化GlyphTypeface。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆