Esempio n. 1
0
        /// <summary>
        /// Returns a rectangle geometry
        /// </summary>
        /// <param name="width">Width of the rectangle</param>
        /// <param name="height">Height of the rectangle</param>
        /// <param name="strokeThickness">Stroke thickness of the rectangle</param>
        /// <param name="topLeft">Top left corner definition of the rectangle</param>
        /// <param name="topRight">Top right corner definition of the rectangle</param>
        /// <param name="bottomRight">Bottom right corner definition of the rectangle</param>
        /// <param name="bottomLeft">Bottom left corner definition of the rectangle</param>
        /// <returns>Rectangle geometry</returns>
        public static Geometry GetRectangle(double width, double height, double strokeThickness, CornerRadius topLeft, CornerRadius topRight, CornerRadius bottomRight, CornerRadius bottomLeft)
        {
            var geometry = new PathGeometry();

            var pathFigure = new PathFigure();

            pathFigure.IsClosed = true;
            geometry.Figures.Add(pathFigure);

            var halfStroke = strokeThickness / 2d;

            if (topLeft != null && !topLeft.IsEmpty)
            {
                pathFigure.StartPoint = new Point(halfStroke, topLeft.RadiusY + halfStroke);
                pathFigure.Segments.Add(new ArcSegment {
                    Point = new Point(topLeft.RadiusX + halfStroke, halfStroke), RotationAngle = 90, SweepDirection = SweepDirection.Clockwise, Size = new Size(topLeft.RadiusX, topLeft.RadiusY), IsLargeArc = false
                });
            }
            else
            {
                pathFigure.StartPoint = new Point(halfStroke, halfStroke);
            }

            if (topRight != null && !topRight.IsEmpty)
            {
                pathFigure.Segments.Add(new LineSegment {
                    Point = new Point(width - topRight.RadiusX - halfStroke, halfStroke)
                });
                pathFigure.Segments.Add(new ArcSegment {
                    Point = new Point(width - halfStroke, topRight.RadiusY + halfStroke), RotationAngle = 90, SweepDirection = SweepDirection.Clockwise, Size = new Size(topRight.RadiusX, topRight.RadiusY)
                });
            }
            else
            {
                pathFigure.Segments.Add(new LineSegment {
                    Point = new Point(width - halfStroke, halfStroke)
                });
            }

            if (bottomRight != null && !bottomRight.IsEmpty)
            {
                pathFigure.Segments.Add(new LineSegment {
                    Point = new Point(width - halfStroke, height - bottomRight.RadiusY - halfStroke)
                });
                pathFigure.Segments.Add(new ArcSegment {
                    Point = new Point(width - bottomRight.RadiusX - halfStroke, height - halfStroke), RotationAngle = 90, SweepDirection = SweepDirection.Clockwise, Size = new Size(bottomRight.RadiusX, bottomRight.RadiusY)
                });
            }
            else
            {
                pathFigure.Segments.Add(new LineSegment {
                    Point = new Point(width - halfStroke, height - halfStroke)
                });
            }

            if (bottomLeft != null && !bottomLeft.IsEmpty)
            {
                pathFigure.Segments.Add(new LineSegment {
                    Point = new Point(bottomLeft.RadiusX + halfStroke, height - halfStroke)
                });
                pathFigure.Segments.Add(new ArcSegment {
                    Point = new Point(halfStroke, height - bottomLeft.RadiusY - halfStroke), RotationAngle = 90, SweepDirection = SweepDirection.Clockwise, Size = new Size(bottomLeft.RadiusX, bottomLeft.RadiusY)
                });
            }
            else
            {
                pathFigure.Segments.Add(new LineSegment {
                    Point = new Point(halfStroke, height - halfStroke)
                });
            }

            return(geometry);
        }
