Coverage Report

Created: 2025-11-09 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/QPDFAnnotationObjectHelper.cc
Line
Count
Source
1
#include <qpdf/QPDFAnnotationObjectHelper.hh>
2
3
#include <qpdf/QPDFMatrix.hh>
4
#include <qpdf/QPDFObjectHandle_private.hh>
5
#include <qpdf/QTC.hh>
6
#include <qpdf/QUtil.hh>
7
8
using namespace qpdf;
9
10
QPDFAnnotationObjectHelper::QPDFAnnotationObjectHelper(QPDFObjectHandle oh) :
11
67.7k
    QPDFObjectHelper(oh)
12
67.7k
{
13
67.7k
}
14
15
std::string
16
QPDFAnnotationObjectHelper::getSubtype()
17
18.2k
{
18
18.2k
    return oh().getKey("/Subtype").getName();
19
18.2k
}
20
21
QPDFObjectHandle::Rectangle
22
QPDFAnnotationObjectHelper::getRect()
23
3.93k
{
24
3.93k
    return oh().getKey("/Rect").getArrayAsRectangle();
25
3.93k
}
26
27
QPDFObjectHandle
28
QPDFAnnotationObjectHelper::getAppearanceDictionary()
29
58.2k
{
30
58.2k
    return oh().getKey("/AP");
31
58.2k
}
32
33
std::string
34
QPDFAnnotationObjectHelper::getAppearanceState()
35
43.2k
{
36
43.2k
    Name AS = (*this)["/AS"];
37
43.2k
    return AS ? AS.value() : "";
38
43.2k
}
39
40
int
41
QPDFAnnotationObjectHelper::getFlags()
42
9.76k
{
43
9.76k
    Integer flags_obj = (*this)["/F"];
44
9.76k
    return flags_obj ? flags_obj : 0;
45
9.76k
}
46
47
QPDFObjectHandle
48
QPDFAnnotationObjectHelper::getAppearanceStream(std::string const& which, std::string const& state)
49
43.2k
{
50
43.2k
    QPDFObjectHandle ap = getAppearanceDictionary();
51
43.2k
    std::string desired_state = state.empty() ? getAppearanceState() : state;
52
43.2k
    if (ap.isDictionary()) {
53
34.6k
        QPDFObjectHandle ap_sub = ap.getKey(which);
54
34.6k
        if (ap_sub.isStream()) {
55
            // According to the spec, Appearance State is supposed to refer to a subkey of the
56
            // appearance stream when /AP is a dictionary, but files have been seen in the wild
57
            // where Appearance State is `/N` and `/AP` is a stream. Therefore, if `which` points to
58
            // a stream, disregard state and just use the stream. See qpdf issue #949 for details.
59
30.0k
            return ap_sub;
60
30.0k
        }
61
4.68k
        if (ap_sub.isDictionary() && !desired_state.empty()) {
62
1.96k
            QPDFObjectHandle ap_sub_val = ap_sub.getKey(desired_state);
63
1.96k
            if (ap_sub_val.isStream()) {
64
897
                return ap_sub_val;
65
897
            }
66
1.96k
        }
67
4.68k
    }
68
12.3k
    return Null::temp();
69
43.2k
}
70
71
std::string
72
QPDFAnnotationObjectHelper::getPageContentForAppearance(
73
    std::string const& name, int rotate, int required_flags, int forbidden_flags)
