Coverage Report

Created: 2025-11-24 06:47

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 = get("/AS");
37
0
    return AS ? AS.value() : "";
38
0
}
39
40
int
41
QPDFAnnotationObjectHelper::getFlags()
42
0
{
43
0
    Integer flags_obj = get("/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
            return ap_sub;
60
0
        }
61
0
        if (ap_sub.isDictionary() && !desired_state.empty()) {
62
0
            QPDFObjectHandle ap_sub_val = ap_sub.getKey(desired_state);
63
0
            if (ap_sub_val.isStream()) {
64
0
                return ap_sub_val;
65
0
            }
66
0
        }
67
0
    }
68
0
    return Null::temp();
69
0
}
70
71
std::string
72
QPDFAnnotationObjectHelper::getPageContentForAppearance(
73
    std::string const& name, int rotate, int required_flags, int forbidden_flags)
74
0
{
75
0
    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
0
    QPDFObjectHandle rect_obj = oh().getKey("/Rect");
141
0
    QPDFObjectHandle as = getAppearanceStream("/N").getDict();
142
0
    QPDFObjectHandle bbox_obj = as.getKey("/BBox");
143
0
    QPDFObjectHandle matrix_obj = as.getKey("/Matrix");
144
145
0
    int flags = getFlags();
146
0
    if (flags & forbidden_flags) {
147
0
        QTC::TC("qpdf", "QPDFAnnotationObjectHelper forbidden flags");
148
0
        return "";
149
0
    }
150
0
    if ((flags & required_flags) != required_flags) {
151
0
        QTC::TC("qpdf", "QPDFAnnotationObjectHelper missing required flags");
152
0
        return "";
153
0
    }
154
155
0
    if (!(bbox_obj.isRectangle() && rect_obj.isRectangle())) {
156
0
        return "";
157
0
    }
158
0
    QPDFMatrix matrix;
159
0
    if (matrix_obj.isMatrix()) {
160
0
        QTC::TC("qpdf", "QPDFAnnotationObjectHelper explicit matrix");
161
0
        matrix = QPDFMatrix(matrix_obj.getArrayAsMatrix());
162
0
    } else {
163
0
        QTC::TC("qpdf", "QPDFAnnotationObjectHelper default matrix");
164
0
    }
165
0
    QPDFObjectHandle::Rectangle rect = rect_obj.getArrayAsRectangle();
166
0
    bool do_rotate = (rotate && (flags & an_no_rotate));
167
0
    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
0
        QPDFMatrix mr;
175
0
        mr.rotatex90(rotate);
176
0
        mr.concat(matrix);
177
0
        matrix = mr;
178
0
        double rect_w = rect.urx - rect.llx;
179
0
        double rect_h = rect.ury - rect.lly;
180
0
        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
0
        default:
197
            // ignore
198
0
            break;
199
0
        }
200
0
    }
201
202
    // Transform bounding box by matrix to get T
203
0
    QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle();
204
0
    QPDFObjectHandle::Rectangle T = matrix.transformRectangle(bbox);
205
0
    if ((T.urx == T.llx) || (T.ury == T.lly)) {
206
        // avoid division by zero
207
0
        return "";
208
0
    }
209
    // Compute a matrix to transform the appearance box to the rectangle
210
0
    QPDFMatrix AA;
211
0
    AA.translate(rect.llx, rect.lly);
212
0
    AA.scale((rect.urx - rect.llx) / (T.urx - T.llx), (rect.ury - rect.lly) / (T.ury - T.lly));
213
0
    AA.translate(-T.llx, -T.lly);
214
0
    if (do_rotate) {
215
0
        AA.rotatex90(rotate);
216
0
    }
217
218
0
    as.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
219
0
    return ("q\n" + AA.unparse() + " cm\n" + name + " Do\n" + "Q\n");
220
0
}