2 * PaperJam -- Common declarations
4 * (c) 2018 Martin Mares <mj@ucw.cz>
9 #include <unordered_map>
14 typedef unsigned int uint;
16 #define NONRET __attribute__((noreturn))
17 #define UNUSED __attribute__((unused))
18 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
20 static inline bool is_zero(double z)
22 return fabs(z) < 1e-6;
25 static inline bool is_equal(double x, double y)
30 #include "pdf-tools.h"
32 /*** Representation of commands ***/
43 AT_TYPE_MASK = 0xffff,
44 AT_MANDATORY = 0x10000,
45 AT_POSITIONAL = 0x20000,
56 virtual bool given() { return false; }
57 virtual int as_int(int def) { return def; }
58 virtual double as_double(double def) { return def; }
59 virtual const string as_string(const string def) { return def; }
60 virtual string dump() { return "<undef>"; }
63 extern arg_val null_arg;
67 QPDFObjectHandle resources;
68 QPDFObjectHandle xobjects;
69 QPDFObjectHandle egstates;
72 string new_resource(const string type);
73 out_context() { res_cnt = 0; }
77 int index; // Position in the source PDF, 0 for synthesized pages
78 double width; // Physical dimensions of media
80 BBox image_box; // Bounds useful contents
81 virtual void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { abort(); }
82 page(double _w=0, double _h=0) : index(0), width(_w), height(_h), image_box() { }
88 image_box = p->image_box;
92 struct empty_page : public page {
93 void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { }
94 empty_page(double _w=0, double _h=0) : page(_w, _h) { };
98 virtual vector<page *> process(vector <page *> &pages UNUSED) = 0;
103 const arg_def *arg_defs;
105 cmd_exec *(*constructor)(cmd *cmd);
111 unordered_map<string, arg_val *> args;
114 arg_val *arg(const string name)
116 auto it = args.find(name);
117 if (it != args.end())
124 struct pipeline_selector {
129 struct pipeline_branch {
130 vector<pipeline_selector> selectors;
131 list<cmd *> commands;
135 vector<pipeline_branch *> branches;
142 extern const char *in_name, *out_name;
143 extern bool recalc_bbox;
144 extern int debug_level;
145 extern int debug_indent;
147 void debug(const char *msg, ...) FORMAT_CHECK(printf, 1, 2);
148 void warn(const char *msg, ...) FORMAT_CHECK(printf, 1, 2);
149 void die(const char *msg, ...) FORMAT_CHECK(printf, 1, 2) NONRET;
151 // This one is called during execution of commands and propagated as an exception
152 void err(const char *msg, ...) FORMAT_CHECK(printf, 1, 2) NONRET;
154 class paperjam_error : public exception {
157 paperjam_error(string m) : message(m) { }
158 const char *what() const noexcept override { return message.c_str(); }
163 void parse(const char *in, list<cmd *> &cmds);
168 extern const cmd_def cmd_table[];
172 void debug_pages(vector<page *> &pages);
173 void process(list<cmd *> &cmds);
174 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages);
175 vector<BBox> gs_bboxes(const char *in);