4 * (c) 2018 Martin Mares <mj@ucw.cz>
16 class null_cmd : public cmd_exec {
18 null_cmd(cmd *c UNUSED) { }
19 vector<page *> process(vector<page *> &pages) override { return pages; }
22 static const arg_def no_args[] = {
26 /*** Generic routines ***/
30 class xform_page : public page {
34 void render(out_context *out, pdf_matrix xform) override;
35 xform_page(page *p, pdf_matrix xf);
38 xform_page::xform_page(page *p, pdf_matrix xf)
44 BBox media(p->width, p->height);
46 width = media.width();
47 height = media.height();
53 void xform_page::render(out_context *out, pdf_matrix parent_xform)
55 orig_page->render(out, xform * parent_xform);
58 // Commands acting on individual pages
60 class cmd_exec_simple : public cmd_exec {
61 virtual page *process_page(page *p) = 0;
62 vector<page *> process(vector<page *> &pages) override;
65 vector<page *> cmd_exec_simple::process(vector<page *> &pages)
69 out.push_back(process_page(p));
73 // Paper specifications
78 paper_spec(cmd *c, bool maybe=true)
80 arg_val *aname = c->arg("paper");
81 arg_val *aw = c->arg("w");
82 arg_val *ah = c->arg("h");
83 if (!aname->given() && !aw->given() && !ah->given() && maybe)
88 if (aw->given() != ah->given() || aname->given() == aw->given())
89 die("Either paper format name or width and height must be given");
92 const char *name = aname->as_string("").c_str();
93 const paper *pap = paperinfo(name);
95 die("No paper called %s is known", name);
96 w = paperpswidth(pap);
97 h = paperpsheight(pap);
101 w = aw->as_double(0);
102 h = ah->as_double(0);
108 { "paper", AT_STRING | AT_POSITIONAL }, \
112 // Position specification
117 pos_spec() { v = h = 0; }
121 die("Value of pos must have two characters");
124 else if (s[0] == 'c')
126 else if (s[0] == 'b')
129 die("First character of pos must be t/c/b");
132 else if (s[1] == 'c')
134 else if (s[1] == 'r')
137 die("Second character of pos must be l/c/r");
139 pos_spec(cmd *c) : pos_spec(c->arg("pos")->as_string("cc")) { }
140 pdf_matrix place(BBox &inner, BBox &outer)
143 m.shift(-inner.x_min, -inner.y_min);
149 m.shift((outer.width() - inner.width()) / 2, 0);
152 m.shift(outer.width() - inner.width(), 0);
162 m.shift(0, (outer.height() - inner.height()) / 2);
165 m.shift(0, outer.height() - inner.height());
170 m.shift(outer.x_min, outer.y_min);
183 margin_spec(cmd *c, string basic, string sx)
186 m = c->arg(basic)->as_double(0);
187 h = c->arg("h" + sx)->as_double(m);
188 v = c->arg("v" + sx)->as_double(m);
189 l = c->arg("l" + sx)->as_double(h);
190 r = c->arg("r" + sx)->as_double(h);
191 t = c->arg("t" + sx)->as_double(v);
192 b = c->arg("b" + sx)->as_double(v);
194 void shrink_box(BBox *bb)
200 if (bb->x_min >= bb->x_max || bb->y_min >= bb->y_max)
201 die("Margins cannot be larger than the whole page");
203 void expand_box(BBox *bb)
212 #define MARGIN_ARGS(basic, sx) \
213 { basic, AT_DIMEN }, \
214 { "h" sx, AT_DIMEN }, \
215 { "v" sx, AT_DIMEN }, \
216 { "l" sx, AT_DIMEN }, \
217 { "r" sx, AT_DIMEN }, \
218 { "t" sx, AT_DIMEN }, \
221 // Scaling preserving aspect ratio
223 double scale_to_fit(BBox &from, BBox &to)
225 double fw = from.width(), fh = from.height();
226 double tw = to.width(), th = to.height();
227 if (is_zero(fw) || is_zero(fh) || is_zero(tw) || is_zero(th))
230 return min(tw/fw, th/fh);
235 class move_cmd : public cmd_exec_simple {
240 x = c->arg("x")->as_double(0);
241 y = c->arg("y")->as_double(0);
243 page *process_page(page *p) override
247 return new xform_page(p, m);
251 static const arg_def move_args[] = {
252 { "x", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
253 { "y", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
259 class scale_cmd : public cmd_exec_simple {
260 double x_factor, y_factor;
264 x_factor = c->arg("x")->as_double(1);
265 y_factor = c->arg("y")->as_double(x_factor);
267 page *process_page(page *p) override
270 m.scale(x_factor, y_factor);
271 return new xform_page(p, m);
275 static const arg_def scale_args[] = {
276 { "x", AT_DOUBLE | AT_MANDATORY | AT_POSITIONAL },
277 { "y", AT_DOUBLE | AT_POSITIONAL },
283 class rotate_cmd : public cmd_exec_simple {
288 deg = c->arg("deg")->as_int(0) % 360;
292 die("Rotate requires a multiple of 90 degrees");
294 page *process_page(page *p) override
303 m.shift(0, p->width);
307 m.shift(p->width, p->height);
311 m.shift(p->height, 0);
316 return new xform_page(p, m);
320 static const arg_def rotate_args[] = {
321 { "angle", AT_INT | AT_MANDATORY | AT_POSITIONAL },
327 class flip_cmd : public cmd_exec_simple {
333 horizontal = c->arg("h")->as_int(0);
334 vertical = c->arg("v")->as_int(0);
335 if (!horizontal && !vertical)
336 die("Flip has no direction specified");
338 page *process_page(page *p) override
344 m.shift(0, p->height);
349 m.shift(p->width, 0);
351 return new xform_page(p, m);
355 static const arg_def flip_args[] = {
363 class select_cmd : public cmd_exec {
370 vector<page *> process(vector<page *> &pages);
373 static int validate_page_index(vector<page *> &pages, int idx)
375 if (idx >= 1 && idx <= (int) pages.size())
377 if (idx <= -1 && idx >= (int) -pages.size())
378 return idx + pages.size();
379 die("Page index %d out of range", idx);
382 vector<page *> select_cmd::process(vector<page *> &pages)
385 for (auto pb: pipe->branches)
387 vector<page *> selected;
388 for (auto ps: pb->selectors)
390 int f = validate_page_index(pages, ps.from);
391 int t = validate_page_index(pages, ps.to);
392 int step = (f <= t) ? 1 : -1;
393 for (int i=f; f<=t; f += step)
394 selected.push_back(pages[i]);
396 auto processed = run_command_list(pb->commands, selected);
397 for (auto p: processed)
405 class apply_cmd : public cmd_exec {
412 vector<page *> process(vector<page *> &pages);
415 static pipeline_branch *find_branch(pipeline *pipe, vector <page *> &pages, int idx)
417 for (auto pb: pipe->branches)
418 for (auto ps: pb->selectors)
420 int f = validate_page_index(pages, ps.from);
421 int t = validate_page_index(pages, ps.to);
422 if (f <= idx && idx <= t || t <= idx && idx <= f)
428 vector<page *> apply_cmd::process(vector<page *> &pages)
435 pipeline_branch *pb = find_branch(pipe, pages, cnt);
440 auto processed = run_command_list(pb->commands, tmp);
441 for (auto q: processed)
454 class modulo_cmd : public cmd_exec {
460 n = c->arg("n")->as_int(0);
462 die("Modulo must have n > 0");
465 vector<page *> process(vector<page *> &pages);
468 vector<page *> modulo_cmd::process(vector<page *> &pages)
471 int tuples = ((int) pages.size() + n - 1) / n;
473 for (int tuple=0; tuple < tuples; tuple++)
475 debug("# Tuple %d", tuple);
477 for (auto pb: pipe->branches)
480 for (auto ps: pb->selectors)
484 int step = (f <= t) ? 1 : -1;
485 for (int i=f; i<=t; i += step)
490 else if (i < 0 && i >= -n)
491 j = (tuples-1-tuple)*n + (-i) - 1;
493 die("Modulo: invalid index %d", i);
494 if (j < (int) pages.size())
495 tmp.push_back(pages[j]);
498 page *ref_page = pages[tuple*n];
499 tmp.push_back(new empty_page(ref_page->width, ref_page->height));
503 auto processed = run_command_list(pb->commands, tmp);
504 for (auto q: processed)
513 static const arg_def modulo_args[] = {
514 { "n", AT_INT | AT_MANDATORY | AT_POSITIONAL },
520 class draw_bbox_cmd : public cmd_exec {
522 draw_bbox_cmd(cmd *c UNUSED) { }
523 vector<page *> process(vector<page *> &pages);
526 class draw_bbox_page : public page {
529 void render(out_context *out, pdf_matrix xform) override;
530 draw_bbox_page(page *p) : page(p) { orig_page = p; }
533 void draw_bbox_page::render(out_context *out, pdf_matrix xform)
535 orig_page->render(out, xform);
538 xform.to_string() + " cm " +
540 bbox.to_rect() + " re S " +
544 vector<page *> draw_bbox_cmd::process(vector<page *> &pages)
548 out.push_back(new draw_bbox_page(p));
554 class merge_cmd : public cmd_exec {
556 merge_cmd(cmd *c UNUSED) { }
557 vector<page *> process(vector<page *> &pages);
560 class merge_page : public page {
561 vector<page *> orig_pages;
563 merge_page(vector<page *> &orig) : page(0, 0)
578 if (!is_equal(width, p->width) || !is_equal(height, p->height))
579 die("All pages participating in a merge must have the same dimensions");
584 void render(out_context *out, pdf_matrix xform) override
586 for (auto p: orig_pages)
587 p->render(out, xform);
591 vector<page *> merge_cmd::process(vector<page *> &pages)
595 out.push_back(new merge_page(pages));
601 class paper_cmd : public cmd_exec_simple {
605 paper_cmd(cmd *c) : paper(c), pos(c) { }
606 page *process_page(page *p) override
608 BBox paper_box = BBox(paper.w, paper.h);
609 pdf_matrix xf = pos.place(p->bbox, paper_box);
610 page *q = new xform_page(p, xf);
617 static const arg_def paper_args[] = {
625 class scaleto_cmd : public cmd_exec_simple {
629 scaleto_cmd(cmd *c) : paper(c), pos(c) { }
630 page *process_page(page *p) override
632 BBox orig_box = BBox(p->width, p->height);
633 BBox paper_box = BBox(paper.w, paper.h);
635 xf.scale(scale_to_fit(orig_box, paper_box));
636 orig_box.transform(xf);
637 xf.concat(pos.place(orig_box, paper_box));
638 page *q = new xform_page(p, xf);
645 static const arg_def scaleto_args[] = {
653 class fit_cmd : public cmd_exec_simple {
658 fit_cmd(cmd *c) : paper(c, true), pos(c), marg(c, "margin", "margin") { }
659 page *process_page(page *p) override
664 if (!is_zero(paper.w) && !is_zero(paper.h))
666 // Paper given: scale image to fit paper
667 BBox orig_box = p->bbox;
668 BBox paper_box = BBox(paper.w, paper.h);
669 marg.shrink_box(&paper_box);
670 xf.scale(scale_to_fit(orig_box, paper_box));
671 orig_box.transform(xf);
672 xf.concat(pos.place(orig_box, paper_box));
673 q = new xform_page(p, xf);
679 // No paper given: adjust paper to fit image
680 xf.shift(-p->bbox.x_min, -p->bbox.y_min);
681 xf.shift(marg.l, marg.b);
682 q = new xform_page(p, xf);
683 q->width = p->bbox.width() + marg.l + marg.r;
684 q->height = p->bbox.height() + marg.t + marg.b;
690 static const arg_def fit_args[] = {
693 MARGIN_ARGS("margin", "margin"),
697 /*** Command table ***/
699 template<typename T> cmd_exec *ctor(cmd *c) { return new T(c); }
701 const cmd_def cmd_table[] = {
702 { "null", no_args, 0, &ctor<null_cmd> },
703 { "move", move_args, 0, &ctor<move_cmd> },
704 { "scale", scale_args, 0, &ctor<scale_cmd> },
705 { "rotate", rotate_args, 0, &ctor<rotate_cmd> },
706 { "flip", flip_args, 0, &ctor<flip_cmd> },
707 { "select", no_args, 1, &ctor<select_cmd> },
708 { "apply", no_args, 1, &ctor<apply_cmd> },
709 { "modulo", modulo_args, 1, &ctor<modulo_cmd> },
710 { "draw_bbox",no_args, 0, &ctor<draw_bbox_cmd> },
711 { "merge", no_args, 0, &ctor<merge_cmd> },
712 { "paper", paper_args, 0, &ctor<paper_cmd> },
713 { "scaleto", scaleto_args, 0, &ctor<scaleto_cmd> },
714 { "fit", fit_args, 0, &ctor<fit_cmd> },
715 { NULL, NULL, 0, NULL }