Beispiel #1
0
        public void RenderMarker0(WpfDrawingRenderer renderer, WpfDrawingContext gr,
                                  SvgMarkerPosition markerPos, SvgStyleableElement refElement)
        {
            //PathGeometry g;
            //g.GetPointAtFractionLength(

            ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
            SvgMarkerElement markerElm     = (SvgMarkerElement)_svgElement;

            SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
            int         start;
            int         len;

            // Choose which part of the position array to use
            switch (markerPos)
            {
            case SvgMarkerPosition.Start:
                start = 0;
                len   = 1;
                break;

            case SvgMarkerPosition.Mid:
                start = 1;
                len   = vertexPositions.Length - 2;
                break;

            default:
                // == MarkerPosition.End
                start = vertexPositions.Length - 1;
                len   = 1;
                break;
            }

            for (int i = start; i < start + len; i++)
            {
                SvgPointF point = vertexPositions[i];

                Matrix m = GetTransformMatrix(_svgElement);

                //GraphicsContainer gc = gr.BeginContainer();

                this.BeforeRender(renderer);

                //gr.TranslateTransform(point.X, point.Y);

                //PAUL:
                //m.Translate(point.X, point.Y);

                if (markerElm.OrientType.AnimVal.Equals((ushort)SvgMarkerOrient.Angle))
                {
                    m.Rotate(markerElm.OrientAngle.AnimVal.Value);
                    //gr.RotateTransform((double)markerElm.OrientAngle.AnimVal.Value);
                }
                else
                {
                    double angle;

                    switch (markerPos)
                    {
                    case SvgMarkerPosition.Start:
                        angle = markerHostElm.GetStartAngle(i + 1);
                        break;

                    case SvgMarkerPosition.Mid:
                        //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
                        angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
                        break;

                    default:
                        angle = markerHostElm.GetEndAngle(i);
                        break;
                    }
                    //gr.RotateTransform(angle);
                    m.Rotate(angle);
                }

                if (markerElm.MarkerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth))
                {
                    string propValue = refElement.GetPropertyValue("stroke-width");
                    if (propValue.Length == 0)
                    {
                        propValue = "1";
                    }

                    SvgLength strokeWidthLength = new SvgLength("stroke-width", propValue, refElement, SvgLengthDirection.Viewport);
                    double    strokeWidth       = strokeWidthLength.Value;
                    //gr.ScaleTransform(strokeWidth, strokeWidth);
                    m.Scale(strokeWidth, strokeWidth);
                }

                SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal;
                double[] translateAndScale  = spar.FitToViewBox(
                    (SvgRect)markerElm.ViewBox.AnimVal, new SvgRect(0, 0,
                                                                    markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value));


                //PAUL:
                //m.Translate(-(double)markerElm.RefX.AnimVal.Value * translateAndScale[2], -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3]);

                //PAUL:
                m.Scale(translateAndScale[2], translateAndScale[3]);
                m.Translate(point.X, point.Y);

                //Matrix oldTransform = TransformMatrix;
                //TransformMatrix = m;
                //try
                //{
                //newTransform.Append(m);
                //TransformGroup tg = new TransformGroup();

                //renderer.Canvas.re

                //gr.TranslateTransform(
                //    -(double)markerElm.RefX.AnimVal.Value * translateAndScale[2],
                //    -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3]
                //    );

                //gr.ScaleTransform(translateAndScale[2], translateAndScale[3]);

                renderer.RenderChildren(markerElm);
                //                markerElm.RenderChildren(renderer);
                //}
                //finally
                //{
                //    TransformMatrix = oldTransform;
                //}
                //    //gr.EndContainer(gc);

                _matrix = m;
                this.Render(renderer);

                //gr.EndContainer(gc);

                this.AfterRender(renderer);
            }
        }
        private GraphicsPath CreateClippingRegion(GdiGraphics graphics, SvgClipPathElement clipPath)
        {
            GraphicsPath path = new GraphicsPath();

            foreach (XmlNode node in clipPath.ChildNodes)
            {
                if (node.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                // Handle a case where the clip element has "use" element as a child...
                if (string.Equals(node.LocalName, "use"))
                {
                    SvgUseElement useElement = (SvgUseElement)node;

                    XmlElement refEl = useElement.ReferencedElement;
                    if (refEl != null)
                    {
                        XmlElement refElParent = (XmlElement)refEl.ParentNode;
                        useElement.OwnerDocument.Static = true;
                        useElement.CopyToReferencedElement(refEl);
                        refElParent.RemoveChild(refEl);
                        useElement.AppendChild(refEl);

                        foreach (XmlNode useChild in useElement.ChildNodes)
                        {
                            if (useChild.NodeType != XmlNodeType.Element)
                            {
                                continue;
                            }

                            SvgStyleableElement element = useChild as SvgStyleableElement;
                            if (element != null && element.RenderingHint == SvgRenderingHint.Shape)
                            {
                                GraphicsPath childPath = CreatePath(element);

                                if (childPath != null)
                                {
                                    string clipRule = element.GetPropertyValue("clip-rule");
                                    path.FillMode = (clipRule == "evenodd") ? FillMode.Alternate : FillMode.Winding;

                                    path.AddPath(childPath, true);
                                }
                            }
                        }

                        useElement.RemoveChild(refEl);
                        useElement.RestoreReferencedElement(refEl);
                        refElParent.AppendChild(refEl);
                        useElement.OwnerDocument.Static = false;
                    }
                }
                else
                {
                    SvgStyleableElement element = node as SvgStyleableElement;
                    if (element != null)
                    {
                        if (element.RenderingHint == SvgRenderingHint.Shape)
                        {
                            GraphicsPath childPath = CreatePath(element);

                            if (childPath != null)
                            {
                                string clipRule = element.GetPropertyValue("clip-rule");
                                path.FillMode = (clipRule == "evenodd") ? FillMode.Alternate : FillMode.Winding;

                                path.AddPath(childPath, true);
                            }
                        }
                        else if (element.RenderingHint == SvgRenderingHint.Text)
                        {
                            GdiTextRendering textRendering = new GdiTextRendering(element);
                            textRendering.TextMode = GdiTextMode.Outlining;

                            GdiGraphicsRenderer renderer = new GdiGraphicsRenderer(graphics);

                            textRendering.BeforeRender(renderer);
                            textRendering.Render(renderer);
                            textRendering.AfterRender(renderer);

                            GraphicsPath childPath = textRendering.Path;
                            if (childPath != null)
                            {
                                string clipRule = element.GetPropertyValue("clip-rule");
                                path.FillMode = (clipRule == "evenodd") ? FillMode.Alternate : FillMode.Winding;

                                path.AddPath(childPath, true);
                            }
                        }
                    }
                }
            }

            return(path);
        }
Beispiel #3
0
        private void RenderPath(WpfDrawingRenderer renderer)
        {
            WpfDrawingContext context = renderer.Context;

            SvgRenderingHint hint = _svgElement.RenderingHint;

            if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            var parentNode = _svgElement.ParentNode;

            // We do not directly render the contents of the clip-path, unless specifically requested...
            if (string.Equals(parentNode.LocalName, "clipPath") &&
                !context.RenderingClipRegion)
            {
                return;
            }

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            string sVisibility = styleElm.GetPropertyValue("visibility");
            string sDisplay    = styleElm.GetPropertyValue("display");

            if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            {
                return;
            }

            DrawingGroup drawGroup = context.Peek();

            Debug.Assert(drawGroup != null);

            Geometry geometry = CreateGeometry(_svgElement, context.OptimizePath);

            string elementId    = this.GetElementName();
            string elementClass = this.GetElementClass();

            if (geometry != null && !geometry.IsEmpty())
            {
                context.UpdateBounds(geometry.Bounds);

//                SetClip(context);

                WpfSvgPaint fillPaint = new WpfSvgPaint(context, styleElm, "fill");

                string fileValue = styleElm.GetAttribute("fill");

                Brush brush            = fillPaint.GetBrush(geometry);
                bool  isFillTransmable = fillPaint.IsFillTransformable;

                WpfSvgPaint strokePaint = new WpfSvgPaint(context, styleElm, "stroke");
                Pen         pen         = strokePaint.GetPen(geometry);

                if (_paintContext != null)
                {
                    _paintContext.Fill   = fillPaint;
                    _paintContext.Stroke = strokePaint;
                    _paintContext.Tag    = geometry;
                }

                if (brush != null || pen != null)
                {
                    Transform transform = this.Transform;
                    if (transform != null && !transform.Value.IsIdentity)
                    {
                        geometry.Transform = transform;
                        if (brush != null && isFillTransmable)
                        {
                            Transform brushTransform = brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                brush.Transform = groupTransform;
                            }
                        }
                        if (pen != null && pen.Brush != null)
                        {
                            Transform brushTransform = pen.Brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                pen.Brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                pen.Brush.Transform = groupTransform;
                            }
                        }
                    }
                    else
                    {
                        transform = null; // render any identity transform useless...
                    }

                    GeometryDrawing drawing = new GeometryDrawing(brush, pen, geometry);

                    if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                    {
                        SvgObject.SetName(drawing, elementId);

                        context.RegisterId(elementId);

                        if (context.IncludeRuntime)
                        {
                            SvgObject.SetId(drawing, elementId);
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime)
                    {
                        SvgObject.SetClass(drawing, elementClass);
                    }

                    Brush    maskBrush = this.Masking;
                    Geometry clipGeom  = this.ClipGeometry;
                    if (clipGeom != null || maskBrush != null)
                    {
                        //Geometry clipped = Geometry.Combine(geometry, clipGeom,
                        //    GeometryCombineMode.Exclude, null);

                        //if (clipped != null && !clipped.IsEmpty())
                        //{
                        //    geometry = clipped;
                        //}
                        DrawingGroup clipMaskGroup = new DrawingGroup();

                        Rect geometryBounds = geometry.Bounds;

                        if (clipGeom != null)
                        {
                            clipMaskGroup.ClipGeometry = clipGeom;

                            SvgUnitType clipUnits = this.ClipUnits;
                            if (clipUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }

                                TransformGroup transformGroup = new TransformGroup();

                                // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                transformGroup.Children.Add(new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                                transformGroup.Children.Add(new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                                clipGeom.Transform = transformGroup;
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    clipGeom.Transform = transform;
                                }
                            }
                        }
                        if (maskBrush != null)
                        {
                            DrawingBrush drawingBrush = (DrawingBrush)maskBrush;

                            SvgUnitType maskUnits        = this.MaskUnits;
                            SvgUnitType maskContentUnits = this.MaskContentUnits;
                            if (maskUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }
                                DrawingGroup maskGroup = drawingBrush.Drawing as DrawingGroup;
                                if (maskGroup != null)
                                {
                                    DrawingCollection maskDrawings = maskGroup.Children;
                                    for (int i = 0; i < maskDrawings.Count; i++)
                                    {
                                        Drawing         maskDrawing  = maskDrawings[i];
                                        GeometryDrawing maskGeomDraw = maskDrawing as GeometryDrawing;
                                        if (maskGeomDraw != null)
                                        {
                                            if (maskGeomDraw.Brush != null)
                                            {
                                                ConvertColors(maskGeomDraw.Brush);
                                            }
                                            if (maskGeomDraw.Pen != null)
                                            {
                                                ConvertColors(maskGeomDraw.Pen.Brush);
                                            }
                                        }
                                    }
                                }

                                if (maskContentUnits == SvgUnitType.ObjectBoundingBox)
                                {
                                    TransformGroup transformGroup = new TransformGroup();

                                    // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                    var scaleTransform = new ScaleTransform(drawingBounds.Width, drawingBounds.Height);
                                    transformGroup.Children.Add(scaleTransform);
                                    var translateTransform = new TranslateTransform(drawingBounds.X, drawingBounds.Y);
                                    transformGroup.Children.Add(translateTransform);

                                    Matrix scaleMatrix     = new Matrix();
                                    Matrix translateMatrix = new Matrix();

                                    scaleMatrix.Scale(drawingBounds.Width, drawingBounds.Height);
                                    translateMatrix.Translate(drawingBounds.X, drawingBounds.Y);

                                    Matrix matrix = Matrix.Multiply(scaleMatrix, translateMatrix);
                                    //maskBrush.Transform = transformGroup;
                                    maskBrush.Transform = new MatrixTransform(matrix);
                                }
                                else
                                {
                                    drawingBrush.Viewbox      = drawingBounds;
                                    drawingBrush.ViewboxUnits = BrushMappingMode.Absolute;

                                    drawingBrush.Stretch = Stretch.Uniform;

                                    drawingBrush.Viewport      = drawingBounds;
                                    drawingBrush.ViewportUnits = BrushMappingMode.Absolute;
                                }
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    maskBrush.Transform = transform;
                                }
                            }

                            clipMaskGroup.OpacityMask = maskBrush;
                        }

                        clipMaskGroup.Children.Add(drawing);
                        drawGroup.Children.Add(clipMaskGroup);
                    }
                    else
                    {
                        drawGroup.Children.Add(drawing);
                    }
                }
            }

            // If this is not the child of a "marker", then try rendering a marker...
            if (!string.Equals(parentNode.LocalName, "marker"))
            {
                RenderMarkers(renderer, styleElm, context);
            }
        }
