SvgFragment.cs 2.88 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.ComponentModel;

namespace Svg
{
    /// <summary>
    /// An <see cref="SvgFragment"/> represents an SVG fragment that can be the root element or an embedded fragment of an SVG document.
    /// </summary>
    public class SvgFragment : SvgElement, ISvgViewPort
    {
        private SvgUnit _width;
        private SvgUnit _height;
        private SvgViewBox _viewBox;

        /// <summary>
        /// Gets the SVG namespace string.
        /// </summary>
        public static readonly Uri Namespace = new Uri("http://www.w3.org/2000/svg");

        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>The width.</value>
        [SvgAttribute("width")]
        public SvgUnit Width
        {
            get { return this._width; }
            set { this._width = value; }
        }

        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        /// <value>The height.</value>
        [SvgAttribute("height")]
        public SvgUnit Height
        {
            get { return this._height; }
            set { this._height = value; }
        }

        /// <summary>
        /// Gets or sets the viewport of the element.
        /// </summary>
        /// <value></value>
        [SvgAttribute("viewBox")]
        public SvgViewBox ViewBox
        {
            get { return this._viewBox; }
            set { this._viewBox = value; }
        }

        /// <summary>
        /// Gets the name of the element.
        /// </summary>
        /// <value></value>
        protected override string ElementName
        {
            get { return "svg"; }
        }

        /// <summary>
68
        /// Pushes the transforms.
davescriven's avatar
davescriven committed
69
        /// </summary>
70
71
        /// <param name="graphics">The graphics.</param>
        protected internal override void PushTransforms(Graphics graphics)
davescriven's avatar
davescriven committed
72
        {
73
            base.PushTransforms(graphics);
davescriven's avatar
davescriven committed
74
75
76
77
78

            if (!this.ViewBox.Equals(SvgViewBox.Empty))
            {
                if (this.ViewBox.MinX > 0 || this.ViewBox.MinY > 0)
                {
79
                    graphics.TranslateTransform(this.ViewBox.MinX, this.ViewBox.MinY, MatrixOrder.Append);
davescriven's avatar
davescriven committed
80
81
                }

82
                graphics.ScaleTransform(this.Width.ToDeviceValue() / this.ViewBox.Width, this.Height.ToDeviceValue() / this.ViewBox.Height, MatrixOrder.Append);
davescriven's avatar
davescriven committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SvgFragment"/> class.
        /// </summary>
        public SvgFragment()
        {
            this._height = new SvgUnit(SvgUnitType.Percentage, 100.0f);
            this._width = 1000.0f;
            this.ViewBox = SvgViewBox.Empty;
        }
    }
}