]> mj.ucw.cz Git - paperjam.git/blobdiff - jam.h
Implemented expand, margins
[paperjam.git] / jam.h
diff --git a/jam.h b/jam.h
index f5ff88a3b4f71a640ea8338e796ed22944fd785e..31510d8c2b5f3469d3e6ab831919df33b92d231a 100644 (file)
--- a/jam.h
+++ b/jam.h
@@ -1,25 +1,45 @@
+/*
+ *     PaperJam -- Common declarations
+ *
+ *     (c) 2018 Martin Mares <mj@ucw.cz>
+ */
+
 #include <vector>
 #include <list>
+#include <unordered_map>
+#include <cmath>
 
-#include "pdf-tools.h"
+using namespace std;
 
 typedef unsigned int uint;
 
 #define NONRET __attribute__((noreturn))
+#define UNUSED __attribute__((unused))
+#define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
 
-struct pipeline;
+static inline bool is_zero(double z)
+{
+  return fabs(z) < 0.001;
+}
 
-union arg_val {
-  string *s;
-  double d;
-  pipeline *p;
-};
+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_PIPELINE,                 // Pipeline has an empty name
+  AT_SWITCH,
   AT_TYPE_MASK = 0xffff,
   AT_MANDATORY = 0x10000,
   AT_POSITIONAL = 0x20000,
@@ -30,27 +50,71 @@ struct arg_def {
   uint type;
 };
 
-struct page_out {
+class arg_val {
+public:
+  virtual bool given() { return false; }
+  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 "<undef>"; }
+};
+
+extern arg_val null_arg;
+
+struct out_context {
+  QPDFObjectHandle resources;
+  QPDFObjectHandle xobjects;
+  string contents;
+  int res_cnt;
+  string new_resource(const string type);
+  out_context() { res_cnt = 0; }
 };
 
 struct page {
-  double width;
+  int index;
+  double width;                // Physical dimensions of media
   double height;
-  void render(page_out *out, pdf_matrix xform);
+  BBox bbox;           // 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), bbox() { }
+  page(page *p)
+    {
+      index = p->index;
+      width = p->width;
+      height = p->height;
+      bbox = p->bbox;
+    }
 };
 
-struct cmd {
+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_args {
-  vector<arg_val> arg;
-  vector<bool> arg_given;
+struct cmd_exec {
+  virtual vector<page *> process(vector <page *> &pages UNUSED) = 0;
 };
 
 struct cmd_def {
   const char *name;
   const arg_def *arg_defs;
-  cmd (*constructor)(cmd_args *args);
+  bool has_pipeline;
+  cmd_exec *(*constructor)(cmd *cmd);
+};
+
+struct cmd {
+  const cmd_def *def;
+  unordered_map<string, arg_val *> 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 {
@@ -60,9 +124,38 @@ struct pipeline_selector {
 
 struct pipeline_branch {
   vector<pipeline_selector> selectors;
-  list<cmd> commands;
+  list<cmd *> commands;
 };
 
 struct pipeline {
   vector<pipeline_branch *> 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<cmd *> &cmds);
+void parser_help();
+
+// cmds.cc
+
+extern const cmd_def cmd_table[];
+
+// pdf.cc
+
+void debug_pages(vector<page *> &pages);
+void process(list<cmd *> &cmds);
+vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages);
+vector<BBox> gs_bboxes(const char *in);