SvgDocument.cs 9.06 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing.Drawing2D;
using System.Xml.Schema;

namespace Svg
{
    /// <summary>
    /// The class used to create and load all SVG documents.
    /// </summary>
    public class SvgDocument : SvgFragment, ITypeDescriptorContext
    {
        private SvgElementIdManager _idManager;
        private int _ppi;
        public static readonly int PPI = 96;
        /// <summary>
        /// Gets a <see cref="string"/> containing the XLink namespace (http://www.w3.org/1999/xlink).
        /// </summary>
        public static readonly string XLinkNamespace = "http://www.w3.org/1999/xlink";

        /// <summary>
        /// Retrieves the <see cref="SvgElement"/> with the specified ID.
        /// </summary>
        /// <param name="id">A <see cref="string"/> containing the ID of the element to find.</param>
        /// <returns>An <see cref="SvgElement"/> of one exists with the specified ID; otherwise false.</returns>
        public virtual SvgElement GetElementById(string id)
        {
            return this.IdManager.GetElementById(id);
        }

        /// <summary>
        /// Gets an <see cref="SvgElementIdManager"/> for this document.
        /// </summary>
        protected internal virtual SvgElementIdManager IdManager
        {
            get
            {
                if (this._idManager == null)
                {
                    this._idManager = new SvgElementIdManager(this);
                }

                return this._idManager;
            }
        }

        public int Ppi
        {
            get { return this._ppi; }
            set { this._ppi = value; }
        }

        public SvgDocument()
        {
            this._ppi = 96;
        }

        /// <summary>
        /// Opens the document at the specified path and loads the contents.
        /// </summary>
        /// <param name="path">A <see cref="string"/> containing the path of the file to open.</param>
        /// <returns>An <see cref="SvgDocument"/> with the contents loaded.</returns>
        /// <exception cref="FileNotFoundException">The document at the specified <paramref name="path"/> cannot be found.</exception>
        public static SvgDocument Open(string path)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("The specified document cannot be found.", path);
            }

            return Open(File.OpenRead(path));
        }

        public static SvgDocument Open(Stream stream)
        {
            return Open(stream, null);
        }

        public static SvgDocument OpenRender(Stream stream, Graphics graphics)
        {
            return null;
        }

91
        public static SvgDocument Open(Stream stream, Dictionary<string, string> entities)
davescriven's avatar
davescriven committed
92
93
94
        {
            Trace.TraceInformation("Begin Read");

95
            using (SvgTextReader reader = new SvgTextReader(stream, entities))
davescriven's avatar
davescriven committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
            {
                Stack<SvgElement> elementStack = new Stack<SvgElement>();
                StringBuilder value = new StringBuilder();
                SvgElement element = null;
                SvgElement parent = null;
                SvgDocument svgDocument = null;
                reader.XmlResolver = new SvgDtdResolver();
                bool isEmpty;
                reader.WhitespaceHandling = WhitespaceHandling.None;

                while (reader.Read())
                {
                    try
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                // Does this element have a value or children
                                // (Must do this check here before we progress to another node)
                                isEmpty = reader.IsEmptyElement;
                                // Create element
                                if (elementStack.Count > 0)
                                {
                                    element = SvgElementFactory.CreateElement(reader, svgDocument);
                                }
                                else
                                {
                                    element = SvgElementFactory.CreateDocument(reader);
                                    svgDocument = (SvgDocument)element;
                                }

                                if (element == null)
                                    continue;

                                // Add to the parents children
                                if (elementStack.Count > 0)
                                {
                                    parent = elementStack.Peek();
                                    parent.Children.Add(element);
                                }

                                // Push element into stack
                                elementStack.Push(element);

                                // Need to process if the element is empty
                                if (isEmpty)
142
                                {
davescriven's avatar
davescriven committed
143
                                    goto case XmlNodeType.EndElement;
144
                                }
davescriven's avatar
davescriven committed
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

                                break;
                            case XmlNodeType.EndElement:
                                // Skip if no element was created
                                if (element == null)
                                    continue;
                                // Pop the element out of the stack
                                element = elementStack.Pop();

                                if (value.Length > 0)
                                {
                                    element.Content = value.ToString();
                                    // Reset content value for new element
                                    value = new StringBuilder();
                                }
                                break;
                            case XmlNodeType.CDATA:
                            case XmlNodeType.Text:
                                value.Append(reader.Value);
                                break;
                        }
                    }
                    catch (Exception exc)
                    {
                        Trace.TraceError(exc.Message);
                    }
                }

                Trace.TraceInformation("End Read");
                return svgDocument;
            }
        }

        public static SvgDocument Open(System.Xml.XmlDocument document)
        {
            return null;
        }

        public static Bitmap OpenAsBitmap(string path)
        {
            return null;
        }

        public static Bitmap OpenAsBitmap(System.Xml.XmlDocument document)
        {
            return null;
        }

        public RectangleF GetDimensions()
        {
            return new RectangleF(0, 0, this.Width.ToDeviceValue(), this.Height.ToDeviceValue());
            return new RectangleF();
        }

        public void Draw(Graphics graphics)
        {
            this.Render(graphics);
        }

        public virtual Bitmap Draw()
        {
            Trace.TraceInformation("Begin Render");

            RectangleF size = this.GetDimensions();
            Bitmap bitmap = new Bitmap((int)this.Width.ToDeviceValue(), (int)this.Height.ToDeviceValue());

            using (Graphics g = Graphics.FromImage(bitmap))
            {
213
214
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                g.TextContrast = 1;
davescriven's avatar
davescriven committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
                g.PixelOffsetMode = PixelOffsetMode.Half;
                this.Render(g);
                g.Save();
            }

            Trace.TraceInformation("End Render");
            return bitmap;
        }

        public void Write(Stream stream)
        {
            using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
            {
            }
        }

        public void Write(string path)
        {
            this.Write(File.Create(path));
        }

        IContainer ITypeDescriptorContext.Container
        {
            get { throw new NotImplementedException(); }
        }

        object ITypeDescriptorContext.Instance
        {
            get { return this; }
        }

        void ITypeDescriptorContext.OnComponentChanged()
        {
            throw new NotImplementedException();
        }

        bool ITypeDescriptorContext.OnComponentChanging()
        {
            throw new NotImplementedException();
        }

        PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor
        {
            get { throw new NotImplementedException(); }
        }

        object IServiceProvider.GetService(Type serviceType)
        {
            throw new NotImplementedException();
        }
    }
}