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