8 #include <qpdf/QPDFWriter.hh>
13 string page_out::new_resource(const string type)
15 return "/" + type + to_string(++res_cnt);
18 class in_page : public page {
19 QPDFObjectHandle pdf_page;
20 QPDFObjectHandle xobject;
23 void render(page_out *out, pdf_matrix xform);
24 in_page(QPDFObjectHandle inpg, int idx);
27 in_page::in_page(QPDFObjectHandle inpg, int idx)
30 xobject = QPDFObjectHandle::newNull();
32 media_box = BBox(inpg.getKey("/MediaBox"));
33 width = media_box.width();
34 height = media_box.height();
37 void in_page::render(page_out *out, pdf_matrix xform)
39 // Convert page to xobject
41 xobject = out_pdf.makeIndirectObject( page_to_xobject(&out_pdf, out_pdf.copyForeignObject(pdf_page)) );
42 string xobj_res = out->new_resource("XO");
43 out->xobjects.replaceKey(xobj_res, xobject);
46 m.shift(-media_box.x_min, -media_box.y_min);
49 out->contents += "q " + m.to_string() + " cm " + xobj_res + " Do Q";
52 static int run_indent;
54 static void debug_pages(vector<page *> &pages)
60 debug("%*sPage #%d: w=%.3f h=%.3f", run_indent, "", pg->index, pg->width, pg->height);
63 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
65 debug("%*s# Input", run_indent, "");
70 debug("%*s# Executing %s", run_indent, "", c->def->name);
72 pages = c->exec->process(pages);
80 static void process(list<cmd *> &cmds, const char *in_name, const char *out_name)
82 in_pdf.processFile(in_name);
83 in_pdf.pushInheritedAttributesToPage();
86 vector<QPDFObjectHandle> const &in_pages = in_pdf.getAllPages();
89 QPDFObjectHandle page_copy = out_pdf.copyForeignObject(in_pages[0]);
92 for (auto inpg: in_pages)
93 pages.push_back(new in_page(inpg, ++cnt));
95 pages = run_command_list(cmds, pages);
100 out.resources = QPDFObjectHandle::newDictionary();
101 // FIXME: What if the source page requires a broader ProcSet?
102 out.resources.replaceKey("/ProcSet", QPDFObjectHandle::parse("[/PDF /Text]"));
103 out.xobjects = QPDFObjectHandle::newDictionary();
104 out.resources.replaceKey("/XObject", out.xobjects);
105 pg->render(&out, pdf_matrix());
107 QPDFObjectHandle contents = QPDFObjectHandle::newStream(&out_pdf, out.contents);
109 // Create the page object
110 QPDFObjectHandle out_page = out_pdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
111 out_page.replaceKey("/Type", QPDFObjectHandle::newName("/Page"));
112 out_page.replaceKey("/MediaBox", BBox(pg->width, pg->height).to_array());
113 out_page.replaceKey("/Contents", contents);
114 out_page.replaceKey("/Resources", out.resources);
115 out_pdf.addPage(out_page, false);
118 // Produce info dictionary
119 QPDFObjectHandle trailer = out_pdf.getTrailer();
120 QPDFObjectHandle info = trailer.getKey("/Info");
123 info = QPDFObjectHandle::newDictionary();
124 trailer.replaceKey("/Info", info);
127 assert(info.isDictionary());
128 // FIXME: More meta-data
129 info.replaceKey("/Producer", unicode_string("PaperJam"));
131 // Write the output file
132 QPDFWriter writer(out_pdf, out_name);
136 int main(int argc, char **argv)
140 fprintf(stderr, "Usage: paperjam <commands> <input> <output>\n");
147 parse(argv[1], cmds);
151 process(cmds, argv[2], argv[3]);