2 * Auxiliary functions for processing PDF files
4 * (c) 2018 Martin Mares <mj@ucw.cz>
14 #include <qpdf/QUtil.hh>
15 #include <qpdf/Pl_Concatenate.hh>
17 /*** Transformation matrices ***/
19 // Construct string representation of a transformation matrix
20 string pdf_matrix::to_string() {
22 for (int i=0; i<6; i++) {
26 snprintf(buf, sizeof(buf), "%.3f", m[i]);
32 /*** Bounding boxes ***/
34 QPDFObjectHandle BBox::to_array()
36 QPDFObjectHandle a = QPDFObjectHandle::newArray();
37 a.appendItem(QPDFObjectHandle::newReal(x_min, 1));
38 a.appendItem(QPDFObjectHandle::newReal(y_min, 1));
39 a.appendItem(QPDFObjectHandle::newReal(x_max, 1));
40 a.appendItem(QPDFObjectHandle::newReal(y_max, 1));
44 string BBox::to_rect()
47 snprintf(buf, sizeof(buf), "%.1f %.1f %.1f %.1f", x_min, y_min, width(), height());
51 bool BBox::parse(QPDFObjectHandle h)
53 if (!h.isArray() || h.getArrayNItems() != 4)
56 for (int i=0; i<4; i++) {
57 QPDFObjectHandle item = h.getArrayItem(i);
60 x[i] = item.getNumericValue();
69 BBox::BBox(QPDFObjectHandle box) {
71 warn("Invalid bounding box, falling back to A4");
72 x_min = 0, x_max = A4_WIDTH;
73 y_min = 0, y_max = A4_HEIGHT;
77 void BBox::transform(pdf_matrix &m)
79 m.apply(&x_min, &y_min);
80 m.apply(&x_max, &y_max);
87 /*** Unicode strings ***/
89 // Construct PDF representation of a UTF-8 string
90 QPDFObjectHandle unicode_string(string s)
92 // If it is ASCII only, use the string directly
93 bool ascii_only = true;
95 if (c < 0x20 || c > 0x7e)
98 return QPDFObjectHandle::newString(s);
100 // Use iconv to convert the string to big-endian UTF-16
101 iconv_t conv = iconv_open("UTF-16BE", "UTF-8");
102 if (conv == (iconv_t) -1)
103 die("Cannot initialize iconv: %m");
105 char *in_ptr = (char *) s.c_str(); // Work around bad API of iconv()
106 size_t in_len = strlen(in_ptr);
107 size_t out_len = 2*in_len + 2; // Worst case (including the BOM)
108 char out_buf[out_len];
109 char *out_ptr = out_buf;
110 size_t res = iconv(conv, &in_ptr, &in_len, &out_ptr, &out_len);
111 if (res == (size_t) -1)
112 die("iconv failed: %m");
114 die("iconv stopped before the end of input");
118 // Package UTF-16 in a PDF string
122 for (char *p = out_buf; p < out_ptr; p++)
124 return QPDFObjectHandle::newString(out);
127 /*** Conversion of pages to XObjects ***/
129 static BBox get_trim_box(QPDFObjectHandle page)
131 static const char * const boxes[] = { "/TrimBox", "/CropBox", "/MediaBox", NULL };
132 for (int i=0; boxes[i]; i++)
133 if (page.hasKey(boxes[i]))
134 return BBox(page.getKey(boxes[i]));
135 warn("Page has no trimbox, falling back to A4");
136 return BBox(0, 0, A4_WIDTH, A4_HEIGHT);
139 /* Conversion of pages to XObjects is inspired by CUPS's pdftopdf filter. */
140 class CombineFromContents_Provider : public QPDFObjectHandle::StreamDataProvider {
142 vector<QPDFObjectHandle> contents;
144 CombineFromContents_Provider(const vector<QPDFObjectHandle> &contents) : contents(contents) { }
145 void provideStreamData(int objid UNUSED, int generation UNUSED, Pipeline* pipeline) {
146 Pl_Concatenate concat("concat", pipeline);
147 for (int i=0; i < (int)contents.size(); i++)
148 contents[i].pipeStreamData(&concat, true, false, false);
149 concat.manualFinish();
153 QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page)
155 page.assertPageObject();
157 QPDFObjectHandle xo_stream = QPDFObjectHandle::newStream(out);
158 QPDFObjectHandle xo_dict = xo_stream.getDict();
160 xo_dict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
161 xo_dict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
162 xo_dict.replaceKey("/FormType", QPDFObjectHandle::newInteger(1));
164 BBox box = get_trim_box(page);
165 xo_dict.replaceKey("/BBox", box.to_array());
167 xo_dict.replaceKey("/Resources", page.getKey("/Resources"));
168 if (page.hasKey("/Group"))
169 xo_dict.replaceKey("/Group", page.getKey("/Group"));
171 if (page.hasKey("/UserUnit")) {
172 double u = page.getKey("/UserUnit").getNumericValue();
173 QPDFObjectHandle m = QPDFObjectHandle::newArray();
174 m.appendItem(QPDFObjectHandle::newReal(u, 3));
175 m.appendItem(QPDFObjectHandle::newReal(0, 0));
176 m.appendItem(QPDFObjectHandle::newReal(0, 0));
177 m.appendItem(QPDFObjectHandle::newReal(u, 3));
178 m.appendItem(QPDFObjectHandle::newReal(0, 0));
179 m.appendItem(QPDFObjectHandle::newReal(0, 0));
180 xo_dict.replaceKey("/Matrix", m);
183 vector<QPDFObjectHandle> contents = page.getPageContents();
184 auto ph = PointerHolder<QPDFObjectHandle::StreamDataProvider>(new CombineFromContents_Provider(contents));
185 xo_stream.replaceStreamData(ph, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());