Beispiel #4
0
        public override void BeforeRender(WpfDrawingRenderer renderer)
        {
            base.BeforeRender(renderer);

            if (renderer == null)
            {
                return;
            }

            _isLineSegment   = false;
            _setBrushOpacity = true;

            WpfDrawingContext context = renderer.Context;

            //SetQuality(context);
            //SetTransform(context);
            //SetMask(context);

//            _drawGroup = new DrawingGroup();

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            float opacityValue   = -1;
            bool  isStyleOpacity = false;

            string opacity = styleElm.GetAttribute("opacity");

            if (string.IsNullOrWhiteSpace(opacity))
            {
                opacity = styleElm.GetPropertyValue("opacity");
                if (!string.IsNullOrWhiteSpace(opacity))
                {
                    isStyleOpacity = true;
                }
            }
            if (!string.IsNullOrWhiteSpace(opacity))
            {
                opacityValue = (float)SvgNumber.ParseNumber(opacity);
                opacityValue = Math.Min(opacityValue, 1);
                opacityValue = Math.Max(opacityValue, 0);
                if (isStyleOpacity && (opacityValue >= 0 && opacityValue < 1))
                {
                    _setBrushOpacity = false;
                    if (styleElm.HasAttribute("fill-opacity") || (styleElm.HasAttribute("style") &&
                                                                  styleElm.GetAttribute("style").Contains("fill-opacity")))
                    {
                        _setBrushOpacity = true;
                    }
                }
            }
            //string eVisibility = _svgElement.GetAttribute("visibility");
            //string eDisplay    = _svgElement.GetAttribute("display");
            //if (string.Equals(eVisibility, "hidden") || string.Equals(eDisplay, "none"))
            //{
            //    opacityValue = 0;
            //}
            //else
            //{
            //    string sVisibility = styleElm.GetPropertyValue("visibility");
            //    string sDisplay = styleElm.GetPropertyValue("display");
            //    if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            //    {
            //        opacityValue = 0;
            //    }
            //}
            string sVisibility = styleElm.GetPropertyValue("visibility");

            if (string.IsNullOrWhiteSpace(sVisibility))
            {
                sVisibility = _svgElement.GetAttribute("visibility");
            }
            string sDisplay = styleElm.GetPropertyValue("display");

            if (string.IsNullOrWhiteSpace(sDisplay))
            {
                sDisplay = _svgElement.GetAttribute("display");
            }
            if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            {
                opacityValue = 0;
            }

            Transform pathTransform = this.Transform;

            if (pathTransform != null && !pathTransform.Value.IsIdentity)
            {
                if (_drawGroup == null)
                {
                    _drawGroup = new DrawingGroup();
                }
                _drawGroup.Transform = pathTransform;
            }
            else
            {
                pathTransform = null; // render any identity transform useless...
            }

            Geometry pathClip = this.ClipGeometry;

            if (pathClip != null && !pathClip.IsEmpty())
            {
                if (_drawGroup == null)
                {
                    _drawGroup = new DrawingGroup();
                }
                _drawGroup.ClipGeometry = pathClip;
            }
            else
            {
                pathClip = null; // render any empty geometry useless...
            }
            Brush pathMask = this.Masking;

            if (pathMask != null)
            {
                if (_drawGroup == null)
                {
                    _drawGroup = new DrawingGroup();
                }
                _drawGroup.OpacityMask = pathMask;
            }

            if (pathTransform != null || pathClip != null || pathMask != null || (opacityValue >= 0 && opacityValue < 1))
            {
                if (_drawGroup == null)
                {
                    _drawGroup = new DrawingGroup();
                }
                if ((opacityValue >= 0 && opacityValue < 1))
                {
                    _drawGroup.Opacity = opacityValue;
                }

                DrawingGroup curGroup = _context.Peek();
                Debug.Assert(curGroup != null);
                if (curGroup != null)
                {
                    curGroup.Children.Add(_drawGroup);
                    context.Push(_drawGroup);
                }
            }
            else
            {
                _drawGroup = null;
            }

            if (_drawGroup != null)
            {
                string elementClass = this.GetElementClass();
                if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime)
                {
                    SvgObject.SetClass(_drawGroup, elementClass);
                }

                string elementId = this.GetElementName();
                if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                {
                    SvgObject.SetName(_drawGroup, elementId);

                    context.RegisterId(elementId);

                    if (context.IncludeRuntime)
                    {
                        SvgObject.SetId(_drawGroup, elementId);
                    }
                }
            }
        }
