Skip to content

Commit

Permalink
added SVGZ export
Browse files Browse the repository at this point in the history
  • Loading branch information
gomi42 committed Dec 13, 2020
1 parent a27ce7c commit a8c9500
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
51 changes: 40 additions & 11 deletions Shape Converter/BusinessLogic/Exporter/SvgExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// along with this program. If not, see<http://www.gnu.org/licenses/>.

using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Windows.Media;
using System.Xml;
Expand All @@ -35,9 +37,41 @@ namespace ShapeConverter.BusinessLogic.Exporter
public static class SvgExporter
{
/// <summary>
/// Export to an image format
/// Export to SVG
/// </summary>
public static void ExportSvg(GraphicVisual visual, int width, string filename)
{
XElement root = CreateXmlExport(visual, width);

using (var writer = XmlWriter.Create(filename, new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true }))
{
root.Save(writer);
}
}

/// <summary>
/// Export to SVGZ
/// </summary>
public static void ExportSvgz(GraphicVisual visual, int width, string filename)
{
XElement root = CreateXmlExport(visual, width);

using (var fs = File.Create(filename))
{
using (var gz = new GZipStream(fs, CompressionMode.Compress))
{
using (var writer = XmlWriter.Create(gz, new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true }))
{
root.Save(writer);
}
}
}
}

/// <summary>
/// Create the xml export structure
/// </summary>
private static XElement CreateXmlExport(GraphicVisual visual, int width)
{
XNamespace ns = "http://www.w3.org/2000/svg";
XElement root = new XElement(ns + "svg");
Expand All @@ -51,26 +85,21 @@ public static void ExportSvg(GraphicVisual visual, int width, string filename)

XElement definitions = new XElement(ns + "defs");
int definitionsCount = 0;
var element = Generate(normalizedVisual, ns, definitions, ref definitionsCount);
var element = GenerateXmlTree(normalizedVisual, ns, definitions, ref definitionsCount);

if (definitions.HasElements)
{
root.Add(definitions);
}

root.Add(element);

using (var writer =
XmlWriter.Create(filename, new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true }))
{
root.Save(writer);
}
return root;
}

/// <summary>
/// Generate a visual recursively to xml
/// </summary>
private static XElement Generate(GraphicVisual visual, XNamespace ns, XElement definitions, ref int definitionsCount)
private static XElement GenerateXmlTree(GraphicVisual visual, XNamespace ns, XElement definitions, ref int definitionsCount)
{
XElement element = null;

Expand Down Expand Up @@ -106,7 +135,7 @@ private static XElement Generate(GraphicVisual visual, XNamespace ns, XElement d

foreach (var childVisual in group.Children)
{
var path = Generate(childVisual, ns, definitions, ref definitionsCount);
var path = GenerateXmlTree(childVisual, ns, definitions, ref definitionsCount);
element.Add(path);
}

Expand Down Expand Up @@ -192,7 +221,7 @@ private static void SetColors(GraphicPath graphicPath, XElement pathElement, XNa

if (!DoubleUtilities.IsZero(graphicPath.StrokeDashOffset))
{
pathElement.Add(new XAttribute("stroke-dashoffset", DoubleUtilities.FormatString(graphicPath.StrokeDashOffset * graphicPath.StrokeThickness)));
pathElement.Add(new XAttribute("stroke-dashoffset", DoubleUtilities.FormatString(graphicPath.StrokeDashOffset * graphicPath.StrokeThickness)));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Shape Converter/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.26.0.0")]
[assembly: AssemblyFileVersion("2.26.0.0")]
[assembly: AssemblyVersion("2.27.0.0")]
[assembly: AssemblyFileVersion("2.27.0.0")]
6 changes: 5 additions & 1 deletion Shape Converter/Shell/Exporter/VisualExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void Export(GraphicVisual visual, int width, bool addMargin, out s
{
message = string.Empty;
var saveDialog = new Microsoft.Win32.SaveFileDialog();
saveDialog.Filter = "ICO File (*.ico)|*.ico|SVG File (*.svg)|*.svg|PNG File (*.png)|*.png|JPG File (*.jpg)|*.jpg|TIFF File (*.tiff)|*.tiff|BMP File (*.bmp)|*.bmp|GIF File (*.gif)|*.gif|EPS File (*.eps)|*.eps";
saveDialog.Filter = "ICO File (*.ico)|*.ico|SVG File (*.svg)|*.svg|SVGZ File (*.svgz)|*.svgz|PNG File (*.png)|*.png|JPG File (*.jpg)|*.jpg|TIFF File (*.tiff)|*.tiff|BMP File (*.bmp)|*.bmp|GIF File (*.gif)|*.gif|EPS File (*.eps)|*.eps";

var result = saveDialog.ShowDialog();

Expand All @@ -53,6 +53,10 @@ public static void Export(GraphicVisual visual, int width, bool addMargin, out s
SvgExporter.ExportSvg(visual, width, saveDialog.FileName);
break;

case ".svgz":
SvgExporter.ExportSvgz(visual, width, saveDialog.FileName);
break;

case ".ico":
IcoExporter.ExportIco(visual, saveDialog.FileName);
break;
Expand Down

0 comments on commit a8c9500

Please sign in to comment.