]> mj.ucw.cz Git - paperjam.git/commitdiff
Implemented clip
authorMartin Mares <mj@ucw.cz>
Fri, 6 Apr 2018 21:40:44 +0000 (23:40 +0200)
committerMartin Mares <mj@ucw.cz>
Fri, 6 Apr 2018 21:40:44 +0000 (23:40 +0200)
TODO
cmds.cc
pdf-tools.cc
pdf-tools.h

diff --git a/TODO b/TODO
index aba6a97484089cea0f78179202d42ca1cd694cf8..2463ead6980f9c23815066a41fdb7624740996ea 100644 (file)
--- a/TODO
+++ b/TODO
@@ -43,9 +43,9 @@
 | book
 |      signature=<n>
 
-# Crop to image box
-crop
-       bleed=5mm       # Allow bleeding over the image box
+| # Clip to image box
+| clip
+|      bleed=5mm       # Allow bleeding over the image box
 
 # Can be written as: mix(cat) { modulo(2){1}, modulo(2){2} }
 duplex
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    }
 };
index 68a8f8cae4201373f2de193619a7a338badafd80..809238c3b662e6138f55eecd2f968584c61e89c5 100644 (file)
@@ -99,6 +99,21 @@ void BBox::join(BBox &with)
        y_max = max(y_max, with.y_max);
 }
 
+void BBox::enlarge(double by)
+{
+       x_min -= by;
+       x_max += by;
+       y_min -= by;
+       y_max += by;
+}
+
+BBox BBox::enlarged(double by)
+{
+       BBox b = *this;
+       b.enlarge(by);
+       return b;
+}
+
 /*** Unicode strings ***/
 
 // Construct PDF representation of a UTF-8 string
index 0225e9c457f951806aa07129c9a692ab55856f9e..de79f534565d51476914470bb74755712754975f 100644 (file)
@@ -130,6 +130,8 @@ struct BBox {
        double height() { return y_max - y_min; }
        void transform(pdf_matrix &m);
        BBox transformed(pdf_matrix &m);
+       void enlarge(double by);
+       BBox enlarged(double by);
        void join(BBox &with);
 private:
        bool parse(QPDFObjectHandle h);