Beispiel #5
0
        protected static void PaintMarkers(GdiGraphicsRenderer renderer,
                                           SvgStyleableElement styleElm, GdiGraphics gr)
        {
            // OPTIMIZE

            if (styleElm is ISharpMarkerHost)
            {
                string markerStartUrl  = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-start", "marker"));
                string markerMiddleUrl = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-mid", "marker"));
                string markerEndUrl    = ExtractMarkerUrl(styleElm.GetPropertyValue("marker-end", "marker"));
                string markerAll       = ExtractMarkerUrl(styleElm.GetPropertyValue("marker", "marker"));

                //  The SVG specification defines three properties to reference markers: marker-start,
                // marker -mid, marker-end. It also provides a shorthand property, marker. Using the marker
                // property from a style sheet is equivalent to using all three (start, mid, end).
                // However, shorthand properties cannot be used as presentation attributes.
                if (!string.IsNullOrWhiteSpace(markerAll) && !IsPresentationMarker(styleElm))
                {
                    if (string.IsNullOrWhiteSpace(markerStartUrl))
                    {
                        markerStartUrl = markerAll;
                    }
                    if (string.IsNullOrWhiteSpace(markerMiddleUrl))
                    {
                        markerMiddleUrl = markerAll;
                    }
                    if (string.IsNullOrWhiteSpace(markerEndUrl))
                    {
                        markerEndUrl = markerAll;
                    }
                }

                if (markerStartUrl.Length > 0)
                {
                    GdiMarkerRendering grNode = CreateByUri(styleElm.OwnerDocument,
                                                            styleElm.BaseURI, markerStartUrl) as GdiMarkerRendering;
                    if (grNode != null)
                    {
                        grNode.PaintMarker(renderer, gr, SvgMarkerPosition.Start, styleElm);
                    }
                }

                if (markerMiddleUrl.Length > 0)
                {
                    // TODO markerMiddleUrl != markerStartUrl
                    GdiMarkerRendering grNode = CreateByUri(styleElm.OwnerDocument,
                                                            styleElm.BaseURI, markerMiddleUrl) as GdiMarkerRendering;
                    if (grNode != null)
                    {
                        grNode.PaintMarker(renderer, gr, SvgMarkerPosition.Mid, styleElm);
                    }
                }

                if (markerEndUrl.Length > 0)
                {
                    // TODO: markerEndUrl != markerMiddleUrl
                    GdiMarkerRendering grNode = CreateByUri(styleElm.OwnerDocument,
                                                            styleElm.BaseURI, markerEndUrl) as GdiMarkerRendering;
                    if (grNode != null)
                    {
                        grNode.PaintMarker(renderer, gr, SvgMarkerPosition.End, styleElm);
                    }
                }
            }
        }
