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++) {
25 s += pdf_coord(m[i], 6);
30 /*** Bounding boxes ***/
32 QPDFObjectHandle BBox::to_array()
34 QPDFObjectHandle a = QPDFObjectHandle::newArray();
35 a.appendItem(QPDFObjectHandle::newReal(x_min, 1));
36 a.appendItem(QPDFObjectHandle::newReal(y_min, 1));
37 a.appendItem(QPDFObjectHandle::newReal(x_max, 1));
38 a.appendItem(QPDFObjectHandle::newReal(y_max, 1));
42 string BBox::to_rect()
45 pdf_coord(x_min) + " " +
46 pdf_coord(y_min) + " " +
47 pdf_coord(width()) + " " +
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 BBox BBox::transformed(pdf_matrix &m)
94 void BBox::join(BBox &with)
96 x_min = min(x_min, with.x_min);
97 x_max = max(x_max, with.x_max);
98 y_min = min(y_min, with.y_min);
99 y_max = max(y_max, with.y_max);
102 /*** Unicode strings ***/
104 // Construct PDF representation of a UTF-8 string
105 QPDFObjectHandle unicode_string(string s)
107 // If it is ASCII only, use the string directly
108 bool ascii_only = true;
110 if (c < 0x20 || c > 0x7e)
113 return QPDFObjectHandle::newString(s);
115 // Use iconv to convert the string to big-endian UTF-16
116 iconv_t conv = iconv_open("UTF-16BE", "UTF-8");
117 if (conv == (iconv_t) -1)
118 die("Cannot initialize iconv: %m");
120 char *in_ptr = (char *) s.c_str(); // Work around bad API of iconv()
121 size_t in_len = strlen(in_ptr);
122 size_t out_len = 2*in_len + 2; // Worst case (including the BOM)
123 char out_buf[out_len];
124 char *out_ptr = out_buf;
125 size_t res = iconv(conv, &in_ptr, &in_len, &out_ptr, &out_len);
126 if (res == (size_t) -1)
127 die("iconv failed: %m");
129 die("iconv stopped before the end of input");
133 // Package UTF-16 in a PDF string
137 for (char *p = out_buf; p < out_ptr; p++)
139 return QPDFObjectHandle::newString(out);
142 /*** Conversion of pages to XObjects ***/
144 static BBox get_trim_box(QPDFObjectHandle page)
146 static const char * const boxes[] = { "/TrimBox", "/CropBox", "/MediaBox", NULL };
147 for (int i=0; boxes[i]; i++)
148 if (page.hasKey(boxes[i]))
149 return BBox(page.getKey(boxes[i]));
150 warn("Page has no trimbox, falling back to A4");
151 return BBox(0, 0, A4_WIDTH, A4_HEIGHT);
154 /* Conversion of pages to XObjects is inspired by CUPS's pdftopdf filter. */
155 class CombineFromContents_Provider : public QPDFObjectHandle::StreamDataProvider {
157 vector<QPDFObjectHandle> contents;
159 CombineFromContents_Provider(const vector<QPDFObjectHandle> &contents) : contents(contents) { }
160 void provideStreamData(int objid UNUSED, int generation UNUSED, Pipeline* pipeline) {
161 Pl_Concatenate concat("concat", pipeline);
162 for (int i=0; i < (int)contents.size(); i++)
163 contents[i].pipeStreamData(&concat, true, false, false);
164 concat.manualFinish();
168 QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page)
170 page.assertPageObject();
172 QPDFObjectHandle xo_stream = QPDFObjectHandle::newStream(out);
173 QPDFObjectHandle xo_dict = xo_stream.getDict();
175 xo_dict.replaceKey("/Type", QPDFObjectHandle::newName("/XObject"));
176 xo_dict.replaceKey("/Subtype", QPDFObjectHandle::newName("/Form"));
177 xo_dict.replaceKey("/FormType", QPDFObjectHandle::newInteger(1));
179 BBox box = get_trim_box(page);
180 xo_dict.replaceKey("/BBox", box.to_array());
182 xo_dict.replaceKey("/Resources", page.getKey("/Resources"));
183 if (page.hasKey("/Group"))
184 xo_dict.replaceKey("/Group", page.getKey("/Group"));
186 if (page.hasKey("/UserUnit")) {
187 double u = page.getKey("/UserUnit").getNumericValue();
188 QPDFObjectHandle m = QPDFObjectHandle::newArray();
189 m.appendItem(QPDFObjectHandle::newReal(u, 3));
190 m.appendItem(QPDFObjectHandle::newReal(0, 0));
191 m.appendItem(QPDFObjectHandle::newReal(0, 0));
192 m.appendItem(QPDFObjectHandle::newReal(u, 3));
193 m.appendItem(QPDFObjectHandle::newReal(0, 0));
194 m.appendItem(QPDFObjectHandle::newReal(0, 0));
195 xo_dict.replaceKey("/Matrix", m);
198 vector<QPDFObjectHandle> contents = page.getPageContents();
199 auto ph = PointerHolder<QPDFObjectHandle::StreamDataProvider>(new CombineFromContents_Provider(contents));
200 xo_stream.replaceStreamData(ph, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
204 /*** Formatting of coordinates ***/
206 string pdf_coord(double x, uint digits)
209 snprintf(buf, sizeof(buf), "%.*f", digits, x);