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();
49 image_box = p->image_box;
50 image_box.transform(xf);
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 bool may_shrink(BBox *bb)
196 return (bb->width() > l+r && bb->height() > t+b);
198 void shrink_box(BBox *bb)
204 if (bb->x_min >= bb->x_max || bb->y_min >= bb->y_max)
205 die("Margins cannot be larger than the whole page");
207 void expand_box(BBox *bb)
216 #define MARGIN_ARGS1_NAMED(name) \
219 #define MARGIN_ARGS1_POSNL(name) \
220 { name, AT_DIMEN | AT_POSITIONAL }
222 #define MARGIN_ARGS2(sx) \
223 { "h" sx, AT_DIMEN }, \
224 { "v" sx, AT_DIMEN }, \
225 { "l" sx, AT_DIMEN }, \
226 { "r" sx, AT_DIMEN }, \
227 { "t" sx, AT_DIMEN }, \
232 class cropmark_spec {
243 string crop_cross(double x, double y, uint mask);
244 QPDFObjectHandle egstate;
246 cropmark_spec(cmd *c, string prefix="", string def_type="cross")
248 string t = c->arg(prefix + "mark")->as_string(def_type);
253 else if (t == "cross")
260 die("Invalid cropmark type %s", t.c_str());
262 pen_width = c->arg(prefix + "pen")->as_double(1);
263 arm_length = c->arg(prefix + "len")->as_double(5*mm);
264 offset = c->arg(prefix + "offset")->as_double(0);
265 egstate = QPDFObjectHandle::newNull();
267 string pdf_stream(out_context *out, BBox &box, pdf_matrix &xform);
270 string cropmark_spec::crop_cross(double x, double y, uint mask)
274 for (uint i=0; i<4; i++)
275 if (mask & (1U << i))
277 double x2 = x, y2 = y;
280 case 3: x2 -= arm_length; break;
281 case 2: x2 += arm_length; break;
282 case 1: y2 += arm_length; break;
283 case 0: y2 -= arm_length; break;
285 s += pdf_coord(x) + " " + pdf_coord(y) + " m " + pdf_coord(x2) + " " + pdf_coord(y2) + " l S ";
291 string cropmark_spec::pdf_stream(out_context *out, BBox &box, pdf_matrix &xform)
295 s += xform.to_string() + " cm ";
297 if (egstate.isNull())
299 auto egs = QPDFObjectHandle::newDictionary();
300 egs.replaceKey("/Type", QPDFObjectHandle::newName("/ExtGState"));
301 egs.replaceKey("/LW", QPDFObjectHandle::newReal(pen_width, 1));
302 egs.replaceKey("/LC", QPDFObjectHandle::newInteger(2));
303 egs.replaceKey("/LJ", QPDFObjectHandle::newInteger(0));
304 egstate = out->pdf->makeIndirectObject(egs);
307 string egs_res = out->new_resource("GS");
308 out->egstates.replaceKey(egs_res, egstate);
309 s += egs_res + " gs ";
322 s += b.to_rect() + " re S ";
325 s += crop_cross(b.x_min, b.y_min, 0b1111);
326 s += crop_cross(b.x_max, b.y_min, 0b1111);
327 s += crop_cross(b.x_max, b.y_max, 0b1111);
328 s += crop_cross(b.x_min, b.y_max, 0b1111);
331 s += crop_cross(b.x_min, b.y_min, 0b0110);
332 s += crop_cross(b.x_max, b.y_min, 0b1010);
333 s += crop_cross(b.x_max, b.y_max, 0b1001);
334 s += crop_cross(b.x_min, b.y_max, 0b0101);
337 s += crop_cross(b.x_min, b.y_min, 0b1001);
338 s += crop_cross(b.x_max, b.y_min, 0b0101);
339 s += crop_cross(b.x_max, b.y_max, 0b0110);
340 s += crop_cross(b.x_min, b.y_max, 0b1010);
348 #define CROPMARK_ARGS(px) \
349 { px "mark", AT_STRING }, \
350 { px "pen", AT_DIMEN }, \
351 { px "len", AT_DIMEN }, \
352 { px "offset",AT_DIMEN }
354 // Scaling preserving aspect ratio
356 double scale_to_fit(BBox &from, BBox &to)
358 double fw = from.width(), fh = from.height();
359 double tw = to.width(), th = to.height();
360 if (is_zero(fw) || is_zero(fh) || is_zero(tw) || is_zero(th))
363 return min(tw/fw, th/fh);
368 class move_cmd : public cmd_exec_simple {
373 x = c->arg("x")->as_double(0);
374 y = c->arg("y")->as_double(0);
376 page *process_page(page *p) override
380 return new xform_page(p, m);
384 static const arg_def move_args[] = {
385 { "x", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
386 { "y", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
392 class scale_cmd : public cmd_exec_simple {
393 double x_factor, y_factor;
397 x_factor = c->arg("x")->as_double(1);
398 y_factor = c->arg("y")->as_double(x_factor);
400 page *process_page(page *p) override
403 m.scale(x_factor, y_factor);
404 return new xform_page(p, m);
408 static const arg_def scale_args[] = {
409 { "x", AT_DOUBLE | AT_MANDATORY | AT_POSITIONAL },
410 { "y", AT_DOUBLE | AT_POSITIONAL },
416 class rotate_cmd : public cmd_exec_simple {
421 deg = c->arg("deg")->as_int(0) % 360;
425 die("Rotate requires a multiple of 90 degrees");
427 page *process_page(page *p) override
436 m.shift(0, p->width);
440 m.shift(p->width, p->height);
444 m.shift(p->height, 0);
449 return new xform_page(p, m);
453 static const arg_def rotate_args[] = {
454 { "angle", AT_INT | AT_MANDATORY | AT_POSITIONAL },
460 class flip_cmd : public cmd_exec_simple {
466 horizontal = c->arg("h")->as_int(0);
467 vertical = c->arg("v")->as_int(0);
468 if (!horizontal && !vertical)
469 die("Flip has no direction specified");
471 page *process_page(page *p) override
477 m.shift(0, p->height);
482 m.shift(p->width, 0);
484 return new xform_page(p, m);
488 static const arg_def flip_args[] = {
496 class select_cmd : public cmd_exec {
503 vector<page *> process(vector<page *> &pages) override;
506 static int validate_page_index(vector<page *> &pages, int idx)
508 if (idx >= 1 && idx <= (int) pages.size())
510 if (idx <= -1 && idx >= (int) -pages.size())
511 return idx + pages.size();
512 die("Page index %d out of range", idx);
515 vector<page *> select_cmd::process(vector<page *> &pages)
518 for (auto pb: pipe->branches)
520 vector<page *> selected;
521 for (auto ps: pb->selectors)
523 int f = validate_page_index(pages, ps.from);
524 int t = validate_page_index(pages, ps.to);
525 int step = (f <= t) ? 1 : -1;
526 for (int i=f; f<=t; f += step)
527 selected.push_back(pages[i]);
529 auto processed = run_command_list(pb->commands, selected);
530 for (auto p: processed)
538 class apply_cmd : public cmd_exec {
545 vector<page *> process(vector<page *> &pages) override;
548 static pipeline_branch *find_branch(pipeline *pipe, vector <page *> &pages, int idx)
550 for (auto pb: pipe->branches)
551 for (auto ps: pb->selectors)
553 int f = validate_page_index(pages, ps.from);
554 int t = validate_page_index(pages, ps.to);
555 if (f <= idx && idx <= t || t <= idx && idx <= f)
561 vector<page *> apply_cmd::process(vector<page *> &pages)
568 pipeline_branch *pb = find_branch(pipe, pages, cnt);
573 auto processed = run_command_list(pb->commands, tmp);
574 for (auto q: processed)
587 class modulo_cmd : public cmd_exec {
594 n = c->arg("n")->as_int(0);
596 die("Modulo must have n > 0");
597 half = c->arg("half")->as_int(0);
600 vector<page *> process(vector<page *> &pages) override;
603 vector<page *> modulo_cmd::process(vector<page *> &pages)
606 int tuples = ((int) pages.size() + n - 1) / n;
607 int use_tuples = half ? tuples/2 : tuples;
609 for (int tuple=0; tuple < use_tuples; tuple++)
611 debug("# Tuple %d", tuple);
613 for (auto pb: pipe->branches)
616 for (auto ps: pb->selectors)
620 int step = (f <= t) ? 1 : -1;
621 for (int i=f; i<=t; i += step)
626 else if (i < 0 && i >= -n)
627 j = (tuples-1-tuple)*n + (-i) - 1;
629 die("Modulo: invalid index %d", i);
630 if (j < (int) pages.size())
631 tmp.push_back(pages[j]);
634 page *ref_page = pages[tuple*n];
635 tmp.push_back(new empty_page(ref_page->width, ref_page->height));
639 auto processed = run_command_list(pb->commands, tmp);
640 for (auto q: processed)
649 static const arg_def modulo_args[] = {
650 { "n", AT_INT | AT_MANDATORY | AT_POSITIONAL },
651 { "half", AT_SWITCH },
657 class draw_bbox_page : public page {
660 void render(out_context *out, pdf_matrix xform) override;
661 draw_bbox_page(page *p) : page(p) { orig_page = p; }
664 class draw_bbox_cmd : public cmd_exec_simple {
666 draw_bbox_cmd(cmd *c UNUSED) { }
667 page *process_page(page *p) override
669 return new draw_bbox_page(p);
673 void draw_bbox_page::render(out_context *out, pdf_matrix xform)
675 orig_page->render(out, xform);
678 xform.to_string() + " cm " +
680 image_box.to_rect() + " re S " +
686 class merge_cmd : public cmd_exec {
688 merge_cmd(cmd *c UNUSED) { }
689 vector<page *> process(vector<page *> &pages) override;
692 class merge_page : public page {
693 vector<page *> orig_pages;
695 merge_page(vector<page *> &orig) : page(0, 0)
705 image_box = p->image_box;
710 if (!is_equal(width, p->width) || !is_equal(height, p->height))
711 die("All pages participating in a merge must have the same dimensions");
712 image_box.join(p->image_box);
716 void render(out_context *out, pdf_matrix xform) override
718 for (auto p: orig_pages)
719 p->render(out, xform);
723 vector<page *> merge_cmd::process(vector<page *> &pages)
727 out.push_back(new merge_page(pages));
733 class paper_cmd : public cmd_exec_simple {
737 paper_cmd(cmd *c) : paper(c), pos(c) { }
738 page *process_page(page *p) override
740 BBox paper_box = BBox(paper.w, paper.h);
741 pdf_matrix xf = pos.place(p->image_box, paper_box);
742 page *q = new xform_page(p, xf);
749 static const arg_def paper_args[] = {
757 class scaleto_cmd : public cmd_exec_simple {
761 scaleto_cmd(cmd *c) : paper(c), pos(c) { }
762 page *process_page(page *p) override
764 BBox orig_box = BBox(p->width, p->height);
765 BBox paper_box = BBox(paper.w, paper.h);
767 xf.scale(scale_to_fit(orig_box, paper_box));
768 orig_box.transform(xf);
769 xf.concat(pos.place(orig_box, paper_box));
770 page *q = new xform_page(p, xf);
777 static const arg_def scaleto_args[] = {
785 class fit_cmd : public cmd_exec_simple {
790 fit_cmd(cmd *c) : paper(c, true), pos(c), marg(c, "margin", "margin") { }
791 page *process_page(page *p) override
796 if (!is_zero(paper.w) && !is_zero(paper.h))
798 // Paper given: scale image to fit paper
799 BBox orig_box = p->image_box;
800 BBox paper_box = BBox(paper.w, paper.h);
801 marg.shrink_box(&paper_box);
802 xf.scale(scale_to_fit(orig_box, paper_box));
803 orig_box.transform(xf);
804 xf.concat(pos.place(orig_box, paper_box));
805 q = new xform_page(p, xf);
811 // No paper given: adjust paper to fit image
812 xf.shift(-p->image_box.x_min, -p->image_box.y_min);
813 xf.shift(marg.l, marg.b);
814 q = new xform_page(p, xf);
815 q->width = p->image_box.width() + marg.l + marg.r;
816 q->height = p->image_box.height() + marg.t + marg.b;
822 static const arg_def fit_args[] = {
825 MARGIN_ARGS1_NAMED("margin"),
826 MARGIN_ARGS2("margin"),
832 class expand_cmd : public cmd_exec_simple {
835 expand_cmd(cmd *c) : marg(c, "by", "") { }
836 page *process_page(page *p) override
839 xf.shift(marg.l, marg.b);
840 page *q = new xform_page(p, xf);
841 q->width = p->width + marg.l + marg.r;
842 q->height = p->height + marg.t + marg.b;
843 if (q->width < 0.001 || q->height < 0.001)
844 die("Expansion must result in positive page dimensions");
849 static const arg_def expand_args[] = {
850 MARGIN_ARGS1_POSNL("by"),
857 class margins_cmd : public cmd_exec_simple {
860 margins_cmd(cmd *c) : marg(c, "size", "") { }
861 page *process_page(page *p) override
864 xf.shift(-p->image_box.x_min, -p->image_box.y_min);
865 xf.shift(marg.l, marg.b);
866 page *q = new xform_page(p, xf);
867 q->width = p->image_box.width() + marg.l + marg.r;
868 q->height = p->image_box.height() + marg.t + marg.b;
869 if (q->width < 0.001 || q->height < 0.001)
870 die("Margins must result in positive page dimensions");
875 static const arg_def margins_args[] = {
876 MARGIN_ARGS1_POSNL("size"),
883 class add_blank_cmd : public cmd_exec {
887 add_blank_cmd(cmd *c) : paper(c, true)
889 n = c->arg("n")->as_int(1);
891 vector<page *> process(vector<page *> &pages) override;
894 vector<page *> add_blank_cmd::process(vector<page *> &pages)
901 for (int i=0; i<n; i++)
903 double w = paper.w, h = paper.h;
904 if (is_zero(w) || is_zero(h))
905 w = p->width, h = p->height;
906 out.push_back(new empty_page(w, h));
913 static const arg_def add_blank_args[] = {
914 { "n", AT_INT | AT_POSITIONAL },
921 class book_cmd : public cmd_exec {
926 n = c->arg("n")->as_int(0);
928 die("Number of pages per signature must be divisible by 4");
930 vector<page *> process(vector<page *> &pages) override;
933 vector<page *> book_cmd::process(vector<page *> &pages)
935 vector<page *> in, out;
938 while (in.size() % 4)
939 in.push_back(new empty_page(in[0]->width, in[0]->height));
942 while (i < (int) in.size())
944 int sig = in.size() - i;
947 for (int j=0; j<sig/2; j+=2)
949 out.push_back(in[i + sig-1-j]);
950 out.push_back(in[i + j]);
951 out.push_back(in[i + j+1]);
952 out.push_back(in[i + sig-2-j]);
960 static const arg_def book_args[] = {
961 { "n", AT_INT | AT_POSITIONAL },
969 double tile_w, tile_h;
970 double paper_w, paper_h;
975 class nup_cmd : public cmd_exec {
977 int grid_n, grid_m, num_tiles;
991 double hspace, vspace;
994 page *process_single(vector<page *> &in);
995 void find_config(vector<page *> &in, BBox *page_boxes);
996 void try_config(nup_state &st);
999 BBox common_page_box;
1002 nup_cmd(cmd *c) : paper(c), marg(c, "margin", "margin"), pos(c), tpos(c->arg("tpos")->as_string("tl"))
1004 grid_n = c->arg("n")->as_int(0);
1005 grid_m = c->arg("m")->as_int(0);
1006 if (grid_n > 0 && grid_m > 0)
1007 num_tiles = grid_n * grid_m;
1008 else if (grid_n > 0 && !grid_m)
1011 die("Grid size must be at least 1x1");
1013 const string by = c->arg("by")->as_string("rows");
1014 if (by == "rows" || by == "row" || by == "r")
1016 else if (by == "cols" || by == "cols" || by == "c")
1018 else if (by == "tile" || by == "t")
1021 die("Parameter \"by\" must be rows/cols/tile");
1023 crop = c->arg("crop")->as_int(0);
1024 mixed = c->arg("mixed")->as_int(0);
1025 rotate = c->arg("rotate")->as_int(-1);
1026 scale = c->arg("scale")->as_double(0);
1028 double space = c->arg("space")->as_double(0);
1029 hspace = c->arg("hspace")->as_double(space);
1030 vspace = c->arg("vspace")->as_double(space);
1032 if (!is_zero(scale) && (!is_zero(paper.w) || rotate >= 0))
1033 die("When nup is used with explicit scaling, paper size nor rotation may be given");
1034 if (!is_zero(scale) && !grid_m)
1035 die("When nup is used with explicit scaling, both grid sizes must be given");
1038 vector<page *> process(vector<page *> &pages) override;
1041 vector<page *> nup_cmd::process(vector<page *> &pages)
1045 // Unless mixed is given, find the common page size
1048 for (int i=0; i < (int) pages.size(); i++)
1051 BBox pb = crop ? p->image_box : BBox(p->width, p->height);
1053 common_page_box = pb;
1055 common_page_box.join(pb);
1057 debug("NUP: Common page box [%.3f %.3f]-[%.3f %.3f]",
1058 common_page_box.x_min, common_page_box.y_min, common_page_box.x_max, common_page_box.y_max);
1061 // Process one tile set after another
1063 while (i < (int) pages.size())
1066 if (fill_by == BY_TILE)
1068 for (int j=0; j<num_tiles; j++)
1069 in.push_back(pages[i]);
1074 for (int j=0; j<num_tiles; j++)
1076 if (i < (int) pages.size())
1077 in.push_back(pages[i]);
1079 in.push_back(new empty_page(in[0]->width, in[0]->height));
1083 out.push_back(process_single(in));
1089 void nup_cmd::try_config(nup_state &st)
1091 BBox window(st.paper_w, st.paper_h);
1092 if (!marg.may_shrink(&window))
1094 marg.shrink_box(&window);
1095 window.x_max -= (st.cols - 1) * hspace;
1096 window.y_max -= (st.rows - 1) * vspace;
1097 if (window.width() < 0 || window.height() < 0)
1100 BBox image(st.cols * st.tile_w, st.rows * st.tile_h);
1101 st.scale = scale_to_fit(image, window);
1102 st.fill_factor = (st.scale*image.width() * st.scale*image.height()) / (st.paper_w * st.paper_h);
1104 debug("Try: %dx%d on %.3f x %.3f => scale %.3f, fill %.6f",
1106 st.paper_w, st.paper_h,
1107 st.scale, st.fill_factor);
1109 if (!found_solution || best.fill_factor < st.fill_factor)
1111 found_solution = true;
1116 void nup_cmd::find_config(vector<page *> &in, BBox *page_boxes)
1120 // Determine tile size
1121 st.tile_w = st.tile_h = 0;
1122 for (int i=0; i<num_tiles; i++)
1125 page_boxes[i] = common_page_box;
1127 page_boxes[i] = in[i]->image_box;
1129 page_boxes[i] = BBox(in[i]->width, in[i]->height);
1130 st.tile_w = max(st.tile_w, page_boxes[i].width());
1131 st.tile_h = max(st.tile_h, page_boxes[i].height());
1133 debug("NUP: %d tiles of size [%.3f,%.3f]", num_tiles, st.tile_w, st.tile_h);
1136 // Try all possible configurations of tiles
1137 found_solution = false;
1138 if (!is_zero(scale))
1140 // If explicit scaling is requested, we choose page size ourselves,
1141 // so there is just one configuration.
1144 st.paper_w = marg.l + st.cols*st.tile_w + (st.cols-1)*hspace + marg.r;
1145 st.paper_h = marg.t + st.rows*st.tile_h + (st.rows-1)*vspace + marg.b;
1150 // Page size is fixed (either given or copied from the source pages),
1151 // but we can have freedom to rotate and/or to choose grid size.
1152 for (int rot=0; rot<=1; rot++)
1154 if (rotate >= 0 && rot != rotate)
1157 // Establish paper size
1158 if (!is_zero(paper.w))
1160 st.paper_w = paper.w;
1161 st.paper_h = paper.h;
1165 st.paper_w = in[0]->width;
1166 st.paper_h = in[0]->height;
1169 swap(st.paper_w, st.paper_h);
1180 for (int r=1; r<=grid_n; r++)
1184 st.cols = grid_n / r;
1191 if (!found_solution)
1192 die("Nup did not find a feasible solution");
1193 debug("Best: %dx%d on %.3f x %.3f", best.cols, best.rows, best.paper_w, best.paper_h);
1197 class nup_page : public page {
1199 vector<page *> orig_pages;
1200 vector<pdf_matrix> xforms;
1201 void render(out_context *out, pdf_matrix xform) override;
1202 nup_page(nup_state &st) : page(st.paper_w, st.paper_h) { }
1205 void nup_page::render(out_context *out, pdf_matrix parent_xform)
1207 for (int i=0; i < (int) orig_pages.size(); i++)
1208 orig_pages[i]->render(out, xforms[i] * parent_xform);
1211 page *nup_cmd::process_single(vector<page *> &in)
1213 BBox page_boxes[num_tiles];
1214 find_config(in, page_boxes);
1215 double tw = best.scale * best.tile_w;
1216 double th = best.scale * best.tile_h;
1218 // Construct transform from paper to grid of tiles
1219 BBox paper_box(best.paper_w, best.paper_h);
1220 marg.shrink_box(&paper_box);
1221 BBox grid_box(best.cols * tw + (best.cols-1) * hspace,
1222 best.rows * th + (best.rows-1) * vspace);
1223 pdf_matrix place_xform = pos.place(grid_box, paper_box);
1225 nup_page *p = new nup_page(best);
1226 p->image_box = grid_box;
1227 p->image_box.transform(place_xform);
1229 for (int i=0; i<num_tiles; i++)
1232 if (fill_by == BY_ROWS || fill_by == BY_TILE)
1244 BBox &page_box = page_boxes[i];
1245 m.shift(-page_box.x_min, -page_box.y_min);
1246 m.scale(best.scale);
1247 page_box.transform(m);
1249 double x = c * (tw + hspace);
1250 double y = (best.rows-1-r) * (th + vspace);
1251 BBox tile_box = BBox(x, y, x+tw, y+th);
1252 m.concat(tpos.place(page_box, tile_box));
1254 p->orig_pages.push_back(in[i]);
1255 p->xforms.push_back(m * place_xform);
1261 static const arg_def nup_args[] = {
1262 { "n", AT_INT | AT_POSITIONAL | AT_MANDATORY },
1263 { "m", AT_INT | AT_POSITIONAL },
1264 { "by", AT_STRING },
1265 { "crop", AT_SWITCH },
1266 { "mixed", AT_SWITCH },
1267 { "rotate", AT_SWITCH },
1268 { "scale", AT_DOUBLE },
1270 MARGIN_ARGS1_NAMED("margin"),
1271 MARGIN_ARGS2("margin"),
1273 { "tpos", AT_STRING },
1274 { "space", AT_DIMEN },
1275 { "hspace", AT_DIMEN },
1276 { "vspace", AT_DIMEN },
1282 class cropmarks_page : public page {
1286 void render(out_context *out, pdf_matrix xform) override
1288 orig_page->render(out, xform);
1289 out->contents += cm->pdf_stream(out, image_box, xform);
1291 cropmarks_page(page *p, cropmark_spec *cms) : page(p), orig_page(p), cm(cms) { }
1294 class cropmarks_cmd : public cmd_exec_simple {
1296 page *process_page(page *p) override
1298 return new cropmarks_page(p, &cm);
1302 cropmarks_cmd(cmd *c) : cm(c) { }
1305 static const arg_def cropmarks_args[] = {
1310 /*** Command table ***/
1312 template<typename T> cmd_exec *ctor(cmd *c) { return new T(c); }
1314 const cmd_def cmd_table[] = {
1315 { "null", no_args, 0, &ctor<null_cmd> },
1316 { "move", move_args, 0, &ctor<move_cmd> },
1317 { "scale", scale_args, 0, &ctor<scale_cmd> },
1318 { "rotate", rotate_args, 0, &ctor<rotate_cmd> },
1319 { "flip", flip_args, 0, &ctor<flip_cmd> },
1320 { "select", no_args, 1, &ctor<select_cmd> },
1321 { "apply", no_args, 1, &ctor<apply_cmd> },
1322 { "modulo", modulo_args, 1, &ctor<modulo_cmd> },
1323 { "draw-bbox",no_args, 0, &ctor<draw_bbox_cmd> },
1324 { "merge", no_args, 0, &ctor<merge_cmd> },
1325 { "paper", paper_args, 0, &ctor<paper_cmd> },
1326 { "scaleto", scaleto_args, 0, &ctor<scaleto_cmd> },
1327 { "fit", fit_args, 0, &ctor<fit_cmd> },
1328 { "expand", expand_args, 0, &ctor<expand_cmd> },
1329 { "margins", margins_args, 0, &ctor<margins_cmd> },
1330 { "add-blank",add_blank_args, 0, &ctor<add_blank_cmd> },
1331 { "book", book_args, 0, &ctor<book_cmd> },
1332 { "nup", nup_args, 0, &ctor<nup_cmd> },
1333 { "cropmarks",cropmarks_args, 0, &ctor<cropmarks_cmd> },
1334 { NULL, NULL, 0, NULL }