Fix some longstanding irks in CFX_Matrix
- Initialize in header to identity matrix.
- Add standard operator overloads.
- Use default methods where possible.
This, in turn, identified an unused variable now flagged by the
compiler which was removed from cpdf_array.cpp
Change-Id: I712cdcb1dd11a5a93314d54c836027b2c7ad472e
Reviewed-on: https://pdfium-review.googlesource.com/c/47450
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_array.cpp b/core/fpdfapi/parser/cpdf_array.cpp
index 78a52f9..d3f7213 100644
--- a/core/fpdfapi/parser/cpdf_array.cpp
+++ b/core/fpdfapi/parser/cpdf_array.cpp
@@ -80,7 +80,6 @@
}
CFX_Matrix CPDF_Array::GetMatrix() const {
- CFX_Matrix matrix;
if (!IsArray() || m_Objects.size() != 6)
return CFX_Matrix();
diff --git a/core/fxcrt/fx_coordinates.cpp b/core/fxcrt/fx_coordinates.cpp
index fbacb5d..2771992 100644
--- a/core/fxcrt/fx_coordinates.cpp
+++ b/core/fxcrt/fx_coordinates.cpp
@@ -51,15 +51,6 @@
"FX_RECT vs. RECT mismatch");
#endif
-inline CFX_Matrix ConcatInternal(const CFX_Matrix& left,
- const CFX_Matrix& right) {
- return CFX_Matrix(
- left.a * right.a + left.b * right.c, left.a * right.b + left.b * right.d,
- left.c * right.a + left.d * right.c, left.c * right.b + left.d * right.d,
- left.e * right.a + left.f * right.c + right.e,
- left.e * right.b + left.f * right.d + right.f);
-}
-
} // namespace
void FX_RECT::Normalize() {
@@ -322,11 +313,11 @@
}
void CFX_Matrix::Concat(const CFX_Matrix& m) {
- *this = ConcatInternal(*this, m);
+ *this *= m;
}
void CFX_Matrix::ConcatPrepend(const CFX_Matrix& m) {
- *this = ConcatInternal(m, *this);
+ *this = m * (*this);
}
void CFX_Matrix::ConcatInverse(const CFX_Matrix& src) {
diff --git a/core/fxcrt/fx_coordinates.h b/core/fxcrt/fx_coordinates.h
index 67d3736..16facf3 100644
--- a/core/fxcrt/fx_coordinates.h
+++ b/core/fxcrt/fx_coordinates.h
@@ -521,35 +521,39 @@
//
class CFX_Matrix {
public:
- CFX_Matrix() { SetIdentity(); }
+ CFX_Matrix() = default;
explicit CFX_Matrix(const float n[6])
: a(n[0]), b(n[1]), c(n[2]), d(n[3]), e(n[4]), f(n[5]) {}
- CFX_Matrix(const CFX_Matrix& other) = default;
-
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
: a(a1), b(b1), c(c1), d(d1), e(e1), f(f1) {}
- void operator=(const CFX_Matrix& other) {
- a = other.a;
- b = other.b;
- c = other.c;
- d = other.d;
- e = other.e;
- f = other.f;
- }
+ CFX_Matrix(const CFX_Matrix& other) = default;
std::tuple<float, float, float, float, float, float> AsTuple() const;
- void SetIdentity() {
- a = 1;
- b = 0;
- c = 0;
- d = 1;
- e = 0;
- f = 0;
+ CFX_Matrix& operator=(const CFX_Matrix& other) = default;
+
+ bool operator==(const CFX_Matrix& other) const {
+ return a == other.a && b == other.b && c == other.c && d == other.d &&
+ e == other.e && f == other.f;
}
+ bool operator!=(const CFX_Matrix& other) const { return !(*this == other); }
+
+ CFX_Matrix operator*(const CFX_Matrix& right) const {
+ return CFX_Matrix(a * right.a + b * right.c, a * right.b + b * right.d,
+ c * right.a + d * right.c, c * right.b + d * right.d,
+ e * right.a + f * right.c + right.e,
+ e * right.b + f * right.d + right.f);
+ }
+ CFX_Matrix& operator*=(const CFX_Matrix& other) {
+ *this = *this * other;
+ return *this;
+ }
+
+ void SetIdentity() { *this = CFX_Matrix(); }
+ bool IsIdentity() const { return *this == CFX_Matrix(); }
CFX_Matrix GetInverse() const;
@@ -558,10 +562,6 @@
void ConcatInverse(const CFX_Matrix& m);
void ConcatInversePrepend(const CFX_Matrix& m);
- bool IsIdentity() const {
- return a == 1 && b == 0 && c == 0 && d == 1 && e == 0 && f == 0;
- }
-
bool Is90Rotated() const;
bool IsScaled() const;
bool WillScale() const { return a != 1.0f || b != 0 || c != 0 || d != 1.0f; }
@@ -593,12 +593,12 @@
CFX_RectF TransformRect(const CFX_RectF& rect) const;
CFX_FloatRect TransformRect(const CFX_FloatRect& rect) const;
- float a;
- float b;
- float c;
- float d;
- float e;
- float f;
+ float a = 1.0f;
+ float b = 0.0f;
+ float c = 0.0f;
+ float d = 1.0f;
+ float e = 0.0f;
+ float f = 0.0f;
};
#endif // CORE_FXCRT_FX_COORDINATES_H_