]> mj.ucw.cz Git - paperjam.git/blobdiff - pdf.cc
Better error messages from instantiating and running of commands
[paperjam.git] / pdf.cc
diff --git a/pdf.cc b/pdf.cc
index daf68c254bb30ecb8883d5943d43c0b624673a6e..f262f5ba6952554dde4444fbd572aaab2d4c547f 100644 (file)
--- a/pdf.cc
+++ b/pdf.cc
@@ -47,14 +47,14 @@ in_page::in_page(QPDFObjectHandle inpg, int idx)
   if (art_box.isNull())
     art_box = inpg.getKey("/CropBox");
   if (art_box.isNull())
-    bbox = BBox(width, height);
+    image_box = BBox(width, height);
   else
     {
-      bbox = BBox(art_box);
-      bbox.x_min -= media_box.x_min;
-      bbox.x_max -= media_box.x_min;
-      bbox.y_min -= media_box.y_min;
-      bbox.y_max -= media_box.y_min;
+      image_box = BBox(art_box);
+      image_box.x_min -= media_box.x_min;
+      image_box.x_max -= media_box.x_min;
+      image_box.y_min -= media_box.y_min;
+      image_box.y_max -= media_box.y_min;
     }
 }
 
@@ -62,7 +62,7 @@ void in_page::render(out_context *out, pdf_matrix xform)
 {
   // Convert page to xobject
   if (xobject.isNull())
-    xobject = out_pdf.makeIndirectObject( page_to_xobject(&out_pdf, out_pdf.copyForeignObject(pdf_page)) );
+    xobject = out->pdf->makeIndirectObject( page_to_xobject(out->pdf, out->pdf->copyForeignObject(pdf_page)) );
   string xobj_res = out->new_resource("XO");
   out->xobjects.replaceKey(xobj_res, xobject);
 
@@ -79,10 +79,10 @@ void debug_pages(vector<page *> &pages)
     return;
 
   for (auto pg: pages)
-    debug("Page #%d: media[%.3f %.3f] bbox[%.3f %.3f %.3f %.3f]",
+    debug("Page #%d: media[%.3f %.3f] image[%.3f %.3f %.3f %.3f]",
       pg->index,
       pg->width, pg->height,
-      pg->bbox.x_min, pg->bbox.y_min, pg->bbox.x_max, pg->bbox.y_max);
+      pg->image_box.x_min, pg->image_box.y_min, pg->image_box.x_max, pg->image_box.y_max);
 }
 
 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
@@ -94,7 +94,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);
     }
@@ -125,10 +132,11 @@ void process(list<cmd *> &cmds)
   for (auto pg: pages)
     {
       out_context out;
+      out.pdf = &out_pdf;
       out.resources = QPDFObjectHandle::newDictionary();
       out.resources.replaceKey("/ProcSet", QPDFObjectHandle::parse("[/PDF]"));
       out.xobjects = QPDFObjectHandle::newDictionary();
-      out.resources.replaceKey("/XObject", out.xobjects);
+      out.egstates = QPDFObjectHandle::newDictionary();
       pg->render(&out, pdf_matrix());
 
       QPDFObjectHandle contents = QPDFObjectHandle::newStream(&out_pdf, out.contents);
@@ -138,8 +146,12 @@ void process(list<cmd *> &cmds)
       out_page.replaceKey("/Type", QPDFObjectHandle::newName("/Page"));
       out_page.replaceKey("/MediaBox", BBox(pg->width, pg->height).to_array());
       // FIXME:
-      // out_page.replaceKey("/CropBox", pg->bbox.to_array());
+      // out_page.replaceKey("/CropBox", pg->image_box.to_array());
       out_page.replaceKey("/Contents", contents);
+      if (!out.xobjects.getKeys().empty())
+       out.resources.replaceKey("/XObject", out.xobjects);
+      if (!out.egstates.getKeys().empty())
+       out.resources.replaceKey("/ExtGState", out.egstates);
       out_page.replaceKey("/Resources", out.resources);
       out_pdf.addPage(out_page, false);
     }
@@ -221,10 +233,11 @@ vector<BBox> gs_bboxes(const char *in)
 
 static void do_recalc_bbox(vector<page *> &pages, const char *in_name)
 {
+  debug("Calling Ghostscript to re-calculate bounding boxes");
   vector<BBox> bboxes = gs_bboxes(in_name);
   if (pages.size() != bboxes.size())
     die("Ghostscript failed to produce the right number of bboxes");
 
   for (size_t i=0; i<pages.size(); i++)
-    pages[i]->bbox = bboxes[i];
+    pages[i]->image_box = bboxes[i];
 }