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; }
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); }
/// <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); }
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; }
private Matrix3x2 CalulateUpdatedMatrix(SvgGraphicsElement elm) { var matrix = activeMatrix; foreach (var t in elm.Transform) { matrix = matrix * t.Matrix.AsMatrix3x2(); } return(matrix); }
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); }
private static bool IsVisible(SvgGraphicsElement element) { if (element.Style.Display == CssDisplayType.None) { return(false); } if (element.Style.Visibility != CssVisibilityType.Visible) { return(false); } return(true); }
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)); } }
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)); }
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); }
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); } }
public static Color ConvertToColor(SvgPaintServer paintServer, VectorElementPdfPageViewport pageViewport, float pageHeight, SvgGraphicsElement element, PdfSpotColor spotColorInk) => paintServer?.Accept(new Visitor(pageViewport, element, pageHeight, spotColorInk));
private static bool ShouldDisplay(SvgGraphicsElement element) => element.Style.Display != CssDisplayType.None && element.Style.Visibility == CssVisibilityType.Visible;