]> mj.ucw.cz Git - paperjam.git/blobdiff - pdf.cc
TODO
[paperjam.git] / pdf.cc
diff --git a/pdf.cc b/pdf.cc
index 0c9b27b491be88b3b248d4901b692e2820bfd2a9..ff7a8b03aa4578c9bf9a11ab658e48d136befccc 100644 (file)
--- a/pdf.cc
+++ b/pdf.cc
@@ -7,6 +7,7 @@
 #include <cassert>
 #include <cstdlib>
 #include <cstdio>
+#include <cstring>
 #include <unistd.h>
 #include <sys/wait.h>
 
@@ -30,6 +31,7 @@ class in_page : public page {
 public:
   BBox media_box;
   void render(out_context *out, pdf_matrix xform);
+  void debug_dump() { debug("Input page %d", index); }
   in_page(QPDFObjectHandle inpg, int idx);
 };
 
@@ -75,14 +77,23 @@ void in_page::render(out_context *out, pdf_matrix xform)
 
 void debug_pages(vector<page *> &pages)
 {
-  if (!debug_mode)
+  if (!debug_level)
     return;
 
   for (auto pg: pages)
-    debug("Page #%d: media[%.3f %.3f] image[%.3f %.3f %.3f %.3f]",
-      pg->index,
-      pg->width, pg->height,
-      pg->image_box.x_min, pg->image_box.y_min, pg->image_box.x_max, pg->image_box.y_max);
+    {
+      debug("Page #%d: media[%.3f %.3f] image[%.3f %.3f %.3f %.3f][%.3f %.3f]",
+       pg->index,
+       pg->width, pg->height,
+       pg->image_box.x_min, pg->image_box.y_min, pg->image_box.x_max, pg->image_box.y_max,
+       pg->image_box.width(), pg->image_box.height());
+      if (debug_level > 2)
+       {
+         debug_indent += 4;
+         pg->debug_dump();
+         debug_indent -= 4;
+       }
+    }
 }
 
 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
@@ -94,7 +105,14 @@ vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
     {
       debug("# Executing %s", c->def->name);
       debug_indent += 4;
-      pages = c->exec->process(pages);
+      try
+       {
+         pages = c->exec->process(pages);
+       }
+      catch (exception &e)
+       {
+         die("Error in %s: %s", c->def->name, e.what());
+       }
       debug_indent -= 4;
       debug_pages(pages);
     }
@@ -102,8 +120,35 @@ vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
   return pages;
 }
 
+static void make_info_dict()
+{
+  // Create info dictionary if it did not exist yet
+  QPDFObjectHandle trailer = out_pdf.getTrailer();
+  QPDFObjectHandle info = trailer.getKey("/Info");
+  if (info.isNull())
+    {
+      info = QPDFObjectHandle::newDictionary();
+      trailer.replaceKey("/Info", info);
+    }
+  else
+    assert(info.isDictionary());
+
+  info.replaceKey("/Producer", unicode_string("PaperJam"));
+
+  // Copy entries from the source file's info dictionary
+  QPDFObjectHandle orig_trailer = in_pdf.getTrailer();
+  QPDFObjectHandle orig_info = orig_trailer.getKey("/Info");
+  if (!orig_info.isNull())
+    {
+      const string to_copy[] = { "/Title", "/Author", "/Subject", "/Keywords", "/Creator", "/CreationDate" };
+      for (string key: to_copy)
+       info.replaceOrRemoveKey(key, orig_info.getKey(key));
+    }
+}
+
 void process(list<cmd *> &cmds)
 {
+  debug("### Reading input");
   in_pdf.processFile(in_name);
   in_pdf.pushInheritedAttributesToPage();
   out_pdf.emptyPDF();
@@ -120,10 +165,22 @@ void process(list<cmd *> &cmds)
   if (recalc_bbox)
     do_recalc_bbox(pages, in_name);
 
+  debug("### Running commands");
   pages = run_command_list(cmds, pages);
 
+  debug("### Writing output");
+  int out_page = 0;
   for (auto pg: pages)
     {
+      ++out_page;
+      if (debug_level > 1)
+       {
+         debug("Page #%d", out_page);
+         debug_indent += 4;
+         pg->debug_dump();
+         debug_indent -= 4;
+       }
+
       out_context out;
       out.pdf = &out_pdf;
       out.resources = QPDFObjectHandle::newDictionary();
@@ -150,21 +207,12 @@ void process(list<cmd *> &cmds)
     }
 
   // Produce info dictionary
-  QPDFObjectHandle trailer = out_pdf.getTrailer();
-  QPDFObjectHandle info = trailer.getKey("/Info");
-  if (info.isNull())
-    {
-      info = QPDFObjectHandle::newDictionary();
-      trailer.replaceKey("/Info", info);
-    }
-  else
-    assert(info.isDictionary());
-  // FIXME: More meta-data
-  info.replaceKey("/Producer", unicode_string("PaperJam"));
+  make_info_dict();
 
   // Write the output file
   QPDFWriter writer(out_pdf, out_name);
   writer.write();
+  debug("### Done");
 }
 
 /*** Re-calculation of bboxes ***/