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>
25 void debug(const char *msg, ...)
31 fprintf(stderr, "%*s", debug_indent, "");
32 vfprintf(stderr, msg, args);
37 void warn(const char *msg, ...)
41 fprintf(stderr, "WARNING: ");
42 vfprintf(stderr, msg, args);
47 void die(const char *msg, ...)
51 fprintf(stderr, "ERROR: ");
52 vfprintf(stderr, msg, args);
58 void bad(const char *msg, ...)
63 vsnprintf(buf, sizeof(buf), msg, args);
66 printf("error: %s\n", buf);
70 /*** Transformation matrices ***/
72 // Construct string representation of a transformation matrix
73 string pdf_matrix::to_string() {
75 for (int i=0; i<6; i++) {
79 snprintf(buf, sizeof(buf), "%.3f", m[i]);
85 /*** Bounding boxes ***/
87 QPDFObjectHandle BBox::to_array()
89 QPDFObjectHandle a = QPDFObjectHandle::newArray();
90 a.appendItem(QPDFObjectHandle::newReal(x_min, 1));
91 a.appendItem(QPDFObjectHandle::newReal(y_min, 1));
92 a.appendItem(QPDFObjectHandle::newReal(x_max, 1));
93 a.appendItem(QPDFObjectHandle::newReal(y_max, 1));
97 bool BBox::parse(QPDFObjectHandle h)
99 if (!h.isArray() || h.getArrayNItems() != 4)
102 for (int i=0; i<4; i++) {
103 QPDFObjectHandle item = h.getArrayItem(i);
104 if (!item.isNumber())
106 x[i] = item.getNumericValue();
115 void BBox::transform(pdf_matrix &m)
117 m.apply(&x_min, &y_min);
118 m.apply(&x_max, &y_max);
125 /*** Unicode strings ***/
127 // Construct PDF representation of a UTF-8 string
128 QPDFObjectHandle unicode_string(string s)
130 // If it is ASCII only, use the string directly
131 bool ascii_only = true;
133 if (c < 0x20 || c > 0x7e)
136 return QPDFObjectHandle::newString(s);
138 // Use iconv to convert the string to big-endian UTF-16
139 iconv_t conv = iconv_open("UTF-16BE", "UTF-8");
140 if (conv == (iconv_t) -1)
141 die("Cannot initialize iconv: %m");
143 char *in_ptr = (char *) s.c_str(); // Work around bad API of iconv()
144 size_t in_len = strlen(in_ptr);
145 size_t out_len = 2*in_len + 2; // Worst case (including the BOM)
146 char out_buf[out_len];
147 char *out_ptr = out_buf;
148 size_t res = iconv(conv, &in_ptr, &in_len, &out_ptr, &out_len);
149 if (res == (size_t) -1)
150 die("iconv failed: %m");
152 die("iconv stopped before the end of input");
156 // Package UTF-16 in a PDF string
160 for (char *p = out_buf; p < out_ptr; p++)
162 return QPDFObjectHandle::newString(out);
165 /*** Conversion of pages to XObjects ***/
167 static BBox get_trim_box(QPDFObjectHandle page)
169 static const char * const boxes[] = { "/TrimBox", "/CropBox", "/MediaBox", NULL };
170 for (int i=0; boxes[i]; i++)
171 if (page.hasKey(boxes[i]))
172 return BBox(page.getKey(boxes[i]));
173 warn("Page has no trimbox, falling back to A4");
174 return BBox(0, 0, A4_WIDTH, A4_HEIGHT);
177 /* Conversion of pages to XObjects is inspired by CUPS's pdftopdf filter. */
178 class CombineFromContents_Provider : public QPDFObjectHandle::StreamDataProvider {
180 vector<QPDFObjectHandle> contents;
182 CombineFromContents_Provider(const vector<QPDFObjectHandle> &contents) : contents(contents) { }
183 void provideStreamData(int objid UNUSED, int generation UNUSED, Pipeline* pipeline) {
184 Pl_Concatenate concat("concat", pipeline);
185 for (int i=0; i < (int)contents.size(); i++)
186 contents[i].pipeStreamData(&concat, true, false, false);
187 concat.manualFinish();
191 QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page)
193 page.assertPageObject();
195 QPDFObjectHandle xo_stream = QPDFObjectHandle::newStream(out);
196 QPDFObjectHandle xo_dict = xo_stream.getDict();
198 xo_dict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
199 xo_dict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
200 xo_dict.replaceKey("/FormType", QPDFObjectHandle::newInteger(1));
202 BBox box = get_trim_box(page);
203 xo_dict.replaceKey("/BBox", box.to_array());
205 xo_dict.replaceKey("/Resources", page.getKey("/Resources"));
206 if (page.hasKey("/Group"))
207 xo_dict.replaceKey("/Group", page.getKey("/Group"));
209 if (page.hasKey("/UserUnit")) {
210 double u = page.getKey("/UserUnit").getNumericValue();
211 QPDFObjectHandle m = QPDFObjectHandle::newArray();
212 m.appendItem(QPDFObjectHandle::newReal(u, 3));
213 m.appendItem(QPDFObjectHandle::newReal(0, 0));
214 m.appendItem(QPDFObjectHandle::newReal(0, 0));
215 m.appendItem(QPDFObjectHandle::newReal(u, 3));
216 m.appendItem(QPDFObjectHandle::newReal(0, 0));
217 m.appendItem(QPDFObjectHandle::newReal(0, 0));
218 xo_dict.replaceKey("/Matrix", m);
221 vector<QPDFObjectHandle> contents = page.getPageContents();
222 auto ph = PointerHolder<QPDFObjectHandle::StreamDataProvider>(new CombineFromContents_Provider(contents));
223 xo_stream.replaceStreamData(ph, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());