]> mj.ucw.cz Git - paperjam.git/blobdiff - cmds.cc
Implemented clip
[paperjam.git] / cmds.cc
diff --git a/cmds.cc b/cmds.cc
index 802e19362a2dd0356de6a48b4e25d360b89e2579..f86424461f5c3798babe65f34265f031bf62035c 100644 (file)
--- a/cmds.cc
+++ b/cmds.cc
@@ -360,11 +360,7 @@ string cropmark_spec::pdf_stream(out_context *out, BBox &box, pdf_matrix &xform)
   out->egstates.replaceKey(egs_res, egstate);
   s += egs_res + " gs ";
 
-  BBox b = box;
-  b.x_min -= offset;
-  b.x_max += offset;
-  b.y_min -= offset;
-  b.y_max += offset;
+  BBox b = box.enlarged(offset);
 
   switch (type)
     {
@@ -1373,7 +1369,6 @@ class cropmarks_cmd : public cmd_exec_simple {
     {
       return new cropmarks_page(p, &cm);
     }
-
 public:
   cropmarks_cmd(cmd *c) : cm(c) { }
 };
@@ -1383,6 +1378,40 @@ static const arg_def cropmarks_args[] = {
   { NULL,      0 }
 };
 
+/*** clip ***/
+
+class clip_page : public page {
+  page *orig_page;
+  BBox clip_to;
+public:
+  void render(out_context *out, pdf_matrix xform) override
+    {
+      out->contents += "q " + clip_to.transformed(xform).to_rect() + " re W n ";
+      orig_page->render(out, xform);
+      out->contents += "Q ";
+    }
+  clip_page(page *p, BBox &to) : page(p), orig_page(p), clip_to(to) { }
+};
+
+class clip_cmd : public cmd_exec_simple {
+  double bleed;
+  page *process_page(page *p) override
+    {
+      BBox to = p->image_box.enlarged(bleed);
+      return new clip_page(p, to);
+    }
+public:
+  clip_cmd(cmd *c)
+    {
+      bleed = c->arg("bleed")->as_double(0);
+    }
+};
+
+static const arg_def clip_args[] = {
+  { "bleed",   AT_DIMEN },
+  { NULL,      0 }
+};
+
 /*** Command table ***/
 
 template<typename T> cmd_exec *ctor(cmd *c) { return new T(c); }
@@ -1407,5 +1436,6 @@ const cmd_def cmd_table[] = {
   { "book",    book_args,      0,      &ctor<book_cmd>         },
   { "nup",     nup_args,       0,      &ctor<nup_cmd>          },
   { "cropmarks",cropmarks_args,        0,      &ctor<cropmarks_cmd>    },
+  { "clip",    clip_args,      0,      &ctor<clip_cmd>         },
   { NULL,      NULL,           0,      NULL    }
 };