/*
* PaperJam -- Commands
*
- * (c) 2018 Martin Mares <mj@ucw.cz>
+ * (c) 2018--2022 Martin Mares <mj@ucw.cz>
*/
#include <cassert>
/*** 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 {
{
bb->x_min += l;
bb->x_max -= r;
- bb->y_min += b;
- bb->y_max -= t;
+ bb->y_min += t;
+ bb->y_max -= b;
if (bb->x_min >= bb->x_max || bb->y_min >= bb->y_max)
err("Margins cannot be larger than the whole page");
}
{
bb->x_min -= l;
bb->x_max += r;
- bb->y_min -= b;
- bb->y_max += t;
+ bb->y_min -= t;
+ bb->y_max += b;
}
};
{
pdf_matrix m;
m.shift(x, y);
- return new xform_page(p, m);
+ return new xform_page(p, "move", m);
}
};
{
pdf_matrix m;
m.scale(x_factor, y_factor);
- return new xform_page(p, m);
+ return new xform_page(p, "scale", m);
}
};
}
page *process_page(page *p) override
{
- pdf_matrix m;
- switch (deg)
- {
- case 0:
- break;
- case 90:
- m.rotate_deg(-90);
- m.shift(0, p->width);
- break;
- case 180:
- m.rotate_deg(180);
- m.shift(p->width, p->height);
- break;
- case 270:
- m.rotate_deg(90);
- m.shift(p->height, 0);
- break;
- default:
- abort();
- }
- return new xform_page(p, m);
+ return new xform_page(p, "rotate", pdf_rotation_matrix(deg, p->width, p->height));
}
};
m.scale(-1, 1);
m.shift(p->width, 0);
}
- return new xform_page(p, m);
+ return new xform_page(p, "flip", m);
}
};
int f = validate_page_index(pages, ps.from);
int t = validate_page_index(pages, ps.to);
int step = (f <= t) ? 1 : -1;
- for (int i=f; i<=t; i += step)
+ for (int i=f; i != t + step; i += step)
selected.push_back(pages[i]);
}
auto processed = run_command_list(pb->commands, selected);
{
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;
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;
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;
}
// 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;
}
{
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)
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");
vector<page *> 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;
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);