From: Martin Mares Date: Fri, 6 Apr 2018 21:40:44 +0000 (+0200) Subject: Implemented clip X-Git-Tag: v0.1~8 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=7a6d4cae1346a5075c4b7f6af68ca79cf67d4f14;p=paperjam.git Implemented clip --- diff --git a/TODO b/TODO index aba6a97..2463ead 100644 --- a/TODO +++ b/TODO @@ -43,9 +43,9 @@ | book | signature= -# 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 802e193..f864244 100644 --- 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 cmd_exec *ctor(cmd *c) { return new T(c); } @@ -1407,5 +1436,6 @@ const cmd_def cmd_table[] = { { "book", book_args, 0, &ctor }, { "nup", nup_args, 0, &ctor }, { "cropmarks",cropmarks_args, 0, &ctor }, + { "clip", clip_args, 0, &ctor }, { NULL, NULL, 0, NULL } }; diff --git a/pdf-tools.cc b/pdf-tools.cc index 68a8f8c..809238c 100644 --- a/pdf-tools.cc +++ b/pdf-tools.cc @@ -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 diff --git a/pdf-tools.h b/pdf-tools.h index 0225e9c..de79f53 100644 --- a/pdf-tools.h +++ b/pdf-tools.h @@ -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);