]> mj.ucw.cz Git - paperjam.git/commitdiff
Implemented merge
authorMartin Mares <mj@ucw.cz>
Tue, 3 Apr 2018 18:18:01 +0000 (20:18 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 3 Apr 2018 18:18:01 +0000 (20:18 +0200)
cmds.cc
jam.h
paperjam.cc
pdf-tools.cc
pdf-tools.h

diff --git a/cmds.cc b/cmds.cc
index 1c9dcd5449e35b496045610b509405a19b54f864..4212ddf6373783789117ecda0fff3f6d2288af3b 100644 (file)
--- 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<page *> draw_bbox_cmd::process(vector<page *> &pages)
   return out;
 }
 
+/*** merge ***/
+
+class merge_cmd : public cmd_exec {
+public:
+  merge_cmd(cmd *c UNUSED) { }
+  vector<page *> process(vector<page *> &pages);
+};
+
+class merge_page : public page {
+  vector<page *> orig_pages;
+public:
+  merge_page(vector<page *> &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<page *> merge_cmd::process(vector<page *> &pages)
+{
+  vector<page *> out;
+  if (pages.size())
+    out.push_back(new merge_page(pages));
+  return out;
+}
+
 /*** Command table ***/
 
 template<typename T> cmd_exec *ctor(cmd *c) { return new T(c); }
@@ -398,5 +445,6 @@ const cmd_def cmd_table[] = {
   { "apply",   no_args,        1,      &ctor<apply_cmd>        },
   { "modulo",  modulo_args,    1,      &ctor<modulo_cmd>       },
   { "draw_bbox",no_args,       0,      &ctor<draw_bbox_cmd>    },
+  { "merge",   no_args,        0,      &ctor<merge_cmd>        },
   { NULL,      NULL,           0,      NULL    }
 };
diff --git a/jam.h b/jam.h
index 47d80be2e534b5d811958d27c8225fd3d2888e00..0c52b3b42db0f574c3088557a8bc358331033d85 100644 (file)
--- 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 {
index c8e0a4f003a2af2557de026871668970dc590722..284cf5fe0dc26e1bf9d2753d686d0e6923b14405 100644 (file)
@@ -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 }
 };
 
index 136fe99d72b2037fea0e19ddd82b9a38fdd5a774..29e79bff9f835e4ca10eb595883f7cca09eaee6c 100644 (file)
@@ -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
index 18aac2476761ef8ed7ff31674d154eb1fe60e272..132062d16135d2293bd7b6581ab6d3ec6c1a386f 100644 (file)
@@ -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);
 };