View.cs 10.1 KB
Newer Older
tebjan's avatar
tebjan committed
1
2
using System;
using System.Collections.Generic;
3
using System.Drawing.Imaging;
tebjan's avatar
tebjan committed
4
5
6
7
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
8
using System.Text.RegularExpressions;
tebjan's avatar
tebjan committed
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
using Svg;
using System.Diagnostics;


namespace SvgW3CTestRunner
{
    public partial class View : Form
    {
		//DIRECTORY SEPARATOR: The value of this field is a slash ("/") on UNIX and on Mac OSX, and a backslash ("\") on the Windows operating systems.
		static private string sprt = Path.DirectorySeparatorChar.ToString (); 

		//Data folders
		private string _svgBasePath = @".."+sprt+".."+sprt+".."+sprt+"W3CTestSuite"+sprt+"svg"+sprt;
		private string _pngBasePath = @".."+sprt+".."+sprt+".."+sprt+"W3CTestSuite"+sprt+"png"+sprt;

        public View()
        {
            InitializeComponent();
            // ignore tests pertaining to javascript or xml reading
			var passes = File.ReadAllLines(_svgBasePath + @".."+sprt+"PassingTests.txt").ToDictionary((f) => f, (f) => true);
            var files = (from f in
                         (from g in Directory.GetFiles(_svgBasePath)
                          select Path.GetFileName(g))
                         where !f.StartsWith("animate-") && !f.StartsWith("conform-viewer") &&
                         !f.Contains("-dom-") && !f.StartsWith("linking-") && !f.StartsWith("interact-") &&
                         !f.StartsWith("script-")
                         orderby f
                         select (object)f);

38
39
40
41
            lstFilesOther.Items.AddRange(files.Where(f => ((string)f).StartsWith("__")).ToArray());
            files = files.Where(f => !((string)f).StartsWith("__"));
            lstFilesPassing.Items.AddRange(files.Where(f => passes.ContainsKey((string)f)).ToArray());
            lstFilesFailing.Items.AddRange(files.Where(f => !passes.ContainsKey((string)f)).ToArray());
tebjan's avatar
tebjan committed
42
43
44
45
        }



46
        private void boxConsoleLog_MouseDown(object sender, MouseEventArgs e)
tebjan's avatar
tebjan committed
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
		{
			if (e.Button == System.Windows.Forms.MouseButtons.Right)
			{   //click event
				
				ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
				MenuItem menuItem = new MenuItem("Copy");
				menuItem.Click += new EventHandler(CopyAction);
				contextMenu.MenuItems.Add(menuItem);

				boxConsoleLog.ContextMenu = contextMenu;
			}
		}


		void CopyAction(object sender, EventArgs e)
		{
			if (boxConsoleLog.SelectedText != null && boxConsoleLog.SelectedText != "")
			{
				//Clipboard.SetText(boxConsoleLog.SelectedText.Replace("\n", "\r\n"));

				boxConsoleLog.Copy ();
			}
		}


        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            //render svg
75
            var lstFiles = sender as ListBox;
tebjan's avatar
tebjan committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
            var fileName = lstFiles.SelectedItem.ToString();
            if (fileName.StartsWith("#")) return;
            
            //display png
            var png = Image.FromFile(_pngBasePath + Path.GetFileNameWithoutExtension(fileName) + ".png");
            picPng.Image = png;
            
            var doc = new SvgDocument();
            try
            {
                Debug.Print(fileName);
                doc = SvgDocument.Open(_svgBasePath + fileName);
                if (fileName.StartsWith("__"))
                {
                    picSvg.Image = doc.Draw();
                }
                else
                {
                    var img = new Bitmap(480, 360);
                    doc.Draw(img);
                    picSvg.Image = img;
                }

99
100
                this.boxConsoleLog.AppendText ("\n\nWC3 TEST " + fileName + "\n");
                this.boxDescription.Text = GetDescription(doc);
tebjan's avatar
tebjan committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

            }
            catch (Exception ex)
            {
				this.boxConsoleLog.AppendText ("Result: TEST FAILED\n");
				this.boxConsoleLog.AppendText ("SVG RENDERING ERROR for " + fileName + "\n");
				this.boxConsoleLog.AppendText (ex.ToString());
                picSvg.Image = null;
            }
            
