コード例 #1
0
 public Visitor(VectorElementPdfPageViewport pageViewport, SvgGraphicsElement element, float pageHeight, PdfSpotColor spotColorOverride)
 {
     _spotColorOverride = spotColorOverride;
     _pageViewport      = pageViewport ?? throw new ArgumentNullException(nameof(pageViewport));
     _element           = element ?? throw new ArgumentNullException(nameof(element));
     _pageHeight        = pageHeight;
 }
コード例 #2
0
        private void VisitPathSegList(SvgGraphicsElement graphicsElement, SvgPathSegList paths)
        {
            if (!ShouldDisplay(graphicsElement))
            {
                return;
            }

            PushTransformsMatrix(graphicsElement.Transform);
            {
                var pathsToDraw = paths
                                  .ConvertToAbsolute()
                                  .ConvertAllLinesAndCurvesToCubicCurves()
                                  .MultiplyByMatrix(CurrentMatrix);

                var svgToPdfPathConverter = new SvgPathSegToDynamicPdfPathsConverter(graphicsElement, null, 0, _spotColorOverride);
                pathsToDraw.ForEach(i => i.Accept(svgToPdfPathConverter));

                foreach (var path in svgToPdfPathConverter.Paths)
                {
                    path.Draw(_writer);
                    ClippingAreaPainted = true;
                }
            }
            PopTransformsMatrix(graphicsElement.Transform);
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of <see cref="SvgPathSegToDynamicPdfPathsConverter"/> using the specified <see cref="graphicsElement"/>
 /// for the styling properties of the resulting paths
 /// </summary>
 public SvgPathSegToDynamicPdfPathsConverter(SvgGraphicsElement graphicsElement, VectorElementPdfPageViewport pageViewport, float pageHeight, PdfSpotColor spotColorOverride)
 {
     _spotColorOverride = spotColorOverride;
     _pageViewport      = pageViewport;
     _pageHeight        = pageHeight;
     SetFillStyle(graphicsElement);
     SetStrokeStyle(graphicsElement);
 }
コード例 #4
0
        public void SetFillStyle(SvgGraphicsElement graphicsElement)
        {
            var style = graphicsElement?.Style;

            _fillEvenOdd = style?.FillRule == SvgFillRule.EvenOdd;
            _fillColor   = graphicsElement != null && _pageViewport != null
                ? SvgPaintServerToDynamicPdfColorConverter.ConvertToColor(graphicsElement.CreateFillPaintServer(), _pageViewport, _pageHeight, graphicsElement, _spotColorOverride)
                : null;
        }
コード例 #5
0
        private Matrix3x2 CalulateUpdatedMatrix(SvgGraphicsElement elm)
        {
            var matrix = activeMatrix;

            foreach (var t in elm.Transform)
            {
                matrix = matrix * t.Matrix.AsMatrix3x2();
            }
            return(matrix);
        }
コード例 #6
0
        private void RenderPathSegList(SvgGraphicsElement element, SvgPathSegList segs)
        {
            var reenderer = new PathRenderer();

            foreach (var seq in segs)
            {
                seq.Accept(reenderer);
            }
            var path = reenderer.Path();

            this.RenderShapeToCanvas(element, path);
        }
コード例 #7
0
        private static bool IsVisible(SvgGraphicsElement element)
        {
            if (element.Style.Display == CssDisplayType.None)
            {
                return(false);
            }

            if (element.Style.Visibility != CssVisibilityType.Visible)
            {
                return(false);
            }

            return(true);
        }
コード例 #8
0
        private void RenderShapeToCanvas(SvgGraphicsElement svgGraphicsElement, IPath path)
        {
            var matrix = CalulateUpdatedMatrix(svgGraphicsElement);

            var brush = svgGraphicsElement.CreateFillPaintServer()?.Accept(BrushGenerator <TPixel> .Instance);

            IBrush strokFill = null;
            IPath  outline   = null;

            if (svgGraphicsElement.StrokeWidth > 0)
            {
                strokFill = svgGraphicsElement.CreateStrokePaintServer()?.Accept(BrushGenerator <TPixel> .Instance);
                if (strokFill != null)
                {
                    var pattern = svgGraphicsElement.Style.StrokeDashArray.Value?.Select(X => X.Value).ToArray();
                    var joint   = svgGraphicsElement.Style.StrokeLineJoin.AsJointStyle();
                    var end     = svgGraphicsElement.Style.StrokeLineCap.AsEndCapStyle();
                    if (pattern == null || pattern.Length == 0)
                    {
                        outline = path.GenerateOutline(svgGraphicsElement.StrokeWidth, joint, end);
                    }
                    else
                    {
                        outline = path.GenerateOutline(svgGraphicsElement.StrokeWidth,
                                                       pattern,
                                                       false,
                                                       joint, end);
                    }
                }
            }

            var shapeOptions = new ShapeOptions {
                IntersectionRule = IntersectionRule.Nonzero
            };
            var shapeGraphicsOptions = new DrawingOptions()
            {
                ShapeOptions = shapeOptions
            };

            if (brush != null)
            {
                image.Fill(shapeGraphicsOptions, brush, path.Transform(matrix));
            }

            if (outline != null && strokFill != null)
            {
                image.Fill(shapeGraphicsOptions, strokFill, outline.Transform(matrix));
            }
        }
コード例 #9
0
        private PageElement VisitPathSegList(SvgGraphicsElement graphicsElement, SvgPathSegList paths)
        {
            if (!IsVisible(graphicsElement))
            {
                return(null);
            }

            var result = new Group();

            PushTransformsMatrix(graphicsElement.Transform);
            var innerMatrix = CurrentMatrix;

            var pathsToDraw = paths
                              .ConvertToAbsolute()
                              .ConvertAllLinesAndCurvesToCubicCurves()
                              .MultiplyByMatrix(CurrentMatrix);

            var fillOpacity           = Math.Max(0, Math.Min(1, graphicsElement.Style.FillOpacity));
            var strokeOpacity         = Math.Max(0, Math.Min(1, graphicsElement.Style.StrokeOpacity));
            var svgToPdfPathConverter = new SvgPathSegToDynamicPdfPathsConverter(graphicsElement, _pageViewport, _pageHeight, _spotColorOverride);

            bool IsPaintedPath(Path path) => path.FillColor != null || path.LineColor != null;

            if (Math.Abs(fillOpacity - 1) > float.Epsilon || Math.Abs(strokeOpacity - 1) > float.Epsilon)
            {
                var fillGroup = new TransparencyGroup(fillOpacity * _groupAlpha);
                svgToPdfPathConverter.SetStrokeStyle(null);
                pathsToDraw.ForEach(i => i.Accept(svgToPdfPathConverter));
                fillGroup.AddRange(svgToPdfPathConverter.Paths.Where(IsPaintedPath));

                svgToPdfPathConverter.Reset();
                svgToPdfPathConverter.SetFillStyle(null);
                svgToPdfPathConverter.SetStrokeStyle(graphicsElement);
                var strokeGroup = new TransparencyGroup(strokeOpacity * _groupAlpha);
                pathsToDraw.ForEach(i => i.Accept(svgToPdfPathConverter));
                strokeGroup.AddRange(svgToPdfPathConverter.Paths.Where(IsPaintedPath));

                result.Add(fillGroup);
                result.Add(strokeGroup);
            }
            else
            {
                pathsToDraw.ForEach(i => i.Accept(svgToPdfPathConverter));
                result.AddRange(svgToPdfPathConverter.Paths.Where(IsPaintedPath));
            }
            PopTransformsMatrix(graphicsElement.Transform);

            return(result.Count == 0 ? null : CheckAndApplyClipPath(graphicsElement, result, innerMatrix));
        }
コード例 #10
0
        private PageElement CheckAndApplyClipPath(SvgGraphicsElement element, PageElement result, SvgMatrix matrix)
        {
            if (result == null)
            {
                return(null);
            }

            var clipPathRef = element.PresentationStyleData.ClipPath.Value?.FragmentIdentifier;
            var clipPath    = _rootSvgElement.GetElementById(clipPathRef) as SvgClipPathElement;

            if (clipPath != null)
            {
                if (clipPath.Children.Count == 0)
                {
                    // clipPath defines the area to be rendered, no children, nothing to render
                    return(null);
                }
                return(new SvgClipPathPageElement(result, clipPath, matrix, _spotColorOverride));
            }
            return(result);
        }
コード例 #11
0
        public void SetStrokeStyle(SvgGraphicsElement graphicsElement)
        {
            var style = graphicsElement?.Style;

            _strokeColor = graphicsElement != null && _pageViewport != null
                ? SvgPaintServerToDynamicPdfColorConverter.ConvertToColor(graphicsElement.CreateStrokePaintServer(), _pageViewport, _pageHeight, graphicsElement, _spotColorOverride)
                : null;

            _strokeWidth = _strokeColor != null ? graphicsElement?.StrokeWidth ?? 0 : 0;
            _lineJoin    = style?.StrokeLineJoin.ConvertToDynamicPdf() ?? LineJoin.Miter;
            _miterLimit  = style?.StrokeMiterLimit ?? 1f;
            _lineCap     = style?.StrokeLineCap.ConvertToDynamicPdf() ?? LineCap.Butt;
            _strokeStyle = LineStyle.Solid;

            var dashArray = style?.StrokeDashArray.Value;

            if (dashArray?.Length > 0)
            {
                var dashValues = dashArray.Select(i => i.Value).ToArray();
                _strokeStyle = new LineStyle(dashValues, style.StrokeDashOffset.Value.Value);
            }
        }
コード例 #12
0
 public static Color ConvertToColor(SvgPaintServer paintServer, VectorElementPdfPageViewport pageViewport, float pageHeight, SvgGraphicsElement element, PdfSpotColor spotColorInk)
 => paintServer?.Accept(new Visitor(pageViewport, element, pageHeight, spotColorInk));
コード例 #13
0
 private static bool ShouldDisplay(SvgGraphicsElement element)
 => element.Style.Display != CssDisplayType.None && element.Style.Visibility == CssVisibilityType.Visible;