Coverage Report

Created: 2026-06-16 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/QPDFMatrix.cc
Line
Count
Source
1
#include <qpdf/QPDFMatrix.hh>
2
3
#include <qpdf/QUtil.hh>
4
#include <algorithm>
5
6
QPDFMatrix::QPDFMatrix() :
7
12.0k
    a(1.0),
8
12.0k
    b(0.0),
9
12.0k
    c(0.0),
10
12.0k
    d(1.0),
11
12.0k
    e(0.0),
12
12.0k
    f(0.0)
13
12.0k
{
14
12.0k
}
15
16
QPDFMatrix::QPDFMatrix(double a, double b, double c, double d, double e, double f) :
17
17.9k
    a(a),
18
17.9k
    b(b),
19
17.9k
    c(c),
20
17.9k
    d(d),
21
17.9k
    e(e),
22
17.9k
    f(f)
23
17.9k
{
24
17.9k
}
25
26
QPDFMatrix::QPDFMatrix(QPDFObjectHandle::Matrix const& m) :
27
389
    a(m.a),
28
389
    b(m.b),
29
389
    c(m.c),
30
389
    d(m.d),
31
389
    e(m.e),
32
389
    f(m.f)
33
389
{
34
389
}
35
36
static double
37
fix_rounding(double d)
38
35.6k
{
39
35.6k
    if ((d > -0.00001) && (d < 0.00001)) {
40
12.0k
        d = 0.0;
41
12.0k
    }
42
35.6k
    return d;
43
35.6k
}
44
45
std::string
46
QPDFMatrix::unparse() const
47
5.94k
{
48
5.94k
    return (
49
5.94k
        QUtil::double_to_string(fix_rounding(a), 5) + " " +
50
5.94k
        QUtil::double_to_string(fix_rounding(b), 5) + " " +
51
5.94k
        QUtil::double_to_string(fix_rounding(c), 5) + " " +
52
5.94k
        QUtil::double_to_string(fix_rounding(d), 5) + " " +
53
5.94k
        QUtil::double_to_string(fix_rounding(e), 5) + " " +
54
5.94k
        QUtil::double_to_string(fix_rounding(f), 5));
55
5.94k
}
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
18.0k
{
66
18.0k
    double ap = (this->a * other.a) + (this->c * other.b);
67
18.0k
    double bp = (this->b * other.a) + (this->d * other.b);
68
18.0k
    double cp = (this->a * other.c) + (this->c * other.d);
69
18.0k
    double dp = (this->b * other.c) + (this->d * other.d);
70
18.0k
    double ep = (this->a * other.e) + (this->c * other.f) + this->e;
71
18.0k
    double fp = (this->b * other.e) + (this->d * other.f) + this->f;
72
18.0k
    this->a = ap;
73
18.0k
    this->b = bp;
74
18.0k
    this->c = cp;
75
18.0k
    this->d = dp;
76
18.0k
    this->e = ep;
77
18.0k
    this->f = fp;
78
18.0k
}
79
80
void
81
QPDFMatrix::scale(double sx, double sy)
82
5.94k
{
83
5.94k
    concat(QPDFMatrix(sx, 0, 0, sy, 0, 0));
84
5.94k
}
85
86
void
87
QPDFMatrix::translate(double tx, double ty)
88
11.8k
{
89
11.8k
    concat(QPDFMatrix(1, 0, 0, 1, tx, ty));
90
11.8k
}
91
92
void
93
QPDFMatrix::rotatex90(int angle)
94
159
{
95
159
    switch (angle) {
96
14
    case 90:
97
14
        concat(QPDFMatrix(0, 1, -1, 0, 0, 0));
98
14
        break;
99
14
    case 180:
100
14
        concat(QPDFMatrix(-1, 0, 0, -1, 0, 0));
101
14
        break;
102
93
    case 270:
103
93
        concat(QPDFMatrix(0, -1, 1, 0, 0, 0));
104
93
        break;
105
38
    default:
106
        // ignore
107
38
        break;
108
159
    }
109
159
}
110
111
void
112
QPDFMatrix::transform(double x, double y, double& xp, double& yp) const
113
23.9k
{
114
23.9k
    xp = (this->a * x) + (this->c * y) + this->e;
115
23.9k
    yp = (this->b * x) + (this->d * y) + this->f;
116
23.9k
}
117
118
QPDFObjectHandle::Rectangle
119
QPDFMatrix::transformRectangle(QPDFObjectHandle::Rectangle r) const
120
5.97k
{
121
5.97k
    std::vector<double> tx(4);
122
5.97k
    std::vector<double> ty(4);
123
5.97k
    transform(r.llx, r.lly, tx.at(0), ty.at(0));
124
5.97k
    transform(r.llx, r.ury, tx.at(1), ty.at(1));
125
5.97k
    transform(r.urx, r.lly, tx.at(2), ty.at(2));
126
5.97k
    transform(r.urx, r.ury, tx.at(3), ty.at(3));
127
5.97k
    return {
128
5.97k
        *std::min_element(tx.begin(), tx.end()),
129
5.97k
        *std::min_element(ty.begin(), ty.end()),
130
5.97k
        *std::max_element(tx.begin(), tx.end()),
131
5.97k
        *std::max_element(ty.begin(), ty.end())};
132
5.97k
}
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
}