2 * Auxiliary functions for processing PDF files
4 * (c) 2018 Martin Mares <mj@ucw.cz>
15 #include <qpdf/QUtil.hh>
16 #include <qpdf/Pl_Concatenate.hh>
18 /*** Transformation matrices ***/
20 // Construct string representation of a transformation matrix
21 string pdf_matrix::to_string() {
23 for (int i=0; i<6; i++) {
26 s += pdf_coord(m[i], 6);
31 /*** Bounding boxes ***/
33 QPDFObjectHandle BBox::to_array()
35 QPDFObjectHandle a = QPDFObjectHandle::newArray();
36 a.appendItem(QPDFObjectHandle::newReal(x_min, 1));
37 a.appendItem(QPDFObjectHandle::newReal(y_min, 1));
38 a.appendItem(QPDFObjectHandle::newReal(x_max, 1));
39 a.appendItem(QPDFObjectHandle::newReal(y_max, 1));
43 string BBox::to_rect()
46 pdf_coord(x_min) + " " +
47 pdf_coord(y_min) + " " +
48 pdf_coord(width()) + " " +
52 bool BBox::parse(QPDFObjectHandle h)
54 if (!h.isArray() || h.getArrayNItems() != 4)
57 for (int i=0; i<4; i++) {
58 QPDFObjectHandle item = h.getArrayItem(i);
61 x[i] = item.getNumericValue();
70 BBox::BBox(QPDFObjectHandle box) {
72 warn("Invalid bounding box, falling back to A4");
73 x_min = 0, x_max = A4_WIDTH;
74 y_min = 0, y_max = A4_HEIGHT;
78 void BBox::transform(pdf_matrix &m)
80 m.apply(&x_min, &y_min);
81 m.apply(&x_max, &y_max);
88 BBox BBox::transformed(pdf_matrix &m)
95 void BBox::join(BBox &with)
97 x_min = min(x_min, with.x_min);
98 x_max = max(x_max, with.x_max);
99 y_min = min(y_min, with.y_min);
100 y_max = max(y_max, with.y_max);
103 static double clamp(double x, double min, double max)
112 void BBox::intersect(BBox &with)
114 x_min = clamp(x_min, with.x_min, with.x_max);
115 x_max = clamp(x_max, with.x_min, with.x_max);
116 y_min = clamp(y_min, with.y_min, with.y_max);
117 y_max = clamp(y_max, with.y_min, with.y_max);
120 void BBox::enlarge(double by)
128 BBox BBox::enlarged(double by)
135 /*** Unicode strings ***/
137 // Construct PDF representation of a UTF-8 string
138 QPDFObjectHandle unicode_string(string s)
140 // If it is ASCII only, use the string directly
141 bool ascii_only = true;
143 if (c < 0x20 || c > 0x7e)
146 return QPDFObjectHandle::newString(s);
148 // Use iconv to convert the string to big-endian UTF-16
149 iconv_t conv = iconv_open("UTF-16BE", "UTF-8");
150 if (conv == (iconv_t) -1)
151 die("Cannot initialize iconv: %m");
153 char *in_ptr = (char *) s.c_str(); // Work around bad API of iconv()
154 size_t in_len = strlen(in_ptr);
155 size_t out_len = 2*in_len + 2; // Worst case (including the BOM)
156 char out_buf[out_len];
157 char *out_ptr = out_buf;
158 size_t res = iconv(conv, &in_ptr, &in_len, &out_ptr, &out_len);
159 if (res == (size_t) -1)
160 die("iconv failed: %m");
162 die("iconv stopped before the end of input");
166 // Package UTF-16 in a PDF string
170 for (char *p = out_buf; p < out_ptr; p++)
172 return QPDFObjectHandle::newString(out);
175 /*** Conversion of pages to XObjects ***/
177 static BBox get_trim_box(QPDFObjectHandle page)
179 static const char * const boxes[] = { "/TrimBox", "/CropBox", "/MediaBox", NULL };
180 for (int i=0; boxes[i]; i++)
181 if (page.hasKey(boxes[i]))
182 return BBox(page.getKey(boxes[i]));
183 warn("Page has no trimbox, falling back to A4");
184 return BBox(0, 0, A4_WIDTH, A4_HEIGHT);
187 /* Conversion of pages to XObjects is inspired by CUPS's pdftopdf filter. */
188 class CombineFromContents_Provider : public QPDFObjectHandle::StreamDataProvider {
190 vector<QPDFObjectHandle> contents;
192 CombineFromContents_Provider(const vector<QPDFObjectHandle> &contents) : contents(contents) { }
193 void provideStreamData(int objid UNUSED, int generation UNUSED, Pipeline* pipeline) {
194 Pl_Concatenate concat("concat", pipeline);
195 for (int i=0; i < (int)contents.size(); i++)
196 contents[i].pipeStreamData(&concat, true, false, false);
197 concat.manualFinish();
201 QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page)
203 page.assertPageObject();
205 QPDFObjectHandle xo_stream = QPDFObjectHandle::newStream(out);
206 QPDFObjectHandle xo_dict = xo_stream.getDict();
208 xo_dict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
209 xo_dict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
210 xo_dict.replaceKey("/FormType", QPDFObjectHandle::newInteger(1));
212 BBox box = get_trim_box(page);
213 xo_dict.replaceKey("/BBox", box.to_array());
215 xo_dict.replaceKey("/Resources", page.getKey("/Resources"));
216 if (page.hasKey("/Group"))
217 xo_dict.replaceKey("/Group", page.getKey("/Group"));
219 if (page.hasKey("/UserUnit")) {
220 double u = page.getKey("/UserUnit").getNumericValue();
221 QPDFObjectHandle m = QPDFObjectHandle::newArray();
222 m.appendItem(QPDFObjectHandle::newReal(u, 3));
223 m.appendItem(QPDFObjectHandle::newReal(0, 0));
224 m.appendItem(QPDFObjectHandle::newReal(0, 0));
225 m.appendItem(QPDFObjectHandle::newReal(u, 3));
226 m.appendItem(QPDFObjectHandle::newReal(0, 0));
227 m.appendItem(QPDFObjectHandle::newReal(0, 0));
228 xo_dict.replaceKey("/Matrix", m);
231 vector<QPDFObjectHandle> contents = page.getPageContents();
232 auto ph = PointerHolder<QPDFObjectHandle::StreamDataProvider>(new CombineFromContents_Provider(contents));
233 xo_stream.replaceStreamData(ph, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
237 /*** Formatting of coordinates ***/
239 string pdf_coord(double x, uint digits)
242 snprintf(buf, sizeof(buf), "%.*f", digits, x);
244 while (n > 0 && buf[n-1] == '0')
246 if (n > 0 && buf[n-1] == '.')