Kevin Lubick | d2efe52 | 2019-01-04 15:59:06 -0500 | [diff] [blame^] | 1 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 2 | describe('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 Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 5 | const LoadPathKit = new Promise(function(resolve, reject) { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 6 | if (PathKit) { |
| 7 | resolve(); |
| 8 | } else { |
| 9 | PathKitInit({ |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 10 | locateFile: (file) => '/pathkit/'+file, |
Kevin Lubick | 275eaff | 2019-01-04 14:38:29 -0500 | [diff] [blame] | 11 | }).ready().then((_PathKit) => { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 12 | PathKit = _PathKit; |
| 13 | resolve(); |
| 14 | }); |
| 15 | } |
| 16 | }); |
| 17 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 18 | it('can create a path from an SVG string', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 19 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 20 | //.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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 37 | })); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 38 | }); |
| 39 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 40 | it('can create an SVG string from a path', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 41 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 42 | 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 Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 48 | let path = PathKit.FromCmds(cmds); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 49 | |
| 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 55 | })); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 56 | }); |
| 57 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 58 | it('can create an SVG string from hex values', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 59 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 60 | 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 Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 66 | let path = PathKit.FromCmds(cmds); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 67 | |
| 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 72 | })); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 73 | }); |
| 74 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 75 | it('should have input and the output be the same', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 76 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 77 | 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 Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 90 | })); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 91 | }); |
| 92 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 93 | it('approximates arcs (conics) with quads', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 94 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 95 | let path = PathKit.NewPath(); |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame] | 96 | path.moveTo(50, 120); |
| 97 | path.arc(50, 120, 45, 0, 1.75 * Math.PI); |
| 98 | path.lineTo(50, 120); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 99 | 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 Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame] | 105 | |
| 106 | reportSVGString(svgStr, 'conics_quads_approx').then(() => { |
| 107 | done(); |
| 108 | }).catch(reportError(done)); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 109 | })); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 110 | }); |
| 111 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 112 | }); |