Beispiel #6
0
        public override void BeforeRender(WpfDrawingRenderer renderer)
        {
            base.BeforeRender(renderer);

            if (renderer == null)
            {
                return;
            }

            WpfDrawingContext context = renderer.Context;

            //SetQuality(context);
            //SetTransform(context);
            //SetMask(context);

            _drawGroup = new DrawingGroup();

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            Transform pathTransform = this.Transform;

            if (pathTransform != null && !pathTransform.Value.IsIdentity)
            {
                _drawGroup.Transform = pathTransform;
            }
            else
            {
                pathTransform = null; // render any identity transform useless...
            }
            Geometry pathClip = this.ClipGeometry;

            if (pathClip != null && !pathClip.IsEmpty())
            {
                _drawGroup.ClipGeometry = pathClip;
            }
            else
            {
                pathClip = null; // render any empty geometry useless...
            }
            Brush pathMask = this.Masking;

            if (pathMask != null)
            {
                _drawGroup.OpacityMask = pathMask;
            }

            if (pathTransform != null || pathClip != null || pathMask != null)
            {
                DrawingGroup curGroup = _context.Peek();
                Debug.Assert(curGroup != null);
                if (curGroup != null)
                {
                    curGroup.Children.Add(_drawGroup);
                    context.Push(_drawGroup);
                }
            }
            else
            {
                _drawGroup = null;
            }

            if (_drawGroup != null)
            {
                string sVisibility = styleElm.GetPropertyValue("visibility");
                string sDisplay    = styleElm.GetPropertyValue("display");
                if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
                {
                    _drawGroup.Opacity = 0;
                }

                string elementClass = this.GetElementClass();
                if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime)
                {
                    SvgObject.SetClass(_drawGroup, elementClass);
                }

                string elementId = this.GetElementName();
                if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                {
                    SvgObject.SetName(_drawGroup, elementId);

                    context.RegisterId(elementId);

                    if (context.IncludeRuntime)
                    {
                        SvgObject.SetId(_drawGroup, elementId);
                    }
                }
            }
        }
        public override void Render(GdiGraphicsRenderer renderer)
        {
            _graphics = renderer.GdiGraphics;

            //SvgRenderingHint hint = _svgElement.RenderingHint;
            //if (hint == SvgRenderingHint.Clipping)
            //{
            //    return;
            //}
            //if (_svgElement.ParentNode is SvgClipPathElement)
            //{
            //    return;
            //}

            SvgTextBaseElement textElement = _svgElement as SvgTextBaseElement;

            if (textElement == null)
            {
                return;
            }

            string sVisibility = textElement.GetPropertyValue("visibility");
            string sDisplay    = textElement.GetPropertyValue("display");

            if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            {
                return;
            }

            if (_textMode != GdiTextMode.Outlining)
            {
                SetClip(_graphics);
            }

            PointF ctp = new PointF(0, 0); // current text position

            ctp = GetCurrentTextPosition(textElement, ctp);
            string sBaselineShift = textElement.GetPropertyValue("baseline-shift").Trim();
            double shiftBy        = 0;

            if (sBaselineShift.Length > 0)
            {
                float textFontSize = GetComputedFontSize(textElement);
                if (sBaselineShift.EndsWith("%", StringComparison.OrdinalIgnoreCase))
                {
                    shiftBy = SvgNumber.ParseNumber(sBaselineShift.Substring(0,
                                                                             sBaselineShift.Length - 1)) / 100 * textFontSize;
                }
                else if (sBaselineShift == "sub")
                {
                    shiftBy = -0.6F * textFontSize;
                }
                else if (sBaselineShift == "super")
                {
                    shiftBy = 0.6F * textFontSize;
                }
                else if (sBaselineShift == "baseline")
                {
                    shiftBy = 0;
                }
                else
                {
                    shiftBy = SvgNumber.ParseNumber(sBaselineShift);
                }
            }

            // For for fonts loading in the background...
            var svgDoc = _svgElement.OwnerDocument;

            if (svgDoc.IsFontsLoaded == false)
            {
                //TODO: Use of SpinUntil is known to CPU heavy, but will work for now...
                SpinWait.SpinUntil(() => svgDoc.IsFontsLoaded == true);
            }

            XmlNodeType nodeType = XmlNodeType.None;

            foreach (XmlNode child in _svgElement.ChildNodes)
            {
                SvgStyleableElement stylable = child as SvgStyleableElement;
                if (stylable != null)
                {
                    sVisibility = stylable.GetPropertyValue("visibility");
                    sDisplay    = stylable.GetPropertyValue("display");
                    if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
                    {
                        continue;
                    }
                }

                nodeType = child.NodeType;
                if (nodeType == XmlNodeType.Text)
                {
                    ctp.Y -= (float)shiftBy;
                    AddGraphicsPath(textElement, ref ctp, GetText(textElement, child));
                    ctp.Y += (float)shiftBy;
                }
                else if (nodeType == XmlNodeType.Element)
                {
                    string nodeName = child.Name;
                    if (string.Equals(nodeName, "tref"))
                    {
                        AddTRefElementPath((SvgTRefElement)child, ref ctp);
                    }
                    else if (string.Equals(nodeName, "tspan"))
                    {
                        AddTSpanElementPath((SvgTSpanElement)child, ref ctp);
                    }
                }
            }

            PaintMarkers(renderer, textElement, _graphics);

            _graphics = null;
        }
