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 }, \
237 rgb[0] = rgb[1] = rgb[2] = 0;
242 die("Invalid color specification \"%s\": expecting 6 hex digits", s.c_str());
243 for (int i=0; i<3; i++)
246 for (int j=0; j<2; j++)
249 if (c >= '0' && c <= '9')
250 x = (x << 4) | (c - '0');
251 else if (c >= 'a' && c <= 'f')
252 x = (x << 4) | (c - 'a' + 10);
253 else if (c >= 'A' && c <= 'F')
254 x = (x << 4) | (c - 'A' + 10);
261 return pdf_coord(rgb[0]) + " " + pdf_coord(rgb[1]) + " " + pdf_coord(rgb[2]);
267 class cropmark_spec {
287 egstate = QPDFObjectHandle::newNull();
289 cropmark_spec(cmd *c, const string prefix="", const string def_type="cross") : color(c->arg(prefix + "color")->as_string("000000"))
291 string t = c->arg(prefix + "mark")->as_string(def_type);
296 else if (t == "cross")
305 die("Invalid cropmark type %s", t.c_str());
307 pen_width = c->arg(prefix + "pen")->as_double(0.2);
308 arm_length = c->arg(prefix + "len")->as_double(5*mm);
309 offset = c->arg(prefix + "offset")->as_double(0);
310 egstate = QPDFObjectHandle::newNull();
312 bool is_bg() { return (type == MARK_BG); }
313 string pdf_stream(out_context *out, BBox &box, pdf_matrix &xform);
315 string crop_cross(double x, double y, uint mask);
316 QPDFObjectHandle egstate;
319 string cropmark_spec::crop_cross(double x, double y, uint mask)
323 for (uint i=0; i<4; i++)
324 if (mask & (1U << i))
326 double x2 = x, y2 = y;
329 case 3: x2 -= arm_length; break;
330 case 2: x2 += arm_length; break;
331 case 1: y2 += arm_length; break;
332 case 0: y2 -= arm_length; break;
334 s += pdf_coord(x) + " " + pdf_coord(y) + " m " + pdf_coord(x2) + " " + pdf_coord(y2) + " l S ";
340 string cropmark_spec::pdf_stream(out_context *out, BBox &box, pdf_matrix &xform)
342 if (type == MARK_NONE)
346 s += color.to_string() + (type == MARK_BG ? " rg " : " RG ");
347 s += xform.to_string() + " cm ";
349 if (egstate.isNull())
351 auto egs = QPDFObjectHandle::newDictionary();
352 egs.replaceKey("/Type", QPDFObjectHandle::newName("/ExtGState"));
353 egs.replaceKey("/LW", QPDFObjectHandle::newReal(pen_width, 1));
354 egs.replaceKey("/LC", QPDFObjectHandle::newInteger(2));
355 egs.replaceKey("/LJ", QPDFObjectHandle::newInteger(0));
356 egstate = out->pdf->makeIndirectObject(egs);
359 string egs_res = out->new_resource("GS");
360 out->egstates.replaceKey(egs_res, egstate);
361 s += egs_res + " gs ";
374 s += b.to_rect() + " re S ";
377 s += crop_cross(b.x_min, b.y_min, 0b1111);
378 s += crop_cross(b.x_max, b.y_min, 0b1111);
379 s += crop_cross(b.x_max, b.y_max, 0b1111);
380 s += crop_cross(b.x_min, b.y_max, 0b1111);
383 s += crop_cross(b.x_min, b.y_min, 0b0110);
384 s += crop_cross(b.x_max, b.y_min, 0b1010);
385 s += crop_cross(b.x_max, b.y_max, 0b1001);
386 s += crop_cross(b.x_min, b.y_max, 0b0101);
389 s += crop_cross(b.x_min, b.y_min, 0b1001);
390 s += crop_cross(b.x_max, b.y_min, 0b0101);
391 s += crop_cross(b.x_max, b.y_max, 0b0110);
392 s += crop_cross(b.x_min, b.y_max, 0b1010);
395 s += b.to_rect() + " re f ";
403 #define CROPMARK_ARGS(px) \
404 { px "mark", AT_STRING }, \
405 { px "pen", AT_DIMEN }, \
406 { px "len", AT_DIMEN }, \
407 { px "offset",AT_DIMEN }, \
408 { px "color", AT_STRING }
410 // Scaling preserving aspect ratio
412 double scale_to_fit(BBox &from, BBox &to)
414 double fw = from.width(), fh = from.height();
415 double tw = to.width(), th = to.height();
416 if (is_zero(fw) || is_zero(fh) || is_zero(tw) || is_zero(th))
419 return min(tw/fw, th/fh);
424 class move_cmd : public cmd_exec_simple {
429 x = c->arg("x")->as_double(0);
430 y = c->arg("y")->as_double(0);
432 page *process_page(page *p) override
436 return new xform_page(p, m);
440 static const arg_def move_args[] = {
441 { "x", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
442 { "y", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
448 class scale_cmd : public cmd_exec_simple {
449 double x_factor, y_factor;
453 x_factor = c->arg("x")->as_double(1);
454 y_factor = c->arg("y")->as_double(x_factor);
456 page *process_page(page *p) override
459 m.scale(x_factor, y_factor);
460 return new xform_page(p, m);
464 static const arg_def scale_args[] = {
465 { "x", AT_DOUBLE | AT_MANDATORY | AT_POSITIONAL },
466 { "y", AT_DOUBLE | AT_POSITIONAL },
472 class rotate_cmd : public cmd_exec_simple {
477 deg = c->arg("deg")->as_int(0) % 360;
481 die("Rotate requires a multiple of 90 degrees");
483 page *process_page(page *p) override
492 m.shift(0, p->width);
496 m.shift(p->width, p->height);
500 m.shift(p->height, 0);
505 return new xform_page(p, m);
509 static const arg_def rotate_args[] = {
510 { "angle", AT_INT | AT_MANDATORY | AT_POSITIONAL },
516 class flip_cmd : public cmd_exec_simple {
522 horizontal = c->arg("h")->as_int(0);
523 vertical = c->arg("v")->as_int(0);
524 if (!horizontal && !vertical)
525 die("Flip has no direction specified");
527 page *process_page(page *p) override
533 m.shift(0, p->height);
538 m.shift(p->width, 0);
540 return new xform_page(p, m);
544 static const arg_def flip_args[] = {
552 class select_cmd : public cmd_exec {
559 vector<page *> process(vector<page *> &pages) override;
562 static int validate_page_index(vector<page *> &pages, int idx)
564 if (idx >= 1 && idx <= (int) pages.size())
566 if (idx <= -1 && idx >= (int) -pages.size())
567 return idx + pages.size();
568 die("Page index %d out of range", idx);
571 vector<page *> select_cmd::process(vector<page *> &pages)
574 for (auto pb: pipe->branches)
576 vector<page *> selected;
577 for (auto ps: pb->selectors)
579 int f = validate_page_index(pages, ps.from);
580 int t = validate_page_index(pages, ps.to);
581 int step = (f <= t) ? 1 : -1;
582 for (int i=f; f<=t; f += step)
583 selected.push_back(pages[i]);
585 auto processed = run_command_list(pb->commands, selected);
586 for (auto p: processed)
594 class apply_cmd : public cmd_exec {
601 vector<page *> process(vector<page *> &pages) override;
604 static pipeline_branch *find_branch(pipeline *pipe, vector <page *> &pages, int idx)
606 for (auto pb: pipe->branches)
607 for (auto ps: pb->selectors)
609 int f = validate_page_index(pages, ps.from);
610 int t = validate_page_index(pages, ps.to);
611 if (f <= idx && idx <= t || t <= idx && idx <= f)
617 vector<page *> apply_cmd::process(vector<page *> &pages)
624 pipeline_branch *pb = find_branch(pipe, pages, cnt);
629 auto processed = run_command_list(pb->commands, tmp);
630 for (auto q: processed)
643 class modulo_cmd : public cmd_exec {
650 n = c->arg("n")->as_int(0);
652 die("Modulo must have n > 0");
653 half = c->arg("half")->as_int(0);
656 vector<page *> process(vector<page *> &pages) override;
659 vector<page *> modulo_cmd::process(vector<page *> &pages)
662 int tuples = ((int) pages.size() + n - 1) / n;
663 int use_tuples = half ? tuples/2 : tuples;
665 for (int tuple=0; tuple < use_tuples; tuple++)
667 debug("# Tuple %d", tuple);
669 for (auto pb: pipe->branches)
672 for (auto ps: pb->selectors)
676 int step = (f <= t) ? 1 : -1;
677 for (int i=f; i<=t; i += step)
682 else if (i < 0 && i >= -n)
683 j = (tuples-1-tuple)*n + (-i) - 1;
685 die("Modulo: invalid index %d", i);
686 if (j < (int) pages.size())
687 tmp.push_back(pages[j]);
690 page *ref_page = pages[tuple*n];
691 tmp.push_back(new empty_page(ref_page->width, ref_page->height));
695 auto processed = run_command_list(pb->commands, tmp);
696 for (auto q: processed)
705 static const arg_def modulo_args[] = {
706 { "n", AT_INT | AT_MANDATORY | AT_POSITIONAL },
707 { "half", AT_SWITCH },
713 class debug_page : public page {
715 cropmark_spec *page_cm, *image_cm;
717 debug_page(page *p, cropmark_spec *page_cms, cropmark_spec *image_cms) : page(p), orig_page(p), page_cm(page_cms), image_cm(image_cms) { }
718 void render(out_context *out, pdf_matrix xform) override
720 orig_page->render(out, xform);
721 BBox page_bbox = BBox(0, 0, width, height);
722 out->contents += page_cm->pdf_stream(out, page_bbox, xform);
723 out->contents += image_cm->pdf_stream(out, image_box, xform);
727 class debug_cmd : public cmd_exec_simple {
728 cropmark_spec page_cmarks;
729 cropmark_spec image_cmarks;
731 debug_cmd(cmd *c UNUSED)
733 page_cmarks.type = cropmark_spec::MARK_BOX;
734 page_cmarks.color.rgb[0] = 1;
735 page_cmarks.color.rgb[1] = 0;
736 page_cmarks.color.rgb[2] = 0;
737 image_cmarks.type = cropmark_spec::MARK_BOX;
738 image_cmarks.color.rgb[0] = 0;
739 image_cmarks.color.rgb[1] = 1;
740 image_cmarks.color.rgb[2] = 0;
742 page *process_page(page *p) override
744 return new debug_page(p, &page_cmarks, &image_cmarks);
750 class merge_cmd : public cmd_exec {
752 merge_cmd(cmd *c UNUSED) { }
753 vector<page *> process(vector<page *> &pages) override;
756 class merge_page : public page {
757 vector<page *> orig_pages;
759 merge_page(vector<page *> &orig) : page(0, 0)
769 image_box = p->image_box;
774 if (!is_equal(width, p->width) || !is_equal(height, p->height))
775 die("All pages participating in a merge must have the same dimensions");
776 image_box.join(p->image_box);
780 void render(out_context *out, pdf_matrix xform) override
782 for (auto p: orig_pages)
783 p->render(out, xform);
787 vector<page *> merge_cmd::process(vector<page *> &pages)
791 out.push_back(new merge_page(pages));
797 class paper_cmd : public cmd_exec_simple {
801 paper_cmd(cmd *c) : paper(c), pos(c) { }
802 page *process_page(page *p) override
804 BBox paper_box = BBox(paper.w, paper.h);
805 pdf_matrix xf = pos.place(p->image_box, paper_box);
806 page *q = new xform_page(p, xf);
813 static const arg_def paper_args[] = {
821 class scaleto_cmd : public cmd_exec_simple {
825 scaleto_cmd(cmd *c) : paper(c), pos(c) { }
826 page *process_page(page *p) override
828 BBox orig_box = BBox(p->width, p->height);
829 BBox paper_box = BBox(paper.w, paper.h);
831 xf.scale(scale_to_fit(orig_box, paper_box));
832 orig_box.transform(xf);
833 xf.concat(pos.place(orig_box, paper_box));
834 page *q = new xform_page(p, xf);
841 static const arg_def scaleto_args[] = {
849 class fit_cmd : public cmd_exec_simple {
854 fit_cmd(cmd *c) : paper(c, true), pos(c), marg(c, "margin", "margin") { }
855 page *process_page(page *p) override
860 if (!is_zero(paper.w) && !is_zero(paper.h))
862 // Paper given: scale image to fit paper
863 BBox orig_box = p->image_box;
864 BBox paper_box = BBox(paper.w, paper.h);
865 marg.shrink_box(&paper_box);
866 xf.scale(scale_to_fit(orig_box, paper_box));
867 orig_box.transform(xf);
868 xf.concat(pos.place(orig_box, paper_box));
869 q = new xform_page(p, xf);
875 // No paper given: adjust paper to fit image
876 xf.shift(-p->image_box.x_min, -p->image_box.y_min);
877 xf.shift(marg.l, marg.b);
878 q = new xform_page(p, xf);
879 q->width = p->image_box.width() + marg.l + marg.r;
880 q->height = p->image_box.height() + marg.t + marg.b;
886 static const arg_def fit_args[] = {
889 MARGIN_ARGS1_NAMED("margin"),
890 MARGIN_ARGS2("margin"),
896 class expand_cmd : public cmd_exec_simple {
899 expand_cmd(cmd *c) : marg(c, "by", "") { }
900 page *process_page(page *p) override
903 xf.shift(marg.l, marg.b);
904 page *q = new xform_page(p, xf);
905 q->width = p->width + marg.l + marg.r;
906 q->height = p->height + marg.t + marg.b;
907 if (q->width < 0.001 || q->height < 0.001)
908 die("Expansion must result in positive page dimensions");
913 static const arg_def expand_args[] = {
914 MARGIN_ARGS1_POSNL("by"),
921 class margins_cmd : public cmd_exec_simple {
924 margins_cmd(cmd *c) : marg(c, "size", "") { }
925 page *process_page(page *p) override
928 xf.shift(-p->image_box.x_min, -p->image_box.y_min);
929 xf.shift(marg.l, marg.b);
930 page *q = new xform_page(p, xf);
931 q->width = p->image_box.width() + marg.l + marg.r;
932 q->height = p->image_box.height() + marg.t + marg.b;
933 if (q->width < 0.001 || q->height < 0.001)
934 die("Margins must result in positive page dimensions");
939 static const arg_def margins_args[] = {
940 MARGIN_ARGS1_POSNL("size"),
947 class add_blank_cmd : public cmd_exec {
951 add_blank_cmd(cmd *c) : paper(c, true)
953 n = c->arg("n")->as_int(1);
955 vector<page *> process(vector<page *> &pages) override;
958 vector<page *> add_blank_cmd::process(vector<page *> &pages)
965 for (int i=0; i<n; i++)
967 double w = paper.w, h = paper.h;
968 if (is_zero(w) || is_zero(h))
969 w = p->width, h = p->height;
970 out.push_back(new empty_page(w, h));
977 static const arg_def add_blank_args[] = {
978 { "n", AT_INT | AT_POSITIONAL },
985 class book_cmd : public cmd_exec {
990 n = c->arg("n")->as_int(0);
992 die("Number of pages per signature must be divisible by 4");
994 vector<page *> process(vector<page *> &pages) override;
997 vector<page *> book_cmd::process(vector<page *> &pages)
999 vector<page *> in, out;
1002 while (in.size() % 4)
1003 in.push_back(new empty_page(in[0]->width, in[0]->height));
1006 while (i < (int) in.size())
1008 int sig = in.size() - i;
1011 for (int j=0; j<sig/2; j+=2)
1013 out.push_back(in[i + sig-1-j]);
1014 out.push_back(in[i + j]);
1015 out.push_back(in[i + j+1]);
1016 out.push_back(in[i + sig-2-j]);
1024 static const arg_def book_args[] = {
1025 { "n", AT_INT | AT_POSITIONAL },
1033 double tile_w, tile_h;
1034 double paper_w, paper_h;
1039 class nup_cmd : public cmd_exec {
1041 int grid_n, grid_m, num_tiles;
1055 double hspace, vspace;
1056 cropmark_spec cmarks;
1059 page *process_single(vector<page *> &in);
1060 void find_config(vector<page *> &in, BBox *page_boxes);
1061 void try_config(nup_state &st);
1063 bool found_solution;
1064 BBox common_page_box;
1067 nup_cmd(cmd *c) : paper(c), marg(c, "margin", "margin"), pos(c), tpos(c->arg("tpos")->as_string("tl")), cmarks(c, "c", "none")
1069 grid_n = c->arg("n")->as_int(0);
1070 grid_m = c->arg("m")->as_int(0);
1071 if (grid_n > 0 && grid_m > 0)
1072 num_tiles = grid_n * grid_m;
1073 else if (grid_n > 0 && !grid_m)
1076 die("Grid size must be at least 1x1");
1078 const string by = c->arg("by")->as_string("rows");
1079 if (by == "rows" || by == "row" || by == "r")
1081 else if (by == "cols" || by == "cols" || by == "c")
1083 else if (by == "tile" || by == "t")
1086 die("Parameter \"by\" must be rows/cols/tile");
1088 crop = c->arg("crop")->as_int(0);
1089 mixed = c->arg("mixed")->as_int(0);
1090 rotate = c->arg("rotate")->as_int(-1);
1091 scale = c->arg("scale")->as_double(0);
1093 double space = c->arg("space")->as_double(0);
1094 hspace = c->arg("hspace")->as_double(space);
1095 vspace = c->arg("vspace")->as_double(space);
1097 if (!is_zero(scale) && (!is_zero(paper.w) || rotate >= 0))
1098 die("When nup is used with explicit scaling, paper size nor rotation may be given");
1099 if (!is_zero(scale) && !grid_m)
1100 die("When nup is used with explicit scaling, both grid sizes must be given");
1103 vector<page *> process(vector<page *> &pages) override;
1106 vector<page *> nup_cmd::process(vector<page *> &pages)
1110 // Unless mixed is given, find the common page size
1113 for (int i=0; i < (int) pages.size(); i++)
1116 BBox pb = crop ? p->image_box : BBox(p->width, p->height);
1118 common_page_box = pb;
1120 common_page_box.join(pb);
1122 debug("NUP: Common page box [%.3f %.3f]-[%.3f %.3f]",
1123 common_page_box.x_min, common_page_box.y_min, common_page_box.x_max, common_page_box.y_max);
1126 // Process one tile set after another
1128 while (i < (int) pages.size())
1131 if (fill_by == BY_TILE)
1133 for (int j=0; j<num_tiles; j++)
1134 in.push_back(pages[i]);
1139 for (int j=0; j<num_tiles; j++)
1141 if (i < (int) pages.size())
1142 in.push_back(pages[i]);
1144 in.push_back(new empty_page(in[0]->width, in[0]->height));
1148 out.push_back(process_single(in));
1154 void nup_cmd::try_config(nup_state &st)
1156 BBox window(st.paper_w, st.paper_h);
1157 if (!marg.may_shrink(&window))
1159 marg.shrink_box(&window);
1160 window.x_max -= (st.cols - 1) * hspace;
1161 window.y_max -= (st.rows - 1) * vspace;
1162 if (window.width() < 0 || window.height() < 0)
1165 BBox image(st.cols * st.tile_w, st.rows * st.tile_h);
1166 st.scale = scale_to_fit(image, window);
1167 st.fill_factor = (st.scale*image.width() * st.scale*image.height()) / (st.paper_w * st.paper_h);
1169 debug("Try: %dx%d on %.3f x %.3f => scale %.3f, fill %.6f",
1171 st.paper_w, st.paper_h,
1172 st.scale, st.fill_factor);
1174 if (!found_solution || best.fill_factor < st.fill_factor)
1176 found_solution = true;
1181 void nup_cmd::find_config(vector<page *> &in, BBox *page_boxes)
1185 // Determine tile size
1186 st.tile_w = st.tile_h = 0;
1187 for (int i=0; i<num_tiles; i++)
1190 page_boxes[i] = common_page_box;
1192 page_boxes[i] = in[i]->image_box;
1194 page_boxes[i] = BBox(in[i]->width, in[i]->height);
1195 st.tile_w = max(st.tile_w, page_boxes[i].width());
1196 st.tile_h = max(st.tile_h, page_boxes[i].height());
1198 debug("NUP: %d tiles of size [%.3f,%.3f]", num_tiles, st.tile_w, st.tile_h);
1201 // Try all possible configurations of tiles
1202 found_solution = false;
1203 if (!is_zero(scale))
1205 // If explicit scaling is requested, we choose page size ourselves,
1206 // so there is just one configuration.
1209 st.paper_w = marg.l + st.cols*st.tile_w + (st.cols-1)*hspace + marg.r;
1210 st.paper_h = marg.t + st.rows*st.tile_h + (st.rows-1)*vspace + marg.b;
1215 // Page size is fixed (either given or copied from the source pages),
1216 // but we can have freedom to rotate and/or to choose grid size.
1217 for (int rot=0; rot<=1; rot++)
1219 if (rotate >= 0 && rot != rotate)
1222 // Establish paper size
1223 if (!is_zero(paper.w))
1225 st.paper_w = paper.w;
1226 st.paper_h = paper.h;
1230 st.paper_w = in[0]->width;
1231 st.paper_h = in[0]->height;
1234 swap(st.paper_w, st.paper_h);
1245 for (int r=1; r<=grid_n; r++)
1249 st.cols = grid_n / r;
1256 if (!found_solution)
1257 die("Nup did not find a feasible solution");
1258 debug("Best: %dx%d on %.3f x %.3f", best.cols, best.rows, best.paper_w, best.paper_h);
1262 class nup_page : public page {
1264 vector<page *> orig_pages;
1265 vector<pdf_matrix> xforms;
1266 vector<BBox> tile_boxes;
1267 cropmark_spec *cmarks;
1268 void render(out_context *out, pdf_matrix xform) override;
1269 nup_page(nup_state &st) : page(st.paper_w, st.paper_h) { }
1272 void nup_page::render(out_context *out, pdf_matrix parent_xform)
1274 for (int i=0; i < (int) orig_pages.size(); i++)
1276 if (cmarks->is_bg())
1277 out->contents += cmarks->pdf_stream(out, tile_boxes[i], parent_xform);
1278 orig_pages[i]->render(out, xforms[i] * parent_xform);
1279 if (!cmarks->is_bg())
1280 out->contents += cmarks->pdf_stream(out, tile_boxes[i], parent_xform);
1284 page *nup_cmd::process_single(vector<page *> &in)
1286 BBox page_boxes[num_tiles];
1287 find_config(in, page_boxes);
1288 double tw = best.scale * best.tile_w;
1289 double th = best.scale * best.tile_h;
1291 // Construct transform from paper to grid of tiles
1292 BBox paper_box(best.paper_w, best.paper_h);
1293 marg.shrink_box(&paper_box);
1294 BBox grid_box(best.cols * tw + (best.cols-1) * hspace,
1295 best.rows * th + (best.rows-1) * vspace);
1296 pdf_matrix place_xform = pos.place(grid_box, paper_box);
1298 nup_page *p = new nup_page(best);
1299 p->image_box = grid_box;
1300 p->image_box.transform(place_xform);
1302 for (int i=0; i<num_tiles; i++)
1305 if (fill_by == BY_ROWS || fill_by == BY_TILE)
1317 BBox &page_box = page_boxes[i];
1318 m.shift(-page_box.x_min, -page_box.y_min);
1319 m.scale(best.scale);
1320 page_box.transform(m);
1322 double x = c * (tw + hspace);
1323 double y = (best.rows-1-r) * (th + vspace);
1324 BBox tile_box = BBox(x, y, x+tw, y+th);
1325 m.concat(tpos.place(page_box, tile_box));
1327 p->orig_pages.push_back(in[i]);
1328 p->xforms.push_back(m * place_xform);
1329 p->tile_boxes.push_back(tile_box.transformed(place_xform));
1330 p->cmarks = &cmarks;
1336 static const arg_def nup_args[] = {
1337 { "n", AT_INT | AT_POSITIONAL | AT_MANDATORY },
1338 { "m", AT_INT | AT_POSITIONAL },
1339 { "by", AT_STRING },
1340 { "crop", AT_SWITCH },
1341 { "mixed", AT_SWITCH },
1342 { "rotate", AT_SWITCH },
1343 { "scale", AT_DOUBLE },
1345 MARGIN_ARGS1_NAMED("margin"),
1346 MARGIN_ARGS2("margin"),
1349 { "tpos", AT_STRING },
1350 { "space", AT_DIMEN },
1351 { "hspace", AT_DIMEN },
1352 { "vspace", AT_DIMEN },
1358 class cropmarks_page : public page {
1362 void render(out_context *out, pdf_matrix xform) override
1364 orig_page->render(out, xform);
1365 out->contents += cm->pdf_stream(out, image_box, xform);
1367 cropmarks_page(page *p, cropmark_spec *cms) : page(p), orig_page(p), cm(cms) { }
1370 class cropmarks_cmd : public cmd_exec_simple {
1372 page *process_page(page *p) override
1374 return new cropmarks_page(p, &cm);
1378 cropmarks_cmd(cmd *c) : cm(c) { }
1381 static const arg_def cropmarks_args[] = {
1386 /*** Command table ***/
1388 template<typename T> cmd_exec *ctor(cmd *c) { return new T(c); }
1390 const cmd_def cmd_table[] = {
1391 { "null", no_args, 0, &ctor<null_cmd> },
1392 { "move", move_args, 0, &ctor<move_cmd> },
1393 { "scale", scale_args, 0, &ctor<scale_cmd> },
1394 { "rotate", rotate_args, 0, &ctor<rotate_cmd> },
1395 { "flip", flip_args, 0, &ctor<flip_cmd> },
1396 { "select", no_args, 1, &ctor<select_cmd> },
1397 { "apply", no_args, 1, &ctor<apply_cmd> },
1398 { "modulo", modulo_args, 1, &ctor<modulo_cmd> },
1399 { "debug", no_args, 0, &ctor<debug_cmd> },
1400 { "merge", no_args, 0, &ctor<merge_cmd> },
1401 { "paper", paper_args, 0, &ctor<paper_cmd> },
1402 { "scaleto", scaleto_args, 0, &ctor<scaleto_cmd> },
1403 { "fit", fit_args, 0, &ctor<fit_cmd> },
1404 { "expand", expand_args, 0, &ctor<expand_cmd> },
1405 { "margins", margins_args, 0, &ctor<margins_cmd> },
1406 { "add-blank",add_blank_args, 0, &ctor<add_blank_cmd> },
1407 { "book", book_args, 0, &ctor<book_cmd> },
1408 { "nup", nup_args, 0, &ctor<nup_cmd> },
1409 { "cropmarks",cropmarks_args, 0, &ctor<cropmarks_cmd> },
1410 { NULL, NULL, 0, NULL }