Beispiel #1
0
        internal static string ToFullSvg(this PdfPath p)
        {
            string BboxToRect(PdfRectangle box, string stroke)
            {
                var overallBbox = $"<rect x='{box.Left}' y='{box.Bottom}' width='{box.Width}' height='{box.Height}' stroke-width='2' fill='none' stroke='{stroke}'></rect>";

                return(overallBbox);
            }

            var glyph  = p.ToSvg();
            var bbox   = p.GetBoundingRectangle();
            var bboxes = new List <PdfRectangle>();

            foreach (var command in p.Commands)
            {
                var segBbox = command.GetBoundingRectangle();
                if (segBbox.HasValue)
                {
                    bboxes.Add(segBbox.Value);
                }
            }

            var path     = $"<path d='{glyph}' stroke='cyan' stroke-width='3'></path>";
            var bboxRect = bbox.HasValue ? BboxToRect(bbox.Value, "yellow") : string.Empty;
            var others   = string.Join(" ", bboxes.Select(x => BboxToRect(x, "gray")));
            var result   = $"<svg width='500' height='500'><g transform=\"scale(0.2, -0.2) translate(100, -700)\">{path} {bboxRect} {others}</g></svg>";

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Simplify this <see cref="PdfPath"/> by converting everything to <see cref="PdfLine"/>s.
        /// </summary>
        /// <returns></returns>
        internal PdfPath Simplify()
        {
            PdfPath simplifiedPath = new PdfPath();
            var     startPoint     = GetStartPoint(Commands.First());

            simplifiedPath.MoveTo(startPoint.X, startPoint.Y);

            foreach (var command in Commands)
            {
                if (command is Line line)
                {
                    simplifiedPath.LineTo(line.To.X, line.To.Y);
                }
                else if (command is BezierCurve curve)
                {
                    foreach (var lineB in curve.ToLines(4))
                    {
                        simplifiedPath.LineTo(lineB.To.X, lineB.To.Y);
                    }
                }
            }

            // Check if Closed, if yes: make sure it is actually closed (last TO = first FROM)
            if (IsClosed())
            {
                var first = GetStartPoint(simplifiedPath.Commands.First());
                if (!first.Equals(GetEndPoint(simplifiedPath.Commands.Last())))
                {
                    simplifiedPath.LineTo(first.X, first.Y);
                }
            }

            return(simplifiedPath);
        }
Beispiel #3
0
        internal static string ToSvg(this PdfPath p)
        {
            var builder = new StringBuilder();

            foreach (var pathCommand in p.Commands)
            {
                pathCommand.WriteSvg(builder);
            }

            if (builder.Length == 0)
            {
                return(string.Empty);
            }

            if (builder[builder.Length - 1] == ' ')
            {
                builder.Remove(builder.Length - 1, 1);
            }

            return(builder.ToString());
        }