Blame SOURCES/cups-filters-page-label.patch

d35507
diff -up cups-filters-1.0.35/filter/pdftopdf/pdftopdf.cc.page-label cups-filters-1.0.35/filter/pdftopdf/pdftopdf.cc
d35507
--- cups-filters-1.0.35/filter/pdftopdf/pdftopdf.cc.page-label	2013-04-09 19:14:42.000000000 +0100
d35507
+++ cups-filters-1.0.35/filter/pdftopdf/pdftopdf.cc	2013-07-24 08:49:23.919081384 +0100
d35507
@@ -9,6 +9,8 @@
d35507
 #include <assert.h>
d35507
 #include <cups/cups.h>
d35507
 #include <cups/ppd.h>
d35507
+#include <iomanip>
d35507
+#include <sstream>
d35507
 #include <memory>
d35507
 
d35507
 #include "pdftopdf_processor.h"
d35507
@@ -402,8 +404,27 @@ void getParameters(ppd_file_t *ppd,int n
d35507
     param.reverse=ppdDefaultOrder(ppd);
d35507
   }
d35507
 
d35507
-  // TODO: pageLabel  (not used)
d35507
-  // param.pageLabel=cupsGetOption("page-label",num_options,options);  // strdup?
d35507
+  std::string rawlabel;
d35507
+  char *classification = getenv("CLASSIFICATION");
d35507
+  if (classification)
d35507
+    rawlabel.append (classification);
d35507
+
d35507
+  if ( (val=cupsGetOption("page-label", num_options, options)) != NULL) {
d35507
+    if (!rawlabel.empty())
d35507
+      rawlabel.append (" - ");
d35507
+    rawlabel.append(cupsGetOption("page-label",num_options,options));
d35507
+  }
d35507
+
d35507
+  std::ostringstream cookedlabel;
d35507
+  for (std::string::iterator it = rawlabel.begin();
d35507
+       it != rawlabel.end ();
d35507
+       ++it) {
d35507
+    if (*it < 32 || *it > 126)
d35507
+      cookedlabel << "\\" << std::oct << std::setfill('0') << std::setw(3) << (unsigned int) *it;
d35507
+    else
d35507
+      cookedlabel.put (*it);
d35507
+  }
d35507
+  param.pageLabel = cookedlabel.str ();
d35507
 
