From: Martin Mares Date: Sat, 31 Mar 2018 22:37:42 +0000 (+0200) Subject: Bits of commands X-Git-Tag: v0.1~47 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=4f08fc1b75c10b9149e60833fb4deb17f414e0df;p=paperjam.git Bits of commands --- diff --git a/cmds.cc b/cmds.cc index 94ee09e..10dd4e0 100644 --- a/cmds.cc +++ b/cmds.cc @@ -7,6 +7,7 @@ /*** null ***/ class null_cmd : public cmd_exec { + vector process(vector pages) { return pages; } }; static const arg_def null_args[] = { @@ -23,12 +24,37 @@ static cmd_exec *null_ctor(cmd *c UNUSED) class move_cmd : public cmd_exec { public: double x, y; + vector process(vector pages); }; +class xform_page : public page { + page *orig_page; +public: + pdf_matrix xform; + void render(page_out *out, pdf_matrix xform); + xform_page(page *_orig, double _w, double _h) : page(_w, _h), orig_page(_orig) { } +}; + +void xform_page::render(page_out *out, pdf_matrix parent_xform) +{ + orig_page->render(out, xform * parent_xform); +} + +vector move_cmd::process(vector pages) +{ + vector out; + for (auto p: pages) + { + xform_page *q = new xform_page(p, p->width, p->height); + q->xform.shift(x, y); + out.push_back(q); + } + return out; +} + static const arg_def move_args[] = { { "x", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL }, { "y", AT_DIMEN | AT_MANDATORY | AT_POSITIONAL }, - { "str", AT_STRING }, { NULL, 0 } }; @@ -40,10 +66,45 @@ static cmd_exec *move_ctor(cmd *c) return m; } +/*** scale ***/ + +class scale_cmd : public cmd_exec { +public: + double x_factor, y_factor; + vector process(vector pages); +}; + +vector scale_cmd::process(vector pages) +{ + vector out; + for (auto p: pages) + { + xform_page *q = new xform_page(p, x_factor*p->width, y_factor*p->height); + q->xform.scale(x_factor, y_factor); + out.push_back(q); + } + return out; +} + +static const arg_def scale_args[] = { + { "x", AT_DOUBLE | AT_MANDATORY | AT_POSITIONAL }, + { "y", AT_DOUBLE | AT_POSITIONAL }, + { NULL, 0 } +}; + +static cmd_exec *scale_ctor(cmd *c) +{ + scale_cmd *s = new scale_cmd; + s->x_factor = c->args.at(0)->double_default(1); + s->y_factor = c->args.at(1)->double_default(s->x_factor); + return s; +} + /*** Command table ***/ const cmd_def cmd_table[] = { - { "move", move_args, 1, move_ctor }, + { "move", move_args, 0, move_ctor }, + { "scale", scale_args, 0, scale_ctor }, { "null", null_args, 0, null_ctor }, { NULL, NULL, 0, NULL } }; diff --git a/jam.h b/jam.h index 6fa314c..f5d2ca1 100644 --- a/jam.h +++ b/jam.h @@ -43,10 +43,12 @@ struct page_out { struct page { double width; double height; - void render(page_out *out, pdf_matrix xform); + virtual void render(page_out *out UNUSED, pdf_matrix xform UNUSED) { abort(); } + page(double _w, double _h) : width(_w), height(_h) { } }; struct cmd_exec { + virtual vector process(vector pages UNUSED) { abort(); } }; struct cmd_def { diff --git a/parse.cc b/parse.cc index 576795d..c0b8a35 100644 --- a/parse.cc +++ b/parse.cc @@ -425,6 +425,19 @@ static void parse_commands(list *cmds) } } +static void instantiate(list *cmds) +{ + for (auto c: *cmds) + { + c->exec = c->def->constructor(c); + if (c->pipe) + { + for (auto pb: c->pipe->branches) + instantiate(&pb->commands); + } + } +} + void parse(const char *in, list *cmds) { in_pos = in; @@ -433,4 +446,5 @@ void parse(const char *in, list *cmds) parse_error("Extra tokens after commands"); debug_cmds(cmds); + instantiate(cmds); }