]> mj.ucw.cz Git - paperjam.git/blob - jam.h
Simplify passing of arguments
[paperjam.git] / jam.h
1 /*
2  *      PaperJam -- Common declarations
3  *
4  *      (c) 2018 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <vector>
8 #include <list>
9 #include <unordered_map>
10
11 using namespace std;
12
13 typedef unsigned int uint;
14
15 #define NONRET __attribute__((noreturn))
16 #define UNUSED __attribute__((unused))
17 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
18
19 #include "pdf-tools.h"
20
21 /*** Representation of commands ***/
22
23 struct pipeline;
24 struct cmd;
25
26 enum arg_type {
27   AT_STRING,
28   AT_INT,
29   AT_DOUBLE,
30   AT_DIMEN,
31   AT_SWITCH,
32   AT_TYPE_MASK = 0xffff,
33   AT_MANDATORY = 0x10000,
34   AT_POSITIONAL = 0x20000,
35 };
36
37 struct arg_def {
38   const char *name;
39   uint type;
40 };
41
42 class arg_val {
43 public:
44   virtual bool given() { return false; }
45   virtual int as_int(int def) { return def; }
46   virtual double as_double(double def) { return def; }
47   virtual const string as_string(const string def) { return def; }
48   virtual string dump() { return "<undef>"; }
49 };
50
51 extern arg_val null_arg;
52
53 struct out_context {
54   QPDFObjectHandle resources;
55   QPDFObjectHandle xobjects;
56   string contents;
57   int res_cnt;
58   string new_resource(const string type);
59   out_context() { res_cnt = 0; }
60 };
61
62 struct page {
63   int index;
64   double width;         // Physical dimensions of media
65   double height;
66   BBox bbox;            // Bounds useful contents
67   virtual void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { abort(); }
68   page(double _w=0, double _h=0) : width(_w), height(_h) { }
69   page(page *p) {
70     index = p->index;
71     width = p->width;
72     height = p->height;
73     bbox = p->bbox;
74   }
75 };
76
77 struct empty_page : public page {
78   void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { }
79   empty_page(double _w=0, double _h=0) : page(_w, _h) { };
80 };
81
82 struct cmd_exec {
83   virtual vector<page *> process(vector <page *> &pages UNUSED) = 0;
84 };
85
86 struct cmd_def {
87   const char *name;
88   const arg_def *arg_defs;
89   bool has_pipeline;
90   cmd_exec *(*constructor)(cmd *cmd);
91 };
92
93 struct cmd {
94   const cmd_def *def;
95   unordered_map<string, arg_val *> args;
96   pipeline *pipe;
97   cmd_exec *exec;
98   arg_val *arg(const string name)
99     {
100       auto it = args.find(name);
101       if (it != args.end())
102         return it->second;
103       else
104         return &null_arg;
105     }
106 };
107
108 struct pipeline_selector {
109   int from;
110   int to;
111 };
112
113 struct pipeline_branch {
114   vector<pipeline_selector> selectors;
115   list<cmd *> commands;
116 };
117
118 struct pipeline {
119   vector<pipeline_branch *> branches;
120 };
121
122 /*** Modules ***/
123
124 // paperjam.cc
125
126 extern const char *in_name, *out_name;
127 extern bool recalc_bbox;
128 extern int debug_mode;
129 extern int debug_indent;
130
131 void debug(const char *msg, ...) FORMAT_CHECK(printf, 1, 2);
132 void warn(const char *msg, ...) FORMAT_CHECK(printf, 1, 2);
133 void die(const char *msg, ...) FORMAT_CHECK(printf, 1, 2) NONRET;
134
135 // parse.cc
136
137 void parse(const char *in, list<cmd *> &cmds);
138 void parser_help();
139
140 // cmds.cc
141
142 extern const cmd_def cmd_table[];
143
144 // pdf.cc
145
146 void debug_pages(vector<page *> &pages);
147 void process(list<cmd *> &cmds);
148 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages);
149 vector<BBox> gs_bboxes(const char *in);