Skip to content

Commit 8a6c0b2

Browse files
refactor: path structure
1 parent 860a7d2 commit 8a6c0b2

11 files changed

Lines changed: 200 additions & 161 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ static private (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFro
2323
using (XmlReader reader = XmlReader.Create(pStream))
2424
{
2525

26-
try
27-
{
26+
2827
while (reader.Read())
2928
{
3029
if (reader.HasAttributes)
@@ -62,6 +61,9 @@ static private (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFro
6261
}
6362

6463
}
64+
// TODO: Fix trycatch
65+
try
66+
{
6567
}
6668
catch (Exception)
6769
{

FreeFrame/Components/Shapes/Path/DrawAttribute.cs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ public abstract class DrawAttribute
2121

2222
private bool _isRelative;
2323
public bool IsRelative { get => _isRelative; set => _isRelative = value; }
24+
25+
// Current X1,Y1 point
2426
public int Y1 { get => _y1; set => _y1 = value; }
2527
public int X1 { get => _x1; set => _x1 = value; }
28+
29+
// Current X,Y point
2630
public int X { get => _x; set => _x = value; }
2731
public int Y { get => _y; set => _y = value; }
2832

29-
33+
// Last drawn values
3034
public static (int X, int Y, int X1, int Y1) Last = (0, 0, 0, 0);
3135

3236
/// <summary>
@@ -950,46 +954,6 @@ public override (Vector2i position, Vector2i? controlPosition) Information()
950954
}
951955
public override string ToString() => String.Format("{0} {1} {2} {3} {4} {5} {6},{7}", IsRelative ? 'a' : 'A', _rx, _ry, _angle, Convert.ToInt32(_largeArcFlag), Convert.ToInt32(_sweepFlag), X, Y);
952956

953-
public override void UpdateLast()
954-
{
955-
throw new NotImplementedException();
956-
}
957-
}
958-
/// <summary>
959-
/// ClosePath, Z or z.
960-
/// Use to draw a straight line from the current position to the first point in the path.
961-
/// More info: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#closepath
962-
/// </summary>
963-
public class ClosePath : DrawAttribute
964-
{
965-
public ClosePath() { }
966-
/// <summary>
967-
/// Draw a straight line from the current position to the first point in the path.
968-
/// </summary>
969-
public override List<Vector2i> GetSelectablePoints()
970-
{
971-
throw new NotImplementedException();
972-
}
973-
974-
public override float[] GetVertices()
975-
{
976-
throw new NotImplementedException();
977-
}
978-
public override uint[] GetVerticesIndexes()
979-
{
980-
throw new NotImplementedException();
981-
}
982-
983-
public override void MoveDelta(Vector2i deltaPosition)
984-
{
985-
throw new NotImplementedException();
986-
}
987-
public override (Vector2i position, Vector2i? controlPosition) Information()
988-
{
989-
throw new NotImplementedException();
990-
}
991-
public override string ToString() => "z";
992-
993957
public override void UpdateLast()
994958
{
995959
throw new NotImplementedException();

FreeFrame/Components/Shapes/SVGCircle.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ namespace FreeFrame.Components.Shapes
1010
{
1111
public class SVGCircle : Shape
1212
{
13+
#region Default values
14+
const int DefaultR = 0;
15+
const int DefaultCY = 0;
16+
const int DefaultCX = 0;
17+
const string DefaultColor = "#000000FF";
18+
#endregion
1319
public SVGCircle(XmlReader reader) : this(
1420
Convert.ToInt32(reader["r"]),
1521
Convert.ToInt32(reader["cx"]),
1622
Convert.ToInt32(reader["cy"]),
17-
Convert.ToString(reader["fill"])) // TODO: Error handler if r, cx or cy are not here
23+
Convert.ToString(reader["fill"] == null ? DefaultColor : reader["fill"]))
1824
{ }
19-
public SVGCircle() : this(0, 0, 0, "#000000FF") { }
25+
public SVGCircle() : this(DefaultR, DefaultCX, DefaultCY, "#000000FF") { }
2026
public SVGCircle(int r, int cx, int cy, string color)
2127
{
2228
IsCornerRadiusChangeable = false;

FreeFrame/Components/Shapes/SVGLine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public SVGLine(XmlReader reader) : this(
2424
Convert.ToInt32(reader["y1"]),
2525
Convert.ToInt32(reader["x2"]),
2626
Convert.ToInt32(reader["y2"]),
27-
Convert.ToString(reader["fill"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
27+
Convert.ToString(reader["fill"] == null ? DefaultColor : reader["fill"]))
2828
{
2929
}
3030
public SVGLine() : this(DefaultX1, DefaultY1, DefaultX2, DefaultY2, DefaultColor) { }

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ namespace FreeFrame.Components.Shapes
1212
{
1313
public class SVGPath : Shape
1414
{
15+
#region Default values
16+
const string DefaultColor = "#000000FF";
17+
#endregion
1518

19+
// First point x,y of the path
20+
int _x0, _y0 = 0;
1621
readonly Dictionary<char, Regex> _dAttributeRegex = new()
1722
{
1823
{ 'm', new Regex(@" *(-?\d+) *, *(-?\d+) *") },
@@ -48,6 +53,13 @@ public SVGPath(XmlReader reader)
4853
if (_dAttributeRegex.ContainsKey(lowerC))
4954
{
5055
match = _dAttributeRegex[lowerC].Match(d, startIndex); // Retrieve the associated regular expression
56+
57+
if (startIndex == 0)
58+
{
59+
_x0 = Convert.ToInt32(match.Groups[1].Value);
60+
_y0 = Convert.ToInt32(match.Groups[2].Value);
61+
}
62+
5163
switch (lowerC)
5264
{
5365
case 'm':
@@ -78,7 +90,7 @@ public SVGPath(XmlReader reader)
7890
DrawAttributes.Add(new EllipticalArc(match.Groups[1], match.Groups[2], match.Groups[3], match.Groups[4], match.Groups[5], match.Groups[6], match.Groups[7], c == 'a')); // 'a' is relative and 'A' absolute
7991
break;
8092
case 'z':
81-
DrawAttributes.Add(new ClosePath());
93+
DrawAttributes.Add(new LineTo(_x0, _y0));
8294
break;
8395
default:
8496
throw new Exception("Unknowed properties in d");
@@ -87,7 +99,7 @@ public SVGPath(XmlReader reader)
8799
}
88100
}
89101

90-
string color = reader["fill"] ?? throw new Exception("color not here");
102+
string color = reader["fill"] ?? DefaultColor;
91103
Color = Importer.HexadecimalToRGB(color);
92104

93105

FreeFrame/Components/Shapes/SVGPolygon.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ namespace FreeFrame.Components.Shapes
1010
{
1111
public class SVGPolygon : Shape
1212
{
13+
#region Default values
14+
const string DefaultColor = "#000000FF";
15+
#endregion
16+
1317
readonly Regex _pointsAttributeRegex = new(@" *(\d+) *, *(\d+) *"); // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/points
1418

1519
#region Geometry properties
@@ -18,8 +22,6 @@ public class SVGPolygon : Shape
1822
public List<Vector2i> Points { get => _points; set => _points = value; }
1923
#endregion
2024

21-
// TODO: Also add Polyline https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/points
22-
2325
public SVGPolygon() { }
2426
public SVGPolygon(XmlReader reader)
2527
{
@@ -32,7 +34,7 @@ public SVGPolygon(XmlReader reader)
3234
foreach (Match match in matches)
3335
Points.Add((Convert.ToInt32(match.Groups[1].Value), Convert.ToInt32(match.Groups[2].Value)));
3436

35-
string color = reader["fill"] ?? throw new Exception("color not here"); // TODO: Error handler if d is note here
37+
string color = reader["fill"] ?? DefaultColor; // TODO: Error handler if d is note here
3638
Color = Importer.HexadecimalToRGB(color);
3739

3840
// Fill geometry values

FreeFrame/Components/Shapes/SVGRectangle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public SVGRectangle(XmlReader reader) : this(
2525
Convert.ToInt32(reader["x"]),
2626
Convert.ToInt32(reader["y"]),
2727
Convert.ToInt32(reader["rx"]),
28-
Convert.ToInt32(reader["ry"]),
29-
Convert.ToString(reader["fill"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
28+
Convert.ToInt32(reader["ry"]),
29+
Convert.ToString(reader["fill"] == null ? DefaultColor : reader["fill"])) // TODO: Error handler if one of the properties in reader is note here, it should be dynamic
3030
{
3131
}
3232
public SVGRectangle() : this(DefaultWidth, DefaultHeight, DefaultX, DefaultY) { }

0 commit comments

Comments
 (0)