]> mj.ucw.cz Git - paperjam.git/blobdiff - pdf.cc
Added example usage to --help
[paperjam.git] / pdf.cc
diff --git a/pdf.cc b/pdf.cc
index bcbfb5d00ff8cdbbeb39aeb21e827d1e7b27329a..9f8dc12bcd5e94c6d620f6d81733e81c9182ea93 100644 (file)
--- a/pdf.cc
+++ b/pdf.cc
@@ -1,7 +1,7 @@
 /*
  *     PaperJam -- Low-level handling of PDFs
  *
- *     (c) 2018 Martin Mares <mj@ucw.cz>
+ *     (c) 2018--2022 Martin Mares <mj@ucw.cz>
  */
 
 #include <cassert>
@@ -33,6 +33,7 @@ public:
   void render(out_context *out, pdf_matrix xform);
   void debug_dump() { debug("Input page %d", index); }
   in_page(QPDFObjectHandle inpg, int idx);
+  int get_rotate();
 };
 
 in_page::in_page(QPDFObjectHandle inpg, int idx)
@@ -75,6 +76,29 @@ void in_page::render(out_context *out, pdf_matrix xform)
   out->contents += "q " + m.to_string() + " cm " + xobj_res + " Do Q ";
 }
 
+int in_page::get_rotate()
+{
+  QPDFObjectHandle rotate = pdf_page.getKey("/Rotate");
+  if (rotate.isNull())
+    return 0;
+  else if (rotate.isInteger())
+    {
+      long long deg = rotate.getIntValue();
+      if (deg < 0 || deg >= 360 || deg % 90)
+       {
+         warn("Page #%d: /Rotate must be 0, 90, 180 or 270", index);
+         return 0;
+       }
+      else
+       return deg;
+    }
+  else
+    {
+      warn("Page #%d: /Rotate is not an integer", index);
+      return 0;
+    }
+}
+
 void debug_pages(vector<page *> &pages)
 {
   if (!debug_level)
@@ -96,6 +120,25 @@ void debug_pages(vector<page *> &pages)
     }
 }
 
+static vector<page *> apply_input_xforms(vector<page *> in_pages)
+{
+  vector<page *> out_pages;
+
+  for (auto pg: in_pages)
+    {
+      in_page * in_pg = dynamic_cast<in_page *>(pg);
+      if (in_pg)
+       {
+         int deg = in_pg->get_rotate();
+         if (deg)
+           pg = new xform_page(pg, "/Rotate", pdf_rotation_matrix(deg, pg->width, pg->height));
+       }
+      out_pages.push_back(pg);
+    }
+
+  return out_pages;
+}
+
 vector<page *> run_command_list(list<cmd *> &cmds, vector<page *> &pages)
 {
   debug("# Input");
@@ -165,6 +208,12 @@ void process(list<cmd *> &cmds)
   if (recalc_bbox)
     do_recalc_bbox(pages, in_name);
 
+  if (!no_auto_transforms)
+    {
+      debug("### Applying input transforms");
+      pages = apply_input_xforms(pages);
+    }
+
   debug("### Running commands");
   pages = run_command_list(cmds, pages);