• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1describe('PathKit\'s SVG Behavior', function() {
2    it('can create a path from an SVG string', function(done) {
3        LoadPathKit.then(catchException(done, () => {
4            //.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();
21        }));
22    });
23
24    it('can create an SVG string from a path', function(done) {
25        LoadPathKit.then(catchException(done, () => {
26            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]];
32            let path = PathKit.FromCmds(cmds);
33
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();
39        }));
40    });
41
42    it('can create an SVG string from hex values', function(done) {
43        LoadPathKit.then(catchException(done, () => {
44            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]];
50            let path = PathKit.FromCmds(cmds);
51
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();
56        }));
57    });
58
59    it('should have input and the output be the same', function(done) {
60        LoadPathKit.then(catchException(done, () => {
61            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();
74        }));
75    });
76
77    it('approximates arcs (conics) with quads', function(done) {
78        LoadPathKit.then(catchException(done, () => {
79            let path = PathKit.NewPath();
80            path.moveTo(50, 120);
81            path.arc(50, 120, 45, 0, 1.75 * Math.PI);
82            path.lineTo(50, 120);
83            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();
89
90             reportSVGString(svgStr, 'conics_quads_approx').then(() => {
91                done();
92            }).catch(reportError(done));
93        }));
94    });
95
96});
97