74
9.76k
{
75
9.76k
    if (!getAppearanceStream("/N").isStream()) {
76
0
        return "";
77
0
    }
78
79
    // The appearance matrix computed by this method is the transformation matrix that needs to be
80
    // in effect when drawing this annotation's appearance stream on the page. The algorithm for
81
    // computing the appearance matrix described in section 12.5.5 of the ISO-32000 PDF spec is
82
    // similar but not identical to what we are doing here.
83
84
    // When rendering an appearance stream associated with an annotation, there are four relevant
85
    // components:
86
    //
87
    // * The appearance stream's bounding box (/BBox)
88
    // * The appearance stream's matrix (/Matrix)
89
    // * The annotation's rectangle (/Rect)
90
    // * In the case of form fields with the NoRotate flag, the page's rotation
91
92
    // When rendering a form xobject in isolation, just drawn with a /Do operator, there is no form
93
    // field, so page rotation is not relevant, and there is no annotation, so /Rect is not
94
    // relevant, so only /BBox and /Matrix are relevant. The effect of these are as follows:
95
96
    // * /BBox is treated as a clipping region
97
    // * /Matrix is applied as a transformation prior to rendering the appearance stream.
98
99
    // There is no relationship between /BBox and /Matrix in this case.
100
101
    // When rendering a form xobject in the context of an annotation, things are a little different.
102
    // In particular, a matrix is established such that /BBox, when transformed by /Matrix, would
103
    // fit completely inside of /Rect. /BBox is no longer a clipping region. To illustrate the
104
    // difference, consider a /Matrix of [2 0 0 2 0 0], which is scaling by a factor of two along
105
    // both axes. If the appearance stream drew a rectangle equal to /BBox, in the case of the form
106
    // xobject in isolation, this matrix would cause only the lower-left quadrant of the rectangle
107
    // to be visible since the scaling would cause the rest of it to fall outside of the clipping
108
    // region. In the case of the form xobject displayed in the context of an annotation, such a
109
    // matrix would have no effect at all because it would be applied to the bounding box first, and
110
    // then when the resulting enclosing quadrilateral was transformed to fit into /Rect, the effect
111
    // of the scaling would be undone.
112
113
    // Our job is to create a transformation matrix that compensates for these differences so that
114
    // the appearance stream of an annotation can be drawn as a regular form xobject.
115
116
    // To do this, we perform the following steps, which overlap significantly with the algorithm
117
    // in 12.5.5:
118
119
    // 1. Transform the four corners of /BBox by applying /Matrix to them, creating an arbitrarily
120
    //    transformed quadrilateral.
121
122
    // 2. Find the minimum upright rectangle that encompasses the resulting quadrilateral. This is
123
    //    the "transformed appearance box", T.
124
125
    // 3. Compute matrix A that maps the lower left and upper right corners of T to the annotation's
126
    //    /Rect. This can be done by scaling so that the sizes match and translating so that the
127
    //    scaled T exactly overlaps /Rect.
128
129
    // If the annotation's /F flag has bit 4 set, this means that annotation is to be rotated about
130
    // its upper left corner to counteract any rotation of the page so it remains upright. To
131
    // achieve this effect, we do the following extra steps:
132
133
    // 1. Perform the rotation on /BBox box prior to transforming it with /Matrix (by replacing
134
    //    matrix with concatenation of matrix onto the rotation)
135
136
    // 2. Rotate the destination rectangle by the specified amount
137
138
    // 3. Apply the rotation to A as computed above to get the final appearance matrix.
139
140
9.76k
    QPDFObjectHandle rect_obj = oh().getKey("/Rect");
141
9.76k
    QPDFObjectHandle as = getAppearanceStream("/N").getDict();
142
9.76k
    QPDFObjectHandle bbox_obj = as.getKey("/BBox");
143
9.76k
    QPDFObjectHandle matrix_obj = as.getKey("/Matrix");
144
145
9.76k
    int flags = getFlags();
146
9.76k
    if (flags & forbidden_flags) {
147
267
        QTC::TC("qpdf", "QPDFAnnotationObjectHelper forbidden flags");
148
267
        return "";
149
267
    }
150
9.49k
    if ((flags & required_flags) != required_flags) {
151
0
        QTC::TC("qpdf", "QPDFAnnotationObjectHelper missing required flags");
152
0
        return "";
153
0
    }
154
155
9.49k
    if (!(bbox_obj.isRectangle() && rect_obj.isRectangle())) {
156
2.53k
        return "";
157
2.53k
    }
158
6.96k
    QPDFMatrix matrix;
159
6.96k
    if (matrix_obj.isMatrix()) {
160
390
        QTC::TC("qpdf", "QPDFAnnotationObjectHelper explicit matrix");
161
390
        matrix = QPDFMatrix(matrix_obj.getArrayAsMatrix());
162
6.57k
    } else {
163
6.57k
        QTC::TC("qpdf", "QPDFAnnotationObjectHelper default matrix");
164
6.57k
    }
165
6.96k
    QPDFObjectHandle::Rectangle rect = rect_obj.getArrayAsRectangle();
166
6.96k
    bool do_rotate = (rotate && (flags & an_no_rotate));
167
6.96k
    if (do_rotate) {
168
        // If the annotation flags include the NoRotate bit and the page is rotated, we have to
169
        // rotate the annotation about its upper left corner by the same amount in the opposite
170
        // direction so that it will remain upright in absolute coordinates. Since the semantics of
171
        // /Rotate for a page are to rotate the page, while the effect of rotating using a
172
        // transformation matrix is to rotate the coordinate system, the opposite directionality is
173
        // explicit in the code.
174
8
        QPDFMatrix mr;
175
8
        mr.rotatex90(rotate);
176
8
        mr.concat(matrix);
177
8
        matrix = mr;
178
8
        double rect_w = rect.urx - rect.llx;
179
8
        double rect_h = rect.ury - rect.lly;
180
8
        switch (rotate) {
181
0
        case 90:
182
0
            QTC::TC("qpdf", "QPDFAnnotationObjectHelper rotate 90");
183
0
            rect = QPDFObjectHandle::Rectangle(
184
0
                rect.llx, rect.ury, rect.llx + rect_h, rect.ury + rect_w);
185
0
            break;
186
0
        case 180:
187
0
            QTC::TC("qpdf", "QPDFAnnotationObjectHelper rotate 180");
188
0
            rect = QPDFObjectHandle::Rectangle(
189
0
                rect.llx - rect_w, rect.ury, rect.llx, rect.ury + rect_h);
190
0
            break;
191
0
        case 270:
192
0
            QTC::TC("qpdf", "QPDFAnnotationObjectHelper rotate 270");
193
0
            rect = QPDFObjectHandle::Rectangle(
194
0
                rect.llx - rect_h, rect.ury - rect_w, rect.llx, rect.ury);
195
0
            break;
196
8
        default:
197
            // ignore
198
8
            break;
199
8
        }
200
8
    }
201
202
    // Transform bounding box by matrix to get T
203
6.96k
    QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle();
204
6.96k
    QPDFObjectHandle::Rectangle T = matrix.transformRectangle(bbox);
205
6.96k
    if ((T.urx == T.llx) || (T.ury == T.lly)) {
206
        // avoid division by zero
207
21
        return "";
208
21
    }
209
    // Compute a matrix to transform the appearance box to the rectangle
210
6.94k
    QPDFMatrix AA;
211
6.94k
    AA.translate(rect.llx, rect.lly);
212
6.94k
    AA.scale((rect.urx - rect.llx) / (T.urx - T.llx), (rect.ury - rect.lly) / (T.ury - T.lly));
213
6.94k
    AA.translate(-T.llx, -T.lly);
214
6.94k
    if (do_rotate) {
215
8
        AA.rotatex90(rotate);
216
8
    }
217
218
6.94k
    as.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
219
6.94k
    return ("q\n" + AA.unparse() + " cm\n" + name + " Do\n" + "Q\n");
220
6.96k
}