d35507
   if ( (val=cupsGetOption("page-set",num_options,options)) != NULL) {
d35507
     if (strcasecmp(val,"even")==0) {
d35507
diff -up cups-filters-1.0.35/filter/pdftopdf/pdftopdf_processor.cc.page-label cups-filters-1.0.35/filter/pdftopdf/pdftopdf_processor.cc
d35507
--- cups-filters-1.0.35/filter/pdftopdf/pdftopdf_processor.cc.page-label	2013-02-15 17:37:59.000000000 +0000
d35507
+++ cups-filters-1.0.35/filter/pdftopdf/pdftopdf_processor.cc	2013-07-24 09:50:04.437720618 +0100
d35507
@@ -80,12 +80,8 @@ void ProcessingParameters::dump() const
d35507
   fprintf(stderr,"evenDuplex: %s\n",
d35507
                  (evenDuplex)?"true":"false");
d35507
 
d35507
-/*
d35507
-  // std::string pageLabel; // or NULL?  must stay/dup!
d35507
-  ...
d35507
-  ...
d35507
-
d35507
-*/
d35507
+  fprintf(stderr,"pageLabel: %s\n",
d35507
+	  pageLabel.empty () ? "(none)" : pageLabel.c_str());
d35507
 
d35507
   fprintf(stderr,"bookletMode: ");
d35507
   BookletMode_dump(booklet);
d35507
@@ -206,6 +202,10 @@ bool processPDFTOPDF(PDFTOPDF_Processor
d35507
         page->mirror();
d35507
       }
d35507
 
d35507
+      if (!param.pageLabel.empty()) {
d35507
+	page->add_label(param.page, param.pageLabel);
d35507
+      }
d35507
+
d35507
       // place border
d35507
       if ( (param.border!=BorderType::NONE)&&(iA
d35507
 #if 0 // would be nice, but is not possible
d35507
@@ -310,6 +310,10 @@ const bool origls=param.nup.landscape;
d35507
         page->add_border_rect(rect,param.border,1.0/pgedit.scale);
d35507
       }
d35507
 
d35507
+      if (!param.pageLabel.empty()) {
d35507
+	page->add_label(param.page, param.pageLabel);
d35507
+      }
d35507
+
d35507
       if (!param.fitplot) {
d35507
         curpage->add_subpage(page,pgedit.xpos+xpos,pgedit.ypos+ypos,pgedit.scale,&rect);
d35507
       } else {
d35507
diff -up cups-filters-1.0.35/filter/pdftopdf/pdftopdf_processor.h.page-label cups-filters-1.0.35/filter/pdftopdf/pdftopdf_processor.h
d35507
--- cups-filters-1.0.35/filter/pdftopdf/pdftopdf_processor.h.page-label	2012-11-15 15:58:39.000000000 +0000
d35507
+++ cups-filters-1.0.35/filter/pdftopdf/pdftopdf_processor.h	2013-07-24 08:49:23.920081389 +0100
d35507
@@ -20,7 +20,7 @@ struct ProcessingParameters {
d35507
       border(NONE),
d35507
       reverse(false),
d35507
 
d35507
-//      pageLabel(NULL),
d35507
+      pageLabel(),
d35507
       evenPages(true),oddPages(true),
d35507
 
d35507
       mirror(false),
d35507
@@ -60,7 +60,7 @@ struct ProcessingParameters {
d35507
   NupParameters nup;
d35507
   bool reverse;
d35507
 
d35507
-  // std::string pageLabel; // or NULL?  must stay/dup!
d35507
+  std::string pageLabel;
d35507
   bool evenPages,oddPages;
d35507
   IntervalSet pageRange;
d35507
 
d35507
@@ -105,6 +105,7 @@ public:
d35507
   virtual void add_subpage(const std::shared_ptr<PDFTOPDF_PageHandle> &sub,float xpos,float ypos,float scale,const PageRect *crop=NULL) =0;
d35507
   virtual void mirror() =0;
d35507
   virtual void rotate(Rotation rot) =0;
d35507
+  virtual void add_label(const PageRect &rect, const std::string label) =0;
d35507
 };
d35507
 
d35507
 // TODO: ... error output?
d35507
diff -up cups-filters-1.0.35/filter/pdftopdf/qpdf_pdftopdf_processor.cc.page-label cups-filters-1.0.35/filter/pdftopdf/qpdf_pdftopdf_processor.cc
d35507
--- cups-filters-1.0.35/filter/pdftopdf/qpdf_pdftopdf_processor.cc.page-label	2013-03-14 20:32:42.000000000 +0000
d35507
+++ cups-filters-1.0.35/filter/pdftopdf/qpdf_pdftopdf_processor.cc	2013-07-24 08:49:23.920081389 +0100
d35507
@@ -257,6 +257,94 @@ void QPDF_PDFTOPDF_PageHandle::rotate(Ro
d35507
 }
d35507
 // }}}
d35507
 
d35507
+void QPDF_PDFTOPDF_PageHandle::add_label(const PageRect &_rect, const std::string label) // {{{
d35507
+{
d35507
+  assert(isExisting());
d35507
+
d35507
+  PageRect rect = ungetRect (_rect, *this, rotation, page);
d35507
+
d35507
+  assert (rect.left <= rect.right);
d35507
+  assert (rect.bottom <= rect.top);
d35507
+
d35507
+  // TODO: Only add in the font once, not once per page.
d35507
+  QPDFObjectHandle font = page.getOwningQPDF()->makeIndirectObject (
d35507
+    QPDFObjectHandle::parse(
d35507
+      "<<"
d35507
+      " /Type /Font"
d35507
+      " /Subtype /Type1"
d35507
+      " /Name /pagelabel-font"
d35507
+      " /BaseFont /Helvetica" // TODO: support UTF-8 labels?
d35507
+      ">>"));
d35507
+  QPDFObjectHandle resources = page.getKey ("/Resources");
d35507
+  QPDFObjectHandle rfont = resources.getKey ("/Font");
d35507
+  rfont.replaceKey ("/pagelabel-font", font);
d35507
+
d35507
+  double margin = 2.25;
d35507
+  double height = 12;
d35507
+
d35507
+  std::string boxcmd = "q\n";
d35507
+
d35507
+  // White filled rectangle (top)
d35507
+  boxcmd += "  1 1 1 rg\n";
d35507
+  boxcmd += "  " + QUtil::double_to_string(rect.left + margin) + " " +
d35507
+		   QUtil::double_to_string(rect.top - height - 2 * margin) + " " +
d35507
+		   QUtil::double_to_string(rect.right - rect.left - 2 * margin) + " " +
d35507
+		   QUtil::double_to_string(height + 2 * margin) + " re f\n";
d35507
+
d35507
+  // White filled rectangle (bottom)
d35507
+  boxcmd += "  " + QUtil::double_to_string(rect.left + margin) + " " +
d35507
+		   QUtil::double_to_string(rect.bottom + height + margin) + " " +
d35507
+		   QUtil::double_to_string(rect.right - rect.left - 2 * margin) + " " +
d35507
+		   QUtil::double_to_string(height + 2 * margin) + " re f\n";
d35507
+
d35507
+  // Black outline (top)
d35507
+  boxcmd += "  0 0 0 RG\n";
d35507
+  boxcmd += "  " + QUtil::double_to_string(rect.left + margin) + " " +
d35507
+		   QUtil::double_to_string(rect.top - height - 2 * margin) + " " +
d35507
+		   QUtil::double_to_string(rect.right - rect.left - 2 * margin) + " " +
d35507
+		   QUtil::double_to_string(height + 2 * margin) + " re S\n";
d35507
+
d35507
+  // Black outline (bottom)
d35507
+  boxcmd += "  " + QUtil::double_to_string(rect.left + margin) + " " +
d35507
+		   QUtil::double_to_string(rect.bottom + height + margin) + " " +
d35507
+		   QUtil::double_to_string(rect.right - rect.left - 2 * margin) + " " +
d35507
+		   QUtil::double_to_string(height + 2 * margin) + " re S\n";
d35507
+
d35507
+  // Black text (top)
d35507
+  boxcmd += "  0 0 0 rg\n";
d35507
+  boxcmd += "  BT\n";
d35507
+  boxcmd += "  /pagelabel-font 12 Tf\n";
d35507
+  boxcmd += "  " + QUtil::double_to_string(rect.left + 2 * margin) + " " +
d35507
+		   QUtil::double_to_string(rect.top - height - margin) + " Td\n";
d35507
+  boxcmd += "  (" + label + ") Tj\n";
d35507
+  boxcmd += "  ET\n";
d35507
+
d35507
+  // Black text (bottom)
d35507
+  boxcmd += "  BT\n";
d35507
+  boxcmd += "  /pagelabel-font 12 Tf\n";
d35507
+  boxcmd += "  " + QUtil::double_to_string(rect.left + 2 * margin) + " " +
d35507
+		   QUtil::double_to_string(rect.bottom + height + 2 * margin) + " Td\n";
d35507
+  boxcmd += "  (" + label + ") Tj\n";
d35507
+  boxcmd += "  ET\n";
d35507
+
d35507
+  boxcmd += "Q\n";
d35507
+
d35507
+  assert(page.getOwningQPDF()); // existing pages are always indirect
d35507
+  static const char *pre="%pdftopdf q\n"
d35507
+                         "q\n",
d35507
+                    *post="%pdftopdf Q\n"
d35507
+                          "Q\n";
d35507
+
d35507
+  QPDFObjectHandle stm1=QPDFObjectHandle::newStream(page.getOwningQPDF(),
d35507
+						    pre),
d35507
+                   stm2=QPDFObjectHandle::newStream(page.getOwningQPDF(),
d35507
+						    std::string(post) + boxcmd);
d35507
+
d35507
+  page.addPageContents(stm1,true); // before
d35507
+  page.addPageContents(stm2,false); // after
d35507
+}
d35507
+// }}}
d35507
+
d35507
 void QPDF_PDFTOPDF_PageHandle::debug(const PageRect &rect,float xpos,float ypos) // {{{
d35507
 {
d35507
   assert(!isExisting());
d35507
@@ -264,7 +352,7 @@ void QPDF_PDFTOPDF_PageHandle::debug(con
d35507
 }
d35507
 // }}}
d35507
 
d35507
-
d35507
+// }}}
d35507
 void QPDF_PDFTOPDF_Processor::closeFile() // {{{
d35507
 {
d35507
   pdf.reset();
d35507
diff -up cups-filters-1.0.35/filter/pdftopdf/qpdf_pdftopdf_processor.h.page-label cups-filters-1.0.35/filter/pdftopdf/qpdf_pdftopdf_processor.h
d35507
--- cups-filters-1.0.35/filter/pdftopdf/qpdf_pdftopdf_processor.h.page-label	2012-11-15 15:58:39.000000000 +0000
d35507
+++ cups-filters-1.0.35/filter/pdftopdf/qpdf_pdftopdf_processor.h	2013-07-24 08:49:23.920081389 +0100
d35507
@@ -11,6 +11,7 @@ public:
d35507
   virtual void add_subpage(const std::shared_ptr<PDFTOPDF_PageHandle> &sub,float xpos,float ypos,float scale,const PageRect *crop=NULL);
d35507
   virtual void mirror();
d35507
   virtual void rotate(Rotation rot);
d35507
+  virtual void add_label(const PageRect &rect, const std::string label);
d35507
 
d35507
   void debug(const PageRect &rect,float xpos,float ypos);
d35507
 private: