示例#1
0
        private void RenderInternal(ISvgRenderer renderer, bool renderFilter)
        {
            if (!(renderFilter && RenderFilter(renderer)))
            {
                var opacity = FixOpacityValue(Opacity);
                if (opacity == 1f)
                {
                    RenderInternal(renderer);
                }
                else
                {
                    IsPathDirty = true;
                    var bounds = Renderable ? Bounds : Path(null).GetBounds();
                    IsPathDirty = true;

                    if (bounds.Width > 0f && bounds.Height > 0f)
                    {
                        using (var canvas = new Bitmap((int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height)))
                        {
                            using (var canvasRenderer = SvgRenderer.FromImage(canvas))
                            {
                                canvasRenderer.SetBoundable(renderer.GetBoundable());
                                canvasRenderer.TranslateTransform(-bounds.X, -bounds.Y);

                                RenderInternal(canvasRenderer);
                            }
                            var srcRect = new RectangleF(0f, 0f, bounds.Width, bounds.Height);
                            renderer.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity);
                        }
                    }
                }
            }
        }
示例#2
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);
        }
示例#3
0
        private void Render(ISvgRenderer renderer, bool renderFilter)
        {
            if (Visible && Displayable && (!Renderable || Path(renderer) != null))
            {
                if (!(renderFilter && RenderFilter(renderer)))
                {
                    try
                    {
                        if (PushTransforms(renderer))
                        {
                            SetClip(renderer);

                            if (Renderable)
                            {
                                var opacity = Math.Min(Math.Max(Opacity, 0f), 1f);
                                if (opacity == 1f)
                                {
                                    RenderFillAndStroke(renderer);
                                }
                                else
                                {
                                    IsPathDirty = true;
                                    var bounds = Bounds;
                                    IsPathDirty = true;

                                    if (bounds.Width > 0f && bounds.Height > 0f)
                                    {
                                        using (var canvas = new Bitmap((int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height)))
                                        {
                                            using (var canvasRenderer = SvgRenderer.FromImage(canvas))
                                            {
                                                canvasRenderer.SetBoundable(renderer.GetBoundable());
                                                canvasRenderer.TranslateTransform(-bounds.X, -bounds.Y);

                                                RenderFillAndStroke(canvasRenderer);
                                            }
                                            var srcRect = new RectangleF(0f, 0f, bounds.Width, bounds.Height);
                                            renderer.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                RenderChildren(renderer);
                            }

                            ResetClip(renderer);
                        }
                    }
                    finally
                    {
                        PopTransforms(renderer);
                    }
                }
            }
        }
示例#4
0
        private void Render(ISvgRenderer renderer, bool renderFilter)
        {
            if (this.Visible && this.Displayable && this.PushTransforms(renderer) &&
                (!this.Renderable || this.Path(renderer) != null))
            {
                if (!(renderFilter && this.RenderFilter(renderer)))
                {
                    this.SetClip(renderer);

                    if (this.Renderable)
                    {
                        var opacity = Math.Min(Math.Max(this.Opacity, 0), 1);
                        if (opacity == 1f)
                        {
                            this.RenderFillAndStroke(renderer);
                        }
                        else
                        {
                            IsPathDirty = true;
                            var bounds = this.Bounds;
                            IsPathDirty = true;

                            using (var canvas = new Bitmap((int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height)))
                            {
                                using (var canvasRenderer = SvgRenderer.FromImage(canvas))
                                {
                                    canvasRenderer.SetBoundable(renderer.GetBoundable());
                                    canvasRenderer.TranslateTransform(-bounds.X, -bounds.Y);

                                    this.RenderFillAndStroke(canvasRenderer);
                                }
                                var srcRect = new RectangleF(0f, 0f, bounds.Width, bounds.Height);
                                renderer.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity);
                            }
                        }
                    }
                    else
                    {
                        base.RenderChildren(renderer);
                    }

                    this.ResetClip(renderer);
                    this.PopTransforms(renderer);
                }
            }
        }
示例#5
0
        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
        protected override void Render(ISvgRenderer renderer)
        {
            if (!Visible || !Displayable)
            {
                return;
            }

            if (Width.Value > 0.0f && Height.Value > 0.0f && this.Href != null)
            {
                var img = GetImage();
                if (img != null)
                {
                    RectangleF srcRect;
                    var        bmp = img as Bitmap;
                    var        svg = img as SvgFragment;
                    if (bmp != null)
                    {
                        srcRect = new RectangleF(0, 0, bmp.Width, bmp.Height);
                    }
                    else if (svg != null)
                    {
                        srcRect = new RectangleF(new PointF(0, 0), svg.GetDimensions());
                    }
                    else
                    {
                        return;
                    }

                    var destClip = new RectangleF(this.Location.ToDeviceValue(renderer, this),
                                                  new SizeF(Width.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
                                                            Height.ToDeviceValue(renderer, UnitRenderingType.Vertical, this)));
                    RectangleF destRect = destClip;

                    this.PushTransforms(renderer);
                    renderer.SetClip(new Region(destClip), CombineMode.Intersect);
                    this.SetClip(renderer);

                    if (AspectRatio != null && AspectRatio.Align != SvgPreserveAspectRatio.none)
                    {
                        var fScaleX = destClip.Width / srcRect.Width;
                        var fScaleY = destClip.Height / srcRect.Height;
                        var xOffset = 0.0f;
                        var yOffset = 0.0f;

                        if (AspectRatio.Slice)
                        {
                            fScaleX = Math.Max(fScaleX, fScaleY);
                            fScaleY = Math.Max(fScaleX, fScaleY);
                        }
                        else
                        {
                            fScaleX = Math.Min(fScaleX, fScaleY);
                            fScaleY = Math.Min(fScaleX, fScaleY);
                        }

                        switch (AspectRatio.Align)
                        {
                        case SvgPreserveAspectRatio.xMinYMin:
                            break;

                        case SvgPreserveAspectRatio.xMidYMin:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX) / 2;
                            break;

                        case SvgPreserveAspectRatio.xMaxYMin:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX);
                            break;

                        case SvgPreserveAspectRatio.xMinYMid:
                            yOffset = (destClip.Height - srcRect.Height * fScaleY) / 2;
                            break;

                        case SvgPreserveAspectRatio.xMidYMid:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX) / 2;
                            yOffset = (destClip.Height - srcRect.Height * fScaleY) / 2;
                            break;

                        case SvgPreserveAspectRatio.xMaxYMid:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX);
                            yOffset = (destClip.Height - srcRect.Height * fScaleY) / 2;
                            break;

                        case SvgPreserveAspectRatio.xMinYMax:
                            yOffset = (destClip.Height - srcRect.Height * fScaleY);
                            break;

                        case SvgPreserveAspectRatio.xMidYMax:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX) / 2;
                            yOffset = (destClip.Height - srcRect.Height * fScaleY);
                            break;

                        case SvgPreserveAspectRatio.xMaxYMax:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX);
                            yOffset = (destClip.Height - srcRect.Height * fScaleY);
                            break;
                        }

                        destRect = new RectangleF(destClip.X + xOffset, destClip.Y + yOffset,
                                                  srcRect.Width * fScaleX, srcRect.Height * fScaleY);
                    }

                    if (bmp != null)
                    {
                        renderer.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
                        bmp.Dispose();
                    }
                    else if (svg != null)
                    {
                        var currOffset = new PointF(renderer.Transform.OffsetX, renderer.Transform.OffsetY);
                        renderer.TranslateTransform(-currOffset.X, -currOffset.Y);
                        renderer.ScaleTransform(destRect.Width / srcRect.Width, destRect.Height / srcRect.Height);
                        renderer.TranslateTransform(currOffset.X + destRect.X, currOffset.Y + destRect.Y);
                        renderer.SetBoundable(new GenericBoundable(srcRect));
                        svg.RenderElement(renderer);
                        renderer.PopBoundable();
                    }


                    this.ResetClip(renderer);
                    this.PopTransforms(renderer);
                }
                // TODO: cache images... will need a shared context for this
                // TODO: support preserveAspectRatio, etc
            }
        }
