19

将 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.
4

1 回答 1

1

问题中的源代码

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

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

祝你好运!

于 2017-11-28T17:59:39.897 回答