X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=jam.h;h=10f547ac55ad7dfe9c58575204a04e9818446328;hb=954187d247a71d34fa3db2bea80372d79cec5add;hp=d5db7c5e4754786d5addf92c70c9baf473f03097;hpb=233f87f9c95f14ef4ddd5ac36cb8c3389501f99b;p=paperjam.git diff --git a/jam.h b/jam.h index d5db7c5..10f547a 100644 --- a/jam.h +++ b/jam.h @@ -1,22 +1,45 @@ +/* + * PaperJam -- Common declarations + * + * (c) 2018 Martin Mares + */ + #include #include +#include +#include using namespace std; -#include "pdf-tools.h" - typedef unsigned int uint; #define NONRET __attribute__((noreturn)) #define UNUSED __attribute__((unused)) +#define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z))) + +static inline bool is_zero(double z) +{ + return fabs(z) < 1e-6; +} + +static inline bool is_equal(double x, double y) +{ + return is_zero(x-y); +} + +#include "pdf-tools.h" + +/*** Representation of commands ***/ struct pipeline; struct cmd; enum arg_type { AT_STRING, + AT_INT, AT_DOUBLE, AT_DIMEN, + AT_SWITCH, AT_TYPE_MASK = 0xffff, AT_MANDATORY = 0x10000, AT_POSITIONAL = 0x20000, @@ -30,31 +53,48 @@ struct arg_def { class arg_val { public: virtual bool given() { return false; } - explicit virtual operator double() { abort(); } - explicit virtual operator string() { abort(); } - double double_default(double def) { return given() ? (double) *this : def; } - const string string_default(const string def) { return given() ? (string) *this : def; } + virtual int as_int(int def) { return def; } + virtual double as_double(double def) { return def; } + virtual const string as_string(const string def) { return def; } virtual string dump() { return ""; } }; -struct page_out { +extern arg_val null_arg; + +struct out_context { + QPDF *pdf; QPDFObjectHandle resources; QPDFObjectHandle xobjects; + QPDFObjectHandle egstates; string contents; int res_cnt; string new_resource(const string type); + out_context() { res_cnt = 0; } }; struct page { - int index; - double width; + int index; // Position in the source PDF, 0 for synthesized pages + double width; // Physical dimensions of media double height; - virtual void render(page_out *out UNUSED, pdf_matrix xform UNUSED) { abort(); } - page(double _w=0, double _h=0) : width(_w), height(_h) { } + BBox image_box; // Bounds useful contents + virtual void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { abort(); } + page(double _w=0, double _h=0) : index(0), width(_w), height(_h), image_box() { } + page(page *p) + { + index = p->index; + width = p->width; + height = p->height; + image_box = p->image_box; + } +}; + +struct empty_page : public page { + void render(out_context *out UNUSED, pdf_matrix xform UNUSED) { } + empty_page(double _w=0, double _h=0) : page(_w, _h) { }; }; struct cmd_exec { - virtual vector process(vector &pages UNUSED) { abort(); } + virtual vector process(vector &pages UNUSED) = 0; }; struct cmd_def { @@ -66,9 +106,17 @@ struct cmd_def { struct cmd { const cmd_def *def; - vector args; + unordered_map args; pipeline *pipe; cmd_exec *exec; + arg_val *arg(const string name) + { + auto it = args.find(name); + if (it != args.end()) + return it->second; + else + return &null_arg; + } }; struct pipeline_selector { @@ -85,10 +133,31 @@ struct pipeline { vector branches; }; +/*** Modules ***/ + +// paperjam.cc + +extern const char *in_name, *out_name; +extern bool recalc_bbox; +extern int debug_mode; +extern int debug_indent; + +void debug(const char *msg, ...) FORMAT_CHECK(printf, 1, 2); +void warn(const char *msg, ...) FORMAT_CHECK(printf, 1, 2); +void die(const char *msg, ...) FORMAT_CHECK(printf, 1, 2) NONRET; + // parse.cc void parse(const char *in, list &cmds); +void parser_help(); // cmds.cc extern const cmd_def cmd_table[]; + +// pdf.cc + +void debug_pages(vector &pages); +void process(list &cmds); +vector run_command_list(list &cmds, vector &pages); +vector gs_bboxes(const char *in);