示例#6
0
        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
        /// </summary>
        protected override void Render(ISvgRenderer renderer)
        {
            if (!(Visible && Displayable && Width.Value > 0f && Height.Value > 0f && Href != null))
            {
                return;
            }

            var img = GetImage(Href);
            var bmp = img as Image;
            var svg = img as SvgFragment;

            if (bmp == null && svg == null)
            {
                return;
            }
            try
            {
                if (PushTransforms(renderer))
                {
                    RectangleF srcRect;
                    if (bmp != null)
                    {
                        srcRect = new RectangleF(0f, 0f, bmp.Width, bmp.Height);
                    }
                    else
                    {
                        srcRect = new RectangleF(new PointF(0f, 0f), svg.GetDimensions());
                    }

                    var destClip = new RectangleF(Location.ToDeviceValue(renderer, this),
                                                  new SizeF(Width.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this),
                                                            Height.ToDeviceValue(renderer, UnitRenderingType.Vertical, this)));
                    var destRect = destClip;
                    renderer.SetClip(new Region(destClip), CombineMode.Intersect);
                    SetClip(renderer);

                    var aspectRatio = AspectRatio;
                    if (aspectRatio.Align != SvgPreserveAspectRatio.none)
                    {
                        var fScaleX = destClip.Width / srcRect.Width;
                        var fScaleY = destClip.Height / srcRect.Height;
                        var xOffset = 0f;
                        var yOffset = 0f;

                        if (aspectRatio.Slice)
                        {
                            fScaleX = Math.Max(fScaleX, fScaleY);
                            fScaleY = Math.Max(fScaleX, fScaleY);
                        }
                        else
                        {
                            fScaleX = Math.Min(fScaleX, fScaleY);
                            fScaleY = Math.Min(fScaleX, fScaleY);
                        }

                        switch (aspectRatio.Align)
                        {
                        case SvgPreserveAspectRatio.xMinYMin:
                            break;

                        case SvgPreserveAspectRatio.xMidYMin:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX) / 2;
                            break;

                        case SvgPreserveAspectRatio.xMaxYMin:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX);
                            break;

                        case SvgPreserveAspectRatio.xMinYMid:
                            yOffset = (destClip.Height - srcRect.Height * fScaleY) / 2;
                            break;

                        case SvgPreserveAspectRatio.xMidYMid:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX) / 2;
                            yOffset = (destClip.Height - srcRect.Height * fScaleY) / 2;
                            break;

                        case SvgPreserveAspectRatio.xMaxYMid:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX);
                            yOffset = (destClip.Height - srcRect.Height * fScaleY) / 2;
                            break;

                        case SvgPreserveAspectRatio.xMinYMax:
                            yOffset = (destClip.Height - srcRect.Height * fScaleY);
                            break;

                        case SvgPreserveAspectRatio.xMidYMax:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX) / 2;
                            yOffset = (destClip.Height - srcRect.Height * fScaleY);
                            break;

                        case SvgPreserveAspectRatio.xMaxYMax:
                            xOffset = (destClip.Width - srcRect.Width * fScaleX);
                            yOffset = (destClip.Height - srcRect.Height * fScaleY);
                            break;
                        }

                        destRect = new RectangleF(destClip.X + xOffset, destClip.Y + yOffset,
                                                  srcRect.Width * fScaleX, srcRect.Height * fScaleY);
                    }

                    if (bmp != null)
                    {
                        var opacity = FixOpacityValue(Opacity);
                        if (opacity == 1f)
                        {
                            renderer.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel);
                        }
                        else
                        {
                            renderer.DrawImage(bmp, destRect, srcRect, GraphicsUnit.Pixel, opacity);
                        }
                    }
                    else
                    {
                        renderer.TranslateTransform(destRect.X, destRect.Y, MatrixOrder.Prepend);
                        renderer.ScaleTransform(destRect.Width / srcRect.Width, destRect.Height / srcRect.Height, MatrixOrder.Prepend);
                        try
                        {
                            renderer.SetBoundable(new GenericBoundable(srcRect));
                            svg.RenderElement(renderer);
                        }
                        finally
                        {
                            renderer.PopBoundable();
                        }
                    }

                    ResetClip(renderer);
                }
            }
            finally
            {
                PopTransforms(renderer);

                if (bmp != null)
                {
                    bmp.Dispose();
                }
            }
            // TODO: cache images... will need a shared context for this
        }
