Skip to content

Commit c7bc39e

Browse files
committed
Bug fixes and code improvements
- Resolved transform parsing issue #14 - Resolved CSS parser key duplication issue
1 parent c693baf commit c7bc39e

File tree

5 files changed

+76
-6
lines changed

5 files changed

+76
-6
lines changed

Source/SVGImage/SVG/CSS/StyleParser.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ public static class StyleParser
1010

1111
public static void ParseStyle(SVG svg, string style)
1212
{
13+
var svgStyles = svg.Styles;
14+
1315
var match = _regexStyle.Match(style);
1416
while (match.Success)
1517
{
16-
var name = match.Groups[1].Value.Trim();
18+
var name = match.Groups[1].Value.Trim();
1719
var value = match.Groups[2].Value.Trim();
1820
foreach (var nm in name.Split(','))
1921
{
20-
svg.m_styles.Add(nm, new List<XmlUtil.StyleItem>());
22+
if (!svgStyles.ContainsKey(nm))
23+
{
24+
svgStyles.Add(nm, new List<XmlUtil.StyleItem>());
25+
}
2126
foreach (ShapeUtil.Attribute styleitem in XmlUtil.SplitStyle(svg, value))
2227
{
23-
svg.m_styles[nm].Add(new XmlUtil.StyleItem(styleitem.Name, styleitem.Value));
28+
svgStyles[nm].Add(new XmlUtil.StyleItem(styleitem.Name, styleitem.Value));
2429
}
2530
}
2631

Source/SVGImage/SVG/SVG.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.IO.Compression;
34
using System.Xml;
45
using System.Linq;
56
using System.Text;
@@ -91,6 +92,13 @@ public Dictionary<string, Brush> CustomBrushes
9192
}
9293
}
9394

95+
internal IDictionary<string, List<XmlUtil.StyleItem>> Styles
96+
{
97+
get {
98+
return m_styles;
99+
}
100+
}
101+
94102
public void AddShape(string id, Shape shape)
95103
{
96104
//System.Diagnostics.Debug.Assert(id.Length > 0 && this.m_shapes.ContainsKey(id) == false);
@@ -147,6 +155,24 @@ public void Load(string filePath)
147155
}
148156
this.Filename = filePath;
149157

158+
// Provide a support for the .svgz files...
159+
UriBuilder fileUrl = new UriBuilder(filePath);
160+
if (string.Equals(fileUrl.Scheme, "file"))
161+
{
162+
string fileExt = Path.GetExtension(filePath);
163+
if (string.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase))
164+
{
165+
using (var fileStream = File.OpenRead(fileUrl.Uri.LocalPath))
166+
{
167+
using (var zipStream = new GZipStream(fileStream, CompressionMode.Decompress))
168+
{
169+
this.Load(zipStream);
170+
}
171+
}
172+
return;
173+
}
174+
}
175+
150176
XmlDocument doc = new XmlDocument();
151177
doc.XmlResolver = null;
152178
doc.Load(filePath);
@@ -156,7 +182,12 @@ public void Load(string filePath)
156182

157183
public void Load(Uri fileUri)
158184
{
159-
var filePath = fileUri.IsFile ? fileUri.LocalPath : fileUri.ToString();
185+
if (fileUri == null || !fileUri.IsAbsoluteUri)
186+
{
187+
return;
188+
}
189+
190+
var filePath = fileUri.IsFile ? fileUri.LocalPath : fileUri.AbsoluteUri;
160191

161192
if (string.IsNullOrWhiteSpace(filePath))
162193
{

Source/SVGImage/SVG/ShapeUtil.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,26 @@ public static Transform ParseTransform(string value)
2020
foreach (var transform in transforms.OrderBy(x => x.StartsWith(SVGTags.sTranslate))) // to check why ordering is needed (see acid.svg)
2121
{
2222
if (!string.IsNullOrEmpty(transform))
23-
tg.Children.Add(ParseTransformInternal(transform + ")"));
23+
{
24+
var transObj = ParseTransformInternal(transform + ")");
25+
if (transObj != null)
26+
{
27+
tg.Children.Add(transObj);
28+
}
29+
}
2430
}
2531
return tg;
2632
}
2733

2834
private static Transform ParseTransformInternal(string value)
2935
{
30-
string type = ExtractUntil(value, '(').TrimStart(',');
36+
if (string.IsNullOrWhiteSpace(value))
37+
{
38+
return null;
39+
}
40+
value = value.Trim();
41+
42+
string type = ExtractUntil(value, '(').TrimStart(',');
3143
string v1 = ExtractBetween(value, '(', ')');
3244

3345
ShapeUtil.StringSplitter split = new ShapeUtil.StringSplitter(v1);

Tests/Issues/css-parsing-issue.svg

Lines changed: 9 additions & 0 deletions
Loading

Tests/Issues/issue-14.svg

Lines changed: 13 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)