Skip to content

Commit 29e6fc5

Browse files
feat: <polygon> readable
1 parent c89a9cc commit 29e6fc5

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ static public List<Shape> ImportFromStream(Stream pStream)
3333
case "xml":
3434
case "svg":
3535
break; // Skip knowned elements
36+
case "polygon":
37+
shapes.Add(new SVGPolygon(reader));
38+
break;
3639
case "path":
3740
shapes.Add(new SVGPath(reader));
3841
break;
@@ -54,7 +57,7 @@ static public List<Shape> ImportFromStream(Stream pStream)
5457
static public List<Shape> ImportFromFile(string pFilename)
5558
{
5659
if (!File.Exists(pFilename))
57-
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); //TODO: replace by a simple alert window
60+
throw new ArgumentException($"'{pFilename}' file cannot be found.", nameof(pFilename)); // TODO: replace by a simple alert window
5861

5962
byte[] byteArray = Encoding.UTF8.GetBytes(File.ReadAllText(pFilename));
6063

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public SVGPath(XmlReader reader) //: this()
100100
}
101101
public override string ToString()
102102
{
103-
string output = "";
103+
string output = "d: ";
104104
_drawAttribute.ForEach(d => output += d.ToString() + " ");
105105
return output.Trim();
106106
}
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.Xml;
5+
using System.Text.RegularExpressions;
46

57
namespace FreeFrame.Components.Shapes
68
{
79
public class SVGPolygon : Shape
810
{
11+
readonly Regex _pointsRegex = new(@" *(\d+) *, *(\d+) *"); // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/points
12+
913
#region Geometry properties
10-
List<float> points = new List<float>();
14+
List<(int, int)> _points = new();
1115
#endregion
12-
public SVGPolygon()
16+
17+
// TODO: Also add Polyline https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/points
18+
19+
public SVGPolygon(XmlReader reader)
1320
{
14-
// TODO: Finish this class
21+
string points = reader["points"] ?? throw new Exception("points not here"); // TODO: Error handler if points is note here
22+
MatchCollection matches = _pointsRegex.Matches(points); // Retrieve every points
23+
24+
foreach (Match match in matches)
25+
_points.Add((Convert.ToInt32(match.Groups[1].Value), Convert.ToInt32(match.Groups[2].Value)));
1526
}
1627

1728
public override string ToString()
1829
{
19-
throw new NotImplementedException();
30+
string output = "points: ";
31+
foreach ((int x, int y) point in _points)
32+
output += string.Format("{0},{1} ", point.x, point.y);
33+
return output.Trim();
2034
}
2135
}
2236
}

FreeFrameTests/ImporterTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,20 @@ public void ImportFromStream_Test_Path()
3434
List<Shape> result = Importer.ImportFromString(svgString);
3535

3636
// Assert
37-
Assert.AreEqual("M 20,0 C 0,100 200,100 180,0 m 50,10 L 10,21 H 10 V 10 s 20,20 10,10 Q 10,10 0,0 t 10,10 A 100 100 60 1 0 20,20 z", ((SVGPath)result[0]).ToString());
37+
Assert.AreEqual("d: M 20,0 C 0,100 200,100 180,0 m 50,10 L 10,21 H 10 V 10 s 20,20 10,10 Q 10,10 0,0 t 10,10 A 100 100 60 1 0 20,20 z", ((SVGPath)result[0]).ToString());
38+
}
39+
[TestMethod]
40+
public void ImportFromStream_Test_Polygon()
41+
{
42+
// Arrange
43+
string svgString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
44+
"<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1000\" height=\"1000\">" +
45+
"<polygon points=\"50,0 21,90 98,35 2,35 79,90\" stroke=\"black\"/>" +
46+
"</svg>";
47+
List<Shape> result = Importer.ImportFromString(svgString);
48+
49+
// Assert
50+
Assert.AreEqual("points: 50,0 21,90 98,35 2,35 79,90", ((SVGPolygon)result[0]).ToString());
3851
}
3952
[TestMethod]
4053
public void ImportFromStream_Test_Rectangle()

0 commit comments

Comments
 (0)