Coverage Report

Created: 2025-10-10 06:20

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