示例#7
0
        private void RenderInternal(ISvgRenderer renderer, bool renderFilter)
        {
            if (!(renderFilter && RenderFilter(renderer)))
            {
                var opacity = FixOpacityValue(Opacity);
                if (opacity == 1f)
                {
                    if (Renderable)
                    {
                        RenderInternal(renderer, RenderFillAndStroke);
                    }
                    else
                    {
                        RenderInternal(renderer, RenderChildren);
                    }
                }
                else
                {
                    IsPathDirty = true;
                    var bounds = Renderable ? Bounds : Path(null).GetBounds();
                    IsPathDirty = true;

                    if (bounds.Width > 0f && bounds.Height > 0f)
                    {
                        var scaleX = 1f;
                        var scaleY = 1f;
                        using (var transform = renderer.Transform)
                        {
                            scaleX = Math.Max(scaleX, Math.Abs(transform.Elements[0]));
                            scaleY = Math.Max(scaleY, Math.Abs(transform.Elements[3]));
                        }

                        using (var canvas = new Bitmap((int)Math.Ceiling(bounds.Width * scaleX), (int)Math.Ceiling(bounds.Height * scaleY)))
                        {
                            using (var canvasRenderer = SvgRenderer.FromImage(canvas))
                            {
                                canvasRenderer.SetBoundable(renderer.GetBoundable());
                                canvasRenderer.TranslateTransform(-bounds.X, -bounds.Y);
                                canvasRenderer.ScaleTransform(scaleX, scaleY);

                                if (Renderable)
                                {
                                    RenderInternal(canvasRenderer, RenderFillAndStroke);
                                }
                                else
                                {
                                    RenderChildren(canvasRenderer);
                                }
                            }
                            var srcRect = new RectangleF(0f, 0f, bounds.Width * scaleX, bounds.Height * scaleY);
                            if (Renderable)
                            {
                                renderer.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity);
                            }
                            else
                            {
                                RenderInternal(renderer, r => r.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity));
                            }
                        }
                    }
                }
            }
        }
示例#8
0
 public void DrawImage(System.Drawing.Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit graphicsUnit)
 {
     _svgRendererImplementation.DrawImage(image, destRect, srcRect, graphicsUnit);
 }