            //save load
            try 
            {
                using(var memStream = new MemoryStream())
                {
Eric Domke's avatar
Eric Domke committed
116
117
118
                    doc.Write(memStream);
                    memStream.Position = 0;  
                    var reader = new StreamReader(memStream);
119
120
                    var tempFilePath = Path.Combine(Path.GetTempPath(), "test.svg");
                    System.IO.File.WriteAllText(tempFilePath, reader.ReadToEnd());
tebjan's avatar
tebjan committed
121
122
                    memStream.Position = 0;
                    var baseUri = doc.BaseUri;
123
                    doc = SvgDocument.Open(tempFilePath);
tebjan's avatar
tebjan committed
124
125
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
                    doc.BaseUri = baseUri;
                    
                    if (fileName.StartsWith("__"))
                    {
                        picSaveLoad.Image = doc.Draw();
                    }
                    else
                    {
                        var img = new Bitmap(480, 360);
                        doc.Draw(img);
                        picSaveLoad.Image = img;
                    }
                }
            } 
            catch (Exception ex)
            {
				this.boxConsoleLog.AppendText ("Result: TEST FAILED\n");
				this.boxConsoleLog.AppendText ("SVG SERIALIZATION ERROR for " + fileName + "\n");
				this.boxConsoleLog.AppendText (ex.ToString());
                picSaveLoad.Image = null;
            }
            
            //compare svg to png
            try
            {
                picSVGPNG.Image = PixelDiff((Bitmap)picPng.Image, (Bitmap)picSvg.Image);
            }
            catch (Exception ex)
            {
				this.boxConsoleLog.AppendText ("Result: TEST FAILED\n");
				this.boxConsoleLog.AppendText ("SVG TO PNG COMPARISON ERROR for " + fileName + "\n");
				this.boxConsoleLog.AppendText (ex.ToString());
                picSVGPNG.Image = null;
            }
158
        }
tebjan's avatar
tebjan committed
159

160
161
162
163
164
165
166
167
        private void fileTabBox_TabIndexChanged(object sender, EventArgs e)
        {
            picSvg.Image = null;
            picPng.Image = null;
            picSaveLoad.Image = null;
            picSVGPNG.Image = null;
        }

168
169
170
171
172
        private SvgElement GetChildWithDescription(SvgElement element, string description)
        {
            var docElements = element.Children.Where(child => child is NonSvgElement && (child as NonSvgElement).Name == description);
            return docElements.Count() > 0 ? docElements.First() : null;
        }
tebjan's avatar
tebjan committed
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
        private string GetDescription(SvgDocument document)
        {
            string description = string.Empty;
            var testCaseElement = GetChildWithDescription(document, "SVGTestCase");
            if (testCaseElement != null)
            {
                var descriptionElement = GetChildWithDescription(testCaseElement, "testDescription");
                if (descriptionElement != null)
                {
                    var regex = new Regex("\r\n *");
                    var descriptionLines = new List<string>();
                    foreach (var child in descriptionElement.Children)
                    {
                        descriptionLines.Add(regex.Replace(child.Content, " "));
                    }
                    return string.Join("\n", descriptionLines.ToArray());
                }
            }
            return description;
tebjan's avatar
tebjan committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
        }
        
        unsafe Bitmap PixelDiff(Bitmap a, Bitmap b)
        {
            Bitmap output = new Bitmap(a.Width, a.Height, PixelFormat.Format32bppArgb);
            Rectangle rect = new Rectangle(Point.Empty, a.Size);
            using (var aData = a.LockBitsDisposable(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb))
                using (var bData = b.LockBitsDisposable(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb))
                    using (var outputData = output.LockBitsDisposable(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb))
            {
                byte* aPtr = (byte*)aData.Scan0;
                byte* bPtr = (byte*)bData.Scan0;
                byte* outputPtr = (byte*)outputData.Scan0;
                int len = aData.Stride * aData.Height;
                for (int i = 0; i < len; i++)
                {
                    // For alpha use the average of both images (otherwise pixels with the same alpha won't be visible)
                    if ((i + 1) % 4 == 0)
                        *outputPtr = (byte)((*aPtr  + *bPtr) / 2);
                    else
                        *outputPtr = (byte)~(*aPtr ^ *bPtr);

                    outputPtr++;
                    aPtr++;
                    bPtr++;
                }
            }
            return output;
221
        }
tebjan's avatar
tebjan committed
222
    }
223

tebjan's avatar
tebjan committed
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
267
268
269
270
271
272
273
274
275
276
277
278
    static class BitmapExtensions
    {
        public static DisposableImageData LockBitsDisposable(this Bitmap bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format)
        {
            return new DisposableImageData(bitmap, rect, flags, format);
        }

        public class DisposableImageData : IDisposable
        {
            private readonly Bitmap _bitmap;
            private readonly BitmapData _data;

            internal DisposableImageData(Bitmap bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format)
            {
                _bitmap = bitmap;
                _data = bitmap.LockBits(rect, flags, format);
            }

            public void Dispose()
            {
                _bitmap.UnlockBits(_data);
            }

            public IntPtr Scan0
            {
                get { return _data.Scan0; }
            }

            public int Stride
            {
                get { return _data.Stride;}
            }

            public int Width
            {
                get { return _data.Width;}
            }

            public int Height
            {
                get { return _data.Height;}
            }

            public PixelFormat PixelFormat
            {
                get { return _data.PixelFormat;}
            }

            public int Reserved
            {
                get { return _data.Reserved;}
            }
        }
    }
}