From: Martin Mares Date: Tue, 3 Apr 2018 18:18:01 +0000 (+0200) Subject: Implemented merge X-Git-Tag: v0.1~27 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=65a19b7c5297c12b66fc870e01e5c6ac7bf734fc;p=paperjam.git Implemented merge --- diff --git a/cmds.cc b/cmds.cc index 1c9dcd5..4212ddf 100644 --- a/cmds.cc +++ b/cmds.cc @@ -361,7 +361,7 @@ public: class draw_bbox_page : public page { page *orig_page; public: - void render(out_context *out, pdf_matrix xform); + void render(out_context *out, pdf_matrix xform) override; draw_bbox_page(page *p) : page(p) { orig_page = p; } }; @@ -384,6 +384,53 @@ vector draw_bbox_cmd::process(vector &pages) return out; } +/*** merge ***/ + +class merge_cmd : public cmd_exec { +public: + merge_cmd(cmd *c UNUSED) { } + vector process(vector &pages); +}; + +class merge_page : public page { + vector orig_pages; +public: + merge_page(vector &orig) : page(0, 0) + { + orig_pages = orig; + bool first = true; + for (auto p: orig) + { + if (first) + { + width = p->width; + height = p->height; + bbox = p->bbox; + first = false; + } + else + { + if (abs(width-p->width) > 0.001 || abs(height-p->height) > 0.001) + die("All pages participating in a merge must have the same dimensions"); + bbox.join(p->bbox); + } + } + } + void render(out_context *out, pdf_matrix xform) override + { + for (auto p: orig_pages) + p->render(out, xform); + } +}; + +vector merge_cmd::process(vector &pages) +{ + vector out; + if (pages.size()) + out.push_back(new merge_page(pages)); + return out; +} + /*** Command table ***/ template cmd_exec *ctor(cmd *c) { return new T(c); } @@ -398,5 +445,6 @@ const cmd_def cmd_table[] = { { "apply", no_args, 1, &ctor }, { "modulo", modulo_args, 1, &ctor }, { "draw_bbox",no_args, 0, &ctor }, + { "merge", no_args, 0, &ctor }, { NULL, NULL, 0, NULL } }; diff --git a/jam.h b/jam.h index 47d80be..0c52b3b 100644 --- a/jam.h +++ b/jam.h @@ -65,13 +65,14 @@ struct page { double height; BBox bbox; // Bounds useful contents virtual void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { abort(); } - page(double _w=0, double _h=0) : width(_w), height(_h) { } - page(page *p) { - index = p->index; - width = p->width; - height = p->height; - bbox = p->bbox; - } + page(double _w=0, double _h=0) : index(0), width(_w), height(_h), bbox() { } + page(page *p) + { + index = p->index; + width = p->width; + height = p->height; + bbox = p->bbox; + } }; struct empty_page : public page { diff --git a/paperjam.cc b/paperjam.cc index c8e0a4f..284cf5f 100644 --- a/paperjam.cc +++ b/paperjam.cc @@ -65,6 +65,7 @@ static const struct option long_opts[] = { { "debug", 0, 0, 'd' }, { "help", 0, 0, OPT_HELP }, { "version", 0, 0, OPT_VERSION }, + { "bbox", 0, 0, 'b' }, { 0, 0, 0, 0 } }; diff --git a/pdf-tools.cc b/pdf-tools.cc index 136fe99..29e79bf 100644 --- a/pdf-tools.cc +++ b/pdf-tools.cc @@ -84,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 diff --git a/pdf-tools.h b/pdf-tools.h index 18aac24..132062d 100644 --- a/pdf-tools.h +++ b/pdf-tools.h @@ -129,6 +129,7 @@ struct BBox { double width() { return x_max - x_min; } double height() { return y_max - y_min; } void transform(pdf_matrix &m); + void join(BBox &with); private: bool parse(QPDFObjectHandle h); };