SVG icon indicating copy to clipboard operation
SVG copied to clipboard

Lost information after reading Svg

Open benberlin7 opened this issue 11 months ago • 8 comments

Description

When I read a SVG with your library in Debug or Release mode from within Visual Studio everything works fine. However after building an installation and trying it without Visual Studio some SVG data gets lost. The input SVG is exactly the same. The elements read from that via SvgDocument.Open(svgFilePath); or SvgDocument.FromSvg<SvgDocument>(File.ReadAllText(svgPath)); don't represent all the information of the SVG.

Below you can see some examples (example data). It's just the beginning of the document to show you the problem. [A] is the SvgContent read via SvgNet in Visual Studio (Debug or Release), plotted via svgDoc.GetXML(). [B] is the exact same Svg Input read from the installation. As you can see the transforms, width, height, stroke-width and x,y values are missing. I already checked the following:

  • both refer to the exact same Svg.DLL
  • both work on the same .NET Framework 4.8 (v4.0.30319).
  • the input SVGs are exactly the same
  • System.Drawing is in the same version
  • copied all missing DLLs from Visual Studio to the installation folder just to make sure it's not a forgotten reference
  • both have the same culture + ui-culture settings

If I read the elements in the installation via XmlDocument and log the InnerXML to a textfile the transforms etc. are displayed correctly.

I have no clue what I am missing or doing wrong.

Example data

[A] SVG Content read in Visual Studio (DEBUG or RELEASE)


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xml="http://www.w3.org/XML/1998/namespace" width="1122.52" height="793.7008" viewBox="0, 0, 1122.52, 793.7008">
  <rect x="37.79527" y="75.59055" width="1046.929" height="680.3149" stroke-width="1.322835" fill-opacity="0" style="fill:white;stroke:black;" />
  <g transform="translate(404.4094, 604.7244)">
    <g>
      <rect x="0" y="0" width="170.0787" height="37.79527" stroke-width="1.322835" fill-opacity="0" stroke-opacity="0" style="fill:white;stroke:white;" />
      <text transform="translate(3.779528, 5.889764) translate(2, 0) translate(0, 3)" text-anchor="start" font-family="Segoe UI" font-size="10" style="dominant-baseline:hanging;fill:black;">TestText</text>
    </g>
(...)

[B] the same SVG Content read from the installation

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xml="http://www.w3.org/XML/1998/namespace" viewBox="0, 0, 1122.52, 793.7008">
  <rect fill-opacity="0" style="fill:white;stroke:black;" />
  <g>
    <g>
      <rect x="0" y="0" fill-opacity="0" stroke-opacity="0" style="fill:white;stroke:white;" />
      <text text-anchor="start" font-family="Segoe UI" font-size="10" style="dominant-baseline:hanging;fill:black;">TestText</text>
    </g>
(...)

Used Versions

NET Framework 4.8 SVG.net Version 3.4.7 Win 11 Visual Studio 2022 Professional.

benberlin7 avatar Dec 16 '24 10:12 benberlin7

