简体   繁体   English

序列化FixedDocument时的FileFormatException

[英]FileFormatException when serializing a FixedDocument

When serializing a FixedDocument to XPS I sometimes get a FileFormatException telling me that the format of a font (I assume) does not conform to the expected file format specification (see exception further down). 当将FixedDocument序列化为XPS时,我有时会得到一个FileFormatException告诉我字体的格式(我假设)不符合预期的文件格式规范(请参阅下面的例外)。

The mysterious part is: 神秘的部分是:

  • The Exception will only happen once in a while 异常只会偶尔发生一次
  • It will only happen for certain FontFamily/Style/Weight combinations (Segoe UI in italic and bold seems to trigger it) 它只会发生在某些FontFamily / Style / Weight组合中(Segoe UI用斜体和粗体似乎触发它)

Does somebody know why this happens (and especially why it does not happen consistently but only in unpredictable intervals)? 有人知道为什么会发生这种情况(特别是为什么它不会一直发生但只是在不可预测的时间间隔内)?

Following minimal reproducable example will trigger the exception about 4 to 5 times per run (on my Windows 10 machine, happens with .NET 4, 4.6.1 etc.): 遵循最小可重复性示例将在每次运行时触发异常约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);
            }
        }
    }
}

Following exception is raised (please excuse the German): 提出以下异常(请原谅德国人):

 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) 

The first line can be translated to: 第一行可以翻译为:

 "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. 

Source code in Question 问题中的源代码

Comments in the source code seem to indicate that this calls native code. 源代码中的注释似乎表明这会调用本机代码。 The font driver needs access to the declared font. 字体驱动程序需要访问声明的字体。 Not all fonts support all variations. 并非所有字体都支持所有变体。 You should confirm that the font family you've chosen is installed and supports italics . 您应该确认已安装的字体系列已安装并支持斜体

If the api supports the option, you should try indicating a specific font (rather than a family) that you've already confirmed is installed and supports the variations you've chosen. 如果api支持该选项,您应该尝试指示已经确认已安装的特定字体(而不是系列),并支持您选择的变体。

Good luck! 祝好运!

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

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