コード例 #1
0
        public void ToString_Should_Return_Width_And_Height()
        {
            var target = new PathSize()
            {
                Width = 50, Height = 30
            };
            var actual = target.ToString();

            Assert.Equal("50,30", actual);
        }
コード例 #2
0
        /// <summary>
        /// Parse a SVG path geometry string.
        /// </summary>
        /// <param name="context">The geometry context.</param>
        /// <param name="pathString">The path geometry string</param>
        /// <param name="startIndex">The string start index.</param>
        public void Parse(GeometryContext context, string pathString, int startIndex)
        {
            _context         = context;
            _pathString      = pathString;
            _pathLength      = pathString.Length;
            _curIndex        = startIndex;
            _secondLastPoint = Point2.FromXY(0, 0);
            _lastPoint       = Point2.FromXY(0, 0);
            _lastStart       = Point2.FromXY(0, 0);
            _figureStarted   = false;
            bool first    = true;
            char last_cmd = ' ';

            while (ReadToken())
            {
                char cmd = _token;

                if (first)
                {
                    if ((cmd != 'M') && (cmd != 'm'))
                    {
                        InvalidToken();
                    }

                    first = false;
                }

                switch (cmd)
                {
                case 'm':
                case 'M':
                    _lastPoint = ReadPoint(cmd, !_allowComma);

                    _context.BeginFigure(_lastPoint.AsPointShape(), _isFilled, !_isClosed);
                    _figureStarted = true;
                    _lastStart     = _lastPoint;
                    last_cmd       = 'M';

                    while (IsNumber(_allowComma))
                    {
                        _lastPoint = ReadPoint(cmd, !_allowComma);
                        _context.LineTo(_lastPoint.AsPointShape(), _isStroked, !_isSmoothJoin);
                        last_cmd = 'L';
                    }
                    break;

                case 'l':
                case 'L':
                case 'h':
                case 'H':
                case 'v':
                case 'V':
                    EnsureFigure();

                    do
                    {
                        switch (cmd)
                        {
                        case 'l':
                            _lastPoint = ReadPoint(cmd, !_allowComma);
                            break;

                        case 'L':
                            _lastPoint = ReadPoint(cmd, !_allowComma);
                            break;

                        case 'h':
                            _lastPoint = Point2.FromXY(_lastPoint.X + ReadNumber(!_allowComma), _lastPoint.Y);
                            break;

                        case 'H':
                            _lastPoint = Point2.FromXY(_lastPoint.X + ReadNumber(!_allowComma), _lastPoint.Y);
                            break;

                        case 'v':
                            _lastPoint = Point2.FromXY(_lastPoint.X, _lastPoint.Y + ReadNumber(!_allowComma));
                            break;

                        case 'V':
                            _lastPoint = Point2.FromXY(_lastPoint.X, _lastPoint.Y + ReadNumber(!_allowComma));
                            break;
                        }

                        _context.LineTo(_lastPoint.AsPointShape(), _isStroked, !_isSmoothJoin);
                    }while (IsNumber(_allowComma));

                    last_cmd = 'L';
                    break;

                case 'c':
                case 'C':
                case 's':
                case 'S':
                    EnsureFigure();

                    do
                    {
                        Point2 p;

                        if ((cmd == 's') || (cmd == 'S'))
                        {
                            if (last_cmd == 'C')
                            {
                                p = Reflect();
                            }
                            else
                            {
                                p = _lastPoint;
                            }

                            _secondLastPoint = ReadPoint(cmd, !_allowComma);
                        }
                        else
                        {
                            p = ReadPoint(cmd, !_allowComma);

                            _secondLastPoint = ReadPoint(cmd, _allowComma);
                        }

                        _lastPoint = ReadPoint(cmd, _allowComma);
                        _context.CubicBezierTo(
                            p.AsPointShape(),
                            _secondLastPoint.AsPointShape(),
                            _lastPoint.AsPointShape(),
                            _isStroked,
                            !_isSmoothJoin);

                        last_cmd = 'C';
                    }while (IsNumber(_allowComma));

                    break;

                case 'q':
                case 'Q':
                case 't':
                case 'T':
                    EnsureFigure();

                    do
                    {
                        if ((cmd == 't') || (cmd == 'T'))
                        {
                            if (last_cmd == 'Q')
                            {
                                _secondLastPoint = Reflect();
                            }
                            else
                            {
                                _secondLastPoint = _lastPoint;
                            }

                            _lastPoint = ReadPoint(cmd, !_allowComma);
                        }
                        else
                        {
                            _secondLastPoint = ReadPoint(cmd, !_allowComma);
                            _lastPoint       = ReadPoint(cmd, _allowComma);
                        }

                        _context.QuadraticBezierTo(
                            _secondLastPoint.AsPointShape(),
                            _lastPoint.AsPointShape(),
                            _isStroked,
                            !_isSmoothJoin);

                        last_cmd = 'Q';
                    }while (IsNumber(_allowComma));

                    break;

                case 'a':
                case 'A':
                    EnsureFigure();

                    do
                    {
                        double w        = ReadNumber(!_allowComma);
                        double h        = ReadNumber(_allowComma);
                        double rotation = ReadNumber(_allowComma);
                        bool   large    = ReadBool();
                        bool   sweep    = ReadBool();

                        _lastPoint = ReadPoint(cmd, _allowComma);

                        _context.ArcTo(
                            _lastPoint.AsPointShape(),
                            PathSize.Create(w, h),
                            rotation,
                            large,
                            sweep ? SweepDirection.Clockwise : SweepDirection.Counterclockwise,
                            _isStroked,
                            !_isSmoothJoin);
                    }while (IsNumber(_allowComma));

                    last_cmd = 'A';
                    break;

                case 'z':
                case 'Z':
                    EnsureFigure();
                    _context.SetClosedState(_isClosed);

                    _figureStarted = false;
                    last_cmd       = 'Z';
                    _lastPoint     = _lastStart;
                    break;

                default:
                    InvalidToken();
                    break;
                }
            }
        }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pg"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static Path.PathGeometry ToPathGeometry(this System.Windows.Media.PathGeometry pg, double dx, double dy)
        {
            var geometry = Path.PathGeometry.Create(
                ImmutableArray.Create <Path.PathFigure>(),
                pg.FillRule == System.Windows.Media.FillRule.EvenOdd ? Path.FillRule.EvenOdd : Path.FillRule.Nonzero);

            var context = new PathGeometryContext(geometry);

            foreach (var pf in pg.Figures)
            {
                context.BeginFigure(
                    PointShape.Create(pf.StartPoint.X + dx, pf.StartPoint.Y + dy),
                    pf.IsFilled,
                    pf.IsClosed);

                foreach (var segment in pf.Segments)
                {
                    if (segment is ArcSegment)
                    {
                        var arcSegment = segment as ArcSegment;
                        context.ArcTo(
                            PointShape.Create(arcSegment.Point.X + dx, arcSegment.Point.Y + dy),
                            PathSize.Create(arcSegment.Size.Width, arcSegment.Size.Height),
                            arcSegment.RotationAngle,
                            arcSegment.IsLargeArc,
                            arcSegment.SweepDirection == System.Windows.Media.SweepDirection.Clockwise ? Path.SweepDirection.Clockwise : Path.SweepDirection.Counterclockwise,
                            arcSegment.IsStroked,
                            arcSegment.IsSmoothJoin);
                    }
                    else if (segment is BezierSegment)
                    {
                        var cubicBezierSegment = segment as BezierSegment;
                        context.CubicBezierTo(
                            PointShape.Create(cubicBezierSegment.Point1.X + dx, cubicBezierSegment.Point1.Y + dy),
                            PointShape.Create(cubicBezierSegment.Point2.X + dx, cubicBezierSegment.Point2.Y + dy),
                            PointShape.Create(cubicBezierSegment.Point3.X + dx, cubicBezierSegment.Point3.Y + dy),
                            cubicBezierSegment.IsStroked,
                            cubicBezierSegment.IsSmoothJoin);
                    }
                    else if (segment is LineSegment)
                    {
                        var lineSegment = segment as LineSegment;
                        context.LineTo(
                            PointShape.Create(lineSegment.Point.X + dx, lineSegment.Point.Y + dy),
                            lineSegment.IsStroked,
                            lineSegment.IsSmoothJoin);
                    }
                    else if (segment is PolyBezierSegment)
                    {
                        var polyCubicBezierSegment = segment as PolyBezierSegment;
                        context.PolyCubicBezierTo(
                            ToPointShapes(polyCubicBezierSegment.Points, dx, dy),
                            polyCubicBezierSegment.IsStroked,
                            polyCubicBezierSegment.IsSmoothJoin);
                    }
                    else if (segment is PolyLineSegment)
                    {
                        var polyLineSegment = segment as PolyLineSegment;
                        context.PolyLineTo(
                            ToPointShapes(polyLineSegment.Points, dx, dy),
                            polyLineSegment.IsStroked,
                            polyLineSegment.IsSmoothJoin);
                    }
                    else if (segment is PolyQuadraticBezierSegment)
                    {
                        var polyQuadraticSegment = segment as PolyQuadraticBezierSegment;
                        context.PolyQuadraticBezierTo(
                            ToPointShapes(polyQuadraticSegment.Points, dx, dy),
                            polyQuadraticSegment.IsStroked,
                            polyQuadraticSegment.IsSmoothJoin);
                    }
                    else if (segment is QuadraticBezierSegment)
                    {
                        var quadraticBezierSegment = segment as QuadraticBezierSegment;
                        context.QuadraticBezierTo(
                            PointShape.Create(quadraticBezierSegment.Point1.X + dx, quadraticBezierSegment.Point1.Y + dy),
                            PointShape.Create(quadraticBezierSegment.Point2.X + dx, quadraticBezierSegment.Point2.Y + dy),
                            quadraticBezierSegment.IsStroked,
                            quadraticBezierSegment.IsSmoothJoin);
                    }
                    else
                    {
                        throw new NotSupportedException("Not supported segment type: " + segment.GetType());
                    }
                }
            }

            return(geometry);
        }
