/src/qpdf/libqpdf/QPDFMatrix.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <qpdf/QPDFMatrix.hh> |
2 | | |
3 | | #include <qpdf/QUtil.hh> |
4 | | #include <algorithm> |
5 | | |
6 | | QPDFMatrix::QPDFMatrix() : |
7 | 56.6k | a(1.0), |
8 | 56.6k | b(0.0), |
9 | 56.6k | c(0.0), |
10 | 56.6k | d(1.0), |
11 | 56.6k | e(0.0), |
12 | 56.6k | f(0.0) |
13 | 56.6k | { |
14 | 56.6k | } |
15 | | |
16 | | QPDFMatrix::QPDFMatrix(double a, double b, double c, double d, double e, double f) : |
17 | 83.4k | a(a), |
18 | 83.4k | b(b), |
19 | 83.4k | c(c), |
20 | 83.4k | d(d), |
21 | 83.4k | e(e), |
22 | 83.4k | f(f) |
23 | 83.4k | { |
24 | 83.4k | } |
25 | | |
26 | | QPDFMatrix::QPDFMatrix(QPDFObjectHandle::Matrix const& m) : |
27 | 376 | a(m.a), |
28 | 376 | b(m.b), |
29 | 376 | c(m.c), |
30 | 376 | d(m.d), |
31 | 376 | e(m.e), |
32 | 376 | f(m.f) |
33 | 376 | { |
34 | 376 | } |
35 | | |
36 | | static double |
37 | | fix_rounding(double d) |
38 | 166k | { |
39 | 166k | if ((d > -0.00001) && (d < 0.00001)) { |
40 | 59.8k | d = 0.0; |
41 | 59.8k | } |
42 | 166k | return d; |
43 | 166k | } |
44 | | |
45 | | std::string |
46 | | QPDFMatrix::unparse() const |
47 | 27.8k | { |
48 | 27.8k | return ( |
49 | 27.8k | QUtil::double_to_string(fix_rounding(a), 5) + " " + |
50 | 27.8k | QUtil::double_to_string(fix_rounding(b), 5) + " " + |
51 | 27.8k | QUtil::double_to_string(fix_rounding(c), 5) + " " + |
52 | 27.8k | QUtil::double_to_string(fix_rounding(d), 5) + " " + |
53 | 27.8k | QUtil::double_to_string(fix_rounding(e), 5) + " " + |
54 | 27.8k | QUtil::double_to_string(fix_rounding(f), 5)); |
55 | 27.8k | } |
56 | | |
57 | | QPDFObjectHandle::Matrix |
58 | | QPDFMatrix::getAsMatrix() const |
59 | 0 | { |
60 | 0 | return {a, b, c, d, e, f}; |
61 | 0 | } |
62 | | |
63 | | void |
64 | | QPDFMatrix::concat(QPDFMatrix const& other) |
65 | 83.5k | { |
66 | 83.5k | double ap = (this->a * other.a) + (this->c * other.b); |
67 | 83.5k | double bp = (this->b * other.a) + (this->d * other.b); |
68 | 83.5k | double cp = (this->a * other.c) + (this->c * other.d); |
69 | 83.5k | double dp = (this->b * other.c) + (this->d * other.d); |
70 | 83.5k | double ep = (this->a * other.e) + (this->c * other.f) + this->e; |
71 | 83.5k | double fp = (this->b * other.e) + (this->d * other.f) + this->f; |
72 | 83.5k | this->a = ap; |
73 | 83.5k | this->b = bp; |
74 | 83.5k | this->c = cp; |
75 | 83.5k | this->d = dp; |
76 | 83.5k | this->e = ep; |
77 | 83.5k | this->f = fp; |
78 | 83.5k | } |
79 | | |
80 | | void |
81 | | QPDFMatrix::scale(double sx, double sy) |
82 | 27.8k | { |
83 | 27.8k | concat(QPDFMatrix(sx, 0, 0, sy, 0, 0)); |
84 | 27.8k | } |
85 | | |
86 | | void |
87 | | QPDFMatrix::translate(double tx, double ty) |
88 | 55.6k | { |
89 | 55.6k | concat(QPDFMatrix(1, 0, 0, 1, tx, ty)); |
90 | 55.6k | } |
91 | | |
92 | | void |
93 | | QPDFMatrix::rotatex90(int angle) |
94 | 50 | { |
95 | 50 | switch (angle) { |
96 | 0 | case 90: |
97 | 0 | concat(QPDFMatrix(0, 1, -1, 0, 0, 0)); |
98 | 0 | break; |
99 | 0 | case 180: |
100 | 0 | concat(QPDFMatrix(-1, 0, 0, -1, 0, 0)); |
101 | 0 | break; |
102 | 0 | case 270: |
103 | 0 | concat(QPDFMatrix(0, -1, 1, 0, 0, 0)); |
104 | 0 | break; |
105 | 50 | default: |
106 | | // ignore |
107 | 50 | break; |
108 | 50 | } |
109 | 50 | } |
110 | | |
111 | | void |
112 | | QPDFMatrix::transform(double x, double y, double& xp, double& yp) const |
113 | 114k | { |
114 | 114k | xp = (this->a * x) + (this->c * y) + this->e; |
115 | 114k | yp = (this->b * x) + (this->d * y) + this->f; |
116 | 114k | } |
117 | | |
118 | | QPDFObjectHandle::Rectangle |
119 | | QPDFMatrix::transformRectangle(QPDFObjectHandle::Rectangle r) const |
120 | 28.7k | { |
121 | 28.7k | std::vector<double> tx(4); |
122 | 28.7k | std::vector<double> ty(4); |
123 | 28.7k | transform(r.llx, r.lly, tx.at(0), ty.at(0)); |
124 | 28.7k | transform(r.llx, r.ury, tx.at(1), ty.at(1)); |
125 | 28.7k | transform(r.urx, r.lly, tx.at(2), ty.at(2)); |
126 | 28.7k | transform(r.urx, r.ury, tx.at(3), ty.at(3)); |
127 | 28.7k | return { |
128 | 28.7k | *std::min_element(tx.begin(), tx.end()), |
129 | 28.7k | *std::min_element(ty.begin(), ty.end()), |
130 | 28.7k | *std::max_element(tx.begin(), tx.end()), |
131 | 28.7k | *std::max_element(ty.begin(), ty.end())}; |
132 | 28.7k | } |
133 | | |
134 | | bool |
135 | | QPDFMatrix::operator==(QPDFMatrix const& rhs) const |
136 | 0 | { |
137 | 0 | return ( |
138 | 0 | (this->a == rhs.a) && (this->b == rhs.b) && (this->c == rhs.c) && (this->d == rhs.d) && |
139 | 0 | (this->e == rhs.e) && (this->f == rhs.f)); |
140 | 0 | } |
141 | | |
142 | | bool |
143 | | QPDFMatrix::operator!=(QPDFMatrix const& rhs) const |
144 | 0 | { |
145 | 0 | return !operator==(rhs); |
146 | 0 | } |