• Dan Backes's avatar
    Performance Improvement · 1b8cb43f
    Dan Backes authored
    - Because querying the Bounds property of an ISvgBoundable or
      SvgVisualElement is expensive, we introduced an ImmutableBoundable that
      is used by the SvgRenderer. This class stores the bounds of the supplied
      ISvgBoundable so that multiple queries for the bounds do not hamper
      performance when rendering.
    
    - Converted the Bounds property on ISvgBoundable and SvgVisualElement to a
      CalculateBounds method to indicate that it is an expensive operation
      that returns a new value each time it is called.
    
    - Removed redundant ISvgBoundable Location and Size properties.
    
    - Fixed a bug in SvgFragment.Path property by converting it to a method that
      indicates it returns a new GraphicsPath instance when called and by
      disposing of that instance in CalculateBounds. There are many more
      instances of GraphicsPath not being disposed in the code base but we did
      not address that here.
    1b8cb43f
SvgRenderer.cs 4.29 KiB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
namespace Svg
    /// <summary>
    /// Convenience wrapper around a graphics object
    /// </summary>
    public sealed class SvgRenderer : IDisposable, IGraphicsProvider, ISvgRenderer
        private Graphics _innerGraphics;
        private Stack<ISvgBoundable> _boundables = new Stack<ISvgBoundable>();
        public void SetBoundable(ISvgBoundable boundable)
            _boundables.Push(new ImmutableBoundable(boundable));
        public ISvgBoundable GetBoundable()
            return _boundables.Peek();
        public ISvgBoundable PopBoundable()
            return _boundables.Pop();
        public float DpiY
            get { return _innerGraphics.DpiY; }
        /// <summary>
        /// Initializes a new instance of the <see cref="ISvgRenderer"/> class.
        /// </summary>
        private SvgRenderer(Graphics graphics)
            this._innerGraphics = graphics;
        public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit graphicsUnit)
            _innerGraphics.DrawImage(image, destRect, srcRect, graphicsUnit);
        public void DrawImageUnscaled(Image image, Point location)
            this._innerGraphics.DrawImageUnscaled(image, location);
        public void DrawPath(Pen pen, GraphicsPath path)
            this._innerGraphics.DrawPath(pen, path);
        public void FillPath(Brush brush, GraphicsPath path)
            this._innerGraphics.FillPath(brush, path);
        public Region GetClip()
            return this._innerGraphics.Clip;
        public void RotateTransform(float fAngle, MatrixOrder order = MatrixOrder.Append)
            this._innerGraphics.RotateTransform(fAngle, order);
        public void ScaleTransform(float sx, float sy, MatrixOrder order = MatrixOrder.Append)
this._innerGraphics.ScaleTransform(sx, sy, order); } public void SetClip(Region region, CombineMode combineMode = CombineMode.Replace) { this._innerGraphics.SetClip(region, combineMode); } public void TranslateTransform(float dx, float dy, MatrixOrder order = MatrixOrder.Append) { this._innerGraphics.TranslateTransform(dx, dy, order); } public SmoothingMode SmoothingMode { get { return this._innerGraphics.SmoothingMode; } set { this._innerGraphics.SmoothingMode = value; } } public Matrix Transform { get { return this._innerGraphics.Transform; } set { this._innerGraphics.Transform = value; } } public void Dispose() { this._innerGraphics.Dispose(); } Graphics IGraphicsProvider.GetGraphics() { return _innerGraphics; } /// <summary> /// Creates a new <see cref="ISvgRenderer"/> from the specified <see cref="Image"/>. /// </summary> /// <param name="image"><see cref="Image"/> from which to create the new <see cref="ISvgRenderer"/>.</param> public static ISvgRenderer FromImage(Image image) { var g = Graphics.FromImage(image); g.TextRenderingHint = TextRenderingHint.AntiAlias; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.TextContrast = 1; return new SvgRenderer(g); } /// <summary> /// Creates a new <see cref="ISvgRenderer"/> from the specified <see cref="Graphics"/>. /// </summary> /// <param name="graphics">The <see cref="Graphics"/> to create the renderer from.</param> public static ISvgRenderer FromGraphics(Graphics graphics) { return new SvgRenderer(graphics); } public static ISvgRenderer FromNull() { var img = new Bitmap(1, 1); return SvgRenderer.FromImage(img); } } }