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 void debug(const char *msg, ...) FORMAT_CHECK(printf, 1, 2);
31 void warn(const char *msg, ...) FORMAT_CHECK(printf, 1, 2);
32 void die(const char *msg, ...) FORMAT_CHECK(printf, 1, 2) NONRET;
34 // This one is called during execution of commands and propagated as an exception
35 void err(const char *msg, ...) FORMAT_CHECK(printf, 1, 2) NONRET;
37 #include "pdf-tools.h"
39 /*** Representation of commands ***/
50 AT_TYPE_MASK = 0xffff,
51 AT_MANDATORY = 0x10000,
52 AT_POSITIONAL = 0x20000,
63 virtual bool given() { return false; }
64 virtual int as_int(int def) { return def; }
65 virtual double as_double(double def) { return def; }
66 virtual const string as_string(const string def) { return def; }
67 virtual string dump() { return "<undef>"; }
70 extern arg_val null_arg;
74 QPDFObjectHandle resources;
75 QPDFObjectHandle xobjects;
76 QPDFObjectHandle egstates;
79 string new_resource(const string type);
80 out_context() { res_cnt = 0; }
84 int index; // Position in the source PDF, 0 for synthesized pages
85 double width; // Physical dimensions of media
87 BBox image_box; // Bounds useful contents
88 virtual void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { abort(); }
89 virtual void debug_dump() = 0;
90 page(double _w=0, double _h=0) : index(0), width(_w), height(_h), image_box() { }
96 image_box = p->image_box;
100 struct empty_page : public page {
101 void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { }
102 void debug_dump() { debug("Empty page"); }
103 empty_page(double _w=0, double _h=0) : page(_w, _h) { };
107 virtual vector<page *> process(vector <page *> &pages UNUSED) = 0;
112 const arg_def *arg_defs;
114 cmd_exec *(*constructor)(cmd *cmd);
120 unordered_map<string, arg_val *> args;
123 arg_val *arg(const string name)
125 auto it = args.find(name);
126 if (it != args.end())
133 struct pipeline_selector {
138 struct pipeline_branch {
139 vector<pipeline_selector> selectors;
140 list<cmd *> commands;
144 vector<pipeline_branch *> branches;
151 extern const char *in_name, *out_name;
152 extern bool recalc_bbox;
153 extern int debug_level;
154 extern int debug_indent;
156 class paperjam_error : public exception {
159 paperjam_error(string m) : message(m) { }
160 const char *what() const noexcept override { return message.c_str(); }
165 void parse(const char *in, list<cmd *> &cmds);
170 extern const cmd_def cmd_table[];
174 void debug_pages(vector<page *> &pages);
175 void process(list<cmd *> &cmds);
176 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages);
177 vector<BBox> gs_bboxes(const char *in);