]> mj.ucw.cz Git - paperjam.git/blob - paperjam.cc
Remember bounding boxes of page contents
[paperjam.git] / paperjam.cc
1 #include <cassert>
2 #include <cstdarg>
3 #include <cstdlib>
4 #include <cstdio>
5
6 #include "jam.h"
7
8 #include <qpdf/QPDFWriter.hh>
9
10 static QPDF in_pdf;
11 static QPDF out_pdf;
12
13 string out_context::new_resource(const string type)
14 {
15   return "/" + type + to_string(++res_cnt);
16 }
17
18 class in_page : public page {
19   QPDFObjectHandle pdf_page;
20   QPDFObjectHandle xobject;
21 public:
22   BBox media_box;
23   void render(out_context *out, pdf_matrix xform);
24   in_page(QPDFObjectHandle inpg, int idx);
25 };
26
27 in_page::in_page(QPDFObjectHandle inpg, int idx)
28 {
29   pdf_page = inpg;
30   xobject = QPDFObjectHandle::newNull();
31   index = idx;
32
33   media_box = BBox(inpg.getKey("/MediaBox"));
34   width = media_box.width();
35   height = media_box.height();
36
37   QPDFObjectHandle art_box = inpg.getKey("/ArtBox");
38   if (art_box.isNull())
39     art_box = inpg.getKey("/CropBox");
40   if (art_box.isNull())
41     bbox = BBox(width, height);
42   else
43     {
44       bbox = BBox(art_box);
45       bbox.x_min -= media_box.x_min;
46       bbox.x_max -= media_box.x_min;
47       bbox.y_min -= media_box.y_min;
48       bbox.y_max -= media_box.y_min;
49     }
50 }
51
52 void in_page::render(out_context *out, pdf_matrix xform)
53 {
54   // Convert page to xobject
55   if (xobject.isNull())
56     xobject = out_pdf.makeIndirectObject( page_to_xobject(&out_pdf, out_pdf.copyForeignObject(pdf_page)) );
57   string xobj_res = out->new_resource("XO");
58   out->xobjects.replaceKey(xobj_res, xobject);
59
60   pdf_matrix m;
61   m.shift(-media_box.x_min, -media_box.y_min);
62   m.concat(xform);
63
64   out->contents += "q " + m.to_string() + " cm " + xobj_res + " Do Q";
65 }
66
67 static void debug_pages(vector<page *> &pages)
68 {
69   if (!debug_mode)
70     return;
71
72   for (auto pg: pages)
73     debug("Page #%d: w=%.3f h=%.3f", pg->index, pg->width, pg->height);
74 }
75
76 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
77 {
78   debug("# Input");
79   debug_pages(pages);
80
81   for (auto c: cmds)
82     {
83       debug("# Executing %s", c->def->name);
84       debug_indent += 4;
85       pages = c->exec->process(pages);
86       debug_indent -= 4;
87       debug_pages(pages);
88     }
89
90   return pages;
91 }
92
93 static void process(list<cmd *> &cmds, const char *in_name, const char *out_name)
94 {
95   in_pdf.processFile(in_name);
96   in_pdf.pushInheritedAttributesToPage();
97   out_pdf.emptyPDF();
98
99   vector<QPDFObjectHandle> const &in_pages = in_pdf.getAllPages();
100   vector<page *> pages;
101
102   QPDFObjectHandle page_copy = out_pdf.copyForeignObject(in_pages[0]);
103
104   int cnt = 0;
105   for (auto inpg: in_pages)
106     pages.push_back(new in_page(inpg, ++cnt));
107
108   pages = run_command_list(cmds, pages);
109
110   for (auto pg: pages)
111     {
112       out_context out;
113       out.resources = QPDFObjectHandle::newDictionary();
114       out.resources.replaceKey("/ProcSet", QPDFObjectHandle::parse("[/PDF]"));
115       out.xobjects = QPDFObjectHandle::newDictionary();
116       out.resources.replaceKey("/XObject", out.xobjects);
117       pg->render(&out, pdf_matrix());
118
119       QPDFObjectHandle contents = QPDFObjectHandle::newStream(&out_pdf, out.contents);
120
121       // Create the page object
122       QPDFObjectHandle out_page = out_pdf.makeIndirectObject(QPDFObjectHandle::newDictionary());
123       out_page.replaceKey("/Type", QPDFObjectHandle::newName("/Page"));
124       out_page.replaceKey("/MediaBox", BBox(pg->width, pg->height).to_array());
125       out_page.replaceKey("/CropBox", pg->bbox.to_array());
126       out_page.replaceKey("/Contents", contents);
127       out_page.replaceKey("/Resources", out.resources);
128       out_pdf.addPage(out_page, false);
129     }
130
131   // Produce info dictionary
132   QPDFObjectHandle trailer = out_pdf.getTrailer();
133   QPDFObjectHandle info = trailer.getKey("/Info");
134   if (info.isNull())
135     {
136       info = QPDFObjectHandle::newDictionary();
137       trailer.replaceKey("/Info", info);
138     }
139   else
140     assert(info.isDictionary());
141   // FIXME: More meta-data
142   info.replaceKey("/Producer", unicode_string("PaperJam"));
143
144   // Write the output file
145   QPDFWriter writer(out_pdf, out_name);
146   writer.write();
147 }
148
149 int main(int argc, char **argv)
150 {
151   if (argc != 4)
152     {
153       fprintf(stderr, "Usage: paperjam <commands> <input> <output>\n");
154       return 1;
155     }
156
157   debug_mode = 100;
158
159   list<cmd *> cmds;
160   parse(argv[1], cmds);
161
162   try
163     {
164       process(cmds, argv[2], argv[3]);
165     }
166   catch (exception& e)
167     {
168       die("%s", e.what());
169     }
170
171   return 0;
172 }