blob: f1d75ddba382f03730dbae1c9fd0e8400fba0e4e [file] [log] [blame]
Kevin Lubick92c91712018-08-09 10:00:02 -04001describe('PathKit\'s SVG Behavior', function() {
Kevin Lubick97d6d982018-08-10 15:53:16 -04002 it('can create a path from an SVG string', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -05003 LoadPathKit.then(catchException(done, () => {
Kevin Lubick92c91712018-08-09 10:00:02 -04004 //.This is a parallelagram from
5 // https://upload.wikimedia.org/wikipedia/commons/e/e7/Simple_parallelogram.svg
6 let path = PathKit.FromSVGString('M 205,5 L 795,5 L 595,295 L 5,295 L 205,5 z');
7
8 let cmds = path.toCmds();
9 expect(cmds).toBeTruthy();
10 // 1 move, 4 lines, 1 close
11 // each element in cmds is an array, with index 0 being the verb, and the rest being args
12 expect(cmds.length).toBe(6);
13 expect(cmds).toEqual([[PathKit.MOVE_VERB, 205, 5],
14 [PathKit.LINE_VERB, 795, 5],
15 [PathKit.LINE_VERB, 595, 295],
16 [PathKit.LINE_VERB, 5, 295],
17 [PathKit.LINE_VERB, 205, 5],
18 [PathKit.CLOSE_VERB]]);
19 path.delete();
20 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050021 }));
Kevin Lubick92c91712018-08-09 10:00:02 -040022 });
23
Kevin Lubick97d6d982018-08-10 15:53:16 -040024 it('can create an SVG string from a path', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050025 LoadPathKit.then(catchException(done, () => {
Kevin Lubick644d8e72018-08-09 13:58:04 -040026 let cmds = [[PathKit.MOVE_VERB, 205, 5],
27 [PathKit.LINE_VERB, 795, 5],
28 [PathKit.LINE_VERB, 595, 295],
29 [PathKit.LINE_VERB, 5, 295],
30 [PathKit.LINE_VERB, 205, 5],
31 [PathKit.CLOSE_VERB]];
Kevin Lubickd9936482018-08-24 10:44:16 -040032 let path = PathKit.FromCmds(cmds);
Kevin Lubick92c91712018-08-09 10:00:02 -040033
34 let svgStr = path.toSVGString();
35 // We output it in terse form, which is different than Wikipedia's version
36 expect(svgStr).toEqual('M205 5L795 5L595 295L5 295L205 5Z');
37 path.delete();
38 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050039 }));
Kevin Lubick92c91712018-08-09 10:00:02 -040040 });
41
Kevin Lubick97d6d982018-08-10 15:53:16 -040042 it('can create an SVG string from hex values', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050043 LoadPathKit.then(catchException(done, () => {
Kevin Lubick644d8e72018-08-09 13:58:04 -040044 let cmds = [[PathKit.MOVE_VERB, "0x15e80300", "0x400004dc"], // 9.37088e-26f, 2.0003f
45 [PathKit.LINE_VERB, 795, 5],
46 [PathKit.LINE_VERB, 595, 295],
47 [PathKit.LINE_VERB, 5, 295],
48 [PathKit.LINE_VERB, "0x15e80300", "0x400004dc"], // 9.37088e-26f, 2.0003f
49 [PathKit.CLOSE_VERB]];
Kevin Lubickd9936482018-08-24 10:44:16 -040050 let path = PathKit.FromCmds(cmds);
Kevin Lubick644d8e72018-08-09 13:58:04 -040051
52 let svgStr = path.toSVGString();
53 expect(svgStr).toEqual('M9.37088e-26 2.0003L795 5L595 295L5 295L9.37088e-26 2.0003Z');
54 path.delete();
55 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050056 }));
Kevin Lubick644d8e72018-08-09 13:58:04 -040057 });
58
Kevin Lubick97d6d982018-08-10 15:53:16 -040059 it('should have input and the output be the same', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050060 LoadPathKit.then(catchException(done, () => {
Kevin Lubick92c91712018-08-09 10:00:02 -040061 let testCases = [
62 'M0 0L1075 0L1075 242L0 242L0 0Z'
63 ];
64
65 for(let svg of testCases) {
66 let path = PathKit.FromSVGString(svg);
67 let output = path.toSVGString();
68
69 expect(svg).toEqual(output);
70
71 path.delete();
72 }
73 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050074 }));
Kevin Lubick92c91712018-08-09 10:00:02 -040075 });
76
Kevin Lubick97d6d982018-08-10 15:53:16 -040077 it('approximates arcs (conics) with quads', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050078 LoadPathKit.then(catchException(done, () => {
Kevin Lubick97d6d982018-08-10 15:53:16 -040079 let path = PathKit.NewPath();
Kevin Lubicka0ba6122018-08-15 13:45:28 -040080 path.moveTo(50, 120);
81 path.arc(50, 120, 45, 0, 1.75 * Math.PI);
82 path.lineTo(50, 120);
Kevin Lubick97d6d982018-08-10 15:53:16 -040083 let svgStr = path.toSVGString();
84 // Q stands for quad. No need to check the whole path, as that's more
85 // what the gold correctness tests are for (can account for changes we make
86 // to the approximation algorithms).
87 expect(svgStr).toContain('Q');
88 path.delete();
Kevin Lubicka0ba6122018-08-15 13:45:28 -040089
90 reportSVGString(svgStr, 'conics_quads_approx').then(() => {
91 done();
92 }).catch(reportError(done));
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050093 }));
Kevin Lubick97d6d982018-08-10 15:53:16 -040094 });
95
Kevin Lubick644d8e72018-08-09 13:58:04 -040096});