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_ARGS1_NAMED(name) \
215 #define MARGIN_ARGS1_POSNL(name) \
216 { name, AT_DIMEN | AT_POSITIONAL }
218 #define MARGIN_ARGS2(sx) \
219 { "h" sx, AT_DIMEN }, \
220 { "v" sx, AT_DIMEN }, \
221 { "l" sx, AT_DIMEN }, \
222 { "r" sx, AT_DIMEN }, \
223 { "t" sx, AT_DIMEN }, \
226 // Scaling preserving aspect ratio
228 double scale_to_fit(BBox &from, BBox &to)
230 double fw = from.width(), fh = from.height();
231 double tw = to.width(), th = to.height();
232 if (is_zero(fw) || is_zero(fh) || is_zero(tw) || is_zero(th))
235 return min(tw/fw, th/fh);
240 class move_cmd : public cmd_exec_simple {
245 x = c->arg("x")->as_double(0);
246 y = c->arg("y")->as_double(0);
248 page *process_page(page *p) override
252 return new xform_page(p, m);
256 static const arg_def move_args[] = {
257 { "x", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
258 { "y", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
264 class scale_cmd : public cmd_exec_simple {
265 double x_factor, y_factor;
269 x_factor = c->arg("x")->as_double(1);
270 y_factor = c->arg("y")->as_double(x_factor);
272 page *process_page(page *p) override
275 m.scale(x_factor, y_factor);
276 return new xform_page(p, m);
280 static const arg_def scale_args[] = {
281 { "x", AT_DOUBLE | AT_MANDATORY | AT_POSITIONAL },
282 { "y", AT_DOUBLE | AT_POSITIONAL },
288 class rotate_cmd : public cmd_exec_simple {
293 deg = c->arg("deg")->as_int(0) % 360;
297 die("Rotate requires a multiple of 90 degrees");
299 page *process_page(page *p) override
308 m.shift(0, p->width);
312 m.shift(p->width, p->height);
316 m.shift(p->height, 0);
321 return new xform_page(p, m);
325 static const arg_def rotate_args[] = {
326 { "angle", AT_INT | AT_MANDATORY | AT_POSITIONAL },
332 class flip_cmd : public cmd_exec_simple {
338 horizontal = c->arg("h")->as_int(0);
339 vertical = c->arg("v")->as_int(0);
340 if (!horizontal && !vertical)
341 die("Flip has no direction specified");
343 page *process_page(page *p) override
349 m.shift(0, p->height);
354 m.shift(p->width, 0);
356 return new xform_page(p, m);
360 static const arg_def flip_args[] = {
368 class select_cmd : public cmd_exec {
375 vector<page *> process(vector<page *> &pages);
378 static int validate_page_index(vector<page *> &pages, int idx)
380 if (idx >= 1 && idx <= (int) pages.size())
382 if (idx <= -1 && idx >= (int) -pages.size())
383 return idx + pages.size();
384 die("Page index %d out of range", idx);
387 vector<page *> select_cmd::process(vector<page *> &pages)
390 for (auto pb: pipe->branches)
392 vector<page *> selected;
393 for (auto ps: pb->selectors)
395 int f = validate_page_index(pages, ps.from);
396 int t = validate_page_index(pages, ps.to);
397 int step = (f <= t) ? 1 : -1;
398 for (int i=f; f<=t; f += step)
399 selected.push_back(pages[i]);
401 auto processed = run_command_list(pb->commands, selected);
402 for (auto p: processed)
410 class apply_cmd : public cmd_exec {
417 vector<page *> process(vector<page *> &pages);
420 static pipeline_branch *find_branch(pipeline *pipe, vector <page *> &pages, int idx)
422 for (auto pb: pipe->branches)
423 for (auto ps: pb->selectors)
425 int f = validate_page_index(pages, ps.from);
426 int t = validate_page_index(pages, ps.to);
427 if (f <= idx && idx <= t || t <= idx && idx <= f)
433 vector<page *> apply_cmd::process(vector<page *> &pages)
440 pipeline_branch *pb = find_branch(pipe, pages, cnt);
445 auto processed = run_command_list(pb->commands, tmp);
446 for (auto q: processed)
459 class modulo_cmd : public cmd_exec {
465 n = c->arg("n")->as_int(0);
467 die("Modulo must have n > 0");
470 vector<page *> process(vector<page *> &pages);
473 vector<page *> modulo_cmd::process(vector<page *> &pages)
476 int tuples = ((int) pages.size() + n - 1) / n;
478 for (int tuple=0; tuple < tuples; tuple++)
480 debug("# Tuple %d", tuple);
482 for (auto pb: pipe->branches)
485 for (auto ps: pb->selectors)
489 int step = (f <= t) ? 1 : -1;
490 for (int i=f; i<=t; i += step)
495 else if (i < 0 && i >= -n)
496 j = (tuples-1-tuple)*n + (-i) - 1;
498 die("Modulo: invalid index %d", i);
499 if (j < (int) pages.size())
500 tmp.push_back(pages[j]);
503 page *ref_page = pages[tuple*n];
504 tmp.push_back(new empty_page(ref_page->width, ref_page->height));
508 auto processed = run_command_list(pb->commands, tmp);
509 for (auto q: processed)
518 static const arg_def modulo_args[] = {
519 { "n", AT_INT | AT_MANDATORY | AT_POSITIONAL },
525 class draw_bbox_cmd : public cmd_exec {
527 draw_bbox_cmd(cmd *c UNUSED) { }
528 vector<page *> process(vector<page *> &pages);
531 class draw_bbox_page : public page {
534 void render(out_context *out, pdf_matrix xform) override;
535 draw_bbox_page(page *p) : page(p) { orig_page = p; }
538 void draw_bbox_page::render(out_context *out, pdf_matrix xform)
540 orig_page->render(out, xform);
543 xform.to_string() + " cm " +
545 bbox.to_rect() + " re S " +
549 vector<page *> draw_bbox_cmd::process(vector<page *> &pages)
553 out.push_back(new draw_bbox_page(p));
559 class merge_cmd : public cmd_exec {
561 merge_cmd(cmd *c UNUSED) { }
562 vector<page *> process(vector<page *> &pages);
565 class merge_page : public page {
566 vector<page *> orig_pages;
568 merge_page(vector<page *> &orig) : page(0, 0)
583 if (!is_equal(width, p->width) || !is_equal(height, p->height))
584 die("All pages participating in a merge must have the same dimensions");
589 void render(out_context *out, pdf_matrix xform) override
591 for (auto p: orig_pages)
592 p->render(out, xform);
596 vector<page *> merge_cmd::process(vector<page *> &pages)
600 out.push_back(new merge_page(pages));
606 class paper_cmd : public cmd_exec_simple {
610 paper_cmd(cmd *c) : paper(c), pos(c) { }
611 page *process_page(page *p) override
613 BBox paper_box = BBox(paper.w, paper.h);
614 pdf_matrix xf = pos.place(p->bbox, paper_box);
615 page *q = new xform_page(p, xf);
622 static const arg_def paper_args[] = {
630 class scaleto_cmd : public cmd_exec_simple {
634 scaleto_cmd(cmd *c) : paper(c), pos(c) { }
635 page *process_page(page *p) override
637 BBox orig_box = BBox(p->width, p->height);
638 BBox paper_box = BBox(paper.w, paper.h);
640 xf.scale(scale_to_fit(orig_box, paper_box));
641 orig_box.transform(xf);
642 xf.concat(pos.place(orig_box, paper_box));
643 page *q = new xform_page(p, xf);
650 static const arg_def scaleto_args[] = {
658 class fit_cmd : public cmd_exec_simple {
663 fit_cmd(cmd *c) : paper(c, true), pos(c), marg(c, "margin", "margin") { }
664 page *process_page(page *p) override
669 if (!is_zero(paper.w) && !is_zero(paper.h))
671 // Paper given: scale image to fit paper
672 BBox orig_box = p->bbox;
673 BBox paper_box = BBox(paper.w, paper.h);
674 marg.shrink_box(&paper_box);
675 xf.scale(scale_to_fit(orig_box, paper_box));
676 orig_box.transform(xf);
677 xf.concat(pos.place(orig_box, paper_box));
678 q = new xform_page(p, xf);
684 // No paper given: adjust paper to fit image
685 xf.shift(-p->bbox.x_min, -p->bbox.y_min);
686 xf.shift(marg.l, marg.b);
687 q = new xform_page(p, xf);
688 q->width = p->bbox.width() + marg.l + marg.r;
689 q->height = p->bbox.height() + marg.t + marg.b;
695 static const arg_def fit_args[] = {
698 MARGIN_ARGS1_NAMED("margin"),
699 MARGIN_ARGS2("margin"),
705 class expand_cmd : public cmd_exec_simple {
708 expand_cmd(cmd *c) : marg(c, "by", "") { }
709 page *process_page(page *p) override
712 xf.shift(marg.l, marg.b);
713 page *q = new xform_page(p, xf);
714 q->width = p->width + marg.l + marg.r;
715 q->height = p->height + marg.t + marg.b;
716 if (q->width < 0.001 || q->height < 0.001)
717 die("Expansion must result in positive page dimensions");
722 static const arg_def expand_args[] = {
723 MARGIN_ARGS1_POSNL("by"),
730 class margins_cmd : public cmd_exec_simple {
733 margins_cmd(cmd *c) : marg(c, "size", "") { }
734 page *process_page(page *p) override
737 xf.shift(-p->bbox.x_min, -p->bbox.y_min);
738 xf.shift(marg.l, marg.b);
739 page *q = new xform_page(p, xf);
740 q->width = p->bbox.width() + marg.l + marg.r;
741 q->height = p->bbox.height() + marg.t + marg.b;
742 if (q->width < 0.001 || q->height < 0.001)
743 die("Margins must result in positive page dimensions");
748 static const arg_def margins_args[] = {
749 MARGIN_ARGS1_POSNL("size"),
754 /*** Command table ***/
756 template<typename T> cmd_exec *ctor(cmd *c) { return new T(c); }
758 const cmd_def cmd_table[] = {
759 { "null", no_args, 0, &ctor<null_cmd> },
760 { "move", move_args, 0, &ctor<move_cmd> },
761 { "scale", scale_args, 0, &ctor<scale_cmd> },
762 { "rotate", rotate_args, 0, &ctor<rotate_cmd> },
763 { "flip", flip_args, 0, &ctor<flip_cmd> },
764 { "select", no_args, 1, &ctor<select_cmd> },
765 { "apply", no_args, 1, &ctor<apply_cmd> },
766 { "modulo", modulo_args, 1, &ctor<modulo_cmd> },
767 { "draw_bbox",no_args, 0, &ctor<draw_bbox_cmd> },
768 { "merge", no_args, 0, &ctor<merge_cmd> },
769 { "paper", paper_args, 0, &ctor<paper_cmd> },
770 { "scaleto", scaleto_args, 0, &ctor<scaleto_cmd> },
771 { "fit", fit_args, 0, &ctor<fit_cmd> },
772 { "expand", expand_args, 0, &ctor<expand_cmd> },
773 { "margins", margins_args, 0, &ctor<margins_cmd> },
774 { NULL, NULL, 0, NULL }