繁体   English   中英

序列化FixedDocument时的FileFormatException

[英]FileFormatException when serializing a FixedDocument

当将FixedDocument序列化为XPS时,我有时会得到一个FileFormatException告诉我字体的格式(我假设)不符合预期的文件格式规范(请参阅下面的例外)。

神秘的部分是:

  • 异常只会偶尔发生一次
  • 它只会发生在某些FontFamily / Style / Weight组合中(Segoe UI用斜体和粗体似乎触发它)

有人知道为什么会发生这种情况(特别是为什么它不会一直发生但只是在不可预测的时间间隔内)?

遵循最小可重复性示例将在每次运行时触发异常约4至5次(在我的Windows 10计算机上,发生在.NET 4,4.6.1等):

private void TestXpsSerialization(object a)
{
    for (int i = 0; i < 400; ++i)
    {
        TextBlock block = new TextBlock
        {
            Text = "Test",
            FontFamily = new FontFamily("Segoe UI"),
            FontStyle = FontStyles.Italic,
            FontWeight = FontWeights.Bold,
            Background = null,
            FontSize = 12
        };
        FixedDocument fixedDoc = new FixedDocument();

        PageContent pageContent = new PageContent();
        FixedPage fixedPage = new FixedPage();
        fixedPage.Children.Add(block);
        ((IAddChild) pageContent).AddChild(fixedPage);
        fixedDoc.Pages.Add(pageContent);
        using (MemoryStream documentStream = new MemoryStream())
        {
            string inMemoryPackageName = string.Format("memorystream://{0}.xps", Guid.NewGuid());
            Uri packageUri = new Uri(inMemoryPackageName);
            using (Package package = Package.Open(documentStream, FileMode.CreateNew))
            {
                MemoryStream resultStream = new MemoryStream();
                PackageStore.AddPackage(packageUri, package);
                using (XpsDocument xpsd =
                new XpsDocument(package, CompressionOption.Maximum, inMemoryPackageName))
                {
                    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsd);
                    writer.Write(fixedDoc);
                    package.Flush();

                    using (MemoryStream outputStream = new MemoryStream())
                    {
                        SerializerWriter serializerWriter =
                        new XpsSerializerFactory().CreateSerializerWriter(outputStream);

                        bool success = true;
                        try
                        {
                            serializerWriter.Write(xpsd.GetFixedDocumentSequence());
                        }
                        catch (Exception e)
                        {
                            success = false;
                            Debug.WriteLine(e);
                        }
                        if (success)
                        {
                            outputStream.Seek(0, SeekOrigin.Begin);
                            outputStream.CopyTo(resultStream);
                        }
                    }
                }
                PackageStore.RemovePackage(packageUri);
                Debug.WriteLine(resultStream.Length);
            }
        }
    }
}

提出以下异常(请原谅德国人):

 Ausnahme ausgelöst: "System.IO.FileFormatException" in PresentationCore.dll System.IO.FileFormatException: Die Datei "pack://memorystream:,,62db450e-87fe-4246-a727-15ab02c5c55e.xps,/Resources/34890974-3e2d-4baf-9003-24c3375636b0.ODTTF" entspricht nicht der erwarteten Dateiformatspezifikation. bei MS.Internal.TrueTypeSubsetter.ComputeSubset(Void* fontData, Int32 fileSize, Uri sourceUri, Int32 directoryOffset, UInt16[] glyphArray) bei MS.Internal.FontFace.TrueTypeFontDriver.ComputeFontSubset(ICollection`1 glyphs) bei System.Windows.Media.GlyphTypeface.ComputeSubset(ICollection`1 glyphs) bei System.Windows.Xps.Serialization.FEMCacheItem.SubSetFont(ICollection`1 glyphs, Stream stream) bei System.Windows.Xps.Serialization.FEMCacheItem.Commit() bei System.Windows.Xps.Serialization.XpsFontSubsetter.CommitFontSubsetsSignal(FontSubsetterCommitPolicies signal) bei System.Windows.Xps.Serialization.XpsFontSerializationService.SignalCommit(Type type) bei System.Windows.Xps.Serialization.XpsSerializationManager.ReleaseXmlWriter(Type writerType) bei System.Windows.Xps.Serialization.DocumentSequenceSerializer.set_XmlWriter(XmlWriter value) bei System.Windows.Xps.Serialization.DocumentSequenceSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext) bei System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(Object serializedObject) bei System.Windows.Xps.Serialization.XpsSerializationManager.SaveAsXaml(Object serializedObject) bei System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync) bei System.Windows.Xps.XpsDocumentWriter.Write(FixedDocumentSequence fixedDocumentSequence) bei System.Windows.Xps.Serialization.XpsSerializerWriter.Write(FixedDocumentSequence fixedDocumentSequence) 

第一行可以翻译为:

 "System.IO.FileFormatException" in PresentationCore.dll System.IO.FileFormatException: "pack://memorystream:,,62db450e-87fe-4246-a727-15ab02c5c55e.xps,/Resources/34890974-3e2d-4baf-9003-24c3375636b0.ODTTF" file does not conform to the expected file format specification. 

问题中的源代码

源代码中的注释似乎表明这会调用本机代码。 字体驱动程序需要访问声明的字体。 并非所有字体都支持所有变体。 您应该确认已安装的字体系列已安装并支持斜体

如果api支持该选项,您应该尝试指示已经确认已安装的特定字体(而不是系列),并支持您选择的变体。

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM