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