Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XpsDocument.GetFixedDocumentSequence throws NotSupportedException #1063

Closed
elyoh opened this issue Jun 23, 2019 · 18 comments
Closed

XpsDocument.GetFixedDocumentSequence throws NotSupportedException #1063

elyoh opened this issue Jun 23, 2019 · 18 comments
Assignees
Labels
Bug Product bug (most likely) rank20 Rank: Priority/rank on a scale of (1..100) regression status: This issue is a regression from a previous build or release
Milestone

Comments

@elyoh
Copy link

elyoh commented Jun 23, 2019

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

@elyoh elyoh changed the title XpsDocument.GetFixedDocumentSequence XpsDocument.GetFixedDocumentSequence throws NotSupportedException Jun 23, 2019
@weltkante

This comment has been minimized.

@elyoh
Copy link
Author

elyoh commented Jun 24, 2019

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)

@weltkante
Copy link

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

@grubioe grubioe added the Bug Product bug (most likely) label Jun 28, 2019
@grubioe grubioe added this to the 3.0 milestone Jun 28, 2019
@vatsan-madhavan vatsan-madhavan added the regression status: This issue is a regression from a previous build or release label Jul 2, 2019
@grubioe grubioe added the rank20 Rank: Priority/rank on a scale of (1..100) label Jul 16, 2019
@rladuca
Copy link
Member

rladuca commented Jul 16, 2019

@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?

@rladuca rladuca added the 📭 waiting-author-feedback To request more information from author. label Jul 16, 2019
@rladuca
Copy link
Member

rladuca commented Jul 16, 2019

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

@rladuca rladuca removed the 📭 waiting-author-feedback To request more information from author. label Jul 16, 2019
@rladuca
Copy link
Member

rladuca commented Jul 16, 2019

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?

@rladuca
Copy link
Member

rladuca commented Jul 16, 2019

DeflateStream has this code:

        public override long Position
        {
            get { throw new NotSupportedException(SR.NotSupported); }
            set { throw new NotSupportedException(SR.NotSupported); }
        }

@rladuca
Copy link
Member

rladuca commented Jul 16, 2019

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.

@stevenbrix
Copy link
Contributor

The workaround here will be fairly straightforward and should follow what I did here: https://github.com/dotnet/wpf/pull/995/files#diff-7b801941d4352acdc7ea9eeaba96fecd

@rladuca
Copy link
Member

rladuca commented Jul 17, 2019

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

@elyoh
Copy link
Author

elyoh commented Jul 17, 2019

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

@weltkante
Copy link

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

@rladuca
Copy link
Member

rladuca commented Jul 17, 2019

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

@rladuca
Copy link
Member

rladuca commented Jul 17, 2019

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

@elyoh
Copy link
Author

elyoh commented Jul 19, 2019

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.

@rladuca
Copy link
Member

rladuca commented Jul 19, 2019

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

@rladuca
Copy link
Member

rladuca commented Jul 19, 2019

@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

@rladuca
Copy link
Member

rladuca commented Jul 19, 2019

Dupe of #585

@rladuca rladuca closed this as completed Jul 19, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Apr 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Product bug (most likely) rank20 Rank: Priority/rank on a scale of (1..100) regression status: This issue is a regression from a previous build or release
Projects
None yet
Development

No branches or pull requests

6 participants