]> mj.ucw.cz Git - paperjam.git/blobdiff - cmds.cc
Implemented book
[paperjam.git] / cmds.cc
diff --git a/cmds.cc b/cmds.cc
index 65a9c4159ac534c96686459ac2faca8a75207482..a0e0548eb3fb1ec3d5d814b7b464eeecfb7cb012 100644 (file)
--- a/cmds.cc
+++ b/cmds.cc
@@ -789,6 +789,52 @@ static const arg_def add_blank_args[] = {
   { NULL,      0 }
 };
 
+/*** book ***/
+
+class book_cmd : public cmd_exec {
+  int n;
+public:
+  book_cmd(cmd *c)
+    {
+      n = c->arg("n")->as_int(0);
+      if (n % 4)
+        die("Number of pages per signature must be divisible by 4");
+    }
+  vector<page *> process(vector<page *> &pages) override;
+};
+
+vector<page *> book_cmd::process(vector<page *> &pages)
+{
+  vector<page *> in, out;
+
+  in = pages;
+  while (in.size() % 4)
+    in.push_back(new empty_page(in[0]->width, in[0]->height));
+
+  int i = 0;
+  while (i < (int) in.size())
+    {
+      int sig = in.size() - i;
+      if (n)
+       sig = min(sig, n);
+      for (int j=0; j<sig/2; j+=2)
+       {
+         out.push_back(in[i + sig-1-j]);
+         out.push_back(in[i + j]);
+         out.push_back(in[i + j+1]);
+         out.push_back(in[i + sig-2-j]);
+       }
+      i += sig;
+    }
+
+  return out;
+}
+
+static const arg_def book_args[] = {
+  { "n",       AT_INT | AT_POSITIONAL },
+  { NULL,      0 }
+};
+
 /*** Command table ***/
 
 template<typename T> cmd_exec *ctor(cmd *c) { return new T(c); }
@@ -810,5 +856,6 @@ const cmd_def cmd_table[] = {
   { "expand",  expand_args,    0,      &ctor<expand_cmd>       },
   { "margins", margins_args,   0,      &ctor<margins_cmd>      },
   { "add-blank",add_blank_args,        0,      &ctor<add_blank_cmd>    },
+  { "book",    book_args,      0,      &ctor<book_cmd>         },
   { NULL,      NULL,           0,      NULL    }
 };