]> mj.ucw.cz Git - paperjam.git/commitdiff
Bits of commands
authorMartin Mares <mj@ucw.cz>
Sat, 31 Mar 2018 22:37:42 +0000 (00:37 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 31 Mar 2018 22:37:42 +0000 (00:37 +0200)
cmds.cc
jam.h
parse.cc

diff --git a/cmds.cc b/cmds.cc
index 94ee09e6490ac41e802c15e01e482605d0919ecf..10dd4e0a17d4cc3f40eecd86128f0e6d73da8585 100644 (file)
--- a/cmds.cc
+++ b/cmds.cc
@@ -7,6 +7,7 @@
 /*** null ***/
 
 class null_cmd : public cmd_exec {
+    vector<page *> process(vector<page *> pages) { return pages; }
 };
 
 static const arg_def null_args[] = {
@@ -23,12 +24,37 @@ static cmd_exec *null_ctor(cmd *c UNUSED)
 class move_cmd : public cmd_exec {
 public:
   double x, y;
+  vector<page *> process(vector<page *> pages);
 };
 
+class xform_page : public page {
+  page *orig_page;
+public:
+  pdf_matrix xform;
+  void render(page_out *out, pdf_matrix xform);
+  xform_page(page *_orig, double _w, double _h) : page(_w, _h), orig_page(_orig) { }
+};
+
+void xform_page::render(page_out *out, pdf_matrix parent_xform)
+{
+  orig_page->render(out, xform * parent_xform);
+}
+
+vector<page *> move_cmd::process(vector<page *> pages)
+{
+  vector<page *> out;
+  for (auto p: pages)
+    {
+      xform_page *q = new xform_page(p, p->width, p->height);
+      q->xform.shift(x, y);
+      out.push_back(q);
+    }
+  return out;
+}
+
 static const arg_def move_args[] = {
   { "x",       AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
   { "y",       AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
-  { "str",     AT_STRING },
   { NULL,      0 }
 };
 
@@ -40,10 +66,45 @@ static cmd_exec *move_ctor(cmd *c)
   return m;
 }
 
+/*** scale ***/
+
+class scale_cmd : public cmd_exec {
+public:
+  double x_factor, y_factor;
+  vector<page *> process(vector<page *> pages);
+};
+
+vector<page *> scale_cmd::process(vector<page *> pages)
+{
+  vector<page *> out;
+  for (auto p: pages)
+    {
+      xform_page *q = new xform_page(p, x_factor*p->width, y_factor*p->height);
+      q->xform.scale(x_factor, y_factor);
+      out.push_back(q);
+    }
+  return out;
+}
+
+static const arg_def scale_args[] = {
+  { "x",       AT_DOUBLE | AT_MANDATORY | AT_POSITIONAL },
+  { "y",       AT_DOUBLE | AT_POSITIONAL },
+  { NULL,      0 }
+};
+
+static cmd_exec *scale_ctor(cmd *c)
+{
+  scale_cmd *s = new scale_cmd;
+  s->x_factor = c->args.at(0)->double_default(1);
+  s->y_factor = c->args.at(1)->double_default(s->x_factor);
+  return s;
+}
+
 /*** Command table ***/
 
 const cmd_def cmd_table[] = {
-  { "move",    move_args,      1,      move_ctor       },
+  { "move",    move_args,      0,      move_ctor       },
+  { "scale",   scale_args,     0,      scale_ctor      },
   { "null",    null_args,      0,      null_ctor       },
   { NULL,      NULL,           0,      NULL    }
 };
diff --git a/jam.h b/jam.h
index 6fa314ceaa77308aa01b66cae6fb943054a81287..f5d2ca1d5e5a9c122cf86b44bd734dd593ddc280 100644 (file)
--- a/jam.h
+++ b/jam.h
@@ -43,10 +43,12 @@ struct page_out {
 struct page {
   double width;
   double height;
-  void render(page_out *out, pdf_matrix xform);
+  virtual void render(page_out *out UNUSED, pdf_matrix xform UNUSED) { abort(); }
+  page(double _w, double _h) : width(_w), height(_h) { }
 };
 
 struct cmd_exec {
+  virtual vector<page *> process(vector <page *> pages UNUSED) { abort(); }
 };
 
 struct cmd_def {
index 576795d3466c436572f07e834cc6aacc102cf831..c0b8a3542439f475f8de92941a3d061cd682a68f 100644 (file)
--- a/parse.cc
+++ b/parse.cc
@@ -425,6 +425,19 @@ static void parse_commands(list<cmd *> *cmds)
     }
 }
 
+static void instantiate(list<cmd *> *cmds)
+{
+  for (auto c: *cmds)
+    {
+      c->exec = c->def->constructor(c);
+      if (c->pipe)
+       {
+         for (auto pb: c->pipe->branches)
+           instantiate(&pb->commands);
+       }
+    }
+}
+
 void parse(const char *in, list<cmd *> *cmds)
 {
   in_pos = in;
@@ -433,4 +446,5 @@ void parse(const char *in, list<cmd *> *cmds)
     parse_error("Extra tokens after commands");
 
   debug_cmds(cmds);
+  instantiate(cmds);
 }