Esempio n. 2
0
        private void Split(string data)
        {
            try
            {
                var patterns = data?.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();

                if (patterns == null || patterns.Count == 0)
                {
                    this.TopRight    = null;
                    this.TopLeft     = null;
                    this.BottomLeft  = null;
                    this.BottomRight = null;
                    return;
                }

                double tlX = 0, tlY = 0, trX = 0, trY = 0, brX = 0, brY = 0, blX = 0, blY = 0;

                List <Match> matches = new List <Match>();

                foreach (var pattern in patterns)
                {
                    var match = Regex.Match(
                        pattern,
                        "^(?<name>tr|tl|br|bl)?(?<number>\\d+(\\.\\d+)?)$",
                        RegexOptions.IgnoreCase);
                    if (!match.Success)
                    {
                        throw this.CreateNotSupportedCornerRadiusException();
                    }

                    matches.Add(match);
                }

                if (matches[0].Groups["name"].Success)
                {
                    for (int i = 0; i < matches.Count; i++)
                    {
                        var currentMatch = matches[i];
                        var nextMatch    = i + 1 < matches.Count ? matches[i + 1] : null;

                        if (!currentMatch.Groups["name"].Success)
                        {
                            throw this.CreateNotSupportedCornerRadiusException();
                        }

                        Tuple <double, double, bool> valuesForCorner;
                        switch (currentMatch.Groups["name"].Value.ToLower())
                        {
                        case "tl":
                            valuesForCorner = this.GetXyFromMatches(currentMatch, nextMatch);
                            tlX             = valuesForCorner.Item1;
                            tlY             = valuesForCorner.Item2;
                            break;

                        case "tr":
                            valuesForCorner = this.GetXyFromMatches(currentMatch, nextMatch);
                            trX             = valuesForCorner.Item1;
                            trY             = valuesForCorner.Item2;
                            break;

                        case "bl":
                            valuesForCorner = this.GetXyFromMatches(currentMatch, nextMatch);
                            blX             = valuesForCorner.Item1;
                            blY             = valuesForCorner.Item2;
                            break;

                        case "br":
                            valuesForCorner = this.GetXyFromMatches(currentMatch, nextMatch);
                            brX             = valuesForCorner.Item1;
                            brY             = valuesForCorner.Item2;
                            break;

                        default: throw this.CreateNotSupportedCornerRadiusException();
                        }

                        if (valuesForCorner.Item3)
                        {
                            i++;
                        }
                    }
                }
                else
                {
                    try
                    {
                        if (matches.Count == 1)
                        {
                            tlX = tlY = trX = trY = brX = brY = blX = blY = Convert.ToDouble(
                                matches[0].Groups["number"].Value);
                        }
                        else if (matches.Count == 2)
                        {
                            tlX = trX = brX = blX = Convert.ToDouble(matches[0].Groups["number"].Value);
                            tlY = trY = brY = blY = Convert.ToDouble(matches[1].Groups["number"].Value);
                        }
                        else if (matches.Count == 4)
                        {
                            tlX = tlY = Convert.ToDouble(matches[0].Groups["number"].Value);
                            trX = trY = Convert.ToDouble(matches[1].Groups["number"].Value);
                            brX = brY = Convert.ToDouble(matches[2].Groups["number"].Value);
                            blX = blY = Convert.ToDouble(matches[3].Groups["number"].Value);
                        }
                        else if (matches.Count == 8)
                        {
                            tlX = Convert.ToDouble(matches[0].Groups["number"].Value);
                            trX = Convert.ToDouble(matches[2].Groups["number"].Value);
                            brX = Convert.ToDouble(matches[4].Groups["number"].Value);
                            blX = Convert.ToDouble(matches[6].Groups["number"].Value);
                            tlY = Convert.ToDouble(matches[1].Groups["number"].Value);
                            trY = Convert.ToDouble(matches[3].Groups["number"].Value);
                            brY = Convert.ToDouble(matches[5].Groups["number"].Value);
                            blY = Convert.ToDouble(matches[7].Groups["number"].Value);
                        }
                        else
                        {
                            throw this.CreateNotSupportedCornerRadiusException();
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is NotSupportedException)
                        {
                            throw;
                        }
                        throw this.CreateNotSupportedCornerRadiusException(e);
                    }
                }

                this.TopLeft     = new CornerRadius(tlX, tlY);
                this.TopRight    = new CornerRadius(trX, trY);
                this.BottomLeft  = new CornerRadius(blX, blY);
                this.BottomRight = new CornerRadius(brX, brY);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"CornerRadiusExpression format error : {ex.Message}");
            }
        }