-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path$$.MatrixArray.jsxlib
More file actions
298 lines (249 loc) · 8.35 KB
/
$$.MatrixArray.jsxlib
File metadata and controls
298 lines (249 loc) · 8.35 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*******************************************************************************
Name: MatrixArray
Desc: Transformation matrices throughout arrays, outside the DOM.
Path: /etc/$$.MatrixArray.jsxlib
Require: ---
Encoding: ÛȚF8
Core: NO
Kind: Module
API: =install()
DOM-access: NO
Todo: Notice, `rev` flag in `_MUL`
Created: 180626 (YYMMDD)
Modified: 180626 (YYMMDD)
*******************************************************************************/
;$$.hasOwnProperty('MatrixArray') || eval(__(MODULE, $$, 'MatrixArray', 180626, 'install'))
[PRIVATE]
({
BKTS: false,
DC2S: function(/*-1|0|+1*/detail,/*digits=2*/dec,/*?func*/F, alpha,theta)
//----------------------------------
// (Decomp-To-String.) `this` :: {sx,sy,cos,sin,tan,tx,ty}
// detail == -1 --> "{ sx:<num>, sy:<num>, cos:<num>, sin:<num>, tan:<num>, tx:<num>, ty:<num> }"
// detail == 0 --> "[ <sx> 0 0 <sy> ]*[ 1 0 <-tan> 1 ]*[ <cos> <-sin> <sin> <cos> ]*[ 1 0 0 1 <tx> <ty> ]"
// detail == 1 --> "SCALE(<sx>, <sy>) * SHEAR(<alphaDeg>) * ROT(<thetaDeg>) * TRANS(<tx>, <ty>)
// => str
{
detail||(detail=0);
'undefined' == typeof dec && (dec=2);
'function' != typeof F && (F=Number.prototype.toDecimal||Number.prototype.toFixed);
switch( detail )
{
case -1:
return __("{ sx:%1, sy:%2, cos:%3, sin:%4, tan:%5, tx:%6, ty:%7 }"
,F.call(this.sx,dec), F.call(this.sy,dec)
,F.call(this.cos,dec), F.call(this.sin,dec), F.call(this.tan,dec)
,F.call(this.tx,dec), F.call(this.ty,dec)
);
case 1:
alpha = (180*Math.atan(this.tan))/Math.PI;
theta = (180*Math.acos(this.cos))/Math.PI; // 0..180
0 > this.sin && (theta=-theta);
return __("SCALE(%1, %2) * SHEAR(%3\xB0) * ROT(%4\xB0) * TRANS(%5, %6)"
,F.call(this.sx,dec), F.call(this.sy,dec)
,F.call(alpha,dec)
,F.call(theta,dec)
,F.call(this.tx,dec), F.call(this.ty,dec)
);
default:
return __("[ %1 0 0 %2 ]*[ 1 0 %3 1 ]*[ %4 %5 %6 %4 ]*[ 1 0 0 1 %7 %8 ]"
,F.call(this.sx,dec), F.call(this.sy,dec)
,F.call(-this.tan,dec)
,F.call(this.cos,dec) ,F.call(-this.sin,dec), F.call(this.sin,dec)
,F.call(this.tx,dec), F.call(this.ty,dec)
);
}
},
SHRT: function(/*num[4|6]*/M, q,A,B,C,D,k,c,s,t)
//----------------------------------
// (SHRT-Decomposition.) Return a volatile structure.
// { sx:num, sy:num, cos:[-1..+1], sin:[-1..+1], tan:num, tx:num, ty:num }
// so that
// M = [sx 0 0 sy 0,0] × [1 0 -tan 1 0,0] × [cos -sin sin cos 0,0] × [1 0 0 1 tx,ty]
{
q = callee.Q||(callee.Q={
'sx':1, 'sy':1,
'cos':1, 'sin':0, 'tan':0,
'tx':0, 'ty':0,
toString: callee.µ['~'].DC2S,
});
// Translation elements.
// ---
q.tx = M[4]||0;
q.ty = M[5]||0;
// Rotation: -180 < θ <= +180 CCW
// Shear: -90 < α < +90 CW
// ---
A = M[0]; // Sx * cosθ ; Sx > 0
B = M[1]; // Sx * -sinθ ; Sx > 0
C = M[2]; // Sy * (sinθ - cosθ*tanα)
D = M[3]; // Sy * (cosθ - sinθ*tanα)
// In InDesign the horizontal scaling factor is
// always made positive (sx > 0)
// ---
q.sx = k = Math.sqrt(A*A+B*B);
q.cos = c = A/k; // -1 <= c <= 1
q.sin = s = -B/k; // -1 <= s <= 1
// tanα = (D*sinθ - C*cosθ) / (D*cosθ + C*sinθ )
// ---
q.tan = t = (D*s-C*c)/(D*c+C*s);
// Sy = C/(sinθ - cosθ*tanα) = D/(cosθ + sinθ*tanα)
// ---
q.sy = (k=s-c*t) ? (C/k) : (D/(c+s*t));
return q;
},
})
//==========================================================================
// ARRAY EXTENSION
//==========================================================================
[PRIVATE]
({
_TOS: function toString(/*digits=2*/dec,/*str=', '|(-1|0|1)*/flag, s,F)
//----------------------------------
// (To-String.) `this` :: Array of four or six numbers.
// `dec` :: decimal digits.
// `flag` :: string or undefined -> separator (default: ", ".)
// -1 | 0 | 1 - -> decomposition flag (cf. ~.DC2S.)
// -> Array.prototype.toString
// ---
// => str
{
// Checkpoint. If `this` doesn't look like a matrix,
// use the native toString method.
// ---
if( this.length < 4 || 6 < this.length || 'number'!=typeof this[0] )
{
return callee.µ['~'].BKTS.call(this);
}
('undefined' == typeof dec) && (dec=2);
F = callee.Q || (callee.Q=Number.prototype.toDecimal||Number.prototype.toFixed);
// Decomposition flag.
if( 'number' == typeof flag )
{
return callee.µ['~'].SHRT(this).toString(flag,dec,F);
}
flag || (flag=', ');
s = [
F.call(this[0],dec), F.call(this[1],dec),
F.call(this[2],dec), F.call(this[3],dec)
]
.join(flag);
4 < this.length && ( s += flag + F.call(this[4],dec) + flag + F.call(this[5],dec) );
return '[ ' + s + ' ]';
},
_EQL: function isEqual(/*num[4|6]*/M, d)
//----------------------------------
// (Is-Equal.) `this` :: Array of four or six numbers.
// Precision is determined by callee.DIGITS (default:4.)
// -> Array.prototype['==']
// ---
// => bool
{
d = callee.DIGITS;
return this.length==M.length && this.toString(d,'\x01')==M.toString(d,'\x01');
}
.setup({ DIGITS:4 }),
_MUL: function multiply(/*num[4|6]*/M,/*bool*/rev, T,a,b,c,d,e,f)
//----------------------------------
// (Multiply.) `this` :: Array of four or six numbers.
// -> Array.prototype['*']
// [TODO] `rev` flag.
// ---
// => new num[4|6]
{
rev ? (T=M, M=this) : (T=this);
a = T[0]*M[0] + T[1]*M[2]; // aa' + bc'
b = T[0]*M[1] + T[1]*M[3]; // ab' + bd'
c = T[2]*M[0] + T[3]*M[2]; // ca' + dc'
d = T[2]*M[1] + T[3]*M[3]; // cb' + dd'
if( 4 >= T.length && 4 >= M.length )
{
return [a,b,c,d];
}
// ea' + fc' + e'
// ---
e = (T[4]||0)*M[0] + (T[5]||0)*M[2] + (M[4]||0);
// eb' + fd' + f'
// ---
f = (T[4]||0)*M[1] + (T[5]||0)*M[3] + (M[5]||0);
return [a,b,c,d,e,f];
},
_INV: function invert( div,a,b,c,d,e,f)
//----------------------------------
// (Invert.) `this` :: Array of four or six numbers.
// -> Array.prototype['~']
// ---
// Test: mx being a TransformationMatrix,
// mx.invertMatrix().matrixValues == ~(mx.matrixValues)
// ---
// => new num[4|6]
{
div = this[0]*this[3] - this[1]*this[2];
a = this[3] / div;
b = -this[1] / div;
c = -this[2] / div;
d = this[0] / div;
if( 4 >= this.length )
{
return [a,b,c,d];
}
// (c*f - d*e ) / div
// ---
e = (this[2]*this[5] - this[3]*this[4]) / div;
// (b*e - a*f ) / div
// ---
f = (this[1]*this[4] - this[0]*this[5]) / div;
return [a,b,c,d,e,f];
},
})
//==========================================================================
// API
//==========================================================================
[PUBLIC]
({
install: function install_( I,o)
//----------------------------------
// Install the custom Array.prototype implemented in this module
// (toString, operators, etc.)
// [REM] This function requires to be *explicitly* invoked by the
// client code before using MatrixArray features; it is not called
// through the load process to avoid overloading Array prototype
// while not needed yet.
{
I = callee.µ['~'];
o = Array.prototype;
// Backup the built-in toString method
// before loading our custom stuff.
// ---
I.BKTS = o.toString;
o.toString = I._TOS;
// Operators.
// ---
o['=='] = I._EQL;
o['*'] = I._MUL;
o['~'] = I._INV;
},
uninstall: function uninstall_( I,o)
//----------------------------------
// Uninstall the custom Array.prototype implemented in this module
// (toString, operators, etc.)
// [REM] This function should be invoked by the client code as
// soon as it no longer uses MatrixArray features, in order to
// go back to the regular Array behavior. Note that the `unload`
// process automatically calls uninstall (see onUnload.)
{
I = callee.µ['~'];
o = Array.prototype;
delete o['=='];
delete o['*'];
delete o['~'];
if( 'function' != typeof(I.BKTS) ) return;
o.toString = I.BKTS;
I.BKTS = false;
},
onUnload: function onUnload_()
//----------------------------------
{
callee.µ.uninstall();
},
})