X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=pdf-tools.cc;h=29e79bff9f835e4ca10eb595883f7cca09eaee6c;hb=356034e375735ec10ea8e0b0b54698c6a297a756;hp=71dde6c9b7744d1d2c0b8176824ba8d519324143;hpb=bc0c38a2fd19e04770b25536e1eb6549b48ce1f3;p=paperjam.git diff --git a/pdf-tools.cc b/pdf-tools.cc index 71dde6c..29e79bf 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 @@ -94,6 +41,13 @@ QPDFObjectHandle BBox::to_array() return a; } +string BBox::to_rect() +{ + char buf[64]; + snprintf(buf, sizeof(buf), "%.1f %.1f %.1f %.1f", x_min, y_min, width(), height()); + return string(buf); +} + bool BBox::parse(QPDFObjectHandle h) { if (!h.isArray() || h.getArrayNItems() != 4) @@ -112,6 +66,14 @@ 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); @@ -122,6 +84,14 @@ void BBox::transform(pdf_matrix &m) swap(y_min, y_max); } +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); +} + /*** Unicode strings ***/ // Construct PDF representation of a UTF-8 string