Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 1 | describe('PathKit\'s SVG Behavior', function() { |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 2 | it('can create a path from an SVG string', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 3 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 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(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 21 | })); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 22 | }); |
| 23 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 24 | it('can create an SVG string from a path', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 25 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 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]]; |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 32 | let path = PathKit.FromCmds(cmds); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 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(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 39 | })); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 40 | }); |
| 41 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 42 | it('can create an SVG string from hex values', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 43 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 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]]; |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 50 | let path = PathKit.FromCmds(cmds); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 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(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 56 | })); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 57 | }); |
| 58 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 59 | it('should have input and the output be the same', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 60 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 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(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 74 | })); |
Kevin Lubick | 92c9171 | 2018-08-09 10:00:02 -0400 | [diff] [blame] | 75 | }); |
| 76 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 77 | it('approximates arcs (conics) with quads', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 78 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 79 | let path = PathKit.NewPath(); |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame] | 80 | path.moveTo(50, 120); |
| 81 | path.arc(50, 120, 45, 0, 1.75 * Math.PI); |
| 82 | path.lineTo(50, 120); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 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(); |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame] | 89 | |
| 90 | reportSVGString(svgStr, 'conics_quads_approx').then(() => { |
| 91 | done(); |
| 92 | }).catch(reportError(done)); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 93 | })); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 94 | }); |
| 95 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 96 | }); |