Beispiel #8
0
        private void RenderPath(WpfDrawingRenderer renderer)
        {
            WpfDrawingContext context = renderer.Context;

            SvgRenderingHint hint = _svgElement.RenderingHint;

            if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            var parentNode = _svgElement.ParentNode;

            // We do not directly render the contents of the clip-path, unless specifically requested...
            if (string.Equals(parentNode.LocalName, "clipPath") &&
                !context.RenderingClipRegion)
            {
                return;
            }

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            string sVisibility = styleElm.GetPropertyValue("visibility");
            string sDisplay    = styleElm.GetPropertyValue("display");

            if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            {
                return;
            }

            DrawingGroup drawGroup = context.Peek();

            Debug.Assert(drawGroup != null);

            Geometry geometry = CreateGeometry(_svgElement, context.OptimizePath);

            string elementId    = this.GetElementName();
            string elementClass = this.GetElementClass();

            if (geometry != null && !geometry.IsEmpty())
            {
                context.UpdateBounds(geometry.Bounds);

//                SetClip(context);

                WpfSvgPaint fillPaint = new WpfSvgPaint(context, styleElm, "fill");

                string fileValue = styleElm.GetAttribute("fill");

                Brush brush            = fillPaint.GetBrush(geometry, _setBrushOpacity);
                bool  isFillTransmable = fillPaint.IsFillTransformable;

                WpfSvgPaint strokePaint = new WpfSvgPaint(context, styleElm, "stroke");
                Pen         pen         = strokePaint.GetPen(geometry, _setBrushOpacity);

                // By the SVG Specifications:
                // Keyword 'objectBoundingBox' should not be used when the geometry of the applicable
                // element has no width or no height, such as the case of a horizontal or vertical line,
                // even when the line has actual thickness when viewed due to having a non-zero stroke
                // width since stroke width is ignored for bounding box calculations. When the geometry
                // of the applicable element has no width or height and 'objectBoundingBox' is specified,
                // then the given effect (e.g., a gradient) will be ignored.
                if (pen != null && _isLineSegment && strokePaint.FillType == WpfFillType.Gradient)
                {
                    LineGeometry    lineGeometry = geometry as LineGeometry;
                    WpfGradientFill gradientFill = (WpfGradientFill)strokePaint.PaintServer;
                    if (gradientFill.IsUserSpace == false && lineGeometry != null)
                    {
                        bool invalidGrad = SvgObject.IsEqual(lineGeometry.EndPoint.X, lineGeometry.StartPoint.X) ||
                                           SvgObject.IsEqual(lineGeometry.EndPoint.Y, lineGeometry.StartPoint.Y);
                        if (invalidGrad)
                        {
                            // Brush is not likely inherited, we need to support fallback too
                            WpfSvgPaint fallbackPaint = strokePaint.WpfFallback;
                            if (fallbackPaint != null)
                            {
                                pen.Brush = fallbackPaint.GetBrush(geometry, _setBrushOpacity);
                            }
                            else
                            {
                                var scopePaint = strokePaint.GetScopeStroke();
                                if (scopePaint != null)
                                {
                                    if (scopePaint != strokePaint)
                                    {
                                        pen.Brush = scopePaint.GetBrush(geometry, _setBrushOpacity);
                                    }
                                    else
                                    {
                                        pen.Brush = null;
                                    }
                                }
                                else
                                {
                                    pen.Brush = null;
                                }
                            }
                        }
                    }
                }

                if (_paintContext != null)
                {
                    _paintContext.Fill   = fillPaint;
                    _paintContext.Stroke = strokePaint;
                    _paintContext.Tag    = geometry;
                }

                if (brush != null || pen != null)
                {
                    Transform transform = this.Transform;
                    if (transform != null && !transform.Value.IsIdentity)
                    {
                        geometry.Transform = transform;
                        if (brush != null && isFillTransmable)
                        {
                            Transform brushTransform = brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                brush.Transform = groupTransform;
                            }
                        }
                        if (pen != null && pen.Brush != null)
                        {
                            Transform brushTransform = pen.Brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                pen.Brush.Transform = transform;
                            }
                            else
                            {
                                TransformGroup groupTransform = new TransformGroup();
                                groupTransform.Children.Add(brushTransform);
                                groupTransform.Children.Add(transform);
                                pen.Brush.Transform = groupTransform;
                            }
                        }
                    }
                    else
                    {
                        transform = null; // render any identity transform useless...
                    }

                    GeometryDrawing drawing = new GeometryDrawing(brush, pen, geometry);

                    if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                    {
                        SvgObject.SetName(drawing, elementId);

                        context.RegisterId(elementId);

                        if (context.IncludeRuntime)
                        {
                            SvgObject.SetId(drawing, elementId);
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime)
                    {
                        SvgObject.SetClass(drawing, elementClass);
                    }

                    Brush    maskBrush = this.Masking;
                    Geometry clipGeom  = this.ClipGeometry;
                    if (clipGeom != null || maskBrush != null)
                    {
                        //Geometry clipped = Geometry.Combine(geometry, clipGeom,
                        //    GeometryCombineMode.Exclude, null);

                        //if (clipped != null && !clipped.IsEmpty())
                        //{
                        //    geometry = clipped;
                        //}
                        DrawingGroup clipMaskGroup = new DrawingGroup();

                        Rect geometryBounds = geometry.Bounds;

                        if (clipGeom != null)
                        {
                            clipMaskGroup.ClipGeometry = clipGeom;

                            SvgUnitType clipUnits = this.ClipUnits;
                            if (clipUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }

                                TransformGroup transformGroup = new TransformGroup();

                                // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                transformGroup.Children.Add(new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                                transformGroup.Children.Add(new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                                clipGeom.Transform = transformGroup;
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    clipGeom.Transform = transform;
                                }
                            }
                        }
                        if (maskBrush != null)
                        {
                            DrawingBrush drawingBrush = (DrawingBrush)maskBrush;

                            SvgUnitType maskUnits        = this.MaskUnits;
                            SvgUnitType maskContentUnits = this.MaskContentUnits;
                            if (maskUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }
                                DrawingGroup maskGroup = drawingBrush.Drawing as DrawingGroup;
                                if (maskGroup != null)
                                {
                                    DrawingCollection maskDrawings = maskGroup.Children;
                                    for (int i = 0; i < maskDrawings.Count; i++)
                                    {
                                        Drawing         maskDrawing  = maskDrawings[i];
                                        GeometryDrawing maskGeomDraw = maskDrawing as GeometryDrawing;
                                        if (maskGeomDraw != null)
                                        {
                                            if (maskGeomDraw.Brush != null)
                                            {
                                                ConvertColors(maskGeomDraw.Brush);
                                            }
                                            if (maskGeomDraw.Pen != null)
                                            {
                                                ConvertColors(maskGeomDraw.Pen.Brush);
                                            }
                                        }
                                    }
                                }

                                if (maskContentUnits == SvgUnitType.ObjectBoundingBox)
                                {
                                    TransformGroup transformGroup = new TransformGroup();

                                    // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                    var scaleTransform = new ScaleTransform(drawingBounds.Width, drawingBounds.Height);
                                    transformGroup.Children.Add(scaleTransform);
                                    var translateTransform = new TranslateTransform(drawingBounds.X, drawingBounds.Y);
                                    transformGroup.Children.Add(translateTransform);

                                    Matrix scaleMatrix     = new Matrix();
                                    Matrix translateMatrix = new Matrix();

                                    scaleMatrix.Scale(drawingBounds.Width, drawingBounds.Height);
                                    translateMatrix.Translate(drawingBounds.X, drawingBounds.Y);

                                    Matrix matrix = Matrix.Multiply(scaleMatrix, translateMatrix);
                                    //maskBrush.Transform = transformGroup;
                                    maskBrush.Transform = new MatrixTransform(matrix);
                                }
                                else
                                {
                                    drawingBrush.Viewbox      = drawingBounds;
                                    drawingBrush.ViewboxUnits = BrushMappingMode.Absolute;

                                    drawingBrush.Stretch = Stretch.Uniform;

                                    drawingBrush.Viewport      = drawingBounds;
                                    drawingBrush.ViewportUnits = BrushMappingMode.Absolute;
                                }
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    maskBrush.Transform = transform;
                                }
                            }

                            clipMaskGroup.OpacityMask = maskBrush;
                        }

                        clipMaskGroup.Children.Add(drawing);
                        drawGroup.Children.Add(clipMaskGroup);
                    }
                    else
                    {
                        drawGroup.Children.Add(drawing);
                    }
                }
            }

            // If this is not the child of a "marker", then try rendering a marker...
            if (!string.Equals(parentNode.LocalName, "marker"))
            {
                RenderMarkers(renderer, styleElm, context);
            }
        }
        public override void Render(GdiGraphicsRenderer renderer)
        {
            _graphics = renderer.GdiGraphics;

            SvgRenderingHint hint = _svgElement.RenderingHint;

            if (hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            if (_svgElement.ParentNode is SvgClipPathElement)
            {
                return;
            }

            SvgTextElement textElement = _svgElement as SvgTextElement;

            if (textElement == null)
            {
                return;
            }

            string sVisibility = textElement.GetPropertyValue("visibility");
            string sDisplay    = textElement.GetPropertyValue("display");

            if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            {
                return;
            }

            Clip(_graphics);

            PointF ctp = new PointF(0, 0); // current text position

            ctp = GetCurrentTextPosition(textElement, ctp);
            string sBaselineShift = textElement.GetPropertyValue("baseline-shift").Trim();
            double shiftBy        = 0;

            if (sBaselineShift.Length > 0)
            {
                float textFontSize = GetComputedFontSize(textElement);
                if (sBaselineShift.EndsWith("%", StringComparison.OrdinalIgnoreCase))
                {
                    shiftBy = SvgNumber.ParseNumber(sBaselineShift.Substring(0,
                                                                             sBaselineShift.Length - 1)) / 100 * textFontSize;
                }
                else if (sBaselineShift == "sub")
                {
                    shiftBy = -0.6F * textFontSize;
                }
                else if (sBaselineShift == "super")
                {
                    shiftBy = 0.6F * textFontSize;
                }
                else if (sBaselineShift == "baseline")
                {
                    shiftBy = 0;
                }
                else
                {
                    shiftBy = SvgNumber.ParseNumber(sBaselineShift);
                }
            }

            XmlNodeType nodeType = XmlNodeType.None;

            foreach (XmlNode child in _svgElement.ChildNodes)
            {
                SvgStyleableElement stylable = child as SvgStyleableElement;
                if (stylable != null)
                {
                    sVisibility = stylable.GetPropertyValue("visibility");
                    sDisplay    = stylable.GetPropertyValue("display");
                    if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
                    {
                        continue;
                    }
                }

                nodeType = child.NodeType;
                if (nodeType == XmlNodeType.Text)
                {
                    ctp.Y -= (float)shiftBy;
                    AddGraphicsPath(textElement, ref ctp, GetText(textElement, child));
                    ctp.Y += (float)shiftBy;
                }
                else if (nodeType == XmlNodeType.Element)
                {
                    string nodeName = child.Name;
                    if (string.Equals(nodeName, "tref"))
                    {
                        AddTRefElementPath((SvgTRefElement)child, ref ctp);
                    }
                    else if (string.Equals(nodeName, "tspan"))
                    {
                        AddTSpanElementPath((SvgTSpanElement)child, ref ctp);
                    }
                }
            }

            PaintMarkers(renderer, textElement, _graphics);

            _graphics = null;
        }
Beispiel #10
0
        private void RenderElement(ISvgElement svgElement)
        {
            bool isNotRenderable = !svgElement.IsRenderable || string.Equals(svgElement.LocalName, "a");

            if (isNotRenderable)
            {
                return;
            }
            SvgElement currentElement = (SvgElement)svgElement;

            GdiRenderingBase renderingNode = GdiRendering.Create(currentElement);

            if (renderingNode == null)
            {
                return;
            }

            if (!renderingNode.NeedRender(_renderer))
            {
                renderingNode.Dispose();
                renderingNode = null;
                return;
            }
            var comparer = StringComparison.OrdinalIgnoreCase;

            bool shouldRender            = true;
            SvgStyleableElement stylable = svgElement as SvgStyleableElement;

            if (stylable != null)
            {
                string sVisibility = stylable.GetPropertyValue(CssConstants.PropVisibility);
                string sDisplay    = stylable.GetPropertyValue(CssConstants.PropDisplay);
                if (string.Equals(sVisibility, CssConstants.ValHidden, comparer) ||
                    string.Equals(sDisplay, CssConstants.ValNone, comparer))
                {
                    shouldRender = false;
                }
            }

            if (shouldRender)
            {
                //_rendererMap[svgElement] = renderingNode;
                //                _rendererMap.Push(renderingNode);

                if (_rendererMap.ContainsKey(currentElement.UniqueId))
                {
                    // Might be circular rendering...
                    //                System.Diagnostics.Debug.WriteLine("Circular Object: " + currentElement.LocalName);
                    return;
                }

                _rendererMap[currentElement.UniqueId] = renderingNode;
                renderingNode.BeforeRender(_renderer);

                renderingNode.Render(_renderer);

                if (!renderingNode.IsRecursive && svgElement.HasChildNodes)
                {
                    RenderChildren(svgElement);
                }

                //renderingNode = _rendererMap[svgElement];
                renderingNode = _rendererMap[currentElement.UniqueId];
//                renderingNode = _rendererMap.Pop();
                Debug.Assert(renderingNode.Element == svgElement);
                renderingNode.AfterRender(_renderer);

                _rendererMap.Remove(currentElement.UniqueId);

                //_rendererMap.Remove(svgElement);
            }

            renderingNode.Dispose();
            renderingNode = null;
        }
Beispiel #11
0
        public static Brush CreateViewportBrush(SvgStyleableElement svgElm)
        {
            string prop    = svgElm.GetAttribute("viewport-fill");
            string opacity = svgElm.GetAttribute("viewport-fill-opacity");          // no auto-inherit

            if (string.Equals(prop, "inherit", StringComparison.OrdinalIgnoreCase)) // if explicitly defined...
            {
                prop = svgElm.GetPropertyValue("viewport-fill");
            }
            if (string.Equals(opacity, "inherit", StringComparison.OrdinalIgnoreCase)) // if explicitly defined...
            {
                opacity = svgElm.GetPropertyValue("viewport-fill-opacity");
            }
            if (!string.IsNullOrWhiteSpace(prop))
            {
                if (string.Equals(prop, "currentColor", StringComparison.OrdinalIgnoreCase))
                {
                    var svgParent = svgElm.ParentNode as SvgStyleableElement;
                    if (svgParent != null)
                    {
                        prop = svgParent.GetPropertyValue("color", "viewport-fill");
                    }
                }

                Color       color    = Colors.Transparent; // no auto-inherited...
                WpfSvgColor svgColor = new WpfSvgColor(svgElm, "viewport-fill");
                color = svgColor.Color;

                if (color.A == 255)
                {
                    double alpha = 255;

                    if (!string.IsNullOrWhiteSpace(opacity))
                    {
                        alpha *= SvgNumber.ParseNumber(opacity);
                    }

                    alpha = Math.Min(alpha, 255);
                    alpha = Math.Max(alpha, 0);

                    color = Color.FromArgb((byte)Convert.ToInt32(alpha), color.R, color.G, color.B);
                }

                var    brush        = new SolidColorBrush(color);
                double opacityValue = 1;
                if (!string.IsNullOrWhiteSpace(opacity) && double.TryParse(opacity, out opacityValue))
                {
                    brush.Opacity = opacityValue;
                }

                return(brush);
            }
            else
            {
                Color  color = Colors.Black; // the default color...
                double alpha = 255;

                if (!string.IsNullOrWhiteSpace(opacity))
                {
                    alpha *= SvgNumber.ParseNumber(opacity);
                }

                alpha = Math.Min(alpha, 255);
                alpha = Math.Max(alpha, 0);

                color = Color.FromArgb((byte)Convert.ToInt32(alpha), color.R, color.G, color.B);

                return(new SolidColorBrush(color));
            }
        }
        public override void Render(GdiGraphicsRenderer renderer)
        {
            GdiGraphicsWrapper graphics = renderer.GraphicsWrapper;

            SvgRenderingHint hint = element.RenderingHint;

            if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            if (element.ParentNode is SvgClipPathElement)
            {
                return;
            }

            SvgStyleableElement styleElm = (SvgStyleableElement)element;

            string sVisibility = styleElm.GetPropertyValue("visibility");
            string sDisplay    = styleElm.GetPropertyValue("display");

            if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
            {
                return;
            }

            GraphicsPath gp = CreatePath(element);

            if (gp != null)
            {
                Clip(graphics);

                GdiSvgPaint fillPaint = new GdiSvgPaint(styleElm, "fill");
                Brush       brush     = fillPaint.GetBrush(gp);

                GdiSvgPaint strokePaint = new GdiSvgPaint(styleElm, "stroke");
                Pen         pen         = strokePaint.GetPen(gp);

                if (brush != null)
                {
                    if (brush is PathGradientBrush)
                    {
                        GdiGradientFill gps = fillPaint.PaintFill as GdiGradientFill;

                        graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude);

                        SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)brush).InterpolationColors.Colors[0]);
                        graphics.FillPath(this, tempBrush, gp);
                        tempBrush.Dispose();
                        graphics.ResetClip();
                    }

                    graphics.FillPath(this, brush, gp);
                    brush.Dispose();
                    brush = null;
                }

                if (pen != null)
                {
                    if (pen.Brush is PathGradientBrush)
                    {
                        GdiGradientFill      gps       = strokePaint.PaintFill as GdiGradientFill;
                        GdiGraphicsContainer container = graphics.BeginContainer();

                        graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude);

                        SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)pen.Brush).InterpolationColors.Colors[0]);
                        Pen        tempPen   = new Pen(tempBrush, pen.Width);
                        graphics.DrawPath(this, tempPen, gp);
                        tempPen.Dispose();
                        tempBrush.Dispose();

                        graphics.EndContainer(container);
                    }

                    graphics.DrawPath(this, pen, gp);
                    pen.Dispose();
                    pen = null;
                }

                gp.Dispose();
                gp = null;
            }

            PaintMarkers(renderer, styleElm, graphics);
        }
