]> mj.ucw.cz Git - paperjam.git/commitdiff
Select works
authorMartin Mares <mj@ucw.cz>
Sun, 1 Apr 2018 11:36:42 +0000 (13:36 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 1 Apr 2018 11:36:42 +0000 (13:36 +0200)
TODO
cmds.cc
jam.h
paperjam.cc

diff --git a/TODO b/TODO
index c2e744d166d78d2dbfa9d70b48d59acbd68e93c1..29db5d501a2da8a2c34b59a2078b607d19ca0517 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,3 +1,4 @@
 - Integrate pdf-tools.cc with the rest of the code
 - What if an input page specifies /Rotate?
 - Better error messages from instantiation
+- Select should work without commands in the pipeline
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    }
 };
diff --git a/jam.h b/jam.h
index b384ddde38a8d3cff2736dec4153a9df0cdad7a6..f4235dd332e1dc0084cf9c907a1b08f7fa7b00c6 100644 (file)
--- a/jam.h
+++ b/jam.h
@@ -85,6 +85,10 @@ struct pipeline {
   vector<pipeline_branch *> branches;
 };
 
+// paperjam.cc
+
+vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages);
+
 // parse.cc
 
 void parse(const char *in, list<cmd *> &cmds);
index f81cc2b09ce7550e50719ccc54947c9de6196d70..07ae6b4289d40f460bc054373c7bcd3c9d11ac7a 100644 (file)
@@ -46,13 +46,32 @@ void in_page::render(page_out *out, pdf_matrix xform)
   out->contents += "q " + m.to_string() + " cm " + xobj_res + " Do Q";
 }
 
+static int run_indent;
+
 static void debug_pages(vector<page *> &pages)
 {
   if (!debug_mode)
     return;
 
   for (auto pg: pages)
-    debug("Page #%d: w=%.3f h=%.3f", pg->index, pg->width, pg->height);
+    debug("%*sPage #%d: w=%.3f h=%.3f", run_indent, "", pg->index, pg->width, pg->height);
+}
+
+vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
+{
+  debug("%*s# Input", run_indent, "");
+  debug_pages(pages);
+
+  for (auto c: cmds)
+    {
+      debug("%*s# Executing %s", run_indent, "", c->def->name);
+      run_indent += 4;
+      pages = c->exec->process(pages);
+      run_indent -= 4;
+      debug_pages(pages);
+    }
+
+  return pages;
 }
 
 static void process(list<cmd *> &cmds, const char *in_name, const char *out_name)
@@ -69,15 +88,8 @@ static void process(list<cmd *> &cmds, const char *in_name, const char *out_name
   int cnt = 0;
   for (auto inpg: in_pages)
     pages.push_back(new in_page(inpg, ++cnt));
-  debug("# Input document");
-  debug_pages(pages);
 
-  for (auto c: cmds)
-    {
-      debug("# Executing %s", c->def->name);
-      pages = c->exec->process(pages);
-      debug_pages(pages);
-    }
+  pages = run_command_list(cmds, pages);
 
   for (auto pg: pages)
     {