X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=pdf-tools.cc;h=809238c3b662e6138f55eecd2f968584c61e89c5;hb=a4b921dcbcc63c6ac28949d16be531f2f4465eab;hp=9d89ad8153e7d02f3a160346cfea38b5d3718863;hpb=0067f5b7bd14a37261df30bfcd5ca71ed2c1a38a;p=paperjam.git diff --git a/pdf-tools.cc b/pdf-tools.cc index 9d89ad8..809238c 100644 --- a/pdf-tools.cc +++ b/pdf-tools.cc @@ -4,69 +4,16 @@ * (c) 2018 Martin Mares */ -#include #include #include #include -using namespace std; - -#include "pdf-tools.h" +#include "jam.h" #include #include -/*** Messages ***/ - -int debug_mode; -int debug_indent; - -void debug(const char *msg, ...) -{ - if (!debug_mode) - return; - va_list args; - va_start(args, msg); - fprintf(stderr, "%*s", debug_indent, ""); - vfprintf(stderr, msg, args); - fputc('\n', stderr); - va_end(args); -} - -void warn(const char *msg, ...) -{ - va_list args; - va_start(args, msg); - fprintf(stderr, "WARNING: "); - vfprintf(stderr, msg, args); - fputc('\n', stderr); - va_end(args); -} - -void die(const char *msg, ...) -{ - va_list args; - va_start(args, msg); - fprintf(stderr, "ERROR: "); - vfprintf(stderr, msg, args); - fputc('\n', stderr); - va_end(args); - exit(1); -} - -void bad(const char *msg, ...) -{ - va_list args; - va_start(args, msg); - char buf[1024]; - vsnprintf(buf, sizeof(buf), msg, args); - va_end(args); - - printf("error: %s\n", buf); - die("BAD: %s", buf); -} - /*** Transformation matrices ***/ // Construct string representation of a transformation matrix @@ -75,9 +22,7 @@ string pdf_matrix::to_string() { for (int i=0; i<6; i++) { if (i) s += " "; - char buf[16]; - snprintf(buf, sizeof(buf), "%.3f", m[i]); - s += buf; + s += pdf_coord(m[i], 6); } return s; } @@ -94,6 +39,15 @@ QPDFObjectHandle BBox::to_array() return a; } +string BBox::to_rect() +{ + return + pdf_coord(x_min) + " " + + pdf_coord(y_min) + " " + + pdf_coord(width()) + " " + + pdf_coord(height()); +} + bool BBox::parse(QPDFObjectHandle h) { if (!h.isArray() || h.getArrayNItems() != 4) @@ -112,6 +66,54 @@ bool BBox::parse(QPDFObjectHandle h) return true; } +BBox::BBox(QPDFObjectHandle box) { + if (!parse(box)) { + warn("Invalid bounding box, falling back to A4"); + x_min = 0, x_max = A4_WIDTH; + y_min = 0, y_max = A4_HEIGHT; + } +} + +void BBox::transform(pdf_matrix &m) +{ + m.apply(&x_min, &y_min); + m.apply(&x_max, &y_max); + if (x_max < x_min) + swap(x_min, x_max); + if (y_max < y_min) + swap(y_min, y_max); +} + +BBox BBox::transformed(pdf_matrix &m) +{ + BBox b = *this; + b.transform(m); + return b; +} + +void BBox::join(BBox &with) +{ + x_min = min(x_min, with.x_min); + x_max = max(x_max, with.x_max); + y_min = min(y_min, with.y_min); + y_max = max(y_max, with.y_max); +} + +void BBox::enlarge(double by) +{ + x_min -= by; + x_max += by; + y_min -= by; + y_max += by; +} + +BBox BBox::enlarged(double by) +{ + BBox b = *this; + b.enlarge(by); + return b; +} + /*** Unicode strings ***/ // Construct PDF representation of a UTF-8 string @@ -213,3 +215,17 @@ QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page) xo_stream.replaceStreamData(ph, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull()); return xo_stream; } + +/*** Formatting of coordinates ***/ + +string pdf_coord(double x, uint digits) +{ + char buf[16]; + snprintf(buf, sizeof(buf), "%.*f", digits, x); + int n = strlen(buf); + while (n > 0 && buf[n-1] == '0') + buf[--n] = 0; + if (n > 0 && buf[n-1] == '.') + buf[--n] = 0; + return buf; +}