mh-two-thousand-and-two
2024-03-25 b8c93990f3fa5e50a8aca16bdc9c2758168aa0fd
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
import PathProxy from '../core/PathProxy';
import {applyTransform as v2ApplyTransform, VectorArray} from '../core/vector';
import { MatrixArray } from '../core/matrix';
 
const CMD = PathProxy.CMD;
 
const points: VectorArray[] = [[], [], []];
const mathSqrt = Math.sqrt;
const mathAtan2 = Math.atan2;
 
export default function transformPath(path: PathProxy, m: MatrixArray) {
    if (!m) {
        return;
    }
 
    let data = path.data;
    const len = path.len();
    let cmd;
    let nPoint: number;
    let i: number;
    let j: number;
    let k: number;
    let p: VectorArray;
 
    const M = CMD.M;
    const C = CMD.C;
    const L = CMD.L;
    const R = CMD.R;
    const A = CMD.A;
    const Q = CMD.Q;
 
    for (i = 0, j = 0; i < len;) {
        cmd = data[i++];
        j = i;
        nPoint = 0;
 
        switch (cmd) {
            case M:
                nPoint = 1;
                break;
            case L:
                nPoint = 1;
                break;
            case C:
                nPoint = 3;
                break;
            case Q:
                nPoint = 2;
                break;
            case A:
                const x = m[4];
                const y = m[5];
                const sx = mathSqrt(m[0] * m[0] + m[1] * m[1]);
                const sy = mathSqrt(m[2] * m[2] + m[3] * m[3]);
                const angle = mathAtan2(-m[1] / sy, m[0] / sx);
                // cx
                data[i] *= sx;
                data[i++] += x;
                // cy
                data[i] *= sy;
                data[i++] += y;
                // Scale rx and ry
                // FIXME Assume psi is 0 here
                data[i++] *= sx;
                data[i++] *= sy;
 
                // Start angle
                data[i++] += angle;
                // end angle
                data[i++] += angle;
                // FIXME psi
                i += 2;
                j = i;
                break;
            case R:
                // x0, y0
                p[0] = data[i++];
                p[1] = data[i++];
                v2ApplyTransform(p, p, m);
                data[j++] = p[0];
                data[j++] = p[1];
                // x1, y1
                p[0] += data[i++];
                p[1] += data[i++];
                v2ApplyTransform(p, p, m);
                data[j++] = p[0];
                data[j++] = p[1];
        }
 
        for (k = 0; k < nPoint; k++) {
            let p = points[k];
            p[0] = data[i++];
            p[1] = data[i++];
 
            v2ApplyTransform(p, p, m);
            // Write back
            data[j++] = p[0];
            data[j++] = p[1];
        }
    }
 
    path.increaseVersion();
}