@mrbean-bremen still looking for help... :(

benberlin7 avatar Dec 19 '24 16:12 benberlin7

Sorry, I'm long out of this... still looking for a new maintainer (see #1111). Maybe @H1Gdev has an idea.

mrbean-bremen avatar Dec 19 '24 17:12 mrbean-bremen

I've never had this issue before, so I don't think I'll be able to find the cause right away...

  • Does this issue occur with certain SVG elements ?
  • Does this issue occur with older versions of SVG lib ?

H1Gdev avatar Dec 21 '24 20:12 H1Gdev

Hey @H1Gdev

Does this issue occur with older versions of SVG lib ?

I tried SVG.NET 3.4.0 and 3.4.7. Both had the same issue.

Does this issue occur with certain SVG elements ?

I think all elements which have these attributes (width, height, transforms,...) are faulty. Some are correctly represented like polygon and path, but they don't use these attributes at all. Heres an analysis from ChatGPT about the differences in 2 SVGs (1 correct from VisualStudio, 1 faulty from the installation):

The analysis shows that in the faulty file (File 2), the following elements and attributes are missing or incorrectly translated: Missing Elements and Attributes in File 2:

SVG Element:
    Attributes missing:
        width="1122.52"
        height="793.7008"

Rectangles (<rect>):
    A rectangle with the following attributes is missing:
        x="37.79527", y="75.59055", width="1046.929", height="680.3149", stroke-width="1.322835", style="fill:#FFFFFF;stroke:#000000;".

Groups (<g>):
    A group with transform="translate(404.4094, 604.7244)" is missing.

Text Elements (<text>):
    Missing transformations and style details:
        Example: transform="translate(3.779528,5.889764) translate(2, 0) translate(0, 3)".

Lines and Other Transformations:
    Precise transformation attributes and styles are absent.

Additional or Divergent Elements in File 2:

SVG Element:
    The width and height attributes are completely missing, but viewBox is present.

Rectangles (<rect>):
    Divergent style definitions:
        Instead of fill:#FFFFFF;stroke:#000000;, it uses fill:white;stroke:black;.

Text Elements (<text>):
    Simplified or reduced styles:
        Original: Includes dominant-baseline, transform, font-family, font-size.
        Faulty: Only includes style="dominant-baseline:hanging;fill:black;".

Summary:

File 2 omits many details and attributes, especially:
    width and height in the <svg> element.
    Precise transform attributes in <g> and <text>.
    Detailed style definitions for <rect> and <text> elements.

benberlin7 avatar Dec 23 '24 11:12 benberlin7

Today I tried passing a XMLReader with different settings

		var settings = new XmlReaderSettings
		               {
			               DtdProcessing = DtdProcessing.Parse, 
			               XmlResolver = new XmlUrlResolver(),  
			               ValidationType = ValidationType.Auto, 
			               IgnoreWhitespace = true,           
			               IgnoreComments = true,             
			               IgnoreProcessingInstructions = true 
		};

		
		XmlReader reader = XmlReader.Create(svgFilePath, settings);
		SvgDocument svgDoc = SvgDocument.Open<SvgDocument>(reader);
		CanvasHeight = svgDoc.Height;

I also tried

			               DtdProcessing = DtdProcessing.Parse,  
			               XmlResolver = new XmlUrlResolver(),   
			               ValidationType = ValidationType.None, 
			               IgnoreWhitespace = false,         
			               IgnoreComments = false,          
			               IgnoreProcessingInstructions = false

Unfortunately it did not help. But maybe it rules out sth. for you so I wanted to share this info @H1Gdev

benberlin7 avatar Dec 23 '24 15:12 benberlin7

solved it : it was the missing System.Numerics.Vectors.Dll in the installation folder.

benberlin7 avatar Dec 27 '24 11:12 benberlin7

but there is still some mystery behind. It works with the System.Numerics.Vectors.Dll now. But if I paste all DLLs in an older installation (I created several with different options, some can seen above) it still doesn't work. I don't have the ambition to investigate it any longer, because it works now and I already spent way too much time for this issue, but if this happens again to someone else : The way it works now : Open it with a distinct reader

	var settings = new XmlReaderSettings
		               {
			               DtdProcessing = DtdProcessing.Parse, 
			               XmlResolver = new XmlUrlResolver(),  
			               ValidationType = ValidationType.Auto, 
			               IgnoreWhitespace = true,           
			               IgnoreComments = true,             
			               IgnoreProcessingInstructions = true 
		};

		
		XmlReader reader = XmlReader.Create(svgFilePath, settings);
		SvgDocument svgDoc = SvgDocument.Open<SvgDocument>(reader);
		CanvasHeight = svgDoc.Height;

Then make sure that System.Vectors.Dll is in your installation folder.

benberlin7 avatar Dec 30 '24 10:12 benberlin7

I, too, ran into this issue. In my case, the issue was a failure to load the System.Runtime.CompilerServices.Unsafe library, likely (in part) because I installed a newer version. The problem is that SVG.NET is eating the exception and pretending that nothing is wrong. To find the root cause, I inspected the debug output within Visual Studio to see the exception thrown, and enabled Break When Thrown for that exception type.

The two exception types I've observed are

  • System.IO.FileLoadException
  • System.IO.FileNotFoundException

Image

Here's the stack trace I observed in one instance:

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=6.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  Source=System.Memory
  StackTrace:
   at System.MemoryExtensions.AsSpan(String text)
   at Svg.SvgUnitConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)

Inner Exception 1:
FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

To direct .Net to use the actual version I have installed, I added the <dependentAssembly> tag to the App.config for the executable project.

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.1.0" newVersion="6.0.1.0" />
      </dependentAssembly>
  </runtime>
</configuration>

aoeuidh avatar Jan 16 '25 20:01 aoeuidh