forked from yergacheffe/lasercam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVG.cs
1275 lines (1109 loc) · 46.5 KB
/
SVG.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2010 Chris Yerga
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
namespace FrickenLaser.SVG
{
/// <summary>
/// Interface for a drawable object found in an SVG file. This
/// is not a general-purpose SVG library -- it only does the
/// bare minimum necessary to drive a CNC machine so this is
/// mostly just to handle vectors and paths.
/// </summary>
public interface ISVGElement
{
// Retrieve the list of contours for this shape
List<List<PointF>> GetContours();
// System.Drawing Path
GraphicsPath GetPath();
// Fill and outline
double OutlineWidth { get; }
Color OutlineColor { get; }
Color FillColor { get; }
}
/// <summary>
/// A contour is a set of points describing a closed polygon.
/// </summary>
public class VectorContour
{
public double Brightness { get; set; }
public Color Color { get; set; }
public IEnumerable<PointF> Points { get; set; }
}
/// <summary>
/// Base class for shapes found in SVG file. This handles common tasks
/// such as parsing styles, applying transforms, etc.
/// </summary>
public class SVGShapeBase
{
/// <summary>
/// Constructor for SVGShapeBase class. Called from derived class
/// constructor.
/// </summary>
/// <param name="reader">XmlTextReader positioned at the XML element
/// for the shape being constructed. This class uses it to look
/// for style/transform attributes to apply to the shape</param>
/// <param name="styleDictionary">Dictionary of named styles
/// defined earlier in the SVG document, to be used should an
/// XML style attribute with a name be encountered.</param>
public SVGShapeBase(XmlTextReader reader, Dictionary<string, SVGStyle> styleDictionary)
{
string styleText = reader.GetAttribute("class");
if (styleText != null)
{
string[] styleNames = styleText.Split(new char[] { ' ', '\t' });
foreach (string styleName in styleNames)
{
SVGStyle style = styleDictionary[styleName];
if (style.FillColorPresent)
{
FillColor = style.FillColor;
}
if (style.OutlineColorPresent)
{
OutlineColor = style.OutlineColor;
}
if (style.OutlineWidthPresent)
{
OutlineWidth = style.OutlineWidth;
}
}
}
string xfs = reader.GetAttribute("transform");
if (xfs != null)
{
if (xfs.StartsWith("matrix"))
{
xfs = xfs.Substring(6);
}
xfs = xfs.Trim(new char[] { '(', ')' });
string[] elements = xfs.Split(new char[] { ' ', '\t' });
matrix = new Matrix(
float.Parse(elements[0]),
float.Parse(elements[1]),
float.Parse(elements[2]),
float.Parse(elements[3]),
float.Parse(elements[4]),
float.Parse(elements[5]));
}
}
/// <summary>
/// Transform the geometry of this shape as appropriate
/// </summary>
/// <param name="points"></param>
/// <returns></returns>
public List<PointF> Transform(List<PointF> points)
{
PointF[] pts = points.ToArray();
matrix.TransformPoints(pts);
return new List<PointF>(pts);
}
internal Matrix matrix = new Matrix();
internal GraphicsPath _path;
public GraphicsPath GetPath() { return _path; }
public double OutlineWidth { get; set; }
public Color OutlineColor { get; set; }
public Color FillColor { get; set; }
}
/// <summary>
/// SVG Rectangle
/// </summary>
public class SVGRect : SVGShapeBase, ISVGElement
{
private List<PointF> points = new List<PointF>();
public SVGRect(XmlTextReader reader, Dictionary<string, SVGStyle> styleDictionary)
: base(reader, styleDictionary)
{
float x = 0;
try
{
x = float.Parse(reader.GetAttribute("x"));
}
catch (ArgumentNullException) { }
float y = 0;
try
{
y = float.Parse(reader.GetAttribute("y"));
}
catch (ArgumentNullException) { }
float w = float.Parse(reader.GetAttribute("width"));
float h = float.Parse(reader.GetAttribute("height"));
points.Add(new PointF(x, y));
points.Add(new PointF(x + w, y));
points.Add(new PointF(x + w, y + h));
points.Add(new PointF(x, y + h));
points.Add(new PointF(x, y));
points = Transform(points);
_path = new GraphicsPath();
_path.AddPolygon(points.ToArray());
}
public List<List<PointF>> GetContours()
{
var result = new List<List<PointF>>();
result.Add(points);
return result;
}
}
/// <summary>
/// SVG Image. This is handled differently from the rest of the shapes
/// as it cannot be represented as vector contours. It loads the bitmap
/// and elsewhere encapsulation is broken willy-nilly and the client
/// code reaches in and grabs said bits. If you don't like it, go on
/// the internet and complain.
/// </summary>
public class SVGImage : SVGShapeBase, ISVGElement
{
float x = 0;
float y = 0;
float width, height;
public Image bits;
public RectangleF DestBounds { get; set; }
public SVGImage(XmlTextReader reader, Dictionary<string, SVGStyle> styleDictionary, string baseDocPath)
: base(reader, styleDictionary)
{
try
{
x = float.Parse(reader.GetAttribute("x"));
}
catch { }
try
{
y = float.Parse(reader.GetAttribute("y"));
}
catch { }
width = float.Parse(reader.GetAttribute("width"));
height = float.Parse(reader.GetAttribute("height"));
string path = reader.GetAttribute("xlink:href");
string dir = Path.GetDirectoryName(baseDocPath);
string bitspath = Path.Combine(dir, path);
bits = Image.FromFile(bitspath);
PointF[] pts = new PointF[2];
pts[0].X = x;
pts[0].Y = y;
pts[1].X = x+width;
pts[1].Y = y+height;
matrix.TransformPoints(pts);
DestBounds = new RectangleF(pts[0].X, pts[0].Y, pts[1].X - pts[0].X, pts[1].Y - pts[0].Y);
}
public List<List<PointF>> GetContours()
{
var result = new List<List<PointF>>();
return result;
}
}
/// <summary>
/// SVG Circle. We like polygons, so we just turn it into one of them.
/// </summary>
public class SVGCircle : SVGShapeBase, ISVGElement
{
private List<PointF> points = new List<PointF>();
public SVGCircle(XmlTextReader reader, Dictionary<string, SVGStyle> styleDictionary)
: base(reader, styleDictionary)
{
float cx = 0;
try
{
cx = float.Parse(reader.GetAttribute("cx"));
}
catch { }
float cy = 0;
try
{
cy = float.Parse(reader.GetAttribute("cy"));
}
catch { }
float r = float.Parse(reader.GetAttribute("r"));
for (double theta = 0.0; theta < 2.0*Math.PI; theta += Math.PI / 50.0)
{
double x = Math.Sin(theta) * r + cx;
double y = Math.Cos(theta) * r + cy;
points.Add(new PointF((float)x, (float)y));
}
points = Transform(points);
_path = new GraphicsPath();
_path.AddPolygon(points.ToArray());
}
public List<List<PointF>> GetContours()
{
var result = new List<List<PointF>>();
result.Add(points);
return result;
}
}
/// <summary>
/// SVG polygon. This maps directly to our canonical representation,
/// so nothing fancy going on in here.
/// </summary>
public class SVGPolygon : SVGShapeBase, ISVGElement
{
private List<List<PointF>> contours = new List<List<PointF>>();
private List<PointF> currentContour = new List<PointF>();
public SVGPolygon(XmlTextReader reader, Dictionary<string, SVGStyle> styleDictionary)
: base(reader, styleDictionary)
{
string data = reader.GetAttribute("points");
string[] textPoints = data.Split(new char[] { ' ', '\t' });
foreach (string textPoint in textPoints)
{
string[] ordinates = textPoint.Split(new char[] { ',' });
if (ordinates.Length > 1)
{
currentContour.Add(new PointF(float.Parse(ordinates[0]), float.Parse(ordinates[1])));
}
}
if (currentContour.Count > 2)
{
float deltaX = currentContour[0].X - currentContour[currentContour.Count - 1].X;
float deltaY = currentContour[0].Y - currentContour[currentContour.Count - 1].Y;
if (Math.Abs(deltaX) + Math.Abs(deltaY) > 0.001)
{
currentContour.Add(currentContour[0]);
}
}
currentContour = Transform(currentContour);
contours.Add(currentContour);
_path = new GraphicsPath();
if (currentContour.Count > 2)
{
_path.AddPolygon(currentContour.ToArray());
}
}
public List<List<PointF>> GetContours()
{
return contours;
}
}
/// <summary>
/// SVG path. The XML mini-language is full-featured and complex, making
/// the parsing here the bulk of the work. Also, for curved portions of
/// the path we approximate with polygons.
/// </summary>
public class SVGPath : SVGShapeBase, ISVGElement
{
private List<List<PointF>> contours = new List<List<PointF>>();
private List<PointF> currentContour = new List<PointF>();
enum ParseState
{
None,
MoveToAbs,
MoveToRel,
CurveToAbs,
CurveToRel,
LineToAbs,
LineToRel
};
ParseState state = ParseState.None;
public float GetFloat(string data, ref int index)
{
StringBuilder builder = new StringBuilder();
bool isnum = true;
while (isnum)
{
if (index >= data.Length)
{
isnum = false;
}
else
{
switch (data[index])
{
case '0': break;
case '1': break;
case '2': break;
case '3': break;
case '4': break;
case '5': break;
case '6': break;
case '7': break;
case '8': break;
case '9': break;
case '-': break;
case '.': break;
case 'e': break;
case '+': break;
default: isnum = false; break;
}
}
if (isnum)
{
builder.Append(data[index]);
++index;
}
}
return float.Parse(builder.ToString());
}
public SVGPath(XmlTextReader reader, Dictionary<string, SVGStyle> styleDictionary)
: base(reader, styleDictionary)
{
_path = new GraphicsPath();
string data = reader.GetAttribute("d");
if (data == null)
{
return;
}
int index = 0;
bool done = false;
while (index < data.Length)
{
SkipSpace(data, ref index);
switch (data[index])
{
case 'M': state = ParseState.MoveToAbs; ++index; break;
case 'm': state = ParseState.MoveToRel; ++index; break;
case 'c': state = ParseState.CurveToRel; ++index; break;
case 'l': state = ParseState.LineToRel; ++index; break;
case 'L': state = ParseState.LineToAbs; ++index; break;
case 'z': state = ParseState.None; ++index;
// Close current contour and open a new one
currentContour.Add(currentContour.First());
currentContour = Transform(currentContour);
contours.Add(currentContour);
_path.AddPolygon(currentContour.ToArray());
currentContour = new List<PointF>();
continue;
case '0': break;
case '1': break;
case '2': break;
case '3': break;
case '4': break;
case '5': break;
case '6': break;
case '7': break;
case '8': break;
case '9': break;
case '-': break;
case ' ': break;
default: throw new ApplicationException(string.Format("Unexpected input {0}", data[index]));
}
if (done)
{
break;
}
SkipSpace(data, ref index);
if (state == ParseState.MoveToAbs)
{
float x = GetFloat(data, ref index);
SkipSpaceOrComma(data, ref index);
float y = GetFloat(data, ref index);
currentContour.Add(new PointF(x, y));
}
else if (state == ParseState.MoveToRel)
{
float x = GetFloat(data, ref index);
SkipSpaceOrComma(data, ref index);
float y = GetFloat(data, ref index);
currentContour.Add(new PointF(PreviousPoint().X + x, PreviousPoint().Y + y));
}
else if (state == ParseState.LineToRel)
{
float x = GetFloat(data, ref index);
SkipSpaceOrComma(data, ref index);
float y = GetFloat(data, ref index);
currentContour.Add(new PointF(PreviousPoint().X + x, PreviousPoint().Y + y));
}
else if (state == ParseState.LineToAbs)
{
float x = GetFloat(data, ref index);
SkipSpaceOrComma(data, ref index);
float y = GetFloat(data, ref index);
currentContour.Add(new PointF(x, y));
}
else if (state == ParseState.CurveToRel)
{
float cx1 = GetFloat(data, ref index);
if (data[index] != ',')
{
throw new ApplicationException("Expected comma");
}
++index;
float cy1 = GetFloat(data, ref index);
if (data[index] != ' ')
{
throw new ApplicationException("Expected space");
}
++index;
float cx2 = GetFloat(data, ref index);
if (data[index] != ',')
{
throw new ApplicationException("Expected comma");
}
++index;
float cy2 = GetFloat(data, ref index);
if (data[index] != ' ')
{
throw new ApplicationException("Expected space");
}
++index;
float x = GetFloat(data, ref index);
if (data[index] != ',')
{
throw new ApplicationException("Expected comma");
}
++index;
float y = GetFloat(data, ref index);
float lx = PreviousPoint().X;
float ly = PreviousPoint().Y;
AddBezierPoints(lx, ly, lx + cx1, ly + cy1, lx + cx2, ly + cy2, lx + x, ly + y);
}
}
if (currentContour.Count > 0)
{
if (currentContour.Count <= 2)
{
// Happens sometimes. This is either a point or
// a line. Empty area, so just toss it.
}
else
{
currentContour.Add(currentContour.First());
currentContour = Transform(currentContour);
contours.Add(currentContour);
_path.AddPolygon(currentContour.ToArray());
}
}
}
private PointF PreviousPoint()
{
if (currentContour.Count > 0)
{
return currentContour.Last();
}
if (contours.Count > 0)
{
return contours.Last().Last();
}
return new PointF(0, 0);
}
private void AddBezierPoints(float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x3, float y3)
{
List<PointF> pointList = new List<PointF>();
// First subdivide the Bezier into 250 line segments. This number is fairly arbitrary
// and anything we pick is wrong because for small curves you'll have multiple segments
// smaller than a pixel and for huge curves no number is large enough. We pick something
// fairly big and then the polygon gets thinned in two separate stages afterwards. Many
// of these get reduced to just a handful of vertices by the time we emit GCode.
pointList.Add(new PointF(x1, y1));
float stepDelta = 1.0f / 250.0f;
for (float t = stepDelta; t < 1.0f; t += stepDelta) // Parametric value
{
float fW = 1 - t;
float fA = fW * fW * fW;
float fB = 3 * t * fW * fW;
float fC = 3 * t * t * fW;
float fD = t * t * t;
float fX = fA * x1 + fB * cx1 + fC * cx2 + fD * x3;
float fY = fA * y1 + fB * cy1 + fC * cy2 + fD * y3;
pointList.Add(new PointF(fX, fY));
}
pointList.Add(new PointF(x3, y3));
// Next thin the points based on a flatness test.
bool done = true;
do
{
done = true;
int pointIndex = 0;
do
{
PointF p1 = pointList[pointIndex];
PointF p2 = pointList[pointIndex + 1];
PointF p3 = pointList[pointIndex + 2];
PointF pb = new PointF((p1.X + p3.X) / 2, (p1.Y + p3.Y) / 2);
double err = Math.Sqrt(Math.Abs(p2.X - pb.X) * Math.Abs(p2.X - pb.X) +
Math.Abs(p2.Y - pb.Y) * Math.Abs(p2.Y - pb.Y));
double dist = Math.Sqrt(Math.Abs(p3.X - p1.X) * Math.Abs(p3.X - p1.X) +
Math.Abs(p3.Y - p1.Y) * Math.Abs(p3.Y - p1.Y));
double relativeErr = err / dist;
// If the subdivided portion is within a pixel at 1000dpi
// then it's flat enough to remove the intermediate vertex.
if (relativeErr < 0.001)
{
pointList.RemoveAt(pointIndex + 1);
done = false;
}
++pointIndex;
} while (pointIndex < pointList.Count - 2);
} while (!done);
foreach (PointF point in pointList)
{
currentContour.Add(point);
}
}
public void SkipSpace(string data, ref int index)
{
while (data[index] < 33)
{
++index;
}
}
public void SkipSpaceOrComma(string data, ref int index)
{
while (data[index] < 33 || data[index] == ',')
{
++index;
}
}
public List<List<PointF>> GetContours()
{
return contours;
}
}
/// <summary>
/// Styles contain colors, stroke widths, etc. We use them to differentiate
/// vector vs. raster portions of the document.
/// </summary>
public class SVGStyle
{
public string Name { get; set; }
public bool OutlineWidthPresent { get; set; }
public double OutlineWidth { get; set; }
public bool OutlineColorPresent { get; set; }
public Color OutlineColor { get; set; }
public bool FillColorPresent { get; set; }
public Color FillColor { get; set; }
static public Color ParseColor(string c)
{
Color result;
if ( c.Length == 7 && c[0] == '#' )
{
string s1 = c.Substring(1, 2);
string s2 = c.Substring(3, 2);
string s3 = c.Substring(5, 2);
byte r = 0;
byte g = 0;
byte b = 0;
try
{
r = Convert.ToByte(s1, 16);
g = Convert.ToByte(s2, 16);
b = Convert.ToByte(s3, 16);
}
catch
{
}
result = Color.FromArgb(r, g, b);
}
else
{
result = Color.FromName(c);
}
return result;
}
public SVGStyle(string name, string style)
{
Name = name;
OutlineColorPresent = false;
OutlineWidthPresent = false;
FillColorPresent = false;
style = style.Trim(new char[] { '{', '}' });
string[] stylePairs = style.Split(new char[] { ':', ';' });
if ((stylePairs.Count() & 1) != 0)
{
throw new ArgumentException("Failed to parse style");
}
for (int index=0; index<stylePairs.Count(); index += 2)
{
switch (stylePairs[index])
{
case "stroke":
OutlineColor = ParseColor(stylePairs[index+1]);
OutlineColorPresent = true;
break;
case "stroke-width":
OutlineWidth = double.Parse(stylePairs[index+1]);
OutlineWidthPresent = true;
break;
case "fill":
FillColor = ParseColor(stylePairs[index+1]);
FillColorPresent = true;
break;
default: break;
}
}
}
}
/// <summary>
/// An SVG Document. Read from file and build in-memory representation.
/// </summary>
public class SVGDocument
{
private List<ISVGElement> shapes = new List<ISVGElement>();
public static SVGDocument LoadFromFile(string path)
{
DateTime start = DateTime.UtcNow;
// All this nonsense is to prevent a 10 second delay when reading
// the first SVG file. It gets hung up trying to resolve the DTD?
MemoryStream ms = new MemoryStream();
{
StreamReader sr = new StreamReader(path);
StreamWriter sw = new StreamWriter(ms);
while (sr.EndOfStream == false)
{
string line = sr.ReadLine();
if (line.StartsWith("<!DOCTYPE") == false)
{
sw.WriteLine(line);
}
}
sw.Flush();
ms.Seek(0, SeekOrigin.Begin);
}
// Here begins the reading of the SVG file
XmlTextReader reader = new XmlTextReader(ms);
SVGDocument doc = new SVGDocument();
Dictionary<string, SVGStyle> styleDictionary = new Dictionary<string, SVGStyle>();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "style")
{
// Inline style
string styleData = reader.ReadElementContentAsString();
StringReader styleReader = new StringReader(styleData);
string line;
while ((line = styleReader.ReadLine()) != null)
{
string[] splitLine;
line = line.Trim();
splitLine = line.Split(new char[] { ' ', '\t' });
string name = splitLine[0];
if (name.StartsWith("."))
{
name = name.Substring(1);
}
if (splitLine.Count() == 2)
{
styleDictionary.Add(name, new SVGStyle(name, splitLine[1]));
}
};
}
else if (reader.Name == "rect")
{
if (reader.GetAttribute("class") != null)
{
doc.AddShape(new SVGRect(reader, styleDictionary));
}
}
else if (reader.Name == "circle")
{
doc.AddShape(new SVGCircle(reader, styleDictionary));
}
else if (reader.Name == "polygon")
{
doc.AddShape(new SVGPolygon(reader, styleDictionary));
}
else if (reader.Name == "polyline")
{
doc.AddShape(new SVGPolygon(reader, styleDictionary));
}
else if (reader.Name == "path")
{
doc.AddShape(new SVGPath(reader, styleDictionary));
}
else if (reader.Name == "image")
{
doc.AddShape(new SVGImage(reader, styleDictionary, path));
}
}
}
TimeSpan duration = DateTime.UtcNow - start;
System.Console.WriteLine("### Load took {0}s", ((double)duration.TotalMilliseconds / 1000.0));
return doc;
}
public void AddShape(ISVGElement shape)
{
shapes.Add(shape);
}
public IEnumerable<IEnumerable<PointF>> GetContours()
{
// Enumerate each shape in the document
foreach (ISVGElement shape in shapes)
{
foreach (var contour in shape.GetContours())
{
yield return contour;
}
}
}
public void Render(Graphics gc, bool rasterOnly)
{
foreach (ISVGElement shape in shapes)
{
if (shape is SVGImage)
{
// Polymorphism? What's that?
SVGImage img = shape as SVGImage;
gc.DrawImage(img.bits, img.DestBounds);
}
if (shape.OutlineWidth < .01 && shape.FillColor.A == 0 && rasterOnly)
{
continue;
}
GraphicsPath p = shape.GetPath();
if (shape.FillColor.A > 0)
{
Brush b = new SolidBrush(shape.FillColor);
gc.FillPath(b, p);
b.Dispose();
}
if (shape.OutlineWidth > 0 && shape.OutlineColor.A > 0)
{
Pen pen = new Pen(shape.OutlineColor, (float)shape.OutlineWidth);
gc.DrawPath(pen, p);
pen.Dispose();
}
}
}
string GCodeHeader =
@"(paperpixels SVG to GCode v0.1)
N30 G17 (active plane)
N35 G40 (turn compensation off)
N40 G20 (inch mode)
N45 G90 (Absolute mode, current coordinates)
N50 G61 (Exact stop mode for raster scanning)";
string GCodeFooter =
@"M5 (Laser Off)
E1P0 (Program End)
G0 X0 Y0 Z1 (Home and really turn off laser)
M30 (End)";
/// <summary>
/// This emits GCode suitable for driving a laser cutter to vector/raster
/// the document. There are numerous assumptions made here so that it
/// works exactly with my cheap Chinese laser cutter, my controller board
/// and my Mach3 config. You may need to fiddle with things here to
/// get the axes directions correct, etc.
/// </summary>
/// <param name="path">Output path for GCode file</param>
/// <param name="raster">If true a raster path is emitted</param>
/// <param name="rasterDpi">DPI resolution for raster</param>
/// <param name="rasterFeedRate">IPS feed for raster scan</param>
/// <param name="vector">If true a vector cut is emitted</param>
/// <param name="vectorDpi">DPI resolution for vector cut</param>
/// <param name="vectorFeedRate">IPS feed for vector cut</param>
/// <param name="vectorCV">Use constant velocity mode? Smooths out
/// discontinuities that occur at polygon vertices/corners using
/// lookahead. Mach3 does all this, this simply emits a GCode to
/// turn this on. This, plus sufficient lookahead configured in
/// Mach3 made my laser perform much smoother.</param>
/// <param name="progressDelegate">Callback for progress notification</param>
public void EmitGCode(string path, bool raster, int rasterDpi, int rasterFeedRate, bool vector, int vectorDpi, int vectorFeedRate, bool vectorCV, Action<double> progressDelegate)
{
// Open up file for writing
StreamWriter gcode = new StreamWriter(path);
// Emit header
gcode.WriteLine(GCodeHeader);
// BUGBUG: Should read size from SVG file header. But we usually are doing 11x11
// This always assumes an 11" x 11" document.
double docWidth = 11.0;
double docHeight = 11.0;
double bandHeight = 0.5;
double totalProgress = docHeight / bandHeight + 1.0;
double progress = 0;
// First pass is raster engraving
if (raster)
{
// Create a bitmap image used for banding
Bitmap band = new Bitmap(
(int)(docWidth * rasterDpi), // Band Width in px
(int)(bandHeight * rasterDpi), // Band Height in px
PixelFormat.Format32bppArgb);
Graphics gc = Graphics.FromImage(band);
// Now render each band of the image
for (double bandTop = 0.0; bandTop <= docHeight - bandHeight; bandTop += bandHeight)
{
// Call progress method once per band
if (progressDelegate != null)
{
progressDelegate(progress / totalProgress);
}
// Set up the GC transform to render this band
gc.ResetTransform();
gc.FillRectangle(Brushes.White, 0, 0, 99999, 99999);
gc.ScaleTransform(-rasterDpi, -rasterDpi);
gc.TranslateTransform((float)-docWidth, (float)-bandTop);
// Erase whatever was there before
gc.FillRectangle(Brushes.White, -50, -50, 50, 50);
// Render just the raster shapes into the band
this.Render(gc, true);
// Now scan the band and emit gcode. We access the bitmap data
// directly for higher performance. The use of unsafe pointer
// access here sped up perf significantly over GetPixel() which
// is obvious but they don't teach PhD's this so I mention it here.
unsafe
{
BitmapData lockedBits = band.LockBits(
Rectangle.FromLTRB(0, 0, band.Width, band.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
bool laserOn = false;
int onStart = 0;
for (int y = 0; y < band.Height; ++y)
{
if (laserOn)