Debian Codegraph @

filter/pdftopdf/qpdf_xobject.cc

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #include "qpdf_xobject.h"
2 //#include <qpdf/Types.h>
•3 #define POINTERHOLDER_TRANSITION 3
4 #include <qpdf/PointerHolder.hh>
5 #include <qpdf/QPDF.hh>
6 #include <qpdf/Pl_Discard.hh>
7 #include <qpdf/Pl_Count.hh>
8 #include <qpdf/Pl_Concatenate.hh>
9 #include "qpdf_tools.h"
10 #include "qpdf_pdftopdf.h"
11
12 // TODO: need to remove Struct Parent stuff (or fix)
13
14 // NOTE: use /TrimBox to position content inside Nup cell, /BleedBox to clip against
15
16 class CombineFromContents_Provider : public QPDFObjectHandle::StreamDataProvider {
17 public:
18 CombineFromContents_Provider(const std::vector<QPDFObjectHandle> &contents);
19
20 void provideStreamData(int objid, int generation, Pipeline* pipeline);
21 private:
22 std::vector<QPDFObjectHandle> contents;
23 };
24
25 CombineFromContents_Provider::CombineFromContents_Provider(const std::vector<QPDFObjectHandle> &contents)
26 : contents(contents)
27 {
28 }
29
30 void CombineFromContents_Provider::provideStreamData(int objid, int generation, Pipeline* pipeline)
31 {
32 Pl_Concatenate concat("concat", pipeline);
33 const int clen=contents.size();
34 for (int iA=0;iA<clen;iA++) {
35 contents[iA].pipeStreamData(&concat, true, false, false);
36 }
37 concat.manualFinish();
38 }
39
40 /*
41 To convert a page to an XObject there are several keys to consider:
42
43 /Type /Page -> /Type /XObject (/Type optional for XObject)
44 -> /Subtype /Form
45 -> [/FormType 1] (optional)
46 /Parent ? ? R -> remove
47 /Resources dict -> copy
48 /MediaBox rect [/CropBox /BleedBox /TrimBox /ArtBox]
49 -> /BBox (use TrimBox [+ Bleed consideration?], with fallback to /MediaBox)
50 note that /BBox is in *Form Space*, see /Matrix!
51 [/BoxColorInfo dict] (used for guidelines that may be shown by viewer)
52 -> ignore/remove
53 [/Contents asfd] -> concatenate into stream data of the XObject (page is a dict, XObject a stream)
54
55 [/Rotate 90] ... must be handled (either use CTM where XObject is /used/ -- or set /Matrix)
56 [/UserUnit] (PDF 1.6) -> to /Matrix ? -- it MUST be handled.
57
58 [/Group dict] -> copy
59 [/Thumb stream] -> remove, not needed any more / would have to be regenerated (combined)
60 [/B] article beads -- ignore for now
61 [/Dur] -> remove (transition duration)
62 [/Trans] -> remove (transitions)
63 [/AA] -> remove (additional-actions)
64
65 [/Metadata] what shall we do?? (kill: we can't combine XML)
66 [/PieceInfo] -> remove, we can't combine private app data (?)
67 [/LastModified date] (opt except /PieceInfo) -> see there
68
69 [/PZ] -> remove, can't combine/keep (preferred zoom level)
70 [/SeparationInfo] -> remove, no way to keep this (needed for separation)
71
72 [/ID] related to web capture -- ignore/kill?
73 [/StructParents] (opt except pdf contains "structural content items")
74 -> copy (is this correct?)
75
76 [/Annots] annotations -- ignore for now
77 [/Tabs] tab order for annotations (/R row, /C column, /S structure order) -- see /Annots
78
79 [/TemplateInstantiated] (reqd, if page was created from named page obj, 1.5) -- ? just ignore?
80 [/PresSteps] -> remove (sub-page navigation for presentations) [no subpage navigation for printing / nup]
81 [/VP] viewport rects -- ignore/drop or recalculate into new page
82
83 */
84 QPDFObjectHandle makeXObject(QPDF *pdf,QPDFObjectHandle page)
85 {
86 page.assertPageObject();
87
88 QPDFObjectHandle ret=QPDFObjectHandle::newStream(pdf);
89 QPDFObjectHandle dict=ret.getDict();
90
91 dict.replaceKey("/Type",QPDFObjectHandle::newName("/XObject")); // optional
92 dict.replaceKey("/Subtype",QPDFObjectHandle::newName("/Form")); // required
93 // dict.replaceKey("/FormType",QPDFObjectHandle::newInteger(1)); // optional
94
95 QPDFObjectHandle box=getTrimBox(page); // already in "form space"
96 dict.replaceKey("/BBox",box); // reqd
97
98 // [/Matrix .] ... default is [1 0 0 1 0 0]; we incorporate /UserUnit and /Rotate here
99 Matrix mtx;
100 if (page.hasKey("/UserUnit")) {
101 mtx.scale(page.getKey("/UserUnit").getNumericValue());
102 }
103
104 // transform, so that bbox is [0 0 w h] (in outer space, but after UserUnit)
105 Rotation rot=getRotate(page);
106
107 // calculate rotation effect on [0 0 w h]
108 PageRect bbox=getBoxAsRect(box),tmp;
109 tmp.left=0;
110 tmp.bottom=0;
111 tmp.right=0;
112 tmp.top=0;
113 tmp.rotate_move(rot,bbox.width,bbox.height);
114 // tmp.rotate_move moves the bbox; we must achieve this move with the matrix.
115 mtx.translate(tmp.left,tmp.bottom); // 1. move origin to end up at left,bottom after rotation
116
117 mtx.rotate(rot); // 2. rotate coordinates according to /Rotate
118 mtx.translate(-bbox.left,-bbox.bottom); // 3. move origin from 0,0 to "form space"
119
120 dict.replaceKey("/Matrix",mtx.get());
121
122 dict.replaceKey("/Resources",page.getKey("/Resources"));
123 if (page.hasKey("/Group")) {
124 dict.replaceKey("/Group",page.getKey("/Group")); // (transparency); opt, copy if there
125 }
126
127 // ?? /StructParents ... can basically copy from page, but would need fixup in Structure Tree
128 // FIXME: remove (globally) Tagged spec (/MarkInfo), and Structure Tree
129
130 // Note: [/Name] (reqd. only in 1.0 -- but there we even can't use our normal img/patter procedures)
131
132 // none:
133 // QPDFObjectHandle filter=QPDFObjectHandle::newArray();
134 // QPDFObjectHandle decode_parms=QPDFObjectHandle::newArray();
135 // null leads to use of "default filters" from qpdf's settings
136 QPDFObjectHandle filter=QPDFObjectHandle::newNull();
137 QPDFObjectHandle decode_parms=QPDFObjectHandle::newNull();
138
139 std::vector<QPDFObjectHandle> contents=page.getPageContents(); // (will assertPageObject)
140
141 auto ph=PointerHolder<QPDFObjectHandle::StreamDataProvider>(new CombineFromContents_Provider(contents));
142 ret.replaceStreamData(ph,filter,decode_parms);
143
144 return ret;
145 }
146
147 /*
148 we will have to fix up the structure tree (e.g. /K in element), when copying /StructParents;
149 (there is /Pg, which has to point to the containing page, /Stm when it's not part of the page's content stream
150 i.e. when it is in our XObject!; then there is /StmOwn ...)
151 when not copying, we have to remove the structure tree completely (also /MarkInfo dict)
152 Still this might not be sufficient(?), as there are probably BDC and EMC operators in the stream.
153 */
154
155 /* /XObject /Form has
156 [/Type /XObject]
157 /Subtype /Form
158 [/FormType 1]
159 /BBox rect from crop box, or recalculate
160 [/Matrix .] ... default is [1 0 0 1 0 0] --- we have to incorporate /UserUnit here?!
161 [/Resources dict] from page.
162 [/Group dict] used for transparency -- can copy from page
163 [/Ref dict] not needed; for external reference
164 [/Metadata] not, as long we can not combine.
165 [/PieceInfo] can copy, but not combine
166 [/LastModified date] copy if /PieceInfo there
167 [/StructParent] . don't want to be one ... have to read more spec
168 [/StructParents] . copy from page!
169 [/OPI] no opi version. don't set
170 [/OC] is this optional content? NO! not needed.
171 [/Name] (only reqd. in 1.0 -- but there we even can't use our normal img/patter procedures)
172 */