Beispiel #1
0
        /// <summary>
        /// Sets the clipping region of the specified <see cref="ISvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="ISvgRenderer"/> to have its clipping region set.</param>
        protected internal virtual void SetClip(ISvgRenderer renderer)
        {
            if (this.ClipPath != null || !string.IsNullOrEmpty(this.Clip))
            {
                this._previousClip = renderer.GetClip();

                if (this.ClipPath != null)
                {
                    SvgClipPath clipPath = this.OwnerDocument.GetElementById <SvgClipPath>(this.ClipPath.ToString());
                    if (clipPath != null)
                    {
                        renderer.SetClip(clipPath.GetClipRegion(this, renderer), CombineMode.Intersect);
                    }
                }

                var clip = this.Clip;
                if (!string.IsNullOrEmpty(clip) && clip.StartsWith("rect("))
                {
                    clip = clip.Trim();
                    var offsets = (from o in clip.Substring(5, clip.Length - 6).Split(',')
                                   select float.Parse(o.Trim(), NumberStyles.Any, CultureInfo.InvariantCulture)).ToList();
                    var bounds   = this.Bounds;
                    var clipRect = new RectangleF(bounds.Left + offsets[3], bounds.Top + offsets[0],
                                                  bounds.Width - (offsets[3] + offsets[1]),
                                                  bounds.Height - (offsets[2] + offsets[0]));
                    renderer.SetClip(new Region(clipRect), CombineMode.Intersect);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Applies the required transforms to <see cref="ISvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
        protected internal virtual bool PushTransforms(ISvgRenderer renderer)
        {
            _graphicsMatrix = renderer.Transform;
            _graphicsClip   = renderer.GetClip();

            // Return if there are no transforms
            if (this.Transforms == null || this.Transforms.Count == 0)
            {
                return(true);
            }
            if (this.Transforms.Count == 1 && this.Transforms[0].Matrix.Equals(_zeroMatrix))
            {
                return(false);
            }

            Matrix transformMatrix = renderer.Transform.Clone();

            foreach (SvgTransform transformation in this.Transforms)
            {
                transformMatrix.Multiply(transformation.Matrix);
            }

            renderer.Transform = transformMatrix;

            return(true);
        }
Beispiel #3
0
        protected override void Render(ISvgRenderer renderer)
        {
            switch (this.Overflow)
            {
            case SvgOverflow.Auto:
            case SvgOverflow.Visible:
            case SvgOverflow.Inherit:
                base.Render(renderer);
                break;

            default:
                var prevClip = renderer.GetClip();
                try
                {
                    var size = (this.Parent == null ? renderer.GetBoundable().Bounds.Size : GetDimensions());
                    var clip = new RectangleF(this.X.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
                                              this.Y.ToDeviceValue(renderer, UnitRenderingType.Vertical, this),
                                              size.Width, size.Height);
                    renderer.SetClip(new Region(clip), CombineMode.Intersect);
                    base.Render(renderer);
                }
                finally
                {
                    renderer.SetClip(prevClip, CombineMode.Replace);
                }
                break;
            }
        }
Beispiel #4
0
        public void ApplyFilter(SvgVisualElement element, ISvgRenderer renderer, Action <ISvgRenderer> renderMethod)
        {
            var inflate   = 0.5f;
            var transform = GetTransform(element);
            var bounds    = GetPathBounds(element, renderer, transform);

            if (bounds.Width == 0 || bounds.Height == 0)
            {
                return;
            }

            var buffer = new ImageBuffer(bounds, inflate, renderer, renderMethod)
            {
                Transform = transform
            };

            foreach (var primitive in this.Children.OfType <SvgFilterPrimitive>())
            {
                primitive.Process(buffer);
            }

            // Render the final filtered image
            var bufferImg = buffer.Buffer;
            var imgDraw   = RectangleF.Inflate(bounds, inflate * bounds.Width, inflate * bounds.Height);
            var prevClip  = renderer.GetClip();

            renderer.SetClip(new Region(imgDraw));
            renderer.DrawImage(bufferImg, imgDraw, new RectangleF(bounds.X, bounds.Y, imgDraw.Width, imgDraw.Height), GraphicsUnit.Pixel);
            renderer.SetClip(prevClip);
        }
Beispiel #5
0
        /// <summary>
        /// Applies the required transforms to <see cref="ISvgRenderer"/>.
        /// </summary>
        /// <param name="renderer">The <see cref="ISvgRenderer"/> to be transformed.</param>
        protected internal virtual bool PushTransforms(ISvgRenderer renderer)
        {
            _graphicsTransform = renderer.Transform;
            _graphicsClip      = renderer.GetClip();

            var transforms = Transforms;

            // Return if there are no transforms
            if (transforms == null || transforms.Count == 0)
            {
                return(true);
            }

            using (var transformMatrix = transforms.GetMatrix())
            {
                using (var zeroMatrix = new Matrix(0f, 0f, 0f, 0f, 0f, 0f))
                    if (zeroMatrix.Equals(transformMatrix))
                    {
                        return(false);
                    }

                using (var graphicsTransform = _graphicsTransform.Clone())
                {
                    graphicsTransform.Multiply(transformMatrix);
                    renderer.Transform = graphicsTransform;
                }
            }

            return(true);
        }
        protected override Brush CreateBrush(SvgVisualElement renderingElement, ISvgRenderer renderer, float opacity, bool forStroke)
        {
            // TODO: figure out how to do the brush transform in the presence of FocalRadius
            try
            {
                if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox)
                {
                    renderer.SetBoundable(renderingElement);
                }

                // Calculate the path and transform it appropriately
                var center = new PointF(NormalizeUnit(CenterX).ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
                                        NormalizeUnit(CenterY).ToDeviceValue(renderer, UnitRenderingType.Vertical, this));
                var focals = new PointF[] { new PointF(NormalizeUnit(FocalX).ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
                                                       NormalizeUnit(FocalY).ToDeviceValue(renderer, UnitRenderingType.Vertical, this)) };
                var specifiedRadius = NormalizeUnit(Radius).ToDeviceValue(renderer, UnitRenderingType.Other, this);
                var path            = new GraphicsPath();
                path.AddEllipse(
                    center.X - specifiedRadius, center.Y - specifiedRadius,
                    specifiedRadius * 2, specifiedRadius * 2
                    );

                using (var transform = EffectiveGradientTransform)
                {
                    var bounds = renderer.GetBoundable().Bounds;
                    transform.Translate(bounds.X, bounds.Y, MatrixOrder.Prepend);
                    if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox)
                    {
                        transform.Scale(bounds.Width, bounds.Height, MatrixOrder.Prepend);
                    }
                    path.Transform(transform);
                    transform.TransformPoints(focals);
                }


                // Calculate any required scaling
                var scaleBounds = RectangleF.Inflate(renderingElement.Bounds, renderingElement.StrokeWidth, renderingElement.StrokeWidth);
                var scale       = CalcScale(scaleBounds, path);

                // Not ideal, but this makes sure that the rest of the shape gets properly filled or drawn
                if (scale > 1.0f && SpreadMethod == SvgGradientSpreadMethod.Pad)
                {
                    var stop        = Stops.Last();
                    var origColor   = stop.GetColor(renderingElement);
                    var renderColor = System.Drawing.Color.FromArgb((int)Math.Round(opacity * stop.StopOpacity * 255), origColor);

                    var origClip = renderer.GetClip();
                    try
                    {
                        using (var solidBrush = new SolidBrush(renderColor))
                        {
                            var newClip = origClip.Clone();
                            newClip.Exclude(path);
                            renderer.SetClip(newClip);

                            var renderPath = (GraphicsPath)renderingElement.Path(renderer);
                            if (forStroke)
                            {
                                using (var pen = new Pen(solidBrush, renderingElement.StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, renderingElement)))
                                {
                                    renderer.DrawPath(pen, renderPath);
                                }
                            }
                            else
                            {
                                renderer.FillPath(solidBrush, renderPath);
                            }
                        }
                    }
                    finally
                    {
                        renderer.SetClip(origClip);
                    }
                }

                // Get the color blend and any tweak to the scaling
                var blend = CalculateColorBlend(renderer, opacity, scale, out scale);

                // Transform the path based on the scaling
                var gradBounds  = path.GetBounds();
                var transCenter = new PointF(gradBounds.Left + gradBounds.Width / 2, gradBounds.Top + gradBounds.Height / 2);
                using (var scaleMat = new Matrix())
                {
                    scaleMat.Translate(-1 * transCenter.X, -1 * transCenter.Y, MatrixOrder.Append);
                    scaleMat.Scale(scale, scale, MatrixOrder.Append);
                    scaleMat.Translate(transCenter.X, transCenter.Y, MatrixOrder.Append);
                    path.Transform(scaleMat);
                }

                // calculate the brush
                var brush = new PathGradientBrush(path);
                brush.CenterPoint         = focals[0];
                brush.InterpolationColors = blend;

                return(brush);
            }
            finally
            {
                if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox)
                {
                    renderer.PopBoundable();
                }
            }
        }
Beispiel #7
0
 public Region GetClip()
 {
     return(_svgRendererImplementation.GetClip());
 }