SvgPatternServer.cs 5.62 KB
Newer Older
davescriven's avatar
davescriven committed
1
2
3
4
5
6
7
8
9
10
11
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.ComponentModel;

using Svg.Transforms;

namespace Svg
{
12
13
14
15
    /// <summary>
    /// A pattern is used to fill or stroke an object using a pre-defined graphic object which can be replicated ("tiled") at fixed intervals in x and y to cover the areas to be painted.
    /// </summary>
    [SvgElement("pattern")]
davescriven's avatar
davescriven committed
16
17
18
19
20
21
22
23
    public sealed class SvgPatternServer : SvgPaintServer, ISvgViewPort
    {
        private SvgUnit _width;
        private SvgUnit _height;
        private SvgUnit _x;
        private SvgUnit _y;
        private SvgViewBox _viewBox;

24
25
26
27
28
29
30
31
		[SvgAttribute("overflow")]
		public SvgOverflow Overflow
		{
			get { return this.Attributes.GetAttribute<SvgOverflow>("overflow"); }
			set { this.Attributes["overflow"] = value; }
		}


32
33
34
35
        /// <summary>
        /// Specifies a supplemental transformation which is applied on top of any 
        /// transformations necessary to create a new pattern coordinate system.
        /// </summary>
davescriven's avatar
davescriven committed
36
37
38
39
40
41
        [SvgAttribute("viewBox")]
        public SvgViewBox ViewBox
        {
            get { return this._viewBox; }
            set { this._viewBox = value; }
        }
Tebjan Halm's avatar
Tebjan Halm committed
42
43
44
45
46
47
48
49
50
51
52
        
        /// <summary>
        /// Gets or sets the aspect of the viewport.
        /// </summary>
        /// <value></value>
        [SvgAttribute("preserveAspectRatio")]
        public SvgAspectRatio AspectRatio 
		{
			get;
			set;
		}
davescriven's avatar
davescriven committed
53

54
55
56
        /// <summary>
        /// Gets or sets the width of the pattern.
        /// </summary>
davescriven's avatar
davescriven committed
57
58
59
60
61
62
63
        [SvgAttribute("width")]
        public SvgUnit Width
        {
            get { return this._width; }
            set { this._width = value; }
        }

64
65
66
        /// <summary>
        /// Gets or sets the height of the pattern.
        /// </summary>
davescriven's avatar
davescriven committed
67
68
69
70
71
72
73
        [SvgAttribute("height")]
        public SvgUnit Height
        {
            get { return this._height; }
            set { this._height = value; }
        }

74
75
76
        /// <summary>
        /// Gets or sets the X-axis location of the pattern.
        /// </summary>
davescriven's avatar
davescriven committed
77
78
79
80
81
82
83
        [SvgAttribute("x")]
        public SvgUnit X
        {
            get { return this._x; }
            set { this._x = value; }
        }

84
85
86
        /// <summary>
        /// Gets or sets the Y-axis location of the pattern.
        /// </summary>
davescriven's avatar
davescriven committed
87
88
89
90
91
92
93
        [SvgAttribute("y")]
        public SvgUnit Y
        {
            get { return this._y; }
            set { this._y = value; }
        }

94
95
96
        /// <summary>
        /// Initializes a new instance of the <see cref="SvgPatternServer"/> class.
        /// </summary>
davescriven's avatar
davescriven committed
97
98
99
100
101
102
103
104
        public SvgPatternServer()
        {
            this._x = new SvgUnit(0.0f);
            this._y = new SvgUnit(0.0f);
            this._width = new SvgUnit(0.0f);
            this._height = new SvgUnit(0.0f);
        }

105
106
107
108
109
        /// <summary>
        /// Gets a <see cref="Brush"/> representing the current paint server.
        /// </summary>
        /// <param name="styleOwner">The owner <see cref="SvgVisualElement"/>.</param>
        /// <param name="opacity">The opacity of the brush.</param>
110
        public override Brush GetBrush(SvgVisualElement renderingElement, float opacity)
davescriven's avatar
davescriven committed
111
112
113
114
115
116
117
118
119
120
121
122
123
        {
            // If there aren't any children, return null
            if (this.Children.Count == 0)
                return null;

            // Can't render if there are no dimensions
            if (this._width.Value == 0.0f || this._height.Value == 0.0f)
                return null;

            float width = this._width.ToDeviceValue(renderingElement);
            float height = this._height.ToDeviceValue(renderingElement, true);

            Bitmap image = new Bitmap((int)width, (int)height);
124
            using (SvgRenderer renderer = SvgRenderer.FromImage(image))
davescriven's avatar
davescriven committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
            {
                Matrix patternMatrix = new Matrix();

                // Apply a translate if needed
                if (this._x.Value > 0.0f || this._y.Value > 0.0f)
                {
                    patternMatrix.Translate(this._x.ToDeviceValue(renderingElement) + -1.0f, this._y.ToDeviceValue(renderingElement, true) + -1.0f);
                }
                else
                {
                    patternMatrix.Translate(-1, -1);
                }

                if (this.ViewBox.Height > 0 || this.ViewBox.Width > 0)
                {
                    patternMatrix.Scale(this.Width.ToDeviceValue() / this.ViewBox.Width, this.Height.ToDeviceValue() / this.ViewBox.Height);
                }

143
144
145
146
                renderer.Transform = patternMatrix;
                renderer.CompositingQuality = CompositingQuality.HighQuality;
                renderer.SmoothingMode = SmoothingMode.AntiAlias;
                renderer.PixelOffsetMode = PixelOffsetMode.Half;
davescriven's avatar
davescriven committed
147
148
149

                foreach (SvgElement child in this.Children)
                {
150
                    child.RenderElement(renderer);
davescriven's avatar
davescriven committed
151
152
                }

153
                renderer.Save();
davescriven's avatar
davescriven committed
154
155
156
157
158
159
            }

            TextureBrush textureBrush = new TextureBrush(image);

            return textureBrush;
        }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182




		public override SvgElement DeepCopy()
		{
			return DeepCopy<SvgPatternServer>();
		}


		public override SvgElement DeepCopy<T>()
		{
			var newObj = base.DeepCopy<T>() as SvgPatternServer;
			newObj.Overflow = this.Overflow;
			newObj.ViewBox = this.ViewBox;
			newObj.AspectRatio = this.AspectRatio;
			newObj.X = this.X;
			newObj.Y = this.Y;
			newObj.Width = this.Width;
			newObj.Height = this.Height;
			return newObj;

		}
davescriven's avatar
davescriven committed
183
184
    }
}