]> mj.ucw.cz Git - paperjam.git/blobdiff - cmds.cc
Implemented expand, margins
[paperjam.git] / cmds.cc
diff --git a/cmds.cc b/cmds.cc
index 3dcf02bf72e24e7ec9dd63038242b07dc9c1ff63..6acb19ce79bebe1719e1aafca156bfcc9d2d2bc4 100644 (file)
--- a/cmds.cc
+++ b/cmds.cc
@@ -209,8 +209,13 @@ public:
     }
 };
 
-#define MARGIN_ARGS(basic, sx)                 \
-  { basic,     AT_DIMEN },             \
+#define MARGIN_ARGS1_NAMED(name)       \
+  { name,      AT_DIMEN }
+
+#define MARGIN_ARGS1_POSNL(name)       \
+  { name,      AT_DIMEN | AT_POSITIONAL }
+
+#define MARGIN_ARGS2(sx)               \
   { "h" sx,    AT_DIMEN },             \
   { "v" sx,    AT_DIMEN },             \
   { "l" sx,    AT_DIMEN },             \
@@ -690,7 +695,59 @@ public:
 static const arg_def fit_args[] = {
   PAPER_ARGS,
   POS_ARGS,
-  MARGIN_ARGS("margin", "margin"),
+  MARGIN_ARGS1_NAMED("margin"),
+  MARGIN_ARGS2("margin"),
+  { NULL,      0 }
+};
+
+/*** expand ***/
+
+class expand_cmd : public cmd_exec_simple {
+  margin_spec marg;
+public:
+  expand_cmd(cmd *c) : marg(c, "by", "") { }
+  page *process_page(page *p) override
+    {
+      pdf_matrix xf;
+      xf.shift(marg.l, marg.b);
+      page *q = new xform_page(p, xf);
+      q->width = p->width + marg.l + marg.r;
+      q->height = p->height + marg.t + marg.b;
+      if (q->width < 0.001 || q->height < 0.001)
+       die("Expansion must result in positive page dimensions");
+      return q;
+    }
+};
+
+static const arg_def expand_args[] = {
+  MARGIN_ARGS1_POSNL("by"),
+  MARGIN_ARGS2(""),
+  { NULL,      0 }
+};
+
+/*** margins ***/
+
+class margins_cmd : public cmd_exec_simple {
+  margin_spec marg;
+public:
+  margins_cmd(cmd *c) : marg(c, "size", "") { }
+  page *process_page(page *p) override
+    {
+      pdf_matrix xf;
+      xf.shift(-p->bbox.x_min, -p->bbox.y_min);
+      xf.shift(marg.l, marg.b);
+      page *q = new xform_page(p, xf);
+      q->width = p->bbox.width() + marg.l + marg.r;
+      q->height = p->bbox.height() + marg.t + marg.b;
+      if (q->width < 0.001 || q->height < 0.001)
+       die("Margins must result in positive page dimensions");
+      return q;
+    }
+};
+
+static const arg_def margins_args[] = {
+  MARGIN_ARGS1_POSNL("size"),
+  MARGIN_ARGS2(""),
   { NULL,      0 }
 };
 
@@ -712,5 +769,7 @@ const cmd_def cmd_table[] = {
   { "paper",   paper_args,     0,      &ctor<paper_cmd>        },
   { "scaleto", scaleto_args,   0,      &ctor<scaleto_cmd>      },
   { "fit",     fit_args,       0,      &ctor<fit_cmd>          },
+  { "expand",  expand_args,    0,      &ctor<expand_cmd>       },
+  { "margins", margins_args,   0,      &ctor<margins_cmd>      },
   { NULL,      NULL,           0,      NULL    }
 };