Skip to content

XpsDocument.GetFixedDocumentSequence throws NotSupportedException #1063

Closed

Description

@elyoh

• NET Core Version: 3.0.0-preview6-27804-01 (also in preview 5).
• Windows version: Win10
• Does the bug reproduce also in WPF for .NET Framework 4.8?: No

An attempt is made to read an XPS document from disk:

var doc = new XpsDocument("path to file", FileAccess.Read);
var seq = doc.GetFixedDocumentSequence();

The following exception is thrown:

System.Windows.Markup.XamlParseException: ''Initialization of 'System.Windows.Documents.FixedDocument' threw an exception.' Line number '1' and line position '479'.'

Inner exception:

NotSupportedException: Specified method is not supported.

Stack trace:

at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, Boolean skipJournaledProperties, Uri baseUri)
   at System.Windows.Markup.XamlReader.Load(XamlReader xamlReader, ParserContext parserContext)
   at System.Windows.Markup.XamlReader.Load(XmlReader reader, ParserContext parserContext, XamlParseMode parseMode, Boolean useRestrictiveXamlReader)
   at System.Windows.Markup.XamlReader.Load(Stream stream, ParserContext parserContext, Boolean useRestrictiveXamlReader)
   at System.Windows.Markup.XamlReader.Load(Stream stream, ParserContext parserContext)
   at System.Windows.Xps.Packaging.XpsDocument.GetFixedDocumentSequence()

This appears to be a different issue to #575.

