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