Inheritance: ISvgPathSegList
Esempio n. 1
0
 private SvgPathSegList SegmentList()
 {
     if (_pathSegList == null)
     {
         _pathSegList = new SvgPathSegList(this.GetAttribute("d"), false);
     }
     return(_pathSegList);
 }
Esempio n. 2
0
 public SvgPathSegHandler(SvgPathSegList pathList)
 {
     _closedPath    = 0;
     _startPoint    = new SvgPointF(0, 0);
     _isClosed      = false;
     _mayHaveCurves = false;
     _pathList      = pathList;
 }
 public void TestMultipleMovetosAbs()
 {
     SvgPathSegList list = new SvgPathSegList("M10, 30 70-20,23 -12", false);
     Assert.AreEqual(3, list.NumberOfItems);
     Assert.AreEqual("M", list[0].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(10,30), ((SvgPathSegMovetoAbs)list[0]).AbsXY);
     Assert.AreEqual("L", list[1].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(70,-20), ((SvgPathSegLinetoAbs)list[1]).AbsXY);
     Assert.AreEqual("L", list[2].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(23,-12), ((SvgPathSegLinetoAbs)list[2]).AbsXY);
 }
Esempio n. 4
0
        public override void HandleAttributeChange(XmlAttribute attribute)
        {
            if (attribute.NamespaceURI.Length == 0)
            {
                switch (attribute.LocalName)
                {
                case "d":
                    _pathSegList = null;
                    Invalidate();
                    return;

                case "pathLength":
                    _pathLength = null;
                    Invalidate();
                    return;

                case "marker-start":
                case "marker-mid":
                case "marker-end":
                // Color.attrib, Paint.attrib
                case "color":
                case "fill":
                case "fill-rule":
                case "stroke":
                case "stroke-dasharray":
                case "stroke-dashoffset":
                case "stroke-linecap":
                case "stroke-linejoin":
                case "stroke-miterlimit":
                case "stroke-width":
                // Opacity.attrib
                case "opacity":
                case "stroke-opacity":
                case "fill-opacity":
                // Graphics.attrib
                case "display":
                case "image-rendering":
                case "shape-rendering":
                case "text-rendering":
                case "visibility":
                    Invalidate();
                    break;

                case "transform":
                    Invalidate();
                    break;
                }
                base.HandleAttributeChange(attribute);
            }
        }
 public void ParseTest1()
 {
     SvgPathSegList list = new SvgPathSegList("M10, 30L70-20,23 -12L23 34z", false);
     Assert.AreEqual(5, list.NumberOfItems);
     Assert.AreEqual("M", list[0].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(10,30), ((SvgPathSegMovetoAbs)list[0]).AbsXY);
     Assert.AreEqual("L", list[1].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(70,-20), ((SvgPathSegLinetoAbs)list[1]).AbsXY);
     Assert.AreEqual("L", list[2].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(23,-12), ((SvgPathSegLinetoAbs)list[2]).AbsXY);
     Assert.AreEqual("L", list[3].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(23,34), ((SvgPathSegLinetoAbs)list[3]).AbsXY);
     Assert.AreEqual("z", list[4].PathSegTypeAsLetter);
 }
Esempio n. 6
0
        public bool Parse(SvgPathSegList pathList, string pathSegs)
        {
            try
            {
                if (pathList == null || string.IsNullOrWhiteSpace(pathSegs))
                {
                    return(false);
                }

                _isClosed      = false;
                _mayHaveCurves = false;

                string[] paths = RegexPathCmd.Split(pathSegs);

                return(this.Parse(pathList, paths));
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.GetType().Name + ": " + ex.Message);
                return(false);
            }
        }
Esempio n. 7
0
 internal void SetList(SvgPathSegList list)
 {
     _list = list;
 }
Esempio n. 8
0
 internal void SetList(SvgPathSegList list)
 {
     this._list = list;
 }
 public void SetUp()
 {
     segments = makeSegments();
             segment = getTestSegment();
 }
Esempio n. 10
0
 internal void setList(SvgPathSegList list)
 {
     this.list = list;
 }
Esempio n. 11
0
        public bool Parse(SvgPathSegList pathList, string pathSegs)
        {
            if (pathList == null || string.IsNullOrWhiteSpace(pathSegs))
            {
                return(false);
            }

            _isClosed      = false;
            _mayHaveCurves = false;

            int closedPath = 0;

            SvgPathSeg seg;

            string[] paths = RegexPathCmd.Split(pathSegs);

            SvgPointF startPoint = new SvgPointF(0, 0);

            foreach (string path in paths)
            {
                string segment = path.Trim();
                if (segment.Length == 0)
                {
                    continue;
                }
                char     cmd    = segment[0];
                double[] coords = SvgNumber.ParseDoubles(segment.Substring(1));

                int length = coords.Length;
                switch (cmd)
                {
                // Parse: moveto
                case 'M':
                    for (int i = 0; i < length; i += 2)
                    {
                        if (i == 0)
                        {
                            seg = new SvgPathSegMovetoAbs(coords[i], coords[i + 1]);

                            startPoint = new SvgPointF(coords[i], coords[i + 1]);
                            SvgPointF endPoint = new SvgPointF(coords[i], coords[i + 1]);
                            seg.Limits = new SvgPointF[] { startPoint, endPoint };
                            startPoint = endPoint;
                        }
                        else
                        {
                            seg = new SvgPathSegLinetoAbs(coords[i], coords[i + 1]);

                            SvgPointF endPoint = new SvgPointF(coords[i], coords[i + 1]);
                            seg.Limits = new SvgPointF[] { startPoint, endPoint };
                            startPoint = endPoint;
                        }
                        pathList.AppendItem(seg);
                    }
                    break;

                case 'm':
                    for (int i = 0; i < length; i += 2)
                    {
                        if (i == 0)
                        {
                            seg = new SvgPathSegMovetoRel(coords[i], coords[i + 1]);

                            SvgPointF endPoint = new SvgPointF(coords[i] + startPoint.X, coords[i + 1] + startPoint.Y);
                            seg.Limits = new SvgPointF[] { startPoint, endPoint };
                            startPoint = endPoint;
                        }
                        else
                        {
                            seg = new SvgPathSegLinetoRel(coords[i], coords[i + 1]);

                            SvgPointF endPoint = new SvgPointF(coords[i] + startPoint.X, coords[i + 1] + startPoint.Y);
                            seg.Limits = new SvgPointF[] { startPoint, endPoint };
                            startPoint = endPoint;
                        }
                        pathList.AppendItem(seg);
                    }
                    break;
                // End of: moveto

                // Parse: lineto
                case 'L':
                    for (int i = 0; i < length; i += 2)
                    {
                        seg = new SvgPathSegLinetoAbs(coords[i], coords[i + 1]);
                        pathList.AppendItem(seg);

                        SvgPointF endPoint = new SvgPointF(coords[i], coords[i + 1]);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'l':
                    for (int i = 0; i < length; i += 2)
                    {
                        seg = new SvgPathSegLinetoRel(coords[i], coords[i + 1]);
                        pathList.AppendItem(seg);

                        SvgPointF endPoint = new SvgPointF(coords[i] + startPoint.X, coords[i + 1] + startPoint.Y);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'H':
                    for (int i = 0; i < length; i++)
                    {
                        seg = new SvgPathSegLinetoHorizontalAbs(coords[i]);
                        pathList.AppendItem(seg);

                        SvgPointF endPoint = new SvgPointF(coords[i], startPoint.Y);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'h':
                    for (int i = 0; i < length; i++)
                    {
                        seg = new SvgPathSegLinetoHorizontalRel(coords[i]);
                        pathList.AppendItem(seg);

                        SvgPointF endPoint = new SvgPointF(coords[i] + startPoint.X, startPoint.Y);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'V':
                    for (int i = 0; i < length; i++)
                    {
                        seg = new SvgPathSegLinetoVerticalAbs(coords[i]);
                        pathList.AppendItem(seg);

                        SvgPointF endPoint = new SvgPointF(startPoint.X, coords[i]);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'v':
                    for (int i = 0; i < length; i++)
                    {
                        seg = new SvgPathSegLinetoVerticalRel(coords[i]);
                        pathList.AppendItem(seg);

                        SvgPointF endPoint = new SvgPointF(startPoint.X, coords[i] + startPoint.Y);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;
                // End of: lineto

                // Parse: beziers
                case 'C':
                    for (int i = 0; i < length; i += 6)
                    {
                        seg = new SvgPathSegCurvetoCubicAbs(
                            coords[i + 4],
                            coords[i + 5],
                            coords[i],
                            coords[i + 1],
                            coords[i + 2],
                            coords[i + 3]);
                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;

                        SvgPointF endPoint = new SvgPointF(coords[i + 4], coords[i + 5]);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'c':
                    for (int i = 0; i < length; i += 6)
                    {
                        if ((i + 5) >= length)
                        {
                            break;
                        }
                        seg = new SvgPathSegCurvetoCubicRel(
                            coords[i + 4],
                            coords[i + 5],
                            coords[i],
                            coords[i + 1],
                            coords[i + 2],
                            coords[i + 3]);

                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;

                        SvgPointF endPoint = new SvgPointF(coords[i + 4] + startPoint.X, coords[i + 5] + startPoint.Y);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'S':
                    for (int i = 0; i < length; i += 4)
                    {
                        seg = new SvgPathSegCurvetoCubicSmoothAbs(
                            coords[i + 2],
                            coords[i + 3],
                            coords[i],
                            coords[i + 1]);
                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;

                        SvgPointF endPoint = new SvgPointF(coords[i + 2], coords[i + 3]);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 's':
                    for (int i = 0; i < length; i += 4)
                    {
                        seg = new SvgPathSegCurvetoCubicSmoothRel(
                            coords[i + 2],
                            coords[i + 3],
                            coords[i],
                            coords[i + 1]);
                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;

                        SvgPointF endPoint = new SvgPointF(coords[i + 2] + startPoint.X, coords[i + 3] + startPoint.Y);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'Q':
                    for (int i = 0; i < length; i += 4)
                    {
                        seg = new SvgPathSegCurvetoQuadraticAbs(
                            coords[i + 2],
                            coords[i + 3],
                            coords[i],
                            coords[i + 1]);
                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;

                        SvgPointF endPoint = new SvgPointF(coords[i + 2], coords[i + 3]);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'q':
                    for (int i = 0; i < length; i += 4)
                    {
                        seg = new SvgPathSegCurvetoQuadraticRel(
                            coords[i + 2],
                            coords[i + 3],
                            coords[i],
                            coords[i + 1]);
                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;

                        SvgPointF endPoint = new SvgPointF(coords[i + 2] + startPoint.X, coords[i + 3] + startPoint.Y);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 'T':
                    for (int i = 0; i < length; i += 2)
                    {
                        seg = new SvgPathSegCurvetoQuadraticSmoothAbs(
                            coords[i], coords[i + 1]);
                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;

                        SvgPointF endPoint = new SvgPointF(coords[i], coords[i + 1]);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;

                case 't':
                    for (int i = 0; i < length; i += 2)
                    {
                        seg = new SvgPathSegCurvetoQuadraticSmoothRel(
                            coords[i], coords[i + 1]);
                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;

                        SvgPointF endPoint = new SvgPointF(coords[i] + startPoint.X, coords[i + 1] + startPoint.Y);
                        seg.Limits = new SvgPointF[] { startPoint, endPoint };
                        startPoint = endPoint;
                    }
                    break;
                // End of: bezier

                // Parse: arcs
                case 'A':
                case 'a':
                    for (int i = 0; i < length; i += 7)
                    {
                        if (cmd == 'A')
                        {
                            seg = new SvgPathSegArcAbs(
                                coords[i + 5],
                                coords[i + 6],
                                coords[i],
                                coords[i + 1],
                                coords[i + 2],
                                (!coords[i + 3].Equals(0)),
                                (!coords[i + 4].Equals(0)));

                            SvgPointF endPoint = new SvgPointF(coords[i + 5], coords[i + 6]);
                            seg.Limits = new SvgPointF[] { startPoint, endPoint };
                            startPoint = endPoint;
                        }
                        else
                        {
                            seg = new SvgPathSegArcRel(
                                coords[i + 5],
                                coords[i + 6],
                                coords[i],
                                coords[i + 1],
                                coords[i + 2],
                                (!coords[i + 3].Equals(0)),
                                (!coords[i + 4].Equals(0)));

                            SvgPointF endPoint = new SvgPointF(coords[i + 5] + startPoint.X, coords[i + 6] + startPoint.Y);
                            seg.Limits = new SvgPointF[] { startPoint, endPoint };
                            startPoint = endPoint;
                        }
                        pathList.AppendItem(seg);

                        _mayHaveCurves = true;
                    }
                    break;
                // End of: arcs

                // Parse: close
                case 'z':
                case 'Z':
                    closedPath++;
                    seg = new SvgPathSegClosePath();
                    pathList.AppendItem(seg);

                    if (pathList.Count >= 2)
                    {
                        SvgPointF endPoint = pathList[0].Limits[0];
                        seg.Limits = new SvgPointF[] { endPoint, startPoint };
                        startPoint = endPoint;
                    }
                    else
                    {
                        seg.Limits = new SvgPointF[] { startPoint, startPoint };
                        startPoint = new SvgPointF(0, 0);
                    }
                    break;
                // End of: close

                // Unknown path command
                default:
                    throw new ApplicationException(string.Format("Unknown path command - ({0})", cmd));
                }
            }

            _isClosed = (closedPath == 1);

            return(true);
        }
Esempio n. 12
0
        public GraphicsPath GetGraphicsPath()
        {
            if (gp == null)
            {
                gp = new GraphicsPath();

                PointF initPoint = new PointF(0, 0);
                PointF lastPoint = new PointF(0, 0);

                SvgPathSegList segments = (SvgPathSegList)PathSegList;
                SvgPathSeg     segment;

                int nElems = segments.NumberOfItems;

                for (int i = 0; i < nElems; i++)
                {
                    segment = (SvgPathSeg)segments.GetItem(i);

                    if (segment is SvgPathSegMoveto)
                    {
                        SvgPathSegMoveto seg = (SvgPathSegMoveto)segment;
                        gp.StartFigure();
                        lastPoint = initPoint = seg.AbsXY;
                    }
                    else if (segment is SvgPathSegLineto)
                    {
                        SvgPathSegLineto seg = (SvgPathSegLineto)segment;
                        PointF           p   = seg.AbsXY;
                        gp.AddLine(lastPoint.X, lastPoint.Y, p.X, p.Y);

                        lastPoint = p;
                    }
                    else if (segment is SvgPathSegCurveto)
                    {
                        SvgPathSegCurveto seg  = (SvgPathSegCurveto)segment;
                        PointF            xy   = seg.AbsXY;
                        PointF            x1y1 = seg.CubicX1Y1;
                        PointF            x2y2 = seg.CubicX2Y2;
                        gp.AddBezier(lastPoint.X, lastPoint.Y, x1y1.X, x1y1.Y, x2y2.X, x2y2.Y, xy.X, xy.Y);

                        lastPoint = xy;
                    }
                    else if (segment is SvgPathSegArc)
                    {
                        SvgPathSegArc seg = (SvgPathSegArc)segment;
                        PointF        p   = seg.AbsXY;
                        if (lastPoint.Equals(p))
                        {
                            // If the endpoints (x, y) and (x0, y0) are identical, then this
                            // is equivalent to omitting the elliptical arc segment entirely.
                        }
                        else if (seg.R1 == 0 || seg.R2 == 0)
                        {
                            // Ensure radii are valid
                            gp.AddLine(lastPoint, p);
                        }
                        else
                        {
                            CalculatedArcValues calcValues = seg.GetCalculatedArcValues();

                            GraphicsPath gp2 = new GraphicsPath();
                            gp2.StartFigure();
                            gp2.AddArc(
                                calcValues.Cx - calcValues.CorrRx,
                                calcValues.Cy - calcValues.CorrRy,
                                calcValues.CorrRx * 2,
                                calcValues.CorrRy * 2,
                                calcValues.AngleStart,
                                calcValues.AngleExtent
                                );

                            Matrix matrix = new Matrix();
                            matrix.Translate(
                                -calcValues.Cx,
                                -calcValues.Cy
                                );
                            gp2.Transform(matrix);

                            matrix = new Matrix();
                            matrix.Rotate(seg.Angle);
                            gp2.Transform(matrix);

                            matrix = new Matrix();
                            matrix.Translate(calcValues.Cx, calcValues.Cy);
                            gp2.Transform(matrix);

                            gp.AddPath(gp2, true);
                        }

                        lastPoint = p;
                    }
                    else if (segment is SvgPathSegClosePath)
                    {
                        gp.CloseFigure();
                        lastPoint = initPoint;
                    }
                }

                string fillRule = GetPropertyValue("fill-rule");
                if (fillRule == "evenodd")
                {
                    gp.FillMode = FillMode.Alternate;
                }
                else
                {
                    gp.FillMode = FillMode.Winding;
                }
            }

            return(gp);
        }
Esempio n. 13
0
        public void TestPathText()
        {
            string d = "M1,2L3,4l5,6H7h8V9v0zC1,2,3,4,5,6c7,8,9,0,1,2S3,4,5,6s7,8,9,0Q1,2,3,4q5,6,7,8T9,0t1,2A3,4,5,1,0,8,9a0,1,2,0,1,5,6";
            SvgPathSegList list = new SvgPathSegList(d, false);

            Assert.AreEqual(d, list.PathText);
        }
Esempio n. 14
0
 public void TestReadOnlyAppend()
 {
     SvgPathSegList list = new SvgPathSegList("", true);
     list.AppendItem(getLineto());
 }
Esempio n. 15
0
        private void diffAndBisectTest(SvgPathSegList list, int startSeg, float expectedDiff, float expectedBisect)
        {
            float diff = SvgNumber.CalcAngleDiff(((SvgPathSeg)list[startSeg]).EndAngle, ((SvgPathSeg)list[startSeg+1]).StartAngle);
            float bisect = SvgNumber.CalcAngleBisection(((SvgPathSeg)list[startSeg]).EndAngle, ((SvgPathSeg)list[startSeg+1]).StartAngle);

            string label = startSeg + "-" + (startSeg+1);

            floatsTest(label + " diff", expectedDiff, diff);
            floatsTest(label + " bisect", expectedBisect, bisect);
        }
Esempio n. 16
0
        public void TestStartEndAngle2()
        {
            SvgPathSegList list = new SvgPathSegList("M5,5 L15,5 25,25 Q30,40 40,30 L50 20 a25,25 -30 0,1 0,50 l-40 20 z", false);

            floatsTest("1 startAngle", 270, ((SvgPathSeg)list[1]).StartAngle);
            floatsTest("1 endAngle", 90, ((SvgPathSeg)list[1]).EndAngle);

            diffAndBisectTest(list, 1, 117, 31.5f);

            floatsTest("2 startAngle", 333, ((SvgPathSeg)list[2]).StartAngle);
            floatsTest("2 endAngle", 153, ((SvgPathSeg)list[2]).EndAngle);

            diffAndBisectTest(list, 2, 172, 67.5f);

            floatsTest("3 startAngle", 342, ((SvgPathSeg)list[3]).StartAngle);
            floatsTest("3 endAngle", 45, ((SvgPathSeg)list[3]).EndAngle);

            diffAndBisectTest(list, 3, 180, 315f);

            floatsTest("4 startAngle", 225, ((SvgPathSeg)list[4]).StartAngle);
            floatsTest("4 endAngle", 45, ((SvgPathSeg)list[4]).EndAngle);

            diffAndBisectTest(list, 4, 135, 337.5f);

            floatsTest("5 startAngle", 270, ((SvgPathSeg)list[5]).StartAngle);
            floatsTest("5 endAngle", 270, ((SvgPathSeg)list[5]).EndAngle);

            diffAndBisectTest(list, 5, 207, 166.5f);

            //rel Lineto 50,70	=> 10,90
            floatsTest("6 startAngle", 63, ((SvgPathSeg)list[6]).StartAngle);
            floatsTest("6 endAngle", 243, ((SvgPathSeg)list[6]).EndAngle);

            diffAndBisectTest(list, 6, 67, 210f);

            //close 10,90	=> 5,5
            floatsTest("7 startAngle", 177, ((SvgPathSeg)list[7]).StartAngle);
            floatsTest("7 endAngle", 357, ((SvgPathSeg)list[7]).EndAngle);
        }
Esempio n. 17
0
        public void TestStartEndAngle1()
        {
            SvgPathSegList list = new SvgPathSegList("M10,10L100,10,100,100,10,100z", false);

            Assert.AreEqual(270, ((SvgPathSeg)list[1]).StartAngle);
            Assert.AreEqual(90, ((SvgPathSeg)list[1]).EndAngle);

            Assert.AreEqual(180, ((SvgPathSeg)list[4]).StartAngle);
            Assert.AreEqual(0, ((SvgPathSeg)list[4]).EndAngle);
        }
Esempio n. 18
0
 public void TestReadOnlyClear()
 {
     SvgPathSegList list = new SvgPathSegList("", true);
     list.Clear();
 }
Esempio n. 19
0
 internal void setList(SvgPathSegList list)
 {
     this.list = list;
 }
Esempio n. 20
0
 public void TestMultipleMovetosRel()
 {
     SvgPathSegList list = new SvgPathSegList("m10,30 20,20", false);
     Assert.AreEqual(2, list.NumberOfItems);
     Assert.AreEqual("m", list[0].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(10,30), ((SvgPathSegMovetoRel)list[0]).AbsXY);
     Assert.AreEqual("l", list[1].PathSegTypeAsLetter);
     Assert.AreEqual(new PointF(30,50), ((SvgPathSegLinetoRel)list[1]).AbsXY);
 }