Activity

  1. changed the title [-]XpsDocument.GetFixedDocumentSequence[/-] [+]XpsDocument.GetFixedDocumentSequence throws NotSupportedException[/+] on Jun 23, 2019
  2. weltkante commented on Jun 24, 2019

    @weltkante
  3. elyoh commented on Jun 24, 2019

    @elyoh
    Author

    Inner exception stack trace:

    at MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32 hr)
       at MS.Internal.Text.TextInterface.Factory.CreateFontFace(Uri filePathUri, UInt32 faceIndex, FontSimulations fontSimulationFlags)
       at System.Windows.Media.GlyphTypeface.Initialize(Uri typefaceSource, StyleSimulations styleSimulations)
       at System.Windows.Media.GlyphTypeface..ctor(Uri typefaceSource, StyleSimulations styleSimulations)
       at System.Windows.Documents.Glyphs.ParseGlyphRunProperties()
       at System.Windows.Documents.Glyphs.ComputeMeasurementGlyphRunAndOrigin()
       at System.Windows.Documents.Glyphs.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Documents.FixedPage.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Documents.FixedDocument.GetPage(Int32 pageNumber)
       at System.Windows.Documents.FixedDocument.OnInitialized(Object sender, EventArgs e)
       at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)
    
  4. weltkante commented on Jun 25, 2019

    @weltkante

    That looks different from what I'd expect for #585 so probably needs a separate investigation.

  5. added
    Bug
    on Jun 28, 2019
  6. added this to the 3.0 milestone on Jun 28, 2019
  7. added
    regression
    on Jul 2, 2019
  8. added
    rank20
    on Jul 16, 2019
  9. rladuca commented on Jul 16, 2019

    @rladuca
    Member

    @elyoh This seems like something specific to the XPS being displayed or something that has been fixed in Preview8.

    I tested this against several XPS documents sitting around in our test code and I was able to display them all without an error on 3.0.100-preview8-013275.

    Can you test against the latest daily build and, if this still reproduces, supply an XPS document that causes the issue to occur?

  10. added
    📭 waiting-author-feedback
    on Jul 16, 2019
  11. rladuca commented on Jul 16, 2019

    @rladuca
    Member

    #585 (comment) seems to actually have a similar stack in Preview 8. Using the document from that repro, I get:

       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)
       at MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32 hr)
       at MS.Internal.Text.TextInterface.Factory.CreateFontFace(Uri filePathUri, UInt32 faceIndex, FontSimulations fontSimulationFlags)
       at System.Windows.Media.GlyphTypeface.Initialize(Uri typefaceSource, StyleSimulations styleSimulations)
       at System.Windows.Media.GlyphTypeface..ctor(Uri typefaceSource, StyleSimulations styleSimulations)
       at System.Windows.Documents.Glyphs.ParseGlyphRunProperties()
       at System.Windows.Documents.Glyphs.ComputeMeasurementGlyphRunAndOrigin()
       at System.Windows.Documents.Glyphs.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Documents.FixedPage.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Documents.FixedDocument.GetPage(Int32 pageNumber)
       at System.Windows.Documents.FixedDocument.OnInitialized(Object sender, EventArgs e)
       at System.Windows.FrameworkContentElement.RaiseInitialized(EventPrivateKey key, EventArgs e)
       at System.Windows.FrameworkContentElement.OnInitialized(EventArgs e)
       at System.Windows.FrameworkContentElement.TryFireInitialized()
       at System.Windows.FrameworkContentElement.EndInit()
       at MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)
    

    @stevenbrix @vatsan-madhavan We should sync up on what we know from #585.

    CC: @jbartlau since I used your reproduction document to mirror the same failure here.

  12. removed
    📭 waiting-author-feedback
    on Jul 16, 2019
  13. rladuca commented on Jul 16, 2019

    @rladuca
    Member

    This looks like our initialization of TypeFaces is directly calling into an unsupported part of the new zip implementation.

    Take a look at this stack:

     	System.IO.Compression.dll!System.IO.Compression.DeflateStream.Position.get() Line 167	C#
     	System.IO.Packaging.dll!System.IO.Packaging.ZipWrappingStream.Position.get() Line 95	C#
    >	PresentationCore.dll!MS.Internal.IO.Packaging.SynchronizingStream.Position.get()	Unknown
     	PresentationCore.dll!MS.Internal.IO.Packaging.DeobfuscatingStream.Read(byte[] buffer, int offset, int count)	Unknown
     	PresentationCore.dll!MS.Internal.FontCache.FontSource.StreamToByteArray(System.IO.Stream fontStream)	Unknown
     	PresentationCore.dll!MS.Internal.FontCache.FontSource.GetUnmanagedStream()	Unknown
     	DirectWriteForwarder.dll!MS.Internal.Text.TextInterface.FontFileStream.FontFileStream(MS.Internal.Text.TextInterface.IFontSource fontSource)	Unknown
     	DirectWriteForwarder.dll!MS.Internal.Text.TextInterface.FontFileLoader.CreateStreamFromKey(void* fontFileReferenceKey, uint fontFileReferenceKeySize, System.IntPtr* fontFileStream)	Unknown
     	[Native to Managed Transition]	
     	[Managed to Native Transition]	
     	DirectWriteForwarder.dll!MS.Internal.Text.TextInterface.Factory.GetFontCollection(System.Uri uri)	Unknown
     	PresentationCore.dll!MS.Internal.FontCache.DWriteFactory.GetFontCollectionFromFileOrFolder(System.Uri fontCollectionUri, bool isFolder)	Unknown
     	PresentationCore.dll!MS.Internal.FontCache.DWriteFactory.GetFontCollectionFromFile(System.Uri fontCollectionUri)	Unknown
     	PresentationCore.dll!System.Windows.Media.GlyphTypeface.Initialize(System.Uri typefaceSource, System.Windows.Media.StyleSimulations styleSimulations)	Unknown
    
    

    I have a feeling this is going to affect any XPS with local font resources. That's a pretty basic feature of XPS documents that won't work if we don't figure out some way around this.

    @elyoh does your XPS document contain fonts?

  14. rladuca commented on Jul 16, 2019

    @rladuca
    Member

    DeflateStream has this code:

            public override long Position
            {
                get { throw new NotSupportedException(SR.NotSupported); }
                set { throw new NotSupportedException(SR.NotSupported); }
            }
    
  15. rladuca commented on Jul 16, 2019

    @rladuca
    Member

    Interestingly enough, we have a test XPS document that has embedded fonts, but the entire archive is a flat structure (the fonts are located in the root of the archive). This file displays with no issue.

    I have to examine what code paths are taken in that instance.

  16. stevenbrix commented on Jul 16, 2019

    @stevenbrix
    Contributor
  17. rladuca commented on Jul 17, 2019

    @rladuca
    Member

    @stevenbrix Oh I see, so, for instance, I just need to wrap the stream used here instead of the direct cast?

    EDIT: Ignore the cast comment, it's just about passing the stream downward, got it.

  18. elyoh commented on Jul 17, 2019

    @elyoh
    Author

    @rladuca The xps file does reference fonts on the local system. It is a print out of an on screen view written in xaml. I'll get a sample xps file uploaded on Friday.

  19. weltkante commented on Jul 17, 2019

    @weltkante

    @rladuca these stack traces here from @elyoh and in #585 from @jbartlau look different. One stack trace is a managed NotSupportedException from DeflateStream.get_Position() but the other is a HRESULT native exception from TextInterface.Factory.CreateFontFace. I'm not sure they have the same root cause, the stack traces sure don't look that way.

  20. rladuca commented on Jul 17, 2019

    @rladuca
    Member

    @weltkante The second stack I posted is the actual exception stack from loading @jbartlau's file with first chance exceptions turned on. Loading the font when the font is located within the XPS always tries to access position, assuming it doesn't live in the root of the archive. Take a look at the offending code in DeobfuscatingStream and you'll see what I am talking about.

    I'm pretty sure the root cause of both issues is exactly the same even if the various stacks are slightly different in how we get there.

  21. rladuca commented on Jul 17, 2019

    @rladuca
    Member

    @weltkante Ah, I see where I might have conflated the two. I assumed the font loading path would get hit both ways. I'll wait for @elyoh's reproduction XPS first.

  22. elyoh commented on Jul 19, 2019

    @elyoh
    Author

    Here is a sample xps file which leads to the error.

    2069bGRVwEKTGite3tlNJw.zip

    I tried using the same file in 3.0.100-preview8-013330 and received the same error.

  23. rladuca commented on Jul 19, 2019

    @rladuca
    Member

    @elyoh Thanks! I'll have a look today and see what I can figure out.

  24. rladuca commented on Jul 19, 2019

    @rladuca
    Member

    @elyoh @weltkante This actually turned out to be the same error, just caught in a slightly different manner.

    When running with my recent fixes for #585, the document displays correctly.

    image

    When running without those changes, I get the same error on accessing DeflateStream.Position. The difference here is the call stack that leads to the error has an error trap that re-throws.

    EDIT: Re-throws isn't the right term, converts the exception to an HR, which then looks like some native call failed on the way back up.

    See here.

    In any case, the same workaround will help with your issue as I gave in #585 , open the XPS as read/write.

    Fixed by #1311

  25. rladuca commented on Jul 19, 2019

    @rladuca
    Member

    Dupe of #585

  26. ghost locked as resolved and limited conversation to collaborators on Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Bugrank20regression

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions