Beispiel #1
0
        public ContentStreamProcessor(PdfRectangle cropBox, IResourceStore resourceStore, UserSpaceUnit userSpaceUnit, PageRotationDegrees rotation,
                                      IPdfTokenScanner pdfScanner,
                                      IPageContentParser pageContentParser,
                                      IFilterProvider filterProvider,
                                      ILog log,
                                      bool clipPaths)
        {
            this.resourceStore     = resourceStore;
            this.userSpaceUnit     = userSpaceUnit;
            this.rotation          = rotation;
            this.pdfScanner        = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner));
            this.pageContentParser = pageContentParser ?? throw new ArgumentNullException(nameof(pageContentParser));
            this.filterProvider    = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider));
            this.log       = log;
            this.clipPaths = clipPaths;

            // initiate CurrentClippingPath to cropBox
            var clippingSubpath = new PdfSubpath();

            clippingSubpath.Rectangle(cropBox.BottomLeft.X, cropBox.BottomLeft.Y, cropBox.Width, cropBox.Height);
            var clippingPath = new PdfPath()
            {
                clippingSubpath
            };

            clippingPath.SetClipping(FillingRule.NonZeroWinding);

            graphicsStack.Push(new CurrentGraphicsState()
            {
                CurrentClippingPath = clippingPath
            });
            ColorSpaceContext = new ColorSpaceContext(GetCurrentState, resourceStore);
        }
Beispiel #2
0
        internal static string ToFullSvg(this PdfSubpath p, double height)
        {
            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(height);
            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 #3
0
        public bool TryGenerate(string name, out PdfSubpath path)
        {
            path = default(PdfSubpath);
            lock (locker)
            {
                if (glyphs.TryGetValue(name, out path))
                {
                    return(true);
                }

                if (!CharStrings.TryGetValue(name, out var sequence))
                {
                    return(false);
                }

                try
                {
                    path = Run(sequence);

                    glyphs[name] = path;
                }
                catch
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #4
0
        public void BeginSubpath()
        {
            if (CurrentPath == null)
            {
                CurrentPath = new PdfPath();
            }

            AddCurrentSubpath();
            CurrentSubpath = new PdfSubpath();
        }
Beispiel #5
0
        public void IsCounterClockwise(double[][] source, bool expected)
        {
            PdfSubpath pdfPath = new PdfSubpath();

            pdfPath.MoveTo(source[0][0], source[0][1]);

            for (int i = 1; i < source.Length; i++)
            {
                var point = source[i];
                pdfPath.LineTo(point[0], point[1]);
            }
            pdfPath.LineTo(source[0][0], source[0][1]); // close path

            Assert.Equal(expected, pdfPath.IsCounterClockwise);
        }
Beispiel #6
0
        internal static string ToSvg(this PdfSubpath p, double height)
        {
            var builder = new StringBuilder();

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

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

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

            return(builder.ToString());
        }
Beispiel #7
0
        private static bool IsRectangle(PdfSubpath subpath)
        {
            if (subpath.IsDrawnAsRectangle)
            {
                return(true);
            }

            if (subpath.Commands.Any(c => c is BezierCurve))
            {
                return(false);                                            // redundant
            }
            var lines = subpath.Commands.OfType <Line>().ToList();

            if (lines.Count == 3 || lines.Count == 4)
            {
                var l0 = lines[0];
                var l1 = lines[1];
                var l2 = lines[2];

                // check if contains slanted lines
                if (l0.From.X != l0.To.X && l0.From.Y != l0.To.Y)
                {
                    return(false);
                }
                if (l1.From.X != l1.To.X && l1.From.Y != l1.To.Y)
                {
                    return(false);
                }
                if (l2.From.X != l2.To.X && l2.From.Y != l2.To.Y)
                {
                    return(false);
                }

                // more checks...
                return(true);
            }

            return(false);
        }
Beispiel #8
0
 public void SetPath(PdfSubpath path)
 {
     Path = path ?? throw new ArgumentNullException(nameof(path));
 }
Beispiel #9
0
 /// <summary>
 /// Create a new <see cref="Type2Glyph"/>.
 /// </summary>
 public Type2Glyph(PdfSubpath path, double?width)
 {
     Path  = path ?? throw new ArgumentNullException(nameof(path));
     Width = width;
 }
Beispiel #10
0
 public TestOperationContext()
 {
     StateStack.Push(new CurrentGraphicsState());
     CurrentSubpath    = new PdfSubpath();
     ColorSpaceContext = new ColorSpaceContext(GetCurrentState, new ResourceStore(new TestPdfTokenScanner(), new TestFontFactory()));
 }