From be89479e02074a696be664f0d8cb1c536dacf495 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 20 Aug 2022 13:15:05 +0200 Subject: [PATCH] xform_page() is now a part of pdf.cc It also remembers the name of the transform for better debug messages. --- cmds.cc | 61 ++++++++++++--------------------------------------------- jam.h | 10 ++++++++++ pdf.cc | 29 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/cmds.cc b/cmds.cc index 719058a..a2ac331 100644 --- a/cmds.cc +++ b/cmds.cc @@ -1,7 +1,7 @@ /* * PaperJam -- Commands * - * (c) 2018 Martin Mares + * (c) 2018--2022 Martin Mares */ #include @@ -25,41 +25,6 @@ static const arg_def no_args[] = { /*** Generic routines ***/ -// Transformed page - -class xform_page : public page { - page *orig_page; - pdf_matrix xform; -public: - void render(out_context *out, pdf_matrix xform) override; - void debug_dump() override - { - debug("Transform [%s]", xform.to_string().c_str()); - orig_page->debug_dump(); - } - xform_page(page *p, pdf_matrix xf); -}; - -xform_page::xform_page(page *p, pdf_matrix xf) -{ - orig_page = p; - index = p->index; - xform = xf; - - BBox media(p->width, p->height); - media.transform(xf); - width = media.width(); - height = media.height(); - - image_box = p->image_box; - image_box.transform(xf); -} - -void xform_page::render(out_context *out, pdf_matrix parent_xform) -{ - orig_page->render(out, xform * parent_xform); -} - // Commands acting on individual pages class cmd_exec_simple : public cmd_exec { @@ -434,7 +399,7 @@ public: { pdf_matrix m; m.shift(x, y); - return new xform_page(p, m); + return new xform_page(p, "move", m); } }; @@ -458,7 +423,7 @@ public: { pdf_matrix m; m.scale(x_factor, y_factor); - return new xform_page(p, m); + return new xform_page(p, "scale", m); } }; @@ -483,7 +448,7 @@ public: } page *process_page(page *p) override { - return new xform_page(p, pdf_rotation_matrix(deg, p->width, p->height)); + return new xform_page(p, "rotate", pdf_rotation_matrix(deg, p->width, p->height)); } }; @@ -518,7 +483,7 @@ public: m.scale(-1, 1); m.shift(p->width, 0); } - return new xform_page(p, m); + return new xform_page(p, "flip", m); } }; @@ -797,7 +762,7 @@ public: { BBox paper_box = BBox(paper.w, paper.h); pdf_matrix xf = pos.place(p->image_box, paper_box); - page *q = new xform_page(p, xf); + page *q = new xform_page(p, "paper", xf); q->width = paper.w; q->height = paper.h; return q; @@ -825,7 +790,7 @@ public: xf.scale(scale_to_fit(orig_box, paper_box)); orig_box.transform(xf); xf.concat(pos.place(orig_box, paper_box)); - page *q = new xform_page(p, xf); + page *q = new xform_page(p, "scaleto", xf); q->width = paper.w; q->height = paper.h; return q; @@ -860,7 +825,7 @@ public: xf.scale(scale_to_fit(orig_box, paper_box)); orig_box.transform(xf); xf.concat(pos.place(orig_box, paper_box)); - q = new xform_page(p, xf); + q = new xform_page(p, "fit", xf); q->width = paper.w; q->height = paper.h; } @@ -869,7 +834,7 @@ public: // No paper given: adjust paper to fit image xf.shift(-p->image_box.x_min, -p->image_box.y_min); xf.shift(marg.l, marg.b); - q = new xform_page(p, xf); + q = new xform_page(p, "fit", xf); q->width = p->image_box.width() + marg.l + marg.r; q->height = p->image_box.height() + marg.t + marg.b; } @@ -895,7 +860,7 @@ public: { pdf_matrix xf; xf.shift(marg.l, marg.b); - page *q = new xform_page(p, xf); + page *q = new xform_page(p, "expand", 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) @@ -918,7 +883,7 @@ public: margins_cmd(cmd *c) : marg(c, "size", "") { } page *process_page(page *p) override { - page *q = new xform_page(p, pdf_matrix()); + page *q = new xform_page(p, "margins", pdf_matrix()); q->image_box = BBox(marg.l, marg.t, p->width - marg.r, p->height - marg.b); if (q->image_box.width() < 0.001 || q->image_box.height() < 0.001) err("Margins must result in positive image dimensions"); @@ -1449,7 +1414,7 @@ class common_cmd : public cmd_exec { vector out; for (auto p: pages) { - page *q = new xform_page(p, pdf_matrix()); + page *q = new xform_page(p, "common", pdf_matrix()); q->width = pbox.width(); q->height = pbox.height(); q->image_box = ibox; @@ -1502,7 +1467,7 @@ vector slice_cmd::process(vector &pages) pdf_matrix xf = placement; xf.shift(-c*pw, -(rows-1-r)*ph); xf.shift(margin.l, margin.t); - page *q = new xform_page(p, xf); + page *q = new xform_page(p, "slice", xf); q->width = paper.w; q->height = paper.h; BBox slice_box = BBox(margin.l, margin.t, paper.w - margin.r, paper.h - margin.b); diff --git a/jam.h b/jam.h index ae56333..fa1e13b 100644 --- a/jam.h +++ b/jam.h @@ -175,3 +175,13 @@ void debug_pages(vector &pages); void process(list &cmds); vector run_command_list(list &cmds, vector &pages); vector gs_bboxes(const char *in); + +class xform_page : public page { + page *orig_page; + pdf_matrix xform; + const char *description; +public: + void render(out_context *out, pdf_matrix xform) override; + void debug_dump() override; + xform_page(page *p, const char *desc, pdf_matrix xf); +}; diff --git a/pdf.cc b/pdf.cc index ff7a8b0..bcbfb5d 100644 --- a/pdf.cc +++ b/pdf.cc @@ -282,3 +282,32 @@ static void do_recalc_bbox(vector &pages, const char *in_name) for (size_t i=0; iimage_box = bboxes[i]; } + +// Transformed page + +xform_page::xform_page(page *p, const char *desc, pdf_matrix xf) +{ + orig_page = p; + index = p->index; + description = desc; + xform = xf; + + BBox media(p->width, p->height); + media.transform(xf); + width = media.width(); + height = media.height(); + + image_box = p->image_box; + image_box.transform(xf); +} + +void xform_page::debug_dump() +{ + debug("Transform (%s): [%s]", description, xform.to_string().c_str()); + orig_page->debug_dump(); +} + +void xform_page::render(out_context *out, pdf_matrix parent_xform) +{ + orig_page->render(out, xform * parent_xform); +} -- 2.39.2