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