Skip to content

Commit 6dba92e

Browse files
chore: change d attr regex
1 parent a0814c1 commit 6dba92e

6 files changed

Lines changed: 120 additions & 118 deletions

File tree

FreeFrame/Components/Importer.cs

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Text.RegularExpressions;
56
using System.Threading.Tasks;
67
using System.Xml;
78
using FreeFrame.Components.Shapes;
@@ -11,93 +12,116 @@ namespace FreeFrame.Components
1112
{
1213
public static class Importer
1314
{
14-
static public (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromStream(Stream pStream)
15+
static private (List<Shape>, SortedDictionary<int, List<Shape>>, bool) ImportFromStream(Stream pStream)
1516
{
1617
List<Shape> shapes = new List<Shape>();
18+
SortedDictionary<int, List<Shape>> timeline = new SortedDictionary<int, List<Shape>>();
1719
bool compatibilityFlag = false;
1820

1921
Shape? previous = null;
2022

2123
using (XmlReader reader = XmlReader.Create(pStream))
2224
{
23-
try
25+
while (reader.Read())
2426
{
25-
while (reader.Read())
27+
if (reader.HasAttributes)
2628
{
27-
//if (reader.HasAttributes)
29+
//Console.WriteLine("Attributes of <" + reader.Name + ">");
30+
switch (reader.Name)
2831
{
29-
//Console.WriteLine("Attributes of <" + reader.Name + ">");
30-
switch (reader.Name)
31-
{
32-
case "xml":
33-
case "svg":
34-
break; // Skip knowned elements
35-
case "polygon":
36-
shapes.Add(new SVGPolygon(reader));
37-
previous = shapes.Last();
38-
break;
39-
case "path":
40-
shapes.Add(new SVGPath(reader));
41-
previous = shapes.Last();
42-
break;
43-
case "rect":
44-
shapes.Add(new SVGRectangle(reader));
45-
previous = shapes.Last();
46-
break;
47-
case "circle":
48-
shapes.Add(new SVGCircle(reader));
49-
previous = shapes.Last();
50-
break;
51-
case "line":
52-
shapes.Add(new SVGLine(reader));
53-
previous = shapes.Last();
54-
break;
55-
case "animate":
56-
if (previous != null)
32+
case "xml":
33+
case "svg":
34+
break; // Skip knowned elements
35+
case "polygon":
36+
shapes.Add(new SVGPolygon(reader));
37+
previous = shapes.Last();
38+
break;
39+
case "path":
40+
shapes.Add(new SVGPath(reader));
41+
previous = shapes.Last();
42+
break;
43+
case "rect":
44+
shapes.Add(new SVGRectangle(reader));
45+
previous = shapes.Last();
46+
break;
47+
case "circle":
48+
shapes.Add(new SVGCircle(reader));
49+
previous = shapes.Last();
50+
break;
51+
case "line":
52+
shapes.Add(new SVGLine(reader));
53+
previous = shapes.Last();
54+
break;
55+
case "animate":
56+
if (previous != null)
57+
{
58+
string attr = reader["attributeName"].ToString();
59+
int nbrKeyFrames = reader["values"].ToString().Count(c => c == ';');
60+
// regex
61+
Regex rx = new Regex(@"\d|#\d{8}");
62+
63+
MatchCollection matches = rx.Matches(reader["values"]);
64+
65+
for (int i = 0; i < nbrKeyFrames + 1; i++)
5766
{
58-
string attr = reader["attributeName"].ToString();
59-
int nbrKeyFrames = reader["values"].ToString().Count(c => c == ';');
60-
// regex
67+
int positionInTimeline = i * (Timeline.MAX_TIMELINE / (nbrKeyFrames + 1));
68+
if (timeline.ContainsKey(positionInTimeline) == false)
69+
timeline.Add(positionInTimeline, null);
70+
if (timeline[positionInTimeline] == null)
71+
timeline[positionInTimeline] = new List<Shape>();
72+
Shape shape = previous.ShallowCopy();
73+
74+
string matchResult = matches[i].Value;
6175

62-
for (int i = 0; i < nbrKeyFrames+1; i++)
63-
{
64-
int positionInTimeline = i * (Timeline.MAX_TIMELINE / (nbrKeyFrames + 1));
65-
}
6676
switch (attr)
6777
{
6878
case "rx":
69-
7079
case "ry":
80+
shape.CornerRadius = Convert.ToInt32(matchResult);
81+
break;
7182
case "width":
83+
shape.Width = Convert.ToInt32(matchResult);
84+
break;
7285
case "height":
86+
shape.Height = Convert.ToInt32(matchResult);
87+
break;
7388
case "x":
89+
shape.X = Convert.ToInt32(matchResult);
90+
break;
7491
case "y":
92+
shape.Y = Convert.ToInt32(matchResult);
93+
break;
7594
case "fill":
76-
case "color":
95+
shape.Color = HexadecimalToRGB(matchResult);
96+
break;
7797
default:
7898
break;
7999
}
80-
81-
82-
//reader["dur"];
100+
timeline[positionInTimeline].Add(shape);
83101
}
84-
break;
85-
case "animateTransform":
86-
if (reader["attributeName"].ToString() == "transform" && reader["type"].ToString() == "rotate")
87-
{
88-
// Only read first one Z from
89-
// from="z _ _"
90-
// to="z _ _"
91-
// get angle
92-
}
93-
break;
94-
default:
95-
compatibilityFlag = true; // If an element is unknow, the flag is trigger
96-
break;
97-
}
98-
}
99102

103+
104+
//reader["dur"]; //TODO: fps base on duration ?
105+
}
106+
break;
107+
case "animateTransform":
108+
if (reader["attributeName"].ToString() == "transform" && reader["type"].ToString() == "rotate")
109+
{
110+
// Only read first one Z from
111+
// from="z _ _"
112+
// to="z _ _"
113+
// get angle
114+
}
115+
break;
116+
default:
117+
compatibilityFlag = true; // If an element is unknow, the flag is trigger
118+
break;
119+
}
100120
}
121+
122+
}
123+
try //TODO: fix trycatch
124+
{
101125
}
102126
catch (Exception)
103127
{

FreeFrame/Components/Shapes/SVGPath.cs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,17 @@ namespace FreeFrame.Components.Shapes
1313
public class SVGPath : Shape
1414
{
1515

16-
readonly Dictionary<char, Regex> _dAttributesRegex = new()
16+
readonly Dictionary<char, Regex> _dAttributeRegex = new()
1717
{
18-
{ 'M', new Regex(@" *(-?\d+) *, *(-?\d+) *") },
1918
{ 'm', new Regex(@" *(-?\d+) *, *(-?\d+) *") },
20-
{ 'L', new Regex(@" *(-?\d+) *, *(-?\d+) *") },
2119
{ 'l', new Regex(@" *(-?\d+) *, *(-?\d+) *") },
22-
{ 'H', new Regex(@" *(-?\d+) *") },
2320
{ 'h', new Regex(@" *(-?\d+) *") },
24-
{ 'V', new Regex(@" *(-?\d+) *") },
2521
{ 'v', new Regex(@" *(-?\d+) *") },
26-
{ 'C', new Regex(@" *(-?\d+) *, *(-?\d+) +(-?\d+) *, *(-?\d+) +(-?\d+) *, *(-?\d+) *") },
2722
{ 'c', new Regex(@" *(-?\d+) *, *(-?\d+) +(-?\d+) *, *(-?\d+) +(-?\d+) *, *(-?\d+) *") },
28-
{ 'S', new Regex(@" *(-?\d+) *, *(-?\d+) +(-?\d+) *, *(-?\d+) *") },
2923
{ 's', new Regex(@" *(-?\d+) *, *(-?\d+) +(-?\d+) *, *(-?\d+) *") },
30-
{ 'Q', new Regex(@" *(-?\d+) *, *(-?\d+) +(-?\d+) *, *(-?\d+) *") },
3124
{ 'q', new Regex(@" *(-?\d+) *, *(-?\d+) +(-?\d+) *, *(-?\d+) *") },
32-
{ 'T', new Regex(@" *(-?\d+) *, *(-?\d+) *") },
3325
{ 't', new Regex(@" *(-?\d+) *, *(-?\d+) *") },
34-
{ 'A', new Regex(@" *(-?\d+) +(-?\d+) +(-?\d+) +(-?\d) +(-?\d) +(-?\d+) *, *(-?\d+) *") },
3526
{ 'a', new Regex(@" *(-?\d+) +(-?\d+) +(-?\d+) +(-?\d) +(-?\d) +(-?\d+) *, *(-?\d+) *") },
36-
{ 'Z', new Regex("") },
3727
{ 'z', new Regex("") },
3828
};
3929

@@ -53,50 +43,40 @@ public SVGPath(XmlReader reader) //: this()
5343
for (int i = 0; i < d.Length; i++)
5444
{
5545
char c = d[i]; // Get current char
46+
char lowerC = char.ToLower(d[i]); // Get current char
5647

57-
if (_dAttributesRegex.ContainsKey(c))
48+
if (_dAttributeRegex.ContainsKey(lowerC))
5849
{
59-
MatchCollection matches = _dAttributesRegex[c].Matches(d, startIndex); // Retrieve the associated regular expression
60-
match = _dAttributesRegex[c].Match(d, startIndex); // Retrieve the associated regular expression
61-
switch (c)
50+
match = _dAttributeRegex[lowerC].Match(d, startIndex); // Retrieve the associated regular expression
51+
switch (lowerC)
6252
{
63-
case 'M':
6453
case 'm':
6554
DrawAttributes.Add(new MoveTo(match.Groups[1], match.Groups[2], c == 'm')); // 'm' is relative and 'M' absolute
6655
break;
67-
case 'l':
6856
case 'L':
6957
DrawAttributes.Add(new LineTo(match.Groups[1], match.Groups[2], c == 'l')); // 'l' is relative and 'L' absolute
7058
break;
71-
case 'H':
7259
case 'h':
7360
DrawAttributes.Add(new HorizontalLineTo(match.Groups[1], c == 'h')); // 'h' is relative and 'H' absolute
7461
break;
75-
case 'V':
7662
case 'v':
7763
DrawAttributes.Add(new VerticalLineTo(match.Groups[1], c == 'v')); // 'v' is relative and 'V' absolute
7864
break;
79-
case 'C':
8065
case 'c':
8166
DrawAttributes.Add(new CurveTo(match.Groups[1], match.Groups[2], match.Groups[3], match.Groups[4], match.Groups[5], match.Groups[6], c == 'c')); // 'c' is relative and 'C' absolute
8267
break;
83-
case 'S':
8468
case 's':
8569
DrawAttributes.Add(new SmoothCurveTo(match.Groups[1], match.Groups[2], match.Groups[3], match.Groups[4], c == 's')); // 's' is relative and 'S' absolute
8670
break;
87-
case 'Q':
8871
case 'q':
8972
DrawAttributes.Add(new QuadraticBezierCurveTo(match.Groups[1], match.Groups[2], match.Groups[3], match.Groups[4], c == 'q')); // 'q' is relative and 'Q' absolute
9073
break;
91-
case 'T':
9274
case 't':
9375
DrawAttributes.Add(new SmoothQuadraticBezierCurveTo(match.Groups[1], match.Groups[2], c == 't')); // 't' is relative and 'T' absolute
9476
break;
95-
case 'A':
9677
case 'a':
9778
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
9879
break;
99-
case 'Z':
10080
case 'z':
10181
DrawAttributes.Add(new ClosePath());
10282
break;

FreeFrame/Components/Shapes/SVGPolygon.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace FreeFrame.Components.Shapes
1010
{
1111
public class SVGPolygon : Shape
1212
{
13-
readonly Regex _pointsRegex = new(@" *(\d+) *, *(\d+) *"); // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/points
13+
readonly Regex _pointsAttributeRegex = new(@" *(\d+) *, *(\d+) *"); // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/points
1414

1515
#region Geometry properties
1616
List<Vector2i> _points = new();
@@ -24,7 +24,7 @@ public SVGPolygon(XmlReader reader)
2424
IsCornerRadiusChangeable = false;
2525

2626
string points = reader["points"] ?? throw new Exception("points not here"); // TODO: Error handler if points is note here
27-
MatchCollection matches = _pointsRegex.Matches(points); // Retrieve every points
27+
MatchCollection matches = _pointsAttributeRegex.Matches(points); // Retrieve every points
2828

2929
foreach (Match match in matches)
3030
_points.Add((Convert.ToInt32(match.Groups[1].Value), Convert.ToInt32(match.Groups[2].Value)));
@@ -69,13 +69,11 @@ public override List<Vector2i> GetSelectablePoints()
6969
List<Vector2i> selectablePoints = new List<Vector2i>();
7070
if (_points.Count == 3) // Because a triangle is also base on the size
7171
{
72-
selectablePoints.Add(new Vector2i(X + Width / 2, Y));
73-
selectablePoints.Add(new Vector2i(X, Y + Height));
74-
selectablePoints.Add(new Vector2i(X + Width, Y + Height));
72+
selectablePoints.Add(new Vector2i(X, Y));
73+
selectablePoints.Add(new Vector2i(X + Width, Y));
7574
}
76-
else
77-
foreach (Vector2i point in _points)
78-
selectablePoints.Add(new Vector2i(X + point.X, Y + point.Y));
75+
foreach (Vector2i point in _points)
76+
selectablePoints.Add(new Vector2i(X + point.X, Y + point.Y));
7977
return selectablePoints;
8078
}
8179

FreeFrame/IDrawable.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace FreeFrame
99
{
1010
public interface IDrawable
1111
{
12-
List<Renderer> Vaos
13-
{
14-
get;
15-
}
12+
//List<Renderer> Vaos
13+
//{
14+
// get;
15+
//}
1616
/// <summary>
1717
/// Trigge draw element through OpenGL context
1818
/// </summary>

FreeFrame/Selector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public enum SelectorType
3838

3939
private List<(Renderer vao, Area hitbox, SelectorType type)> _vaos; // TODO: Just have a List<Renderer> like Shape in order to put it in the interface
4040

41-
public List<Renderer> Vaos { get => null; set => _ = value; } // TODO: Just have a List<Renderer> like Shape in order to put it in the interface
41+
//public List<Renderer> Vaos { get => null; set => _ = value; } // TODO: Just have a List<Renderer> like Shape in order to put it in the interface
4242

4343
public Selector()
4444
{

0 commit comments

Comments
 (0)