Beispiel #13
0
        public override void Render(WpfDrawingRenderer renderer)
        {
            WpfDrawingContext context = renderer.Context;

            SvgRenderingHint hint = _svgElement.RenderingHint;

            if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            // We do not directly render the contents of the clip-path, unless specifically requested...
            if (String.Equals(_svgElement.ParentNode.LocalName, "clipPath") &&
                !context.RenderingClipRegion)
            {
                return;
            }

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            string sVisibility = styleElm.GetPropertyValue("visibility");
            string sDisplay    = styleElm.GetPropertyValue("display");

            if (String.Equals(sVisibility, "hidden") || String.Equals(sDisplay, "none"))
            {
                return;
            }

            DrawingGroup drawGroup = context.Peek();

            Debug.Assert(drawGroup != null);

            Geometry geometry = CreateGeometry(_svgElement, context.OptimizePath);

            if (geometry != null && !geometry.IsEmpty())
            {
                SetClip(context);

                WpfSvgPaint fillPaint = new WpfSvgPaint(context, styleElm, "fill");

                string fileValue = styleElm.GetAttribute("fill");

                Brush brush = fillPaint.GetBrush();

                WpfSvgPaint strokePaint = new WpfSvgPaint(context, styleElm, "stroke");
                Pen         pen         = strokePaint.GetPen();

                if (brush != null || pen != null)
                {
                    Transform transform = this.Transform;
                    if (transform != null && !transform.Value.IsIdentity)
                    {
                        geometry.Transform = transform;
                        if (brush != null)
                        {
                            Transform brushTransform = brush.Transform;
                            if (brushTransform == null || brushTransform == Transform.Identity)
                            {
                                brush.Transform = transform;
                            }
                        }
                    }
                    else
                    {
                        transform = null; // render any identity transform useless...
                    }

                    GeometryDrawing drawing = new GeometryDrawing(brush, pen, geometry);

                    string elementId = this.GetElementName();
                    if (!String.IsNullOrEmpty(elementId) && !context.IsRegisteredId(elementId))
                    {
                        drawing.SetValue(FrameworkElement.NameProperty, elementId);

                        context.RegisterId(elementId);

                        if (context.IncludeRuntime)
                        {
                            SvgObject.SetId(drawing, elementId);
                        }
                    }

                    Brush    maskBrush = this.Masking;
                    Geometry clipGeom  = this.ClipGeometry;
                    if (clipGeom != null || maskBrush != null)
                    {
                        //Geometry clipped = Geometry.Combine(geometry, clipGeom,
                        //    GeometryCombineMode.Exclude, null);

                        //if (clipped != null && !clipped.IsEmpty())
                        //{
                        //    geometry = clipped;
                        //}
                        DrawingGroup clipMaskGroup = new DrawingGroup();

                        Rect geometryBounds = geometry.Bounds;

                        if (clipGeom != null)
                        {
                            clipMaskGroup.ClipGeometry = clipGeom;

                            SvgUnitType clipUnits = this.ClipUnits;
                            if (clipUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }

                                TransformGroup transformGroup = new TransformGroup();

                                // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                transformGroup.Children.Add(
                                    new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                                transformGroup.Children.Add(
                                    new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                                clipGeom.Transform = transformGroup;
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    clipGeom.Transform = transform;
                                }
                            }
                        }
                        if (maskBrush != null)
                        {
                            SvgUnitType maskUnits = this.MaskUnits;
                            if (maskUnits == SvgUnitType.ObjectBoundingBox)
                            {
                                Rect drawingBounds = geometryBounds;

                                if (transform != null)
                                {
                                    drawingBounds = transform.TransformBounds(drawingBounds);
                                }

                                TransformGroup transformGroup = new TransformGroup();

                                // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                                transformGroup.Children.Add(
                                    new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                                transformGroup.Children.Add(
                                    new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                                DrawingGroup maskGroup = ((DrawingBrush)maskBrush).Drawing as DrawingGroup;
                                if (maskGroup != null)
                                {
                                    DrawingCollection maskDrawings = maskGroup.Children;
                                    for (int i = 0; i < maskDrawings.Count; i++)
                                    {
                                        Drawing         maskDrawing  = maskDrawings[i];
                                        GeometryDrawing maskGeomDraw = maskDrawing as GeometryDrawing;
                                        if (maskGeomDraw != null)
                                        {
                                            if (maskGeomDraw.Brush != null)
                                            {
                                                ConvertColors(maskGeomDraw.Brush);
                                            }
                                            if (maskGeomDraw.Pen != null)
                                            {
                                                ConvertColors(maskGeomDraw.Pen.Brush);
                                            }
                                        }
                                    }
                                }

                                //if (transformGroup != null)
                                //{
                                //    drawingBounds = transformGroup.TransformBounds(drawingBounds);
                                //}

                                //maskBrush.Viewbox = drawingBounds;
                                //maskBrush.ViewboxUnits = BrushMappingMode.Absolute;

                                //maskBrush.Stretch = Stretch.Uniform;

                                //maskBrush.Viewport = drawingBounds;
                                //maskBrush.ViewportUnits = BrushMappingMode.Absolute;

                                maskBrush.Transform = transformGroup;
                            }
                            else
                            {
                                if (transform != null)
                                {
                                    maskBrush.Transform = transform;
                                }
                            }

                            clipMaskGroup.OpacityMask = maskBrush;
                        }

                        clipMaskGroup.Children.Add(drawing);
                        drawGroup.Children.Add(clipMaskGroup);
                    }
                    else
                    {
                        drawGroup.Children.Add(drawing);
                    }
                }
            }

            RenderMarkers(renderer, styleElm, context);
        }
