blob: 02cfa4ed6442549b78edb00728ccc7a68567f0a7 [file] [log] [blame]
Kevin Lubickd2efe522019-01-04 15:59:06 -05001jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;
Kevin Lubick92c91712018-08-09 10:00:02 -04002describe('PathKit\'s SVG Behavior', function() {
3 // Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up.
4 var PathKit = null;
Kevin Lubick97d6d982018-08-10 15:53:16 -04005 const LoadPathKit = new Promise(function(resolve, reject) {
Kevin Lubick92c91712018-08-09 10:00:02 -04006 if (PathKit) {
7 resolve();
8 } else {
9 PathKitInit({
Kevin Lubick97d6d982018-08-10 15:53:16 -040010 locateFile: (file) => '/pathkit/'+file,
Kevin Lubick275eaff2019-01-04 14:38:29 -050011 }).ready().then((_PathKit) => {
Kevin Lubick92c91712018-08-09 10:00:02 -040012 PathKit = _PathKit;
13 resolve();
14 });
15 }
16 });
17
Kevin Lubick97d6d982018-08-10 15:53:16 -040018 it('can create a path from an SVG string', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050019 LoadPathKit.then(catchException(done, () => {
Kevin Lubick92c91712018-08-09 10:00:02 -040020 //.This is a parallelagram from
21 // https://upload.wikimedia.org/wikipedia/commons/e/e7/Simple_parallelogram.svg
22 let path = PathKit.FromSVGString('M 205,5 L 795,5 L 595,295 L 5,295 L 205,5 z');
23
24 let cmds = path.toCmds();
25 expect(cmds).toBeTruthy();
26 // 1 move, 4 lines, 1 close
27 // each element in cmds is an array, with index 0 being the verb, and the rest being args
28 expect(cmds.length).toBe(6);
29 expect(cmds).toEqual([[PathKit.MOVE_VERB, 205, 5],
30 [PathKit.LINE_VERB, 795, 5],
31 [PathKit.LINE_VERB, 595, 295],
32 [PathKit.LINE_VERB, 5, 295],
33 [PathKit.LINE_VERB, 205, 5],
34 [PathKit.CLOSE_VERB]]);
35 path.delete();
36 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050037 }));
Kevin Lubick92c91712018-08-09 10:00:02 -040038 });
39
Kevin Lubick97d6d982018-08-10 15:53:16 -040040 it('can create an SVG string from a path', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050041 LoadPathKit.then(catchException(done, () => {
Kevin Lubick644d8e72018-08-09 13:58:04 -040042 let cmds = [[PathKit.MOVE_VERB, 205, 5],
43 [PathKit.LINE_VERB, 795, 5],
44 [PathKit.LINE_VERB, 595, 295],
45 [PathKit.LINE_VERB, 5, 295],
46 [PathKit.LINE_VERB, 205, 5],
47 [PathKit.CLOSE_VERB]];
Kevin Lubickd9936482018-08-24 10:44:16 -040048 let path = PathKit.FromCmds(cmds);
Kevin Lubick92c91712018-08-09 10:00:02 -040049
50 let svgStr = path.toSVGString();
51 // We output it in terse form, which is different than Wikipedia's version
52 expect(svgStr).toEqual('M205 5L795 5L595 295L5 295L205 5Z');
53 path.delete();
54 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050055 }));
Kevin Lubick92c91712018-08-09 10:00:02 -040056 });
57
Kevin Lubick97d6d982018-08-10 15:53:16 -040058 it('can create an SVG string from hex values', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050059 LoadPathKit.then(catchException(done, () => {
Kevin Lubick644d8e72018-08-09 13:58:04 -040060 let cmds = [[PathKit.MOVE_VERB, "0x15e80300", "0x400004dc"], // 9.37088e-26f, 2.0003f
61 [PathKit.LINE_VERB, 795, 5],
62 [PathKit.LINE_VERB, 595, 295],
63 [PathKit.LINE_VERB, 5, 295],
64 [PathKit.LINE_VERB, "0x15e80300", "0x400004dc"], // 9.37088e-26f, 2.0003f
65 [PathKit.CLOSE_VERB]];
Kevin Lubickd9936482018-08-24 10:44:16 -040066 let path = PathKit.FromCmds(cmds);
Kevin Lubick644d8e72018-08-09 13:58:04 -040067
68 let svgStr = path.toSVGString();
69 expect(svgStr).toEqual('M9.37088e-26 2.0003L795 5L595 295L5 295L9.37088e-26 2.0003Z');
70 path.delete();
71 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050072 }));
Kevin Lubick644d8e72018-08-09 13:58:04 -040073 });
74
Kevin Lubick97d6d982018-08-10 15:53:16 -040075 it('should have input and the output be the same', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050076 LoadPathKit.then(catchException(done, () => {
Kevin Lubick92c91712018-08-09 10:00:02 -040077 let testCases = [
78 'M0 0L1075 0L1075 242L0 242L0 0Z'
79 ];
80
81 for(let svg of testCases) {
82 let path = PathKit.FromSVGString(svg);
83 let output = path.toSVGString();
84
85 expect(svg).toEqual(output);
86
87 path.delete();
88 }
89 done();
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050090 }));
Kevin Lubick92c91712018-08-09 10:00:02 -040091 });
92
Kevin Lubick97d6d982018-08-10 15:53:16 -040093 it('approximates arcs (conics) with quads', function(done) {
Kevin Lubicke71e9ef2018-11-05 07:51:40 -050094 LoadPathKit.then(catchException(done, () => {
Kevin Lubick97d6d982018-08-10 15:53:16 -040095 let path = PathKit.NewPath();
Kevin Lubicka0ba6122018-08-15 13:45:28 -040096 path.moveTo(50, 120);
97 path.arc(50, 120, 45, 0, 1.75 * Math.PI);
98 path.lineTo(50, 120);
Kevin Lubick97d6d982018-08-10 15:53:16 -040099 let svgStr = path.toSVGString();
100 // Q stands for quad. No need to check the whole path, as that's more
101 // what the gold correctness tests are for (can account for changes we make
102 // to the approximation algorithms).
103 expect(svgStr).toContain('Q');
104 path.delete();
Kevin Lubicka0ba6122018-08-15 13:45:28 -0400105
106 reportSVGString(svgStr, 'conics_quads_approx').then(() => {
107 done();
108 }).catch(reportError(done));
Kevin Lubicke71e9ef2018-11-05 07:51:40 -0500109 }));
Kevin Lubick97d6d982018-08-10 15:53:16 -0400110 });
111
Kevin Lubick644d8e72018-08-09 13:58:04 -0400112});