Skip to content

Commit da98040

Browse files
committed
2 parents ec0e562 + db1e1de commit da98040

File tree

7 files changed

+133
-476
lines changed

7 files changed

+133
-476
lines changed

Source/SVGImage/SVG/Fill.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,20 @@ public Fill(SVG svg)
2626
this.Opacity = 100;
2727
}
2828

29+
public bool IsEmpty(SVG svg)
30+
{
31+
if (svg == null) return true;
32+
33+
if (!svg.PaintServers.ContainsServer(this.PaintServerKey))
34+
{
35+
return true;
36+
}
37+
return svg.PaintServers.GetServer(this.PaintServerKey) == null;
38+
}
39+
2940
public Brush FillBrush(SVG svg, SVGRender svgRender, Shape shape, double elementOpacity, Rect bounds)
3041
{
31-
var paintServer = svg.PaintServers.GetServer(PaintServerKey);
42+
var paintServer = svg.PaintServers.GetServer(this.PaintServerKey);
3243
if(paintServer != null)
3344
{
3445
if(paintServer is CurrentColorPaintServer)

Source/SVGImage/SVG/PaintServer/PaintServerManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public string Create(SVG svg, XmlNode node)
3636
return null;
3737
}
3838

39+
public bool ContainsServer(string serverKey)
40+
{
41+
if (string.IsNullOrWhiteSpace(serverKey))
42+
{
43+
return false;
44+
}
45+
46+
return m_servers.ContainsKey(serverKey);
47+
}
48+
3949
public void AddServer(string key, PaintServer server)
4050
{
4151
if (string.IsNullOrWhiteSpace(key))

Source/SVGImage/SVG/SVGImage.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,15 @@ private static void OnUriSourceChanged(DependencyObject obj, DependencyPropertyC
716716
}
717717

718718
var sourceUri = (Uri)args.NewValue;
719-
svgImage.SetImage(sourceUri);
719+
if (sourceUri != null)
720+
{
721+
svgImage.SetImage(sourceUri);
722+
}
723+
else
724+
{
725+
svgImage.SetImage((Drawing)null);
726+
}
727+
720728
}
721729

722730
static void OnSizeTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -734,7 +742,14 @@ static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventAr
734742

735743
static void OnFileSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
736744
{
737-
((SVGImage)d).SetImage(new FileStream(e.NewValue.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
745+
if (e.NewValue != null)
746+
{
747+
((SVGImage)d).SetImage(new FileStream(e.NewValue.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
748+
}
749+
else
750+
{
751+
((SVGImage)d).SetImage((Drawing)null);
752+
}
738753
}
739754

740755
static void OnImageSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

Source/SVGImage/SVG/SVGRender.cs

Lines changed: 7 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -477,173 +477,19 @@ internal DrawingGroup LoadGroup(IList<Shape> elements, Rect? viewBox, bool isSwi
477477
if (shape is PathShape)
478478
{
479479
PathShape r = shape as PathShape;
480-
// PathFigure p = null;
481-
Point lastPoint = new Point(0, 0);
482-
483-
// PathShape.CurveTo lastc = null;
484-
// PathShape.QuadraticCurveTo lastq = null;
485-
Point lastcirPoint = new Point(0, 0);
486-
PathGeometry path = PathGeometry.CreateFromGeometry(PathGeometry.Parse(r.Data));
487-
//PathGeometry path = new PathGeometry();
488-
489-
/*
490-
foreach (PathShape.PathElement element in r.Elements)
480+
var svg = this.SVG;
481+
if (r.Fill == null || r.Fill.IsEmpty(svg))
491482
{
492-
bool isRelative = element.IsRelative;
493-
if (element is PathShape.MoveTo)
483+
if (r.Stroke == null || r.Stroke.IsEmpty(svg))
494484
{
495-
p = new PathFigure();
496-
p.IsClosed = r.ClosePath;
497-
if (isRelative) p.StartPoint = lastPoint + (Vector)((PathShape.MoveTo)element).Point;
498-
else p.StartPoint = ((PathShape.MoveTo)element).Point;
499-
lastPoint = p.StartPoint;
500-
path.Figures.Add(p);
501-
continue;
502-
}
503-
if (element is PathShape.LineTo)
504-
{
505-
PathShape.LineTo lineto = element as PathShape.LineTo;
506-
foreach (Point point in lineto.Points)
507-
{
508-
if (isRelative)
509-
{
510-
Point newpoint = lastPoint + (Vector)point;
511-
lastPoint = newpoint;
512-
p.Segments.Add(new LineSegment(newpoint, true));
513-
}
514-
else
515-
{
516-
if (lineto.PositionType == PathShape.LineTo.eType.Point) lastPoint = point;
517-
if (lineto.PositionType == PathShape.LineTo.eType.Horizontal) lastPoint = new Point(point.X, lastPoint.Y);
518-
if (lineto.PositionType == PathShape.LineTo.eType.Vertical) lastPoint = new Point(lastPoint.X, point.Y);
519-
p.Segments.Add(new LineSegment(lastPoint, true));
520-
}
521-
}
522-
continue;
523-
}
524-
if (element is PathShape.CurveTo)
525-
{
526-
PathShape.CurveTo c = element as PathShape.CurveTo;
527-
Point startPoint = lastPoint;
528-
BezierSegment s = new BezierSegment();
529-
if (isRelative)
530-
{
531-
s.Point1 = lastPoint + (Vector)c.CtrlPoint1;
532-
533-
if (c.Command == 's')
534-
{
535-
// first control point is a mirrored point of last end control point
536-
//s.Point1 = lastPoint + new Vector(lastc.Point.X - dx, lastc.Point.Y - dy);
537-
//s.Point1 = new Point(lastctrlpoint.X+2, lastctrlpoint.Y+2);
538-
539-
double dx = lastc.CtrlPoint2.X - lastc.Point.X;
540-
double dy = lastc.CtrlPoint2.Y - lastc.Point.Y;
541-
s.Point1 = new Point(lastcirPoint.X - dx, lastcirPoint.Y - dy);
542-
//s.Point1 = lastctrlpoint;
543-
}
544-
545-
s.Point2 = lastPoint + (Vector)c.CtrlPoint2;
546-
s.Point3 = lastPoint + (Vector)c.Point;
547-
}
548-
else
549-
{
550-
if (c.Command == 's')
551-
{
552-
// first control point is a mirrored point of last end control point
553-
//s.Point1 = lastPoint + new Vector(lastc.Point.X - dx, lastc.Point.Y - dy);
554-
//s.Point1 = new Point(lastctrlpoint.X+2, lastctrlpoint.Y+2);
555-
556-
double dx = lastc.CtrlPoint2.X - lastc.Point.X;
557-
double dy = lastc.CtrlPoint2.Y - lastc.Point.Y;
558-
s.Point1 = new Point(lastcirPoint.X - dx, lastcirPoint.Y - dy);
559-
}
560-
else s.Point1 = c.CtrlPoint1;
561-
s.Point2 = c.CtrlPoint2;
562-
s.Point3 = c.Point;
563-
}
564-
lastPoint = s.Point3;
565-
p.Segments.Add(s);
566-
567-
lastc = c;
568-
lastcirPoint = s.Point3;
569-
570-
//debugPoints.Add(new ControlLine(startPoint, s.Point1));
571-
//debugPoints.Add(new ControlLine(s.Point3, s.Point2));
572-
continue;
573-
}
574-
if (element is PathShape.QuadraticCurveTo)
575-
{
576-
PathShape.QuadraticCurveTo c = element as PathShape.QuadraticCurveTo;
577-
Point startPoint = lastPoint;
578-
QuadraticBezierSegment s = new QuadraticBezierSegment();
579-
if (isRelative)
580-
{
581-
s.Point1 = lastPoint + (Vector)c.CtrlPoint1;
582-
583-
if (c.Command == 'q' && lastq != null) // fix for horse svg! needed ?? or is it wrong in SVG?
584-
{
585-
// first control point is a mirrored point of last end control point
586-
//s.Point1 = lastPoint + new Vector(lastc.Point.X - dx, lastc.Point.Y - dy);
587-
//s.Point1 = new Point(lastctrlpoint.X+2, lastctrlpoint.Y+2);
588-
589-
double dx = lastq.CtrlPoint1.X - lastq.Point.X;
590-
double dy = lastq.CtrlPoint1.Y - lastq.Point.Y;
591-
s.Point1 = new Point(lastcirPoint.X - dx, lastcirPoint.Y - dy);
592-
//s.Point1 = lastctrlpoint;
593-
}
594-
595-
s.Point2 = lastPoint + (Vector)c.Point;
596-
}
597-
else
598-
{
599-
if (c.Command == 'q')
600-
{
601-
// first control point is a mirrored point of last end control point
602-
//s.Point1 = lastPoint + new Vector(lastc.Point.X - dx, lastc.Point.Y - dy);
603-
//s.Point1 = new Point(lastctrlpoint.X+2, lastctrlpoint.Y+2);
604-
605-
double dx = lastq.CtrlPoint1.X - lastq.Point.X;
606-
double dy = lastq.CtrlPoint1.Y - lastq.Point.Y;
607-
s.Point1 = new Point(lastcirPoint.X - dx, lastcirPoint.Y - dy);
608-
}
609-
else s.Point1 = c.CtrlPoint1;
610-
s.Point2 = c.Point;
611-
}
612-
lastPoint = s.Point2;
613-
p.Segments.Add(s);
614-
615-
lastq = c;
616-
lastcirPoint = s.Point2;
617-
618-
//debugPoints.Add(new ControlLine(startPoint, s.Point1));
619-
//debugPoints.Add(new ControlLine(s.Point3, s.Point2));
620-
continue;
621-
}
622-
if (element is PathShape.EllipticalArcTo)
623-
{
624-
PathShape.EllipticalArcTo c = element as PathShape.EllipticalArcTo;
625-
ArcSegment s = new ArcSegment();
626-
if (isRelative) s.Point = lastPoint + new Vector(c.X, c.Y);
627-
else s.Point = new Point(c.X, c.Y);
628-
629-
s.Size = new Size(c.RX, c.RY);
630-
s.RotationAngle = c.AxisRotation;
631-
s.SweepDirection = SweepDirection.Counterclockwise;
632-
if (c.Clockwise) s.SweepDirection = SweepDirection.Clockwise;
633-
s.IsLargeArc = c.LargeArc;
634-
lastPoint = s.Point;
635-
p.Segments.Add(s);
636-
continue;
485+
var fill = new Fill(svg);
486+
fill.PaintServerKey = this.SVG.PaintServers.Parse("black");
487+
r.Fill = fill;
637488
}
638489
}
639-
*/
640-
/*
641-
if (r.Transform != null)
642-
path.Transform = r.Transform;
643-
*/
490+
PathGeometry path = PathGeometry.CreateFromGeometry(PathGeometry.Parse(r.Data));
644491
var di = this.NewDrawingItem(shape, path);
645492
AddDrawingToGroup(grp, shape, di);
646-
//}
647493
}
648494
}
649495

0 commit comments

Comments
 (0)