Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 1 | describe('PathKit\'s Path Behavior', function() { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 2 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 3 | describe('Basic Path Features', function() { |
| 4 | function drawSimplePath() { |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 5 | let path = PathKit.NewPath(); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 6 | path.moveTo(0, 0); |
| 7 | path.lineTo(10, 0); |
| 8 | path.lineTo(10, 10); |
| 9 | path.close(); |
| 10 | return path; |
| 11 | } |
| 12 | |
| 13 | it('supports.equals()', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 14 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 15 | let path = drawSimplePath(); |
| 16 | let otherPath = drawSimplePath(); |
| 17 | let blank = PathKit.NewPath(); |
| 18 | |
| 19 | expect(path.equals(path)).toBe(true); |
| 20 | expect(otherPath.equals(path)).toBe(true); |
| 21 | expect(path.equals(otherPath)).toBe(true); |
| 22 | |
| 23 | expect(path.equals(blank)).toBe(false); |
| 24 | expect(otherPath.equals(blank)).toBe(false); |
| 25 | expect(blank.equals(path)).toBe(false); |
| 26 | expect(blank.equals(otherPath)).toBe(false); |
| 27 | |
| 28 | path.delete(); |
| 29 | otherPath.delete(); |
| 30 | blank.delete(); |
| 31 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 32 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 33 | }); |
| 34 | |
| 35 | it('has a copy constructor', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 36 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 37 | let orig = drawSimplePath(); |
| 38 | let copy = new PathKit.SkPath(orig); |
| 39 | |
| 40 | expect(orig.toSVGString()).toEqual(copy.toSVGString()); |
| 41 | expect(orig.equals(copy)).toBe(true); |
| 42 | |
| 43 | orig.delete(); |
| 44 | copy.delete(); |
| 45 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 46 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 47 | }); |
| 48 | |
| 49 | it('has a copy method', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 50 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 51 | let orig = drawSimplePath(); |
| 52 | let copy = orig.copy(); |
| 53 | |
| 54 | expect(orig.toSVGString()).toEqual(copy.toSVGString()); |
| 55 | expect(orig.equals(copy)).toBe(true); |
| 56 | |
| 57 | orig.delete(); |
| 58 | copy.delete(); |
| 59 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 60 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 61 | }); |
| 62 | |
| 63 | it('can create a copy with MakePath', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 64 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 65 | let orig = drawSimplePath(); |
| 66 | let copy = PathKit.NewPath(orig); |
| 67 | |
| 68 | expect(orig.toSVGString()).toEqual(copy.toSVGString()); |
| 69 | expect(orig.equals(copy)).toBe(true); |
| 70 | |
| 71 | orig.delete(); |
| 72 | copy.delete(); |
| 73 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 74 | })); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 75 | }); |
| 76 | }); |
| 77 | |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 78 | function ExpectRectsToBeEqual(actual, expected) { |
| 79 | if (PathKit.usingWasm) { |
| 80 | // exact match |
| 81 | expect(actual).toEqual(expected); |
| 82 | } else { |
| 83 | // floats get rounded a little bit |
| 84 | expect(actual.fLeft).toBeCloseTo(expected.fLeft, 4); |
| 85 | expect(actual.fTop).toBeCloseTo(expected.fTop, 4); |
| 86 | expect(actual.fRight).toBeCloseTo(expected.fRight, 4); |
| 87 | expect(actual.fBottom).toBeCloseTo(expected.fBottom, 4); |
| 88 | } |
| 89 | } |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 90 | |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 91 | function bits2float(str) { |
| 92 | return PathKit.SkBits2FloatUnsigned(parseInt(str)) |
| 93 | } |
| 94 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 95 | describe('bounds and rect', function(){ |
| 96 | it('dynamically updates getBounds()', function(done){ |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 97 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 98 | // Based on test_bounds_crbug_513799 |
| 99 | let path = PathKit.NewPath(); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 100 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(0, 0, 0, 0)); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 101 | path.moveTo(-5, -8); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 102 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, -5, -8)); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 103 | path.rect(1, 2, 2, 2); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 104 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, 3, 4)); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 105 | path.moveTo(1, 2); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 106 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(-5, -8, 3, 4)); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 107 | path.delete(); |
| 108 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 109 | })); |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 110 | }); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 111 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 112 | it('has getBounds() and computeTightBounds()', function(done){ |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 113 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 114 | // Based on PathOpsTightBoundsIllBehaved |
| 115 | let path = PathKit.NewPath(); |
| 116 | path.moveTo(1, 1); |
| 117 | path.quadraticCurveTo(4, 3, 2, 2); |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 118 | expect(path.getBounds()).toEqual(PathKit.LTRBRect(1, 1, 4, 3)); |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 119 | ExpectRectsToBeEqual(path.computeTightBounds(), |
Kevin Lubick | d993648 | 2018-08-24 10:44:16 -0400 | [diff] [blame] | 120 | PathKit.LTRBRect(1, 1, |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 121 | bits2float("0x40333334"), // 2.8 |
| 122 | bits2float("0x40155556"))); // 2.3333333 |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 123 | path.delete(); |
| 124 | |
| 125 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 126 | })); |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 127 | }); |
| 128 | }); |
| 129 | |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 130 | function ExpectCmdsToBeEqual(actual, expected) { |
| 131 | if (PathKit.usingWasm) { |
| 132 | // exact match |
| 133 | expect(actual).toEqual(expected); |
| 134 | } else { |
| 135 | // lossy match |
| 136 | actual.every((cmd, cmdIdx) => { |
| 137 | cmd.every((arg, argIdx) => { |
| 138 | // The asm.js code is close to the wasm/c++ output, but not quite. |
| 139 | expect(arg).toBeCloseTo(expected[cmdIdx][argIdx], 4) |
| 140 | }); |
| 141 | }); |
| 142 | } |
| 143 | } |
| 144 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 145 | describe('Command arrays', function(){ |
Kevin Lubick | da3d8ac | 2019-01-07 11:08:55 -0500 | [diff] [blame] | 146 | it('does NOT approximates conics when dumping as toCmds', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 147 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 148 | let path = PathKit.NewPath(); |
| 149 | path.moveTo(20, 120); |
| 150 | path.arc(20, 120, 18, 0, 1.75 * Math.PI); |
| 151 | path.lineTo(20, 120); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 152 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 153 | let expectedCmds = [ |
| 154 | [PathKit.MOVE_VERB, 20, 120], |
| 155 | [PathKit.LINE_VERB, 38, 120], |
| 156 | [PathKit.CONIC_VERB, 38, 138, 20, 138, bits2float("0x3f3504f3)")], // 0.707107f |
| 157 | [PathKit.CONIC_VERB, 2, 138, 2, 120, bits2float("0x3f3504f3)")], // 0.707107f |
| 158 | [PathKit.CONIC_VERB, 2, 102, 20, 102, bits2float("0x3f3504f3)")], // 0.707107f |
| 159 | [PathKit.CONIC_VERB, bits2float("0x41dba58e"), 102, bits2float("0x4202e962"), bits2float("0x42d68b4d"), bits2float("0x3f6c8361")], // 27.4558, 102, 32.7279, 107.272, 0.92388 |
| 160 | [PathKit.LINE_VERB, 20, 120], |
| 161 | ]; |
Kevin Lubick | f14a3c0 | 2018-08-22 09:35:32 -0400 | [diff] [blame] | 162 | ExpectCmdsToBeEqual(path.toCmds(), expectedCmds); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 163 | |
Kevin Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 164 | path.delete(); |
| 165 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 166 | })); |
Kevin Lubick | 97d6d98 | 2018-08-10 15:53:16 -0400 | [diff] [blame] | 167 | }); |
| 168 | }); |
| 169 | |
Kevin Lubick | 644d8e7 | 2018-08-09 13:58:04 -0400 | [diff] [blame] | 170 | }); |