9 class null_cmd : public cmd_exec {
10 vector<page *> process(vector<page *> &pages) { return pages; }
13 static const arg_def no_args[] = {
17 static cmd_exec *null_ctor(cmd *c UNUSED)
24 class move_cmd : public cmd_exec {
27 vector<page *> process(vector<page *> &pages);
30 class xform_page : public page {
34 void render(page_out *out, pdf_matrix xform);
35 xform_page(page *_orig, double _w, double _h) : page(_w, _h), orig_page(_orig) { }
38 void xform_page::render(page_out *out, pdf_matrix parent_xform)
40 orig_page->render(out, xform * parent_xform);
43 vector<page *> move_cmd::process(vector<page *> &pages)
48 xform_page *q = new xform_page(p, p->width, p->height);
55 static const arg_def move_args[] = {
56 { "x", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
57 { "y", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL },
61 static cmd_exec *move_ctor(cmd *c)
63 move_cmd *m = new move_cmd;
64 m->x = c->args.at(0)->as_double(0);
65 m->y = c->args.at(1)->as_double(0);
71 class scale_cmd : public cmd_exec {
73 double x_factor, y_factor;
74 vector<page *> process(vector<page *> &pages);
77 vector<page *> scale_cmd::process(vector<page *> &pages)
82 xform_page *q = new xform_page(p, x_factor*p->width, y_factor*p->height);
83 q->xform.scale(x_factor, y_factor);
89 static const arg_def scale_args[] = {
90 { "x", AT_DOUBLE | AT_MANDATORY | AT_POSITIONAL },
91 { "y", AT_DOUBLE | AT_POSITIONAL },
95 static cmd_exec *scale_ctor(cmd *c)
97 scale_cmd *s = new scale_cmd;
98 s->x_factor = c->args.at(0)->as_double(1);
99 s->y_factor = c->args.at(1)->as_double(s->x_factor);
105 class rotate_cmd : public cmd_exec {
108 vector<page *> process(vector<page *> &pages);
111 vector<page *> rotate_cmd::process(vector<page *> &pages)
116 xform_page *q = new xform_page(p, p->width, p->height);
122 q->xform.rotate_deg(-90);
123 q->xform.shift(0, p->width);
124 swap(q->width, q->height);
127 q->xform.rotate_deg(180);
128 q->xform.shift(p->width, p->height);
131 q->xform.rotate_deg(90);
132 q->xform.shift(p->height, 0);
133 swap(q->width, q->height);
143 static const arg_def rotate_args[] = {
144 { "angle", AT_INT | AT_MANDATORY | AT_POSITIONAL },
148 static cmd_exec *rotate_ctor(cmd *c)
150 rotate_cmd *r = new rotate_cmd;
151 r->deg = c->args.at(0)->as_int(0) % 360;
155 die("Rotate requires a multiple of 90 degrees");
161 class select_cmd : public cmd_exec {
164 vector<page *> process(vector<page *> &pages);
167 static int validate_page_index(vector<page *> &pages, int idx)
169 if (idx >= 1 && idx <= (int) pages.size())
171 if (idx <= -1 && idx >= (int) -pages.size())
172 return idx + pages.size();
173 die("Page index %d out of range", idx);
176 vector<page *> select_cmd::process(vector<page *> &pages)
179 for (auto pb: pipe->branches)
180 for (auto ps: pb->selectors)
182 int f = validate_page_index(pages, ps.from);
183 int t = validate_page_index(pages, ps.to);
184 int step = (f <= t) ? 1 : -1;
185 for (int i=f; f<=t; f += step)
187 vector<page *> selected;
188 selected.push_back(pages[i]);
189 selected = run_command_list(pb->commands, selected);
190 for (auto p: selected)
197 static cmd_exec *select_ctor(cmd *c)
199 select_cmd *r = new select_cmd;
204 /*** Command table ***/
206 const cmd_def cmd_table[] = {
207 { "null", no_args, 0, null_ctor },
208 { "move", move_args, 0, move_ctor },
209 { "scale", scale_args, 0, scale_ctor },
210 { "rotate", rotate_args, 0, rotate_ctor },
211 { "select", no_args, 1, select_ctor },
212 { NULL, NULL, 0, NULL }