Beispiel #14
0
        public override void Render(GdiGraphicsRenderer renderer)
        {
            var graphics = renderer.GdiGraphics;

            SvgRenderingHint hint = _svgElement.RenderingHint;

            if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping)
            {
                return;
            }
            if (_svgElement.ParentNode is SvgClipPathElement)
            {
                return;
            }
            var comparer = StringComparison.OrdinalIgnoreCase;

            SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement;

            string sVisibility = styleElm.GetPropertyValue(CssConstants.PropVisibility);
            string sDisplay    = styleElm.GetPropertyValue(CssConstants.PropDisplay);

            if (string.Equals(sVisibility, CssConstants.ValHidden, comparer) ||
                string.Equals(sDisplay, CssConstants.ValNone, comparer))
            {
                return;
            }

            GraphicsPath gp = CreatePath(_svgElement);

            if (gp == null)
            {
                return;
            }

            SetClip(graphics);

            GdiSvgPaint fillPaint = new GdiSvgPaint(styleElm, "fill");
            Brush       brush     = fillPaint.GetBrush(gp);

            GdiSvgPaint strokePaint = new GdiSvgPaint(styleElm, "stroke");
            Pen         pen         = strokePaint.GetPen(gp);

            if (brush != null)
            {
                if (brush is PathGradientBrush)
                {
                    var gps = fillPaint.PaintFill as GdiRadialGradientFill;

                    graphics.SetClip(gps.GetRadialRegion(gp.GetBounds()), CombineMode.Exclude);

                    SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)brush).InterpolationColors.Colors[0]);
                    graphics.FillPath(this, tempBrush, gp);
                    tempBrush.Dispose();
                    graphics.ResetClip();
                }

                graphics.FillPath(this, brush, gp);
                brush.Dispose();
                brush = null;
            }

            if (pen != null)
            {
                if (pen.Brush is PathGradientBrush)
                {
                    var gps = strokePaint.PaintFill as GdiRadialGradientFill;
                    GdiGraphicsContainer container = graphics.BeginContainer();

                    graphics.SetClip(gps.GetRadialRegion(gp.GetBounds()), CombineMode.Exclude);

                    SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)pen.Brush).InterpolationColors.Colors[0]);
                    Pen        tempPen   = new Pen(tempBrush, pen.Width);
                    graphics.DrawPath(this, tempPen, gp);
                    tempPen.Dispose();
                    tempBrush.Dispose();

                    graphics.EndContainer(container);
                }

                graphics.DrawPath(this, pen, gp);
                pen.Dispose();
                pen = null;
            }

            gp.Dispose();
            gp = null;

            PaintMarkers(renderer, styleElm, graphics);
        }
