2 * Auxiliary functions for processing PDF files
4 * (c) 2018 Martin Mares <mj@ucw.cz>
15 #include "pdf-tools.h"
17 #include <qpdf/QUtil.hh>
18 #include <qpdf/Pl_Concatenate.hh>
24 void debug(const char *msg, ...)
30 vfprintf(stderr, msg, args);
35 void warn(const char *msg, ...)
39 fprintf(stderr, "WARNING: ");
40 vfprintf(stderr, msg, args);
45 void die(const char *msg, ...)
49 fprintf(stderr, "ERROR: ");
50 vfprintf(stderr, msg, args);
56 void bad(const char *msg, ...)
61 vsnprintf(buf, sizeof(buf), msg, args);
64 printf("error: %s\n", buf);
68 /*** Transformation matrices ***/
70 // Construct string representation of a transformation matrix
71 string pdf_matrix::to_string() {
73 for (int i=0; i<6; i++) {
77 snprintf(buf, sizeof(buf), "%.3f", m[i]);
83 /*** Bounding boxes ***/
85 QPDFObjectHandle BBox::to_array()
87 QPDFObjectHandle a = QPDFObjectHandle::newArray();
88 a.appendItem(QPDFObjectHandle::newReal(x_min, 1));
89 a.appendItem(QPDFObjectHandle::newReal(y_min, 1));
90 a.appendItem(QPDFObjectHandle::newReal(x_max, 1));
91 a.appendItem(QPDFObjectHandle::newReal(y_max, 1));
95 bool BBox::parse(QPDFObjectHandle h)
97 if (!h.isArray() || h.getArrayNItems() != 4)
100 for (int i=0; i<4; i++) {
101 QPDFObjectHandle item = h.getArrayItem(i);
102 if (!item.isNumber())
104 x[i] = item.getNumericValue();
113 /*** Unicode strings ***/
115 // Construct PDF representation of a UTF-8 string
116 QPDFObjectHandle unicode_string(string s)
118 // If it is ASCII only, use the string directly
119 bool ascii_only = true;
121 if (c < 0x20 || c > 0x7e)
124 return QPDFObjectHandle::newString(s);
126 // Use iconv to convert the string to big-endian UTF-16
127 iconv_t conv = iconv_open("UTF-16BE", "UTF-8");
128 if (conv == (iconv_t) -1)
129 die("Cannot initialize iconv: %m");
131 char *in_ptr = (char *) s.c_str(); // Work around bad API of iconv()
132 size_t in_len = strlen(in_ptr);
133 size_t out_len = 2*in_len + 2; // Worst case (including the BOM)
134 char out_buf[out_len];
135 char *out_ptr = out_buf;
136 size_t res = iconv(conv, &in_ptr, &in_len, &out_ptr, &out_len);
137 if (res == (size_t) -1)
138 die("iconv failed: %m");
140 die("iconv stopped before the end of input");
144 // Package UTF-16 in a PDF string
148 for (char *p = out_buf; p < out_ptr; p++)
150 return QPDFObjectHandle::newString(out);
153 /*** Conversion of pages to XObjects ***/
155 static BBox get_trim_box(QPDFObjectHandle page)
157 static const char * const boxes[] = { "/TrimBox", "/CropBox", "/MediaBox", NULL };
158 for (int i=0; boxes[i]; i++)
159 if (page.hasKey(boxes[i]))
160 return BBox(page.getKey(boxes[i]));
161 warn("Page has no trimbox, falling back to A4");
162 return BBox(0, 0, A4_WIDTH, A4_HEIGHT);
165 /* Conversion of pages to XObjects is inspired by CUPS's pdftopdf filter. */
166 class CombineFromContents_Provider : public QPDFObjectHandle::StreamDataProvider {
168 vector<QPDFObjectHandle> contents;
170 CombineFromContents_Provider(const vector<QPDFObjectHandle> &contents) : contents(contents) { }
171 void provideStreamData(int objid UNUSED, int generation UNUSED, Pipeline* pipeline) {
172 Pl_Concatenate concat("concat", pipeline);
173 for (int i=0; i < (int)contents.size(); i++)
174 contents[i].pipeStreamData(&concat, true, false, false);
175 concat.manualFinish();
179 QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page)
181 page.assertPageObject();
183 QPDFObjectHandle xo_stream = QPDFObjectHandle::newStream(out);
184 QPDFObjectHandle xo_dict = xo_stream.getDict();
186 xo_dict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
187 xo_dict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
188 xo_dict.replaceKey("/FormType", QPDFObjectHandle::newInteger(1));
190 BBox box = get_trim_box(page);
191 xo_dict.replaceKey("/BBox", box.to_array());
193 xo_dict.replaceKey("/Resources", page.getKey("/Resources"));
194 if (page.hasKey("/Group"))
195 xo_dict.replaceKey("/Group", page.getKey("/Group"));
197 if (page.hasKey("/UserUnit")) {
198 double u = page.getKey("/UserUnit").getNumericValue();
199 QPDFObjectHandle m = QPDFObjectHandle::newArray();
200 m.appendItem(QPDFObjectHandle::newReal(u, 3));
201 m.appendItem(QPDFObjectHandle::newReal(0, 0));
202 m.appendItem(QPDFObjectHandle::newReal(0, 0));
203 m.appendItem(QPDFObjectHandle::newReal(u, 3));
204 m.appendItem(QPDFObjectHandle::newReal(0, 0));
205 m.appendItem(QPDFObjectHandle::newReal(0, 0));
206 xo_dict.replaceKey("/Matrix", m);
209 vector<QPDFObjectHandle> contents = page.getPageContents();
210 auto ph = PointerHolder<QPDFObjectHandle::StreamDataProvider>(new CombineFromContents_Provider(contents));
211 xo_stream.replaceStreamData(ph, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());