1 // Copyright 2016 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 "third_party/blink/renderer/core/css/cssom/css_matrix_component.h"
6
7 #include "third_party/blink/renderer/bindings/core/v8/v8_css_matrix_component_options.h"
8 #include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
9 #include "third_party/blink/renderer/core/css/css_primitive_value.h"
10 #include "third_party/blink/renderer/core/geometry/dom_matrix.h"
11 #include "third_party/blink/renderer/platform/wtf/math_extras.h"
12
13 namespace blink {
14
15 namespace {
16
To2DMatrix(DOMMatrixReadOnly * matrix)17 DOMMatrix* To2DMatrix(DOMMatrixReadOnly* matrix) {
18 DOMMatrix* twoDimensionalMatrix = DOMMatrix::Create();
19 twoDimensionalMatrix->setA(matrix->m11());
20 twoDimensionalMatrix->setB(matrix->m12());
21 twoDimensionalMatrix->setC(matrix->m21());
22 twoDimensionalMatrix->setD(matrix->m22());
23 twoDimensionalMatrix->setE(matrix->m41());
24 twoDimensionalMatrix->setF(matrix->m42());
25 return twoDimensionalMatrix;
26 }
27
28 } // namespace
29
Create(DOMMatrixReadOnly * matrix,const CSSMatrixComponentOptions * options)30 CSSMatrixComponent* CSSMatrixComponent::Create(
31 DOMMatrixReadOnly* matrix,
32 const CSSMatrixComponentOptions* options) {
33 return MakeGarbageCollected<CSSMatrixComponent>(
34 matrix, options->hasIs2D() ? options->is2D() : matrix->is2D());
35 }
36
toMatrix(ExceptionState &) const37 DOMMatrix* CSSMatrixComponent::toMatrix(ExceptionState&) const {
38 if (is2D() && !matrix_->is2D())
39 return To2DMatrix(matrix_);
40 return DOMMatrix::Create(matrix_.Get());
41 }
42
FromCSSValue(const CSSFunctionValue & value)43 CSSMatrixComponent* CSSMatrixComponent::FromCSSValue(
44 const CSSFunctionValue& value) {
45 WTF::Vector<double> entries;
46 for (const auto& item : value)
47 entries.push_back(To<CSSPrimitiveValue>(*item).GetDoubleValue());
48
49 return CSSMatrixComponent::Create(
50 DOMMatrixReadOnly::CreateForSerialization(entries.data(), entries.size()),
51 CSSMatrixComponentOptions::Create());
52 }
53
ToCSSValue() const54 const CSSFunctionValue* CSSMatrixComponent::ToCSSValue() const {
55 CSSFunctionValue* result = MakeGarbageCollected<CSSFunctionValue>(
56 is2D() ? CSSValueID::kMatrix : CSSValueID::kMatrix3d);
57
58 if (is2D()) {
59 double values[6] = {matrix_->a(), matrix_->b(), matrix_->c(),
60 matrix_->d(), matrix_->e(), matrix_->f()};
61 for (double value : values) {
62 result->Append(*CSSNumericLiteralValue::Create(
63 value, CSSPrimitiveValue::UnitType::kNumber));
64 }
65 } else {
66 double values[16] = {
67 matrix_->m11(), matrix_->m12(), matrix_->m13(), matrix_->m14(),
68 matrix_->m21(), matrix_->m22(), matrix_->m23(), matrix_->m24(),
69 matrix_->m31(), matrix_->m32(), matrix_->m33(), matrix_->m34(),
70 matrix_->m41(), matrix_->m42(), matrix_->m43(), matrix_->m44()};
71 for (double value : values) {
72 result->Append(*CSSNumericLiteralValue::Create(
73 value, CSSPrimitiveValue::UnitType::kNumber));
74 }
75 }
76
77 return result;
78 }
79
80 } // namespace blink
81