Ejemplo n.º 1
0
        public PathF Rotate(float angle, PointF pivotPoint)
        {
            var path = new PathF();

            var index          = 0;
            var arcIndex       = 0;
            var clockwiseIndex = 0;

            foreach (var operation in _operations)
            {
                if (operation == PathOperation.MoveTo)
                {
                    var point = GetRotatedPoint(index++, pivotPoint, angle);
                    path.MoveTo(point);
                }
                else if (operation == PathOperation.Line)
                {
                    var point = GetRotatedPoint(index++, pivotPoint, angle);
                    path.LineTo(point.X, point.Y);
                }
                else if (operation == PathOperation.Quad)
                {
                    var controlPoint = GetRotatedPoint(index++, pivotPoint, angle);
                    var point        = GetRotatedPoint(index++, pivotPoint, angle);
                    path.QuadTo(controlPoint.X, controlPoint.Y, point.X, point.Y);
                }
                else if (operation == PathOperation.Cubic)
                {
                    var controlPoint1 = GetRotatedPoint(index++, pivotPoint, angle);
                    var controlPoint2 = GetRotatedPoint(index++, pivotPoint, angle);
                    var point         = GetRotatedPoint(index++, pivotPoint, angle);
                    path.CurveTo(controlPoint1.X, controlPoint1.Y, controlPoint2.X, controlPoint2.Y, point.X, point.Y);
                }
                else if (operation == PathOperation.Arc)
                {
                    var topLeft     = GetRotatedPoint(index++, pivotPoint, angle);
                    var bottomRight = GetRotatedPoint(index++, pivotPoint, angle);
                    var startAngle  = _arcAngles[arcIndex++];
                    var endAngle    = _arcAngles[arcIndex++];
                    var clockwise   = _arcClockwise[clockwiseIndex++];

                    path.AddArc(topLeft, bottomRight, startAngle, endAngle, clockwise);
                }
                else if (operation == PathOperation.Close)
                {
                    path.Close();
                }
            }

            return(path);
        }
Ejemplo n.º 2
0
        public PathF(PathF prototype, AffineTransformF transform = null) : this()
        {
            _operations.AddRange(prototype._operations);
            foreach (var point in prototype.Points)
            {
                var newPoint = point;

                if (transform != null)
                {
                    newPoint = transform.Transform(point);
                }

                _points.Add(newPoint);
            }
            if (prototype._arcAngles != null)
            {
                _arcAngles    = new List <float>();
                _arcClockwise = new List <bool>();

                _arcAngles.AddRange(prototype._arcAngles);
                _arcClockwise.AddRange(prototype._arcClockwise);
            }
        }
Ejemplo n.º 3
0
        public PathF BuildPath(string pathAsString)
        {
            try
            {
                _lastCommand           = '~';
                _lastCurveControlPoint = null;
                _path = null;
                _commandStack.Clear();
                _relativePoint = new PointF(0, 0);
                _closeWhenDone = false;

                pathAsString = pathAsString.Replace("Infinity", "0");
                pathAsString = Regex.Replace(pathAsString, "([a-zA-Z])", " $1 ");
                pathAsString = pathAsString.Replace("-", " -");
                pathAsString = pathAsString.Replace(" E  -", "E-");
                pathAsString = pathAsString.Replace(" e  -", "e-");

                var args = pathAsString.Split(new[] { ' ', '\r', '\n', '\t', ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (var i = args.Length - 1; i >= 0; i--)
                {
                    var entry = args[i];
                    var c     = entry[0];
                    if (char.IsLetter(c))
                    {
                        if (entry.Length > 1)
                        {
                            entry = entry.Substring(1);
                            if (char.IsLetter(entry[0]))
                            {
                                if (entry.Length > 1)
                                {
                                    _commandStack.Push(entry.Substring(1));
                                }

                                _commandStack.Push(entry[0].ToString(CultureInfo.InvariantCulture));
                            }
                            else
                            {
                                _commandStack.Push(entry);
                            }
                        }

                        _commandStack.Push(c.ToString(CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        _commandStack.Push(entry);
                    }
                }

                while (_commandStack.Count > 0)
                {
                    if (_path == null)
                    {
                        _path = new PathF();
                    }

                    var vCommand = _commandStack.Pop();
                    HandleCommand(vCommand);
                }

                if (_path != null && !_path.Closed)
                {
                    if (_closeWhenDone)
                    {
                        _path.Close();
                    }
                }
            }
            catch (Exception exc)
            {
                throw new Exception($"An error occurred parsing the path: {pathAsString}", exc);
            }

            return(_path);
        }