1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "config.h"
6#include "core/dom/DOMMatrix.h"
7
8namespace blink {
9
10DOMMatrix* DOMMatrix::create()
11{
12    return new DOMMatrix(TransformationMatrix());
13}
14
15DOMMatrix* DOMMatrix::create(DOMMatrixReadOnly* other)
16{
17    return new DOMMatrix(other->matrix(), other->is2D());
18}
19
20DOMMatrix::DOMMatrix(const TransformationMatrix& matrix, bool is2D)
21{
22    m_matrix = matrix;
23    m_is2D = is2D;
24}
25
26void DOMMatrix::setIs2D(bool value)
27{
28    if (m_is2D)
29        m_is2D = value;
30}
31
32DOMMatrix* DOMMatrix::multiplySelf(DOMMatrix* other)
33{
34    if (!other->is2D())
35        m_is2D = false;
36
37    m_matrix = m_matrix * other->matrix();
38
39    return this;
40}
41
42DOMMatrix* DOMMatrix::preMultiplySelf(DOMMatrix* other)
43{
44    if (!other->is2D())
45        m_is2D = false;
46
47    m_matrix = other->matrix() * m_matrix;
48
49    return this;
50}
51
52DOMMatrix* DOMMatrix::translateSelf(double tx, double ty, double tz)
53{
54    if (!tx && !ty && !tz)
55        return this;
56
57    if (tz)
58        m_is2D = false;
59
60    if (m_is2D)
61        m_matrix.translate(tx, ty);
62    else
63        m_matrix.translate3d(tx, ty, tz);
64
65    return this;
66}
67
68DOMMatrix* DOMMatrix::scaleSelf(double scale, double ox, double oy)
69{
70    return scaleNonUniformSelf(scale, scale, 1, ox, oy);
71}
72
73DOMMatrix* DOMMatrix::scale3dSelf(double scale, double ox, double oy, double oz)
74{
75    return scaleNonUniformSelf(scale, scale, scale, ox, oy, oz);
76}
77
78DOMMatrix* DOMMatrix::scaleNonUniformSelf(double sx, double sy, double sz,
79    double ox, double oy, double oz)
80{
81    if (sz != 1 || oz)
82        m_is2D = false;
83
84    if (sx == 1 && sy == 1 && sz == 1)
85        return this;
86
87    bool hasTranslation = (ox || oy || oz);
88
89    if (hasTranslation)
90        translateSelf(ox, oy, oz);
91
92    if (m_is2D)
93        m_matrix.scaleNonUniform(sx, sy);
94    else
95        m_matrix.scale3d(sx, sy, sz);
96
97    if (hasTranslation)
98        translateSelf(-ox, -oy, -oz);
99
100    return this;
101}
102
103} // namespace blink
104