コード例 #4
0
ファイル: DesignerContext.cs プロジェクト: erisonliang/Core2D
        /// <summary>
        /// Initializes static designer context.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        public static void InitializeContext(IServiceProvider serviceProvider)
        {
            // Editor

            Editor = serviceProvider.GetService <ProjectEditor>();

            // Recent Projects

            Editor.RecentProjects = Editor.RecentProjects.Add(RecentFile.Create("Test1", "Test1.project"));
            Editor.RecentProjects = Editor.RecentProjects.Add(RecentFile.Create("Test2", "Test2.project"));

            // New Project

            Editor.OnNewProject();

            // Transform

            Transform = MatrixObject.Identity;

            // Data

            var db      = Database.Create("Db");
            var fields  = new string[] { "Column0", "Column1" };
            var columns = ImmutableArray.CreateRange(fields.Select(c => Column.Create(db, c)));

            db.Columns = columns;
            var values = Enumerable.Repeat("<empty>", db.Columns.Length).Select(c => Value.Create(c));
            var record = Record.Create(
                db,
                ImmutableArray.CreateRange(values));

            db.Records       = db.Records.Add(record);
            db.CurrentRecord = record;

            Database = db;
            Data     = Context.Create(record);
            Record   = record;

            // Project

            IProjectFactory factory = new ProjectFactory();

            Project = factory.GetProject();

            Template = PageContainer.CreateTemplate();

            Page = PageContainer.CreatePage();
            var layer = Page.Layers.FirstOrDefault();

            layer.Shapes      = layer.Shapes.Add(LineShape.Create(0, 0, null, null));
            Page.CurrentLayer = layer;
            Page.CurrentShape = layer.Shapes.FirstOrDefault();
            Page.Template     = Template;

            Document = DocumentContainer.Create();
            Layer    = LayerContainer.Create();
            Options  = Options.Create();

            // State

            State = ShapeState.Create();

            // Style

            ArgbColor       = ArgbColor.Create(128, 255, 0, 0);
            ArrowStyle      = ArrowStyle.Create();
            FontStyle       = FontStyle.Create();
            LineFixedLength = LineFixedLength.Create();
            LineStyle       = LineStyle.Create();
            Style           = ShapeStyle.Create("Default");
            TextStyle       = TextStyle.Create();

            // Shapes

            Arc             = ArcShape.Create(0, 0, Style, null);
            CubicBezier     = CubicBezierShape.Create(0, 0, Style, null);
            Ellipse         = EllipseShape.Create(0, 0, Style, null);
            Group           = GroupShape.Create(Constants.DefaulGroupName);
            Image           = ImageShape.Create(0, 0, Style, null, "key");
            Line            = LineShape.Create(0, 0, Style, null);
            Path            = PathShape.Create(Style, null);
            Point           = PointShape.Create();
            QuadraticBezier = QuadraticBezierShape.Create(0, 0, Style, null);
            Rectangle       = RectangleShape.Create(0, 0, Style, null);
            Text            = TextShape.Create(0, 0, Style, null, "Text");

            // Path

            ArcSegment                 = ArcSegment.Create(PointShape.Create(), PathSize.Create(), 180, true, SweepDirection.Clockwise, true, true);
            CubicBezierSegment         = CubicBezierSegment.Create(PointShape.Create(), PointShape.Create(), PointShape.Create(), true, true);
            LineSegment                = LineSegment.Create(PointShape.Create(), true, true);
            PathFigure                 = PathFigure.Create(PointShape.Create(), false, true);
            PathGeometry               = PathGeometry.Create(ImmutableArray.Create <PathFigure>(), FillRule.EvenOdd);
            PathSize                   = PathSize.Create();
            PolyCubicBezierSegment     = PolyCubicBezierSegment.Create(ImmutableArray.Create <PointShape>(), true, true);
            PolyLineSegment            = PolyLineSegment.Create(ImmutableArray.Create <PointShape>(), true, true);
            PolyQuadraticBezierSegment = PolyQuadraticBezierSegment.Create(ImmutableArray.Create <PointShape>(), true, true);
            QuadraticBezierSegment     = QuadraticBezierSegment.Create(PointShape.Create(), PointShape.Create(), true, true);
        }