]> mj.ucw.cz Git - paperjam.git/blob - jam.h
Implemented merge
[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) : index(0), width(_w), height(_h), bbox() { }
69   page(page *p)
70     {
71       index = p->index;
72       width = p->width;
73       height = p->height;
74       bbox = p->bbox;
75     }
76 };
77
78 struct empty_page : public page {
79   void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { }
80   empty_page(double _w=0, double _h=0) : page(_w, _h) { };
81 };
82
83 struct cmd_exec {
84   virtual vector<page *> process(vector <page *> &pages UNUSED) = 0;
85 };
86
87 struct cmd_def {
88   const char *name;
89   const arg_def *arg_defs;
90   bool has_pipeline;
91   cmd_exec *(*constructor)(cmd *cmd);
92 };
93
94 struct cmd {
95   const cmd_def *def;
96   unordered_map<string, arg_val *> args;
97   pipeline *pipe;
98   cmd_exec *exec;
99   arg_val *arg(const string name)
100     {
101       auto it = args.find(name);
102       if (it != args.end())
103         return it->second;
104       else
105         return &null_arg;
106     }
107 };
108
109 struct pipeline_selector {
110   int from;
111   int to;
112 };
113
114 struct pipeline_branch {
115   vector<pipeline_selector> selectors;
116   list<cmd *> commands;
117 };
118
119 struct pipeline {
120   vector<pipeline_branch *> branches;
121 };
122
123 /*** Modules ***/
124
125 // paperjam.cc
126
127 extern const char *in_name, *out_name;
128 extern bool recalc_bbox;
129 extern int debug_mode;
130 extern int debug_indent;
131
132 void debug(const char *msg, ...) FORMAT_CHECK(printf, 1, 2);
133 void warn(const char *msg, ...) FORMAT_CHECK(printf, 1, 2);
134 void die(const char *msg, ...) FORMAT_CHECK(printf, 1, 2) NONRET;
135
136 // parse.cc
137
138 void parse(const char *in, list<cmd *> &cmds);
139 void parser_help();
140
141 // cmds.cc
142
143 extern const cmd_def cmd_table[];
144
145 // pdf.cc
146
147 void debug_pages(vector<page *> &pages);
148 void process(list<cmd *> &cmds);
149 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages);
150 vector<BBox> gs_bboxes(const char *in);