2 * PaperJam -- Low-level handling of PDFs
4 * (c) 2018 Martin Mares <mj@ucw.cz>
15 #include <qpdf/QPDFWriter.hh>
20 static void do_recalc_bbox(vector<page *> &pages, const char *in_name);
22 string out_context::new_resource(const string type)
24 return "/" + type + to_string(++res_cnt);
27 class in_page : public page {
28 QPDFObjectHandle pdf_page;
29 QPDFObjectHandle xobject;
32 void render(out_context *out, pdf_matrix xform);
33 in_page(QPDFObjectHandle inpg, int idx);
36 in_page::in_page(QPDFObjectHandle inpg, int idx)
39 xobject = QPDFObjectHandle::newNull();
42 media_box = BBox(inpg.getKey("/MediaBox"));
43 width = media_box.width();
44 height = media_box.height();
46 QPDFObjectHandle art_box = inpg.getKey("/ArtBox");
48 art_box = inpg.getKey("/CropBox");
50 bbox = BBox(width, height);
54 bbox.x_min -= media_box.x_min;
55 bbox.x_max -= media_box.x_min;
56 bbox.y_min -= media_box.y_min;
57 bbox.y_max -= media_box.y_min;
61 void in_page::render(out_context *out, pdf_matrix xform)
63 // Convert page to xobject
65 xobject = out_pdf.makeIndirectObject( page_to_xobject(&out_pdf, out_pdf.copyForeignObject(pdf_page)) );
66 string xobj_res = out->new_resource("XO");
67 out->xobjects.replaceKey(xobj_res, xobject);
70 m.shift(-media_box.x_min, -media_box.y_min);
73 out->contents += "q " + m.to_string() + " cm " + xobj_res + " Do Q ";
76 void debug_pages(vector<page *> &pages)
82 debug("Page #%d: media[%.3f %.3f] bbox[%.3f %.3f %.3f %.3f]",
84 pg->width, pg->height,
85 pg->bbox.x_min, pg->bbox.y_min, pg->bbox.x_max, pg->bbox.y_max);
88 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
95 debug("# Executing %s", c->def->name);
97 pages = c->exec->process(pages);
105 void process(list<cmd *> &cmds)
107 in_pdf.processFile(in_name);
108 in_pdf.pushInheritedAttributesToPage();
111 vector<QPDFObjectHandle> const &in_pages = in_pdf.getAllPages();
112 vector<page *> pages;
114 QPDFObjectHandle page_copy = out_pdf.copyForeignObject(in_pages[0]);
117 for (auto inpg: in_pages)
118 pages.push_back(new in_page(inpg, ++cnt));
121 do_recalc_bbox(pages, in_name);
123 pages = run_command_list(cmds, pages);
128 out.resources = QPDFObjectHandle::newDictionary();
129 out.resources.replaceKey("/ProcSet", QPDFObjectHandle::parse("[/PDF]"));
130 out.xobjects = QPDFObjectHandle::newDictionary();
131 out.resources.replaceKey("/XObject", out.xobjects);
132 pg->render(&out, pdf_matrix());
134 QPDFObjectHandle contents = QPDFObjectHandle::newStream(&out_pdf, out.contents);
136 // Create the page object
137 QPDFObjectHandle out_page = out_pdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
138 out_page.replaceKey("/Type", QPDFObjectHandle::newName("/Page"));
139 out_page.replaceKey("/MediaBox", BBox(pg->width, pg->height).to_array());
141 // out_page.replaceKey("/CropBox", pg->bbox.to_array());
142 out_page.replaceKey("/Contents", contents);
143 out_page.replaceKey("/Resources", out.resources);
144 out_pdf.addPage(out_page, false);
147 // Produce info dictionary
148 QPDFObjectHandle trailer = out_pdf.getTrailer();
149 QPDFObjectHandle info = trailer.getKey("/Info");
152 info = QPDFObjectHandle::newDictionary();
153 trailer.replaceKey("/Info", info);
156 assert(info.isDictionary());
157 // FIXME: More meta-data
158 info.replaceKey("/Producer", unicode_string("PaperJam"));
160 // Write the output file
161 QPDFWriter writer(out_pdf, out_name);
165 /*** Re-calculation of bboxes ***/
167 vector<BBox> gs_bboxes(const char *in)
171 die("Cannot create pipe: %m");
175 die("Cannot fork: %m");
183 execlp("gs", "gs", "-sDEVICE=bbox", "-dSAFER", "-dBATCH", "-dNOPAUSE", "-q", in, NULL);
184 die("Cannot execute gs: %m");
188 FILE *f = fdopen(pipes[0], "r");
190 die("fdopen failed: %m");
194 while (fgets(line, sizeof(line), f))
196 char *eol = strchr(line, '\n');
198 die("Ghostscript produced too long lines");
201 if (!strncmp(line, "%%HiResBoundingBox: ", 20))
203 double x1, y1, x2, y2;
204 if (sscanf(line+20, "%lf%lf%lf%lf", &x1, &y1, &x2, &y2) != 4)
205 die("Cannot parse Ghostscript output: %s", line);
206 bboxes.push_back(BBox(x1, y1, x2, y2));
208 else if (line[0] != '%')
209 fprintf(stderr, "%s\n", line);
214 if (waitpid(pid, &stat, 0) < 0)
215 die("wait failed: %m");
216 if (!WIFEXITED(stat) || WEXITSTATUS(stat))
217 die("Ghostscript failed");
222 static void do_recalc_bbox(vector<page *> &pages, const char *in_name)
224 vector<BBox> bboxes = gs_bboxes(in_name);
225 if (pages.size() != bboxes.size())
226 die("Ghostscript failed to produce the right number of bboxes");
228 for (size_t i=0; i<pages.size(); i++)
229 pages[i]->bbox = bboxes[i];