]> mj.ucw.cz Git - paperjam.git/blobdiff - cmds.cc
Store duplicate pages only once
[paperjam.git] / cmds.cc
diff --git a/cmds.cc b/cmds.cc
index 34fd25abd9bdc9b4109bd32173d4b13ba938a475..fa4f615fadeedf28b62aa917422dca6927b4c8f8 100644 (file)
--- a/cmds.cc
+++ b/cmds.cc
@@ -10,7 +10,7 @@ class null_cmd : public cmd_exec {
   vector<page *> process(vector<page *> &pages) { return pages; }
 };
 
-static const arg_def null_args[] = {
+static const arg_def no_args[] = {
   { NULL,      0 }
 };
 
@@ -156,12 +156,58 @@ static cmd_exec *rotate_ctor(cmd *c)
   return r;
 }
 
+/*** select ***/
+
+class select_cmd : public cmd_exec {
+public:
+  pipeline *pipe;
+  vector<page *> process(vector<page *> &pages);
+};
+
+static int validate_page_index(vector<page *> &pages, int idx)
+{
+  if (idx >= 1 && idx <= (int) pages.size())
+    return idx - 1;
+  if (idx <= -1 && idx >= (int) -pages.size())
+    return idx + pages.size();
+  die("Page index %d out of range", idx);
+}
+
+vector<page *> select_cmd::process(vector<page *> &pages)
+{
+  vector<page *> out;
+  for (auto pb: pipe->branches)
+    for (auto ps: pb->selectors)
+      {
+       int f = validate_page_index(pages, ps.from);
+       int t = validate_page_index(pages, ps.to);
+       int step = (f <= t) ? 1 : -1;
+       for (int i=f; f<=t; f += step)
+         {
+           vector<page *> selected;
+           selected.push_back(pages[i]);
+           selected = run_command_list(pb->commands, selected);
+           for (auto p: selected)
+             out.push_back(p);
+         }
+      }
+  return out;
+}
+
+static cmd_exec *select_ctor(cmd *c)
+{
+  select_cmd *r = new select_cmd;
+  r->pipe = c->pipe;
+  return r;
+}
+
 /*** Command table ***/
 
 const cmd_def cmd_table[] = {
+  { "null",    no_args,        0,      null_ctor       },
   { "move",    move_args,      0,      move_ctor       },
   { "scale",   scale_args,     0,      scale_ctor      },
   { "rotate",  rotate_args,    0,      rotate_ctor     },
-  { "null",    null_args,      0,      null_ctor       },
+  { "select",  no_args,        1,      select_ctor     },
   { NULL,      NULL,           0,      NULL    }
 };