2 * Auxiliary functions for processing PDF files
4 * (c) 2018 Martin Mares <mj@ucw.cz>
13 #include <qpdf/QPDF.hh>
15 /*** Basic macros and constants ***/
19 static const double mm = 72/25.4;
21 /*** Transformation matrices ***/
25 * A transformation matrix corresponds to the linear transform
28 * (x y 1) * (c d 0) = (ax+cy+e bx+dy+f 1)
31 * We represent the non-trivial coefficients of the matrix by
32 * an array {a,b,c,d,e,f}.
44 pdf_matrix(double a, double b, double c, double d, double e, double f)
54 void apply(double *px, double *py)
57 *px = m[0]*x + m[2]*y + m[4];
58 *py = m[1]*x + m[3]*y + m[5];
61 // A*B is a matrix which transforms first by A and then by B
62 pdf_matrix operator *(pdf_matrix y)
65 m[0]*y.m[0] + m[1]*y.m[2],
66 m[0]*y.m[1] + m[1]*y.m[3],
67 m[2]*y.m[0] + m[3]*y.m[2],
68 m[2]*y.m[1] + m[3]*y.m[3],
69 m[4]*y.m[0] + m[5]*y.m[2] + y.m[4],
70 m[4]*y.m[1] + m[5]*y.m[3] + y.m[5]
74 void concat(pdf_matrix y)
76 pdf_matrix t = *this * y;
77 for (int i=0; i<6; i++)
81 void shift(double dx, double dy)
89 concat(pdf_matrix(s, 0, 0, s, 0, 0));
92 void scale(double sx, double sy)
94 concat(pdf_matrix(sx, 0, 0, sy, 0, 0));
97 void rotate_rad(double angle)
99 double c = std::cos(angle), s = std::sin(angle);
100 concat(pdf_matrix(c, s, -s, c, 0, 0));
103 void rotate_deg(double angle)
105 rotate_rad(angle/180. * M_PI);
108 std::string to_string();
111 /*** Bounding boxes ***/
114 double x_min, x_max, y_min, y_max;
116 x_min = y_min = x_max = y_max = 0;
118 BBox(double xmin, double ymin, double xmax, double ymax) {
119 x_min = xmin, x_max = xmax;
120 y_min = ymin, y_max = ymax;
122 BBox(double x, double y) {
123 x_min = 0, x_max = x;
124 y_min = 0, y_max = y;
126 BBox(QPDFObjectHandle box);
127 QPDFObjectHandle to_array();
129 double width() { return x_max - x_min; }
130 double height() { return y_max - y_min; }
131 void transform(pdf_matrix &m);
132 BBox transformed(pdf_matrix &m);
133 void enlarge(double by);
134 BBox enlarged(double by);
135 void join(BBox &with);
137 bool parse(QPDFObjectHandle h);
140 /*** Miscellaneous ***/
142 QPDFObjectHandle unicode_string(std::string s);
143 QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page);
144 string pdf_coord(double x, uint digits=1);