SvgImage.cs 10.4 KB
Newer Older
1
2
using System;
using System.Diagnostics;
3
using System.Drawing;
4
5
6
7
using System.Drawing.Drawing2D;
using System.IO;
using System.Net;
using Svg.Transforms;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

namespace Svg
{
    /// <summary>
    /// Represents and SVG image
    /// </summary>
    [SvgElement("image")]
    public class SvgImage : SvgVisualElement
    {
        /// <summary>
		/// Initializes a new instance of the <see cref="SvgImage"/> class.
        /// </summary>
		public SvgImage()
        {
            Width = new SvgUnit(0.0f);
            Height = new SvgUnit(0.0f);
        }

        /// <summary>
        /// Gets an <see cref="SvgPoint"/> representing the top left point of the rectangle.
        /// </summary>
        public SvgPoint Location
        {
            get { return new SvgPoint(X, Y); }
32
33
34
35
36
37
38
39
40
41
42
        }

        /// <summary>
        /// Gets or sets the aspect of the viewport.
        /// </summary>
        /// <value></value>
        [SvgAttribute("preserveAspectRatio")]
        public SvgAspectRatio AspectRatio
        {
            get { return this.Attributes.GetAttribute<SvgAspectRatio>("preserveAspectRatio"); }
            set { this.Attributes["preserveAspectRatio"] = value; }
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        }

		[SvgAttribute("x")]
		public virtual SvgUnit X
		{
			get { return this.Attributes.GetAttribute<SvgUnit>("x"); }
			set { this.Attributes["x"] = value; }
		}

		[SvgAttribute("y")]
		public virtual SvgUnit Y
		{
			get { return this.Attributes.GetAttribute<SvgUnit>("y"); }
			set { this.Attributes["y"] = value; }
		}


		[SvgAttribute("width")]
		public virtual SvgUnit Width
		{
			get { return this.Attributes.GetAttribute<SvgUnit>("width"); }
			set { this.Attributes["width"] = value; }
		}

		[SvgAttribute("height")]
		public virtual SvgUnit Height
		{
			get { return this.Attributes.GetAttribute<SvgUnit>("height"); }
			set { this.Attributes["height"] = value; }
		}

		[SvgAttribute("href", SvgAttributeAttribute.XLinkNamespace)]
		public virtual Uri Href
		{
			get { return this.Attributes.GetAttribute<Uri>("href"); }
			set { this.Attributes["href"] = value; }
		}



        /// <summary>
        /// Gets the bounds of the element.
        /// </summary>
        /// <value>The bounds.</value>
        public override RectangleF Bounds
        {
89
90
91
			get { return new RectangleF(this.Location.ToDeviceValue(null, this), 
                                        new SizeF(this.Width.ToDeviceValue(null, UnitRenderingType.Horizontal, this), 
                                                  this.Height.ToDeviceValue(null, UnitRenderingType.Vertical, this))); }
92
93
94
95
        }

        /// <summary>
        /// Gets the <see cref="GraphicsPath"/> for this element.
96
97
        /// </summary>
        public override GraphicsPath Path(SvgRenderer renderer)
98
        {
99
		    return null;
100
101
102
103
        }

        /// <summary>
        /// Renders the <see cref="SvgElement"/> and contents to the specified <see cref="Graphics"/> object.
104
105
106
        /// </summary>
        protected override void Render(SvgRenderer renderer)
        {
C Moore's avatar
C Moore committed
107
108
109
            if (!Visible || !Displayable)
                return;

110
111
112
113
114
115
            if (Width.Value > 0.0f && Height.Value > 0.0f && this.Href != null)
            {
                using (Image b = GetImage(this.Href))
                {
                    if (b != null)
                    {
116
117
118
119
120
121
                        var srcRect = new RectangleF(0, 0, b.Width, b.Height);
                        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;
                        
122
                        this.PushTransforms(renderer);
123
                        renderer.AddClip(new Region(destClip));
124
125
                        this.SetClip(renderer);

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
                        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;
                            }
177

178
179
180
181
                            destRect = new RectangleF(destClip.X + xOffset, destClip.Y + yOffset, 
                                                      srcRect.Width * fScaleX, srcRect.Height * fScaleY);
                        }
                        
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
                        renderer.DrawImage(b, destRect, srcRect, GraphicsUnit.Pixel);

                        this.ResetClip(renderer);
                        this.PopTransforms(renderer);
                    }
                }
                // TODO: cache images... will need a shared context for this
                // TODO: support preserveAspectRatio, etc
            }
        }

        protected Image GetImage(Uri uri)
        {
            try
            {
197
                // handle data/uri embedded images (http://en.wikipedia.org/wiki/Data_URI_scheme)
198
                if (uri.IsAbsoluteUri && uri.Scheme == "data")
199
200
201
202
203
204
205
206
207
208
209
210
211
                {
                    string uriString = uri.OriginalString;
                    int dataIdx = uriString.IndexOf(",") + 1;
                    if (dataIdx <= 0 || dataIdx + 1 > uriString.Length)
                        throw new Exception("Invalid data URI");

                    // we're assuming base64, as ascii encoding would be *highly* unsusual for images
                    // also assuming it's png or jpeg mimetype
                    byte[] imageBytes = Convert.FromBase64String(uriString.Substring(dataIdx));
                    Image image = Image.FromStream(new MemoryStream(imageBytes));
                    return image;
                }

212
213
214
215
216
                if (!uri.IsAbsoluteUri)
                {
                    uri = new Uri(OwnerDocument.BaseUri, uri);
                }

217
218
219
220
221
222
                // should work with http: and file: protocol urls
                var httpRequest = WebRequest.Create(uri);

                using (WebResponse webResponse = httpRequest.GetResponse())
                {
                    MemoryStream ms = BufferToMemoryStream(webResponse.GetResponseStream());
223
224
225
226
227
228
229
230
231
                    if (uri.LocalPath.EndsWith(".svg", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var doc = SvgDocument.Open<SvgDocument>(ms);
                        return doc.Draw();
                    }
                    else
                    {
                        return Bitmap.FromStream(ms);
                    }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error loading image: '{0}', error: {1} ", uri, ex.Message);
                return null;
            }
        }

        protected static MemoryStream BufferToMemoryStream(Stream input)
        {
            byte[] buffer = new byte[4 * 1024];
            int len;
            MemoryStream ms = new MemoryStream();
            while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, len);
            }
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
        }


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

		public override SvgElement DeepCopy<T>()
		{
 			var newObj = base.DeepCopy<T>() as SvgImage;
			newObj.Height = this.Height;
			newObj.Width = this.Width;
			newObj.X = this.X;
			newObj.Y = this.Y;
			newObj.Href = this.Href;
			return newObj;
		}
    }
}