Skip to content

Commit

Permalink
Enable SVGZ
Browse files Browse the repository at this point in the history
  • Loading branch information
BerndK committed Sep 2, 2019
1 parent 892c23b commit 2ce07a3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
14 changes: 13 additions & 1 deletion SvgConverter/ConverterLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -359,8 +360,14 @@ internal static DrawingGroup SvgFileToWpfObject(string filepath, WpfDrawingSetti
// }
//}

filepath = Path.GetFullPath(filepath);
Stream stream = IsSvgz(filepath)
? (Stream)new GZipStream(File.OpenRead(filepath), CompressionMode.Decompress, false)
: File.OpenRead(filepath);
var doc = XDocument.Load(stream);
stream.Dispose();

//workaround: error when Id starts with a number
var doc = XDocument.Load(Path.GetFullPath(filepath));
FixIds(doc.Root); //id="3d-view-icon" -> id="_3d-view-icon"
using (var ms = new MemoryStream())
{
Expand All @@ -371,6 +378,11 @@ internal static DrawingGroup SvgFileToWpfObject(string filepath, WpfDrawingSetti
}
}

private static bool IsSvgz(string filepath)
{
return string.Equals(Path.GetExtension(filepath), ".svgz", StringComparison.OrdinalIgnoreCase);
}

private static void FixIds(XElement root)
{
var idAttributesStartingWithDigit = root.DescendantsAndSelf()
Expand Down
4 changes: 4 additions & 0 deletions SvgConverterTest/SvgConverterTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="FileUtilsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ConverterTests.cs" />
<Compile Include="SvgzTest.cs" />
<Compile Include="T4Methods.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -139,6 +140,9 @@
</Content>
</ItemGroup>
<ItemGroup>
<None Include="TestFiles\example.svgz">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="TestFiles\xamlUntidy.xaml">
<SubType>Designer</SubType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
25 changes: 25 additions & 0 deletions SvgConverterTest/SvgzTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace SvgConverterTest
{
public class SvgzTest
{
[Test]
public void TestUnzip()
{
var fs = File.OpenRead(@".\TestFiles\example.svgz");
var stream = new System.IO.Compression.GZipStream(fs, CompressionMode.Decompress);
var destination = File.OpenWrite(@".\TestFiles\example.svg");
stream.CopyTo(destination);

}
}
}
2 changes: 1 addition & 1 deletion SvgConverterTest/TestFiles/Images.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<head>
<title>Browse svg images</title>
</head>
<body>Images in file: ..\..\TestFiles\images<br /><img src="3d-view-icon.svg" title="3d-view-icon" height="128" width="128" /><img src="cloud-3-icon.svg" title="cloud-3-icon" height="128" width="128" /><img src="Handwheel.svg" title="Handwheel" height="128" width="128" /><img src="JOG.svg" title="JOG" height="128" width="128" /></body>
<body>Images in file: ..\..\TestFiles\images<br /><img src="3d-view-icon.svg" title="3d-view-icon" height="128" width="128" /><img src="cloud-3-icon.svg" title="cloud-3-icon" height="128" width="128" /><img src="example.svg" title="example" height="128" width="128" /><img src="example.svgz" title="example" height="128" width="128" /><img src="Handwheel.svg" title="Handwheel" height="128" width="128" /><img src="JOG.svg" title="JOG" height="128" width="128" /></body>
</html>
Binary file added SvgConverterTest/TestFiles/example.svgz
Binary file not shown.

0 comments on commit 2ce07a3

Please sign in to comment.