1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.dom;
16
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20
21 import org.htmlunit.corejs.javascript.Context;
22 import org.htmlunit.corejs.javascript.Function;
23 import org.htmlunit.corejs.javascript.FunctionObject;
24 import org.htmlunit.corejs.javascript.Scriptable;
25 import org.htmlunit.corejs.javascript.ScriptableObject;
26 import org.htmlunit.corejs.javascript.SymbolKey;
27 import org.htmlunit.corejs.javascript.VarScope;
28 import org.htmlunit.corejs.javascript.typedarrays.NativeFloat32Array;
29 import org.htmlunit.corejs.javascript.typedarrays.NativeFloat64Array;
30 import org.htmlunit.corejs.javascript.typedarrays.NativeTypedArrayView;
31 import org.htmlunit.javascript.HtmlUnitScriptable;
32 import org.htmlunit.javascript.JavaScriptEngine;
33 import org.htmlunit.javascript.configuration.JsxClass;
34 import org.htmlunit.javascript.configuration.JsxConstructor;
35 import org.htmlunit.javascript.configuration.JsxFunction;
36 import org.htmlunit.javascript.configuration.JsxGetter;
37 import org.htmlunit.javascript.configuration.JsxStaticFunction;
38 import org.htmlunit.javascript.host.Window;
39
40
41
42
43
44
45
46
47
48 @JsxClass
49 public class DOMMatrixReadOnly extends HtmlUnitScriptable {
50
51
52
53 private double m11_;
54 private double m12_;
55 private double m13_;
56 private double m14_;
57
58 private double m21_;
59 private double m22_;
60 private double m23_;
61 private double m24_;
62
63 private double m31_;
64 private double m32_;
65 private double m33_;
66 private double m34_;
67
68 private double m41_;
69 private double m42_;
70 private double m43_;
71 private double m44_;
72
73 private boolean is2D_;
74
75
76
77
78 public DOMMatrixReadOnly() {
79 m11_ = 1;
80 m12_ = 0;
81 m13_ = 0;
82 m14_ = 0;
83
84 m21_ = 0;
85 m22_ = 1;
86 m23_ = 0;
87 m24_ = 0;
88
89 m31_ = 0;
90 m32_ = 0;
91 m33_ = 1;
92 m34_ = 0;
93
94 m41_ = 0;
95 m42_ = 0;
96 m43_ = 0;
97 m44_ = 1;
98
99 is2D_ = true;
100 }
101
102
103
104
105
106
107
108
109
110
111 @JsxConstructor
112 public static DOMMatrixReadOnly jsConstructor(final Context cx, final VarScope scope,
113 final Object[] args, final Function ctorObj, final boolean inNewExpr) {
114
115 final DOMMatrixReadOnly matrix = new DOMMatrixReadOnly();
116 matrix.init(args, scope, ctorObj);
117 return matrix;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 protected void init(final Object[] args, final VarScope scope, final Function ctorObj) {
135 setParentScope(scope);
136 setPrototype(((FunctionObject) ctorObj).getClassPrototype());
137
138 if (args.length == 0 || JavaScriptEngine.isUndefined(args[0])) {
139 return;
140 }
141
142 final Object init = args[0];
143 if (init instanceof Scriptable initScriptable
144 && ScriptableObject.hasProperty(initScriptable, SymbolKey.ITERATOR)) {
145 final List<Double> values = new ArrayList<>();
146 JavaScriptEngine.iterate(Context.getCurrentContext(), scope, initScriptable,
147 value -> values.add(JavaScriptEngine.toNumber(value)));
148
149 if (values.size() == 6) {
150 setValues2D(values.get(0), values.get(1), values.get(2),
151 values.get(3), values.get(4), values.get(5));
152 return;
153 }
154 if (values.size() == 16) {
155 final double[] matrix = new double[16];
156 for (int i = 0; i < matrix.length; i++) {
157 matrix[i] = values.get(i);
158 }
159 setValues(matrix, false);
160 return;
161 }
162
163 throw JavaScriptEngine.typeError(getClassName() + " constructor: Matrix init sequence must have "
164 + "a length of 6 or 16 (actual value: " + values.size() + ")");
165 }
166
167
168 if (!(getWindowOrWorkerGlobalScope(this) instanceof Window)) {
169 throw JavaScriptEngine.typeError(getClassName()
170 + " constructor: a transform list can only be parsed in a window context");
171 }
172 setTransformList(JavaScriptEngine.toString(init));
173 }
174
175
176
177
178
179
180
181 protected void setTransformList(final String transformList) {
182 try {
183 DOMMatrixTransformListParser.parse(transformList, this);
184 }
185 catch (final IllegalArgumentException e) {
186 throw JavaScriptEngine.asJavaScriptException(this,
187 "Failed to parse '" + transformList + "': " + e.getMessage(),
188 DOMException.SYNTAX_ERR);
189 }
190 }
191
192
193
194
195
196 @JsxGetter
197 public double getM11() {
198 return m11_;
199 }
200
201
202
203
204
205 public void setM11(final double m11) {
206 m11_ = m11;
207 }
208
209
210
211
212
213 @JsxGetter
214 public double getA() {
215 return m11_;
216 }
217
218
219
220
221
222 @JsxGetter
223 public double getM12() {
224 return m12_;
225 }
226
227
228
229
230
231 public void setM12(final double m12) {
232 m12_ = m12;
233 }
234
235
236
237
238
239 @JsxGetter
240 public double getB() {
241 return m12_;
242 }
243
244
245
246
247
248 @JsxGetter
249 public double getM13() {
250 return m13_;
251 }
252
253
254
255
256
257 public void setM13(final double m13) {
258 m13_ = m13;
259 }
260
261
262
263
264
265 @JsxGetter
266 public double getM14() {
267 return m14_;
268 }
269
270
271
272
273
274 public void setM14(final double m14) {
275 m14_ = m14;
276 }
277
278
279
280
281
282 @JsxGetter
283 public double getM21() {
284 return m21_;
285 }
286
287
288
289
290
291 public void setM21(final double m21) {
292 m21_ = m21;
293 }
294
295
296
297
298
299 @JsxGetter
300 public double getC() {
301 return m21_;
302 }
303
304
305
306
307
308 @JsxGetter
309 public double getM22() {
310 return m22_;
311 }
312
313
314
315
316
317 public void setM22(final double m22) {
318 m22_ = m22;
319 }
320
321
322
323
324
325 @JsxGetter
326 public double getD() {
327 return m22_;
328 }
329
330
331
332
333
334 @JsxGetter
335 public double getM23() {
336 return m23_;
337 }
338
339
340
341
342
343 public void setM23(final double m23) {
344 m23_ = m23;
345 }
346
347
348
349
350
351 @JsxGetter
352 public double getM24() {
353 return m24_;
354 }
355
356
357
358
359
360 public void setM24(final double m24) {
361 m24_ = m24;
362 }
363
364
365
366
367
368 @JsxGetter
369 public double getM31() {
370 return m31_;
371 }
372
373
374
375
376
377 public void setM31(final double m31) {
378 m31_ = m31;
379 }
380
381
382
383
384
385 @JsxGetter
386 public double getM32() {
387 return m32_;
388 }
389
390
391
392
393
394 public void setM32(final double m32) {
395 m32_ = m32;
396 }
397
398
399
400
401
402 @JsxGetter
403 public double getM33() {
404 return m33_;
405 }
406
407
408
409
410
411 public void setM33(final double m33) {
412 m33_ = m33;
413 }
414
415
416
417
418
419 @JsxGetter
420 public double getM34() {
421 return m34_;
422 }
423
424
425
426
427
428 public void setM34(final double m34) {
429 m34_ = m34;
430 }
431
432
433
434
435
436 @JsxGetter
437 public double getM41() {
438 return m41_;
439 }
440
441
442
443
444
445 public void setM41(final double m41) {
446 m41_ = m41;
447 }
448
449
450
451
452
453 @JsxGetter
454 public double getE() {
455 return m41_;
456 }
457
458
459
460
461
462 @JsxGetter
463 public double getM42() {
464 return m42_;
465 }
466
467
468
469
470
471 public void setM42(final double m42) {
472 m42_ = m42;
473 }
474
475
476
477
478
479 @JsxGetter
480 public double getF() {
481 return m42_;
482 }
483
484
485
486
487
488 @JsxGetter
489 public double getM43() {
490 return m43_;
491 }
492
493
494
495
496
497 public void setM43(final double m43) {
498 m43_ = m43;
499 }
500
501
502
503
504
505 @JsxGetter
506 public double getM44() {
507 return m44_;
508 }
509
510
511
512
513
514 public void setM44(final double m44) {
515 m44_ = m44;
516 }
517
518
519
520
521
522 @JsxGetter
523 public boolean isIs2D() {
524 return is2D_;
525 }
526
527
528
529
530
531 public void setIs2D(final boolean is2D) {
532 is2D_ = is2D;
533 }
534
535
536
537
538
539
540
541 @JsxGetter
542 public boolean getIsIdentity() {
543 return m11_ == 1 && m22_ == 1 && m33_ == 1 && m44_ == 1
544 && m12_ == 0 && m13_ == 0 && m14_ == 0
545 && m21_ == 0 && m23_ == 0 && m24_ == 0
546 && m31_ == 0 && m32_ == 0 && m34_ == 0
547 && m41_ == 0 && m42_ == 0 && m43_ == 0;
548 }
549
550
551
552
553
554
555
556 @JsxFunction
557 public DOMMatrix flipX() {
558 final double[] values = getValues();
559 for (int i = 0; i < 4; i++) {
560 values[i] = -values[i];
561 }
562
563 final DOMMatrix matrix = copyToDOMMatrix();
564 matrix.setValues(values, is2D_);
565 return matrix;
566 }
567
568
569
570
571
572
573
574 @JsxFunction
575 public DOMMatrix flipY() {
576 final double[] values = getValues();
577 for (int i = 4; i < 8; i++) {
578 values[i] = -values[i];
579 }
580
581 final DOMMatrix matrix = copyToDOMMatrix();
582 matrix.setValues(values, is2D_);
583 return matrix;
584 }
585
586
587
588
589
590
591
592 @JsxFunction
593 public DOMMatrix inverse() {
594 final DOMMatrix matrix = copyToDOMMatrix();
595 matrix.invertSelfInternal();
596 return matrix;
597 }
598
599
600
601
602
603
604
605
606
607 @JsxFunction
608 public DOMMatrix multiply(final Object other) {
609 final DOMMatrixReadOnly otherMatrix = fromMatrixInitForMultiply(other, "multiply", getClassName());
610
611 final DOMMatrix matrix = copyToDOMMatrix();
612 matrix.multiplySelfInternal(otherMatrix);
613 return matrix;
614 }
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629 @JsxFunction
630 public DOMMatrix rotate(final Object rotX, final Object rotY, final Object rotZ) {
631 final DOMMatrix matrix = copyToDOMMatrix();
632 matrix.rotateSelfInternal(rotX, rotY, rotZ);
633 return matrix;
634 }
635
636
637
638
639
640
641
642
643
644 @JsxFunction
645 public DOMMatrix rotateFromVector(final Object x, final Object y) {
646 final DOMMatrix matrix = copyToDOMMatrix();
647 matrix.rotateFromVectorSelfInternal(toDouble(x, 0), toDouble(y, 0));
648 return matrix;
649 }
650
651
652
653
654
655
656
657
658
659
660
661 @JsxFunction
662 public DOMMatrix rotateAxisAngle(final Object x, final Object y, final Object z, final Object angle) {
663 final DOMMatrix matrix = copyToDOMMatrix();
664 matrix.rotateAxisAngleSelfInternal(toDouble(x, 0), toDouble(y, 0), toDouble(z, 0), toDouble(angle, 0));
665 return matrix;
666 }
667
668
669
670
671
672
673
674
675
676
677
678
679
680 @JsxFunction
681 public DOMMatrix scale(final Object scaleX, final Object scaleY, final Object scaleZ,
682 final Object originX, final Object originY, final Object originZ) {
683 final DOMMatrix matrix = copyToDOMMatrix();
684 final double sx = toDouble(scaleX, 1);
685 matrix.scaleSelfInternal(sx, toDouble(scaleY, sx), toDouble(scaleZ, 1),
686 toDouble(originX, 0), toDouble(originY, 0), toDouble(originZ, 0));
687 return matrix;
688 }
689
690
691
692
693
694
695
696
697
698
699
700
701 @JsxFunction
702 public DOMMatrix scale3d(final Object scale, final Object originX, final Object originY, final Object originZ) {
703 final DOMMatrix matrix = copyToDOMMatrix();
704 matrix.scale3dSelfInternal(toDouble(scale, 1),
705 toDouble(originX, 0), toDouble(originY, 0), toDouble(originZ, 0));
706 return matrix;
707 }
708
709
710
711
712
713
714
715
716
717
718 @JsxFunction
719 public DOMMatrix scaleNonUniform(final Object scaleX, final Object scaleY) {
720 final DOMMatrix matrix = copyToDOMMatrix();
721 matrix.scaleSelfInternal(toDouble(scaleX, 1), toDouble(scaleY, 1), 1, 0, 0, 0);
722 return matrix;
723 }
724
725
726
727
728
729
730
731 @JsxFunction
732 public DOMMatrix skewX(final Object sx) {
733 final DOMMatrix matrix = copyToDOMMatrix();
734 matrix.skewXSelfInternal(toDouble(sx, 0));
735 return matrix;
736 }
737
738
739
740
741
742
743
744 @JsxFunction
745 public DOMMatrix skewY(final Object sy) {
746 final DOMMatrix matrix = copyToDOMMatrix();
747 matrix.skewYSelfInternal(toDouble(sy, 0));
748 return matrix;
749 }
750
751
752
753
754
755
756
757
758
759
760
761 @JsxFunction
762 public DOMMatrix translate(final Object tx, final Object ty, final Object tz) {
763 final DOMMatrix matrix = copyToDOMMatrix();
764 matrix.translateSelfInternal(toDouble(tx, 0), toDouble(ty, 0), toDouble(tz, 0));
765 return matrix;
766 }
767
768
769
770
771
772
773
774
775
776 @JsxFunction
777 public DOMPoint transformPoint(final Object point) {
778 double x = 0;
779 double y = 0;
780 double z = 0;
781 double w = 1;
782 if (point != null && !JavaScriptEngine.isUndefined(point)) {
783 if (!(point instanceof Scriptable pointScriptable)) {
784 throw JavaScriptEngine.typeError("Failed to execute 'transformPoint' on '" + getClassName()
785 + "': The provided value is not of type 'DOMPointInit'.");
786 }
787 x = getDictionaryValue(pointScriptable, "x", 0);
788 y = getDictionaryValue(pointScriptable, "y", 0);
789 z = getDictionaryValue(pointScriptable, "z", 0);
790 w = getDictionaryValue(pointScriptable, "w", 1);
791 }
792
793 final DOMPoint result = new DOMPoint(
794 mul(m11_, x) + mul(m21_, y) + mul(m31_, z) + mul(m41_, w),
795 mul(m12_, x) + mul(m22_, y) + mul(m32_, z) + mul(m42_, w),
796 mul(m13_, x) + mul(m23_, y) + mul(m33_, z) + mul(m43_, w),
797 mul(m14_, x) + mul(m24_, y) + mul(m34_, z) + mul(m44_, w));
798 result.setParentScope(getParentScope());
799 result.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), "DOMPoint"));
800 return result;
801 }
802
803
804
805
806
807
808
809
810
811
812
813 protected void multiplySelfInternal(final DOMMatrixReadOnly other) {
814 final boolean is2D = is2D_ && other.is2D_;
815 setValues(multiplyMatrices(getValues(), other.getValues(), is2D), is2D);
816 }
817
818
819
820
821
822
823
824 protected void preMultiplySelfInternal(final DOMMatrixReadOnly other) {
825 final boolean is2D = is2D_ && other.is2D_;
826 setValues(multiplyMatrices(other.getValues(), getValues(), is2D), is2D);
827 }
828
829
830
831
832
833
834
835
836 protected void translateSelfInternal(final double tx, final double ty, final double tz) {
837 final DOMMatrixReadOnly translation = new DOMMatrixReadOnly();
838 translation.m41_ = tx;
839 translation.m42_ = ty;
840 translation.m43_ = tz;
841 translation.is2D_ = tz == 0;
842
843 multiplySelfInternal(translation);
844 }
845
846
847
848
849
850
851
852
853
854
855
856 protected void scaleSelfInternal(final double scaleX, final double scaleY, final double scaleZ,
857 final double originX, final double originY, final double originZ) {
858 translateSelfInternal(originX, originY, originZ);
859
860 final DOMMatrixReadOnly scale = new DOMMatrixReadOnly();
861 scale.m11_ = scaleX;
862 scale.m22_ = scaleY;
863 scale.m33_ = scaleZ;
864 scale.is2D_ = scaleZ == 1;
865 multiplySelfInternal(scale);
866
867 translateSelfInternal(-originX, -originY, -originZ);
868
869 if (scaleZ != 1 || originZ != 0) {
870 is2D_ = false;
871 }
872 }
873
874
875
876
877
878
879
880
881
882 protected void scale3dSelfInternal(final double scale,
883 final double originX, final double originY, final double originZ) {
884 scaleSelfInternal(scale, scale, scale, originX, originY, originZ);
885 if (scale != 1) {
886 is2D_ = false;
887 }
888 }
889
890
891
892
893
894
895
896
897
898 protected void rotateSelfInternal(final Object rotXObj, final Object rotYObj, final Object rotZObj) {
899 double rotX = toDouble(rotXObj, 0);
900 double rotY = toDouble(rotYObj, 0);
901 double rotZ = toDouble(rotZObj, 0);
902
903 if (isMissing(rotYObj) && isMissing(rotZObj)) {
904 rotZ = rotX;
905 rotX = 0;
906 rotY = 0;
907 }
908
909 rotateSelfInternal(rotX, rotY, rotZ);
910 }
911
912
913
914
915
916
917
918
919
920 protected void rotateSelfInternal(final double rotX, final double rotY, final double rotZ) {
921 final boolean is2D = is2D_ && rotX == 0 && rotY == 0;
922
923 multiplySelfInternal(createRotation(0, 0, 1, rotZ));
924 if (rotY != 0) {
925 multiplySelfInternal(createRotation(0, 1, 0, rotY));
926 }
927 if (rotX != 0) {
928 multiplySelfInternal(createRotation(1, 0, 0, rotX));
929 }
930 is2D_ = is2D;
931 }
932
933
934
935
936
937
938
939 protected void rotateFromVectorSelfInternal(final double x, final double y) {
940 double angle = 0;
941 if (x != 0 || y != 0) {
942 angle = Math.toDegrees(Math.atan2(y, x));
943 }
944 multiplySelfInternal(createRotation(0, 0, 1, angle));
945 }
946
947
948
949
950
951
952
953
954
955
956 protected void rotateAxisAngleSelfInternal(final double x, final double y, final double z, final double angle) {
957 final boolean is2D = is2D_ && x == 0 && y == 0;
958 if (x != 0 || y != 0 || z != 0) {
959 multiplySelfInternal(createRotation(x, y, z, angle));
960 }
961 is2D_ = is2D;
962 }
963
964
965
966
967
968
969 protected void skewXSelfInternal(final double sx) {
970 final DOMMatrixReadOnly skew = new DOMMatrixReadOnly();
971 skew.m21_ = Math.tan(Math.toRadians(sx));
972 multiplySelfInternal(skew);
973 }
974
975
976
977
978
979
980 protected void skewYSelfInternal(final double sy) {
981 final DOMMatrixReadOnly skew = new DOMMatrixReadOnly();
982 skew.m12_ = Math.tan(Math.toRadians(sy));
983 multiplySelfInternal(skew);
984 }
985
986
987
988
989
990 protected void invertSelfInternal() {
991 if (is2D_) {
992 final double det = m11_ * m22_ - m12_ * m21_;
993 if (det == 0) {
994 setAllNaN();
995 return;
996 }
997
998 final double a = m22_ / det;
999 final double b = -m12_ / det;
1000 final double c = -m21_ / det;
1001 final double d = m11_ / det;
1002 final double e = (m21_ * m42_ - m22_ * m41_) / det;
1003 final double f = (m12_ * m41_ - m11_ * m42_) / det;
1004 setValues2D(a, b, c, d, e, f);
1005 return;
1006 }
1007
1008 final double[] m = getValues();
1009 final double[] inv = new double[16];
1010 inv[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15]
1011 + m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10];
1012 inv[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15]
1013 - m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10];
1014 inv[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15]
1015 + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9];
1016 inv[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14]
1017 - m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9];
1018 inv[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15]
1019 - m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10];
1020 inv[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15]
1021 + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10];
1022 inv[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15]
1023 - m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9];
1024 inv[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14]
1025 + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9];
1026 inv[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15]
1027 + m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6];
1028 inv[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15]
1029 - m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6];
1030 inv[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15]
1031 + m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5];
1032 inv[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14]
1033 - m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5];
1034 inv[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11]
1035 - m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6];
1036 inv[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11]
1037 + m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6];
1038 inv[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11]
1039 - m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5];
1040 inv[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10]
1041 + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5];
1042
1043 final double det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
1044 if (det == 0) {
1045 setAllNaN();
1046 return;
1047 }
1048
1049 final double invDet = 1.0 / det;
1050 for (int i = 0; i < inv.length; i++) {
1051 inv[i] = inv[i] * invDet;
1052 }
1053 setValues(inv, false);
1054 }
1055
1056 private void setAllNaN() {
1057 final double[] values = new double[16];
1058 Arrays.fill(values, Double.NaN);
1059 setValues(values, false);
1060 }
1061
1062
1063
1064
1065
1066
1067
1068
1069 protected DOMMatrix copyToDOMMatrix() {
1070 final DOMMatrix matrix = new DOMMatrix();
1071 matrix.setParentScope(getParentScope());
1072 matrix.setPrototype(ScriptableObject.getClassPrototype(getParentScope(), "DOMMatrix"));
1073 matrix.setValues(getValues(), is2D_);
1074 return matrix;
1075 }
1076
1077
1078
1079
1080
1081
1082 protected double[] getValues() {
1083 return new double[] {
1084 m11_, m12_, m13_, m14_,
1085 m21_, m22_, m23_, m24_,
1086 m31_, m32_, m33_, m34_,
1087 m41_, m42_, m43_, m44_
1088 };
1089 }
1090
1091
1092
1093
1094
1095
1096 protected void setValues(final double[] values, final boolean is2D) {
1097 m11_ = values[0];
1098 m12_ = values[1];
1099 m13_ = values[2];
1100 m14_ = values[3];
1101
1102 m21_ = values[4];
1103 m22_ = values[5];
1104 m23_ = values[6];
1105 m24_ = values[7];
1106
1107 m31_ = values[8];
1108 m32_ = values[9];
1109 m33_ = values[10];
1110 m34_ = values[11];
1111
1112 m41_ = values[12];
1113 m42_ = values[13];
1114 m43_ = values[14];
1115 m44_ = values[15];
1116
1117 is2D_ = is2D;
1118 }
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131 protected void setValues2D(final double a, final double b, final double c,
1132 final double d, final double e, final double f) {
1133 setValues(new double[] {a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, e, f, 0, 1}, true);
1134 }
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150 private static double[] multiplyMatrices(final double[] left, final double[] right, final boolean only2D) {
1151 if (only2D) {
1152 final double[] result = left.clone();
1153 for (final int col : new int[] {0, 1, 3}) {
1154 for (int row = 0; row < 2; row++) {
1155 double sum = term(left[row], right[col * 4]) + term(left[4 + row], right[col * 4 + 1]);
1156 if (col == 3) {
1157 sum += left[12 + row];
1158 }
1159 result[col * 4 + row] = sum;
1160 }
1161 }
1162 return result;
1163 }
1164
1165 final double[] result = new double[16];
1166 for (int col = 0; col < 4; col++) {
1167 for (int row = 0; row < 4; row++) {
1168 double sum = 0;
1169 for (int k = 0; k < 4; k++) {
1170 sum += term(left[k * 4 + row], right[col * 4 + k]);
1171 }
1172 result[col * 4 + row] = sum;
1173 }
1174 }
1175 return result;
1176 }
1177
1178 private static double term(final double left, final double right) {
1179 if (right == 0) {
1180 return 0;
1181 }
1182 return left * right;
1183 }
1184
1185 private static double mul(final double x, final double y) {
1186 if (x == 0 || y == 0) {
1187 return 0;
1188 }
1189 return x * y;
1190 }
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203 static DOMMatrixReadOnly createRotation(final double x, final double y, final double z, final double angle) {
1204 final DOMMatrixReadOnly rotation = new DOMMatrixReadOnly();
1205
1206 if (x == 0 && y == 0) {
1207
1208 final double[] sinCos = sinCosDegrees(z < 0 ? -angle : angle);
1209 rotation.m11_ = sinCos[1];
1210 rotation.m12_ = sinCos[0];
1211 rotation.m21_ = -sinCos[0];
1212 rotation.m22_ = sinCos[1];
1213 return rotation;
1214 }
1215
1216 if (y == 0 && z == 0) {
1217
1218 final double[] sinCos = sinCosDegrees(x < 0 ? -angle : angle);
1219 rotation.m22_ = sinCos[1];
1220 rotation.m23_ = sinCos[0];
1221 rotation.m32_ = -sinCos[0];
1222 rotation.m33_ = sinCos[1];
1223 rotation.is2D_ = false;
1224 return rotation;
1225 }
1226
1227 if (x == 0 && z == 0) {
1228
1229 final double[] sinCos = sinCosDegrees(y < 0 ? -angle : angle);
1230 rotation.m11_ = sinCos[1];
1231 rotation.m13_ = -sinCos[0];
1232 rotation.m31_ = sinCos[0];
1233 rotation.m33_ = sinCos[1];
1234 rotation.is2D_ = false;
1235 return rotation;
1236 }
1237
1238 final double length = Math.sqrt(x * x + y * y + z * z);
1239 final double nx = x / length;
1240 final double ny = y / length;
1241 final double nz = z / length;
1242
1243 final double halfAngle = Math.toRadians(angle) / 2;
1244 final double sc = Math.sin(halfAngle) * Math.cos(halfAngle);
1245 final double sq = Math.sin(halfAngle) * Math.sin(halfAngle);
1246
1247 rotation.m11_ = 1 - 2 * mul(ny * ny + nz * nz, sq);
1248 rotation.m12_ = 2 * (mul(nx * ny, sq) + mul(nz, sc));
1249 rotation.m13_ = 2 * (mul(nx * nz, sq) - mul(ny, sc));
1250
1251 rotation.m21_ = 2 * (mul(nx * ny, sq) - mul(nz, sc));
1252 rotation.m22_ = 1 - 2 * mul(nx * nx + nz * nz, sq);
1253 rotation.m23_ = 2 * (mul(ny * nz, sq) + mul(nx, sc));
1254
1255 rotation.m31_ = 2 * (mul(nx * nz, sq) + mul(ny, sc));
1256 rotation.m32_ = 2 * (mul(ny * nz, sq) - mul(nx, sc));
1257 rotation.m33_ = 1 - 2 * mul(nx * nx + ny * ny, sq);
1258
1259 rotation.is2D_ = false;
1260 return rotation;
1261 }
1262
1263
1264
1265
1266
1267
1268
1269
1270 private static double[] sinCosDegrees(final double angle) {
1271 if (Double.isFinite(angle) && angle % 90 == 0) {
1272 final int quarter = (int) (((angle / 90) % 4 + 4) % 4);
1273 return switch (quarter) {
1274 case 1 -> new double[] {1, 0};
1275 case 2 -> new double[] {0, -1};
1276 case 3 -> new double[] {-1, 0};
1277 default -> new double[] {0, 1};
1278 };
1279 }
1280
1281 final double radians = Math.toRadians(angle);
1282 return new double[] {Math.sin(radians), Math.cos(radians)};
1283 }
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293 protected static double toDouble(final Object value, final double defaultValue) {
1294 if (isMissing(value)) {
1295 return defaultValue;
1296 }
1297 return JavaScriptEngine.toNumber(value);
1298 }
1299
1300 private static boolean isMissing(final Object value) {
1301 return value == null || JavaScriptEngine.isUndefined(value);
1302 }
1303
1304 private static double getDictionaryValue(final Scriptable dictionary, final String name,
1305 final double defaultValue) {
1306 final Object value = ScriptableObject.getProperty(dictionary, name);
1307 if (value == Scriptable.NOT_FOUND || JavaScriptEngine.isUndefined(value)) {
1308 return defaultValue;
1309 }
1310 return JavaScriptEngine.toNumber(value);
1311 }
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323 static DOMMatrixReadOnly fromMatrixInit(final Object init, final String methodName, final String className) {
1324 final DOMMatrixReadOnly result = new DOMMatrixReadOnly();
1325 if (isMissing(init)) {
1326 return result;
1327 }
1328
1329 if (init instanceof DOMMatrixReadOnly matrix) {
1330 result.setValues(matrix.getValues(), matrix.is2D_);
1331 return result;
1332 }
1333
1334 final String errorPrefix = "Failed to execute '" + methodName + "' on '" + className + "': ";
1335 if (!(init instanceof Scriptable dict)) {
1336 throw JavaScriptEngine.typeError(errorPrefix + "The provided value is not of type 'DOMMatrixInit'.");
1337 }
1338
1339
1340 final double m11 = fixup(dict, "a", "m11", 1, errorPrefix);
1341 final double m12 = fixup(dict, "b", "m12", 0, errorPrefix);
1342 final double m21 = fixup(dict, "c", "m21", 0, errorPrefix);
1343 final double m22 = fixup(dict, "d", "m22", 1, errorPrefix);
1344 final double m41 = fixup(dict, "e", "m41", 0, errorPrefix);
1345 final double m42 = fixup(dict, "f", "m42", 0, errorPrefix);
1346
1347
1348 final double m13 = getDictionaryValue(dict, "m13", 0);
1349 final double m14 = getDictionaryValue(dict, "m14", 0);
1350 final double m23 = getDictionaryValue(dict, "m23", 0);
1351 final double m24 = getDictionaryValue(dict, "m24", 0);
1352 final double m31 = getDictionaryValue(dict, "m31", 0);
1353 final double m32 = getDictionaryValue(dict, "m32", 0);
1354 final double m33 = getDictionaryValue(dict, "m33", 1);
1355 final double m34 = getDictionaryValue(dict, "m34", 0);
1356 final double m43 = getDictionaryValue(dict, "m43", 0);
1357 final double m44 = getDictionaryValue(dict, "m44", 1);
1358
1359 final boolean only2DValues = m13 == 0 && m14 == 0 && m23 == 0 && m24 == 0
1360 && m31 == 0 && m32 == 0 && m34 == 0 && m43 == 0
1361 && m33 == 1 && m44 == 1;
1362
1363 final Object is2DValue = ScriptableObject.getProperty(dict, "is2D");
1364 final boolean is2D;
1365 if (is2DValue == Scriptable.NOT_FOUND || JavaScriptEngine.isUndefined(is2DValue)) {
1366 is2D = only2DValues;
1367 }
1368 else {
1369 is2D = JavaScriptEngine.toBoolean(is2DValue);
1370 if (is2D && !only2DValues) {
1371 throw JavaScriptEngine.typeError(errorPrefix
1372 + "The 'is2D' property is true, but the matrix contains 3D values.");
1373 }
1374 }
1375
1376 result.setValues(new double[] {
1377 m11, m12, m13, m14,
1378 m21, m22, m23, m24,
1379 m31, m32, m33, m34,
1380 m41, m42, m43, m44}, is2D);
1381 return result;
1382 }
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394 static DOMMatrixReadOnly fromMatrixInitForMultiply(final Object init, final String methodName,
1395 final String className) {
1396 final DOMMatrixReadOnly result = fromMatrixInit(init, methodName, className);
1397 if (!(init instanceof DOMMatrixReadOnly)) {
1398 result.is2D_ = result.hasOnly2DValues();
1399 }
1400 return result;
1401 }
1402
1403
1404
1405
1406
1407
1408 boolean hasOnly2DValues() {
1409 return m13_ == 0 && m14_ == 0 && m23_ == 0 && m24_ == 0
1410 && m31_ == 0 && m32_ == 0 && m34_ == 0 && m43_ == 0
1411 && m33_ == 1 && m44_ == 1;
1412 }
1413
1414 private static double fixup(final Scriptable dict, final String shortName, final String longName,
1415 final double defaultValue, final String errorPrefix) {
1416 final Object shortValue = ScriptableObject.getProperty(dict, shortName);
1417 final Object longValue = ScriptableObject.getProperty(dict, longName);
1418
1419 final boolean hasShort = shortValue != Scriptable.NOT_FOUND && !JavaScriptEngine.isUndefined(shortValue);
1420 final boolean hasLong = longValue != Scriptable.NOT_FOUND && !JavaScriptEngine.isUndefined(longValue);
1421
1422 if (hasLong) {
1423 final double value = JavaScriptEngine.toNumber(longValue);
1424 if (hasShort) {
1425 final double shortNumber = JavaScriptEngine.toNumber(shortValue);
1426
1427 final boolean same = value == shortNumber || (Double.isNaN(value) && Double.isNaN(shortNumber));
1428 if (!same) {
1429 throw JavaScriptEngine.typeError(errorPrefix + "The '" + shortName + "' property should equal "
1430 + "the '" + longName + "' property.");
1431 }
1432 }
1433 return value;
1434 }
1435 if (hasShort) {
1436 return JavaScriptEngine.toNumber(shortValue);
1437 }
1438 return defaultValue;
1439 }
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449 @JsxFunction
1450 public NativeFloat32Array toFloat32Array() {
1451 final NativeFloat32Array result =
1452 (NativeFloat32Array) JavaScriptEngine.newObject(getParentScope(), "Float32Array", new Object[] {16});
1453
1454 result.setArrayElement(0, m11_);
1455 result.setArrayElement(1, m12_);
1456 result.setArrayElement(2, m13_);
1457 result.setArrayElement(3, m14_);
1458
1459 result.setArrayElement(4, m21_);
1460 result.setArrayElement(5, m22_);
1461 result.setArrayElement(6, m23_);
1462 result.setArrayElement(7, m24_);
1463
1464 result.setArrayElement(8, m31_);
1465 result.setArrayElement(9, m32_);
1466 result.setArrayElement(10, m33_);
1467 result.setArrayElement(11, m34_);
1468
1469 result.setArrayElement(12, m41_);
1470 result.setArrayElement(13, m42_);
1471 result.setArrayElement(14, m43_);
1472 result.setArrayElement(15, m44_);
1473
1474 return result;
1475 }
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485 @JsxFunction
1486 public NativeFloat64Array toFloat64Array() {
1487 final NativeFloat64Array result =
1488 (NativeFloat64Array) JavaScriptEngine.newObject(getParentScope(), "Float64Array", new Object[] {16});
1489
1490 result.setArrayElement(0, m11_);
1491 result.setArrayElement(1, m12_);
1492 result.setArrayElement(2, m13_);
1493 result.setArrayElement(3, m14_);
1494
1495 result.setArrayElement(4, m21_);
1496 result.setArrayElement(5, m22_);
1497 result.setArrayElement(6, m23_);
1498 result.setArrayElement(7, m24_);
1499
1500 result.setArrayElement(8, m31_);
1501 result.setArrayElement(9, m32_);
1502 result.setArrayElement(10, m33_);
1503 result.setArrayElement(11, m34_);
1504
1505 result.setArrayElement(12, m41_);
1506 result.setArrayElement(13, m42_);
1507 result.setArrayElement(14, m43_);
1508 result.setArrayElement(15, m44_);
1509
1510 return result;
1511 }
1512
1513
1514
1515
1516
1517
1518 @JsxFunction(functionName = "toString")
1519 public String js_toString() {
1520 final StringBuilder result = new StringBuilder();
1521
1522 result.append(is2D_ ? "matrix(" : "matrix3d(");
1523 appendDouble(result, m11_).append(", ");
1524 appendDouble(result, m12_).append(", ");
1525 if (!is2D_) {
1526 appendDouble(result, m13_).append(", ");
1527 appendDouble(result, m14_).append(", ");
1528 }
1529
1530 appendDouble(result, m21_).append(", ");
1531 appendDouble(result, m22_).append(", ");
1532 if (!is2D_) {
1533 appendDouble(result, m23_).append(", ");
1534 appendDouble(result, m24_).append(", ");
1535
1536 appendDouble(result, m31_).append(", ");
1537 appendDouble(result, m32_).append(", ");
1538 appendDouble(result, m33_).append(", ");
1539 appendDouble(result, m34_).append(", ");
1540 }
1541
1542 appendDouble(result, m41_).append(", ");
1543 appendDouble(result, m42_);
1544 if (!is2D_) {
1545 result.append(", ");
1546 appendDouble(result, m43_).append(", ");
1547 appendDouble(result, m44_);
1548 }
1549
1550 result.append(')');
1551
1552 return result.toString();
1553 }
1554
1555
1556
1557
1558
1559
1560 @Override
1561 public Object getDefaultValue(final Class<?> hint) {
1562 if (String.class.equals(hint) || hint == null) {
1563 return js_toString();
1564 }
1565 return super.getDefaultValue(hint);
1566 }
1567
1568 private static StringBuilder appendDouble(final StringBuilder builder, final double d) {
1569 if (Double.isNaN(d) || Double.isInfinite(d)) {
1570 return builder.append(d);
1571 }
1572
1573 if (d % 1 == 0) {
1574 return builder.append((int) d);
1575 }
1576
1577 return builder.append(d);
1578 }
1579
1580
1581
1582
1583
1584 @JsxFunction
1585 public Scriptable toJSON() {
1586 final Scriptable json = JavaScriptEngine.newObject(getParentScope());
1587 json.put("a", json, m11_);
1588 json.put("b", json, m12_);
1589 json.put("c", json, m21_);
1590 json.put("d", json, m22_);
1591 json.put("e", json, m41_);
1592 json.put("f", json, m42_);
1593
1594 json.put("m11", json, m11_);
1595 json.put("m12", json, m12_);
1596 json.put("m13", json, m13_);
1597 json.put("m14", json, m14_);
1598
1599 json.put("m21", json, m21_);
1600 json.put("m22", json, m22_);
1601 json.put("m23", json, m23_);
1602 json.put("m24", json, m24_);
1603
1604 json.put("m31", json, m31_);
1605 json.put("m32", json, m32_);
1606 json.put("m33", json, m33_);
1607 json.put("m34", json, m34_);
1608
1609 json.put("m41", json, m41_);
1610 json.put("m42", json, m42_);
1611 json.put("m43", json, m43_);
1612 json.put("m44", json, m44_);
1613
1614 json.put("is2D", json, is2D_);
1615 json.put("isIdentity", json, getIsIdentity());
1616
1617 return json;
1618 }
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631 @JsxStaticFunction(functionName = "fromMatrix")
1632 public static DOMMatrixReadOnly fromMatrixReadOnly(final Context cx, final VarScope scope,
1633 final Scriptable thisObj, final Object[] args, final Function funObj) {
1634 final DOMMatrixReadOnly init = fromMatrixInit(args.length > 0 ? args[0] : null,
1635 "fromMatrix", "DOMMatrixReadOnly");
1636 return newInstance(new DOMMatrixReadOnly(), scope, "DOMMatrixReadOnly", init);
1637 }
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650 @JsxStaticFunction(functionName = "fromFloat32Array")
1651 public static DOMMatrixReadOnly fromFloat32ArrayReadOnly(final Context cx, final VarScope scope,
1652 final Scriptable thisObj, final Object[] args, final Function funObj) {
1653 final DOMMatrixReadOnly init = fromTypedArray(args, NativeFloat32Array.class,
1654 "fromFloat32Array", "DOMMatrixReadOnly");
1655 return newInstance(new DOMMatrixReadOnly(), scope, "DOMMatrixReadOnly", init);
1656 }
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669 @JsxStaticFunction(functionName = "fromFloat64Array")
1670 public static DOMMatrixReadOnly fromFloat64ArrayReadOnly(final Context cx, final VarScope scope,
1671 final Scriptable thisObj, final Object[] args, final Function funObj) {
1672 final DOMMatrixReadOnly init = fromTypedArray(args, NativeFloat64Array.class,
1673 "fromFloat64Array", "DOMMatrixReadOnly");
1674 return newInstance(new DOMMatrixReadOnly(), scope, "DOMMatrixReadOnly", init);
1675 }
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687 protected static <T extends DOMMatrixReadOnly> T newInstance(final T matrix, final VarScope scope,
1688 final String className, final DOMMatrixReadOnly values) {
1689 matrix.setParentScope(scope);
1690 matrix.setPrototype(ScriptableObject.getClassPrototype(scope, className));
1691 matrix.setValues(values.getValues(), values.is2D_);
1692 return matrix;
1693 }
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705 protected static DOMMatrixReadOnly fromTypedArray(final Object[] args,
1706 final Class<? extends NativeTypedArrayView<?>> type,
1707 final String methodName, final String className) {
1708 final String errorPrefix = "Failed to execute '" + methodName + "' on '" + className + "': ";
1709 if (args.length == 0 || !type.isInstance(args[0])) {
1710 throw JavaScriptEngine.typeError(errorPrefix + "parameter 1 is not of type '"
1711 + (type == NativeFloat32Array.class ? "Float32Array" : "Float64Array") + "'.");
1712 }
1713
1714 final NativeTypedArrayView<?> array = (NativeTypedArrayView<?>) args[0];
1715 final int length = array.getArrayLength();
1716 final double[] values = new double[length];
1717 for (int i = 0; i < length; i++) {
1718 values[i] = JavaScriptEngine.toNumber(array.getArrayElement(i));
1719 }
1720
1721 final DOMMatrixReadOnly result = new DOMMatrixReadOnly();
1722 if (length == 6) {
1723 result.setValues2D(values[0], values[1], values[2], values[3], values[4], values[5]);
1724 return result;
1725 }
1726 if (length == 16) {
1727 result.setValues(values, false);
1728 return result;
1729 }
1730
1731 throw JavaScriptEngine.typeError(errorPrefix
1732 + "The sequence must contain 6 elements for a 2D matrix or 16 elements for a 3D matrix.");
1733 }
1734 }