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).
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)
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.):
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.
xml:lang="de-DE"
. – IoabFontStyle = FontStyles.Italic
, then it works... looks like a bug to me (you're not the only one: questarter.com/q/…), you should report to Microsoft Connect connect.microsoft.com – Thessalonian