Beispiel #15
0
        public override void Render(ISvgRenderer renderer)
        {
            GdiRenderer     gdiRenderer = renderer as GdiRenderer;
            GraphicsWrapper graphics    = gdiRenderer.GraphicsWrapper;

            if (!(element is SvgClipPathElement) && !(element.ParentNode is SvgClipPathElement))
            {
                SvgStyleableElement styleElm = element as SvgStyleableElement;
                if (styleElm != null)
                {
                    string sVisibility = styleElm.GetPropertyValue("visibility");
                    string sDisplay    = styleElm.GetPropertyValue("display");

                    if (element is ISharpGDIPath && sVisibility != "hidden" && sDisplay != "none")
                    {
                        GraphicsPath gp = ((ISharpGDIPath)element).GetGraphicsPath();

                        if (gp != null)
                        {
                            Clip(graphics);

                            GdiSvgPaint fillPaint = new GdiSvgPaint(styleElm, "fill");
                            Brush       brush     = fillPaint.GetBrush(gp);

                            GdiSvgPaint strokePaint = new GdiSvgPaint(styleElm, "stroke");
                            Pen         pen         = strokePaint.GetPen(gp);

                            if (brush != null)
                            {
                                if (brush is PathGradientBrush)
                                {
                                    GradientPaintServer gps = fillPaint.PaintServer as GradientPaintServer;
                                    //GraphicsContainer container = graphics.BeginContainer();

                                    graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude);

                                    SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)brush).InterpolationColors.Colors[0]);
                                    graphics.FillPath(this, tempBrush, gp);
                                    tempBrush.Dispose();
                                    graphics.ResetClip();

                                    //graphics.EndContainer(container);
                                }

                                graphics.FillPath(this, brush, gp);
                                brush.Dispose();
                            }

                            if (pen != null)
                            {
                                if (pen.Brush is PathGradientBrush)
                                {
                                    GradientPaintServer      gps       = strokePaint.PaintServer as GradientPaintServer;
                                    GraphicsContainerWrapper container = graphics.BeginContainer();

                                    graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude);

                                    SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)pen.Brush).InterpolationColors.Colors[0]);
                                    Pen        tempPen   = new Pen(tempBrush, pen.Width);
                                    graphics.DrawPath(this, tempPen, gp);
                                    tempPen.Dispose();
                                    tempBrush.Dispose();

                                    graphics.EndContainer(container);
                                }

                                graphics.DrawPath(this, pen, gp);
                                pen.Dispose();
                            }
                        }
                    }
                    PaintMarkers(gdiRenderer, styleElm, graphics);
                }
            }
        }