• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1diff --git a/filter/pdftoraster.cxx b/filter/pdftoraster.cxx
2index e8af184f..fd0736b3 100755
3--- a/filter/pdftoraster.cxx
4+++ b/filter/pdftoraster.cxx
5@@ -49,12 +49,9 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 #include <cupsfilters/colormanager.h>
7 #include <strings.h>
8 #include <math.h>
9-#include <poppler/cpp/poppler-document.h>
10-#include <poppler/cpp/poppler-page.h>
11-#include <poppler/cpp/poppler-global.h>
12-#include <poppler/cpp/poppler-image.h>
13-#include <poppler/cpp/poppler-page-renderer.h>
14-#include <poppler/cpp/poppler-rectangle.h>
15+#include "fpdfview.h"
16+#include "fpdf_edit.h"
17+#include "cpp/fpdf_scopers.h"
18 #ifdef USE_LCMS1
19 #include <lcms.h>
20 #define cmsColorSpaceSignature icColorSpaceSignature
21@@ -120,6 +117,7 @@ namespace {
22   bool swap_margin_x = false;
23   bool swap_margin_y = false;
24   bool allocLineBuf = false;
25+  bool cupsManualCopies = true;
26   ConvertLineFunc convertLineOdd;
27   ConvertLineFunc convertLineEven;
28   ConvertCSpaceFunc convertCSpace;
29@@ -374,6 +372,12 @@ static void parseOpts(int argc, char **argv)
30 	renderingIntent = INTENT_ABSOLUTE_COLORIMETRIC;
31       }
32     }
33+    attr = ppdFindAttr(ppd,"cupsManualCopies",NULL);
34+    if (attr != NULL && attr->value != NULL) {
35+      if (strcasecmp(attr->value,"true") != 0) {
36+        cupsManualCopies = false;
37+      }
38+    }
39     if (header.Duplex) {
40       /* analyze options relevant to Duplex */
41       const char *backside = "";
42@@ -1551,7 +1555,7 @@ static unsigned char *removeAlpha(unsigned char *src, unsigned char *dst, unsign
43   return temp;
44 }
45
46-static void writePageImage(cups_raster_t *raster, poppler::document *doc,
47+static void writePageImage(cups_raster_t *raster, FPDF_DOCUMENT doc,
48   int pageNo)
49 {
50   ConvertLineFunc convertLine;
51@@ -1559,36 +1563,37 @@ static void writePageImage(cups_raster_t *raster, poppler::document *doc,
52   unsigned char *dp;
53   unsigned int rowsize;
54
55-  poppler::page *current_page =doc->create_page(pageNo-1);
56-  poppler::page_renderer pr;
57-  pr.set_render_hint(poppler::page_renderer::antialiasing, true);
58-  pr.set_render_hint(poppler::page_renderer::text_antialiasing, true);
59+  FPDF_PAGE current_page = FPDF_LoadPage(doc, pageNo -1);
60
61   unsigned char *colordata,*newdata,*graydata,*onebitdata;
62   unsigned int pixel_count;
63-  poppler::image im;
64+  int alpha = FPDFPage_HasTransparency(current_page) ? 1 : 0;
65+  ScopedFPDFBitmap bitmap(FPDFBitmap_Create(header.cupsWidth, header.cupsHeight, alpha));
66+  FPDFBitmap_FillRect(bitmap.get(), 0, 0, header.cupsWidth, header.cupsHeight, 0xFFFFFFFF);
67   //render the page according to the colourspace and generate the requried data
68   switch (header.cupsColorSpace) {
69    case CUPS_CSPACE_W://gray
70    case CUPS_CSPACE_K://black
71    case CUPS_CSPACE_SW://sgray
72     if(header.cupsBitsPerColor==1){ //special case for 1-bit colorspaces
73-      im = pr.render_page(current_page,header.HWResolution[0],header.HWResolution[1],bitmapoffset[0],bitmapoffset[1],bytesPerLine*8,header.cupsHeight);
74-    newdata = (unsigned char *)malloc(sizeof(char)*3*im.width()*im.height());
75-    newdata = removeAlpha((unsigned char *)im.const_data(),newdata,im.width(),im.height());
76-    graydata=(unsigned char *)malloc(sizeof(char)*im.width()*im.height());
77-    cupsImageRGBToWhite(newdata,graydata,im.width()*im.height());
78-    onebitdata=(unsigned char *)malloc(sizeof(char)*bytesPerLine*im.height());
79-    onebitpixel(graydata,onebitdata,im.width(),im.height());
80+    FPDF_RenderPageBitmap(bitmap.get(), current_page, bitmapoffset[0], bitmapoffset[1], bytesPerLine*8, header.cupsHeight, 0, 0);
81+    void *buffer = FPDFBitmap_GetBuffer(bitmap.get());
82+    newdata = (unsigned char *)malloc(sizeof(char)*3*header.cupsWidth*header.cupsHeight);
83+    newdata = removeAlpha((unsigned char *)buffer, newdata, header.cupsWidth, header.cupsHeight);
84+    graydata=(unsigned char *)malloc(sizeof(char)*header.cupsWidth*header.cupsHeight);
85+    cupsImageRGBToWhite(newdata,graydata,header.cupsWidth*header.cupsHeight);
86+    onebitdata=(unsigned char *)malloc(sizeof(char)*bytesPerLine*header.cupsHeight);
87+    onebitpixel(graydata,onebitdata,header.cupsWidth, header.cupsHeight);
88     colordata=onebitdata;
89     rowsize=bytesPerLine;
90     }
91     else{
92-      im = pr.render_page(current_page,header.HWResolution[0],header.HWResolution[1],bitmapoffset[0],bitmapoffset[1],header.cupsWidth,header.cupsHeight);
93-      newdata = (unsigned char *)malloc(sizeof(char)*3*im.width()*im.height());
94-      newdata = removeAlpha((unsigned char *)im.const_data(),newdata,im.width(),im.height());
95-      pixel_count=im.width()*im.height();
96-      graydata=(unsigned char *)malloc(sizeof(char)*im.width()*im.height());
97+      FPDF_RenderPageBitmap(bitmap.get(), current_page, bitmapoffset[0], bitmapoffset[1], header.cupsWidth, header.cupsHeight, 0, 0);
98+      void *buffer = FPDFBitmap_GetBuffer(bitmap.get());
99+      newdata = (unsigned char *)malloc(sizeof(char)*3*header.cupsWidth*header.cupsHeight);
100+      newdata = removeAlpha((unsigned char *)buffer, newdata, header.cupsWidth, header.cupsHeight);
101+      pixel_count=header.cupsWidth * header.cupsHeight;
102+      graydata=(unsigned char *)malloc(sizeof(char)*header.cupsWidth*header.cupsHeight);
103       cupsImageRGBToWhite(newdata,graydata,pixel_count);
104       colordata=graydata;
105       rowsize=header.cupsWidth;
106@@ -1602,10 +1607,15 @@ static void writePageImage(cups_raster_t *raster, poppler::document *doc,
107    case CUPS_CSPACE_CMY:
108    case CUPS_CSPACE_RGBW:
109    default:
110-   im = pr.render_page(current_page,header.HWResolution[0],header.HWResolution[1],bitmapoffset[0],bitmapoffset[1],header.cupsWidth,header.cupsHeight);
111-   newdata = (unsigned char *)malloc(sizeof(char)*3*im.width()*im.height());
112-   newdata = removeAlpha((unsigned char *)im.const_data(),newdata,im.width(),im.height());
113-   pixel_count=im.width()*im.height();
114+  //  im = pr.render_page(current_page,header.HWResolution[0],header.HWResolution[1],bitmapoffset[0],bitmapoffset[1],header.cupsWidth,header.cupsHeight);
115+  //  newdata = (unsigned char *)malloc(sizeof(char)*3*im.width()*im.height());
116+  //  newdata = removeAlpha((unsigned char *)im.const_data(),newdata,im.width(),im.height());
117+  //  pixel_count=im.width()*im.height();
118+   FPDF_RenderPageBitmap(bitmap.get(), current_page, bitmapoffset[0], bitmapoffset[1], header.cupsWidth, header.cupsHeight, 0, 0);
119+   void *buffer = FPDFBitmap_GetBuffer(bitmap.get());
120+   newdata = (unsigned char *)malloc(sizeof(char)*3*header.cupsWidth*header.cupsHeight);
121+   newdata = removeAlpha((unsigned char *)buffer, newdata, header.cupsWidth, header.cupsHeight);
122+   pixel_count=header.cupsWidth * header.cupsHeight;
123    rowsize=header.cupsWidth*3;
124    colordata=newdata;
125      break;
126@@ -1649,7 +1659,7 @@ static void writePageImage(cups_raster_t *raster, poppler::document *doc,
127   if (allocLineBuf) delete[] lineBuf;
128 }
129
130-static void outPage(poppler::document *doc, int pageNo,
131+static void outPage(FPDF_DOCUMENT doc, int pageNo,
132   cups_raster_t *raster)
133 {
134   int rotate = 0;
135@@ -1661,28 +1671,19 @@ static void outPage(poppler::document *doc, int pageNo,
136   int imageable_area_fit = 0;
137   int i;
138
139-  poppler::page *current_page =doc->create_page(pageNo-1);
140-  poppler::page_box_enum box = poppler::page_box_enum::media_box;
141-  poppler::rectf mediaBox = current_page->page_rect(box);
142-  poppler::page::orientation_enum orient = current_page->orientation();
143-  switch (orient) {
144-    case poppler::page::landscape: rotate=90;
145-     break;
146-    case poppler::page::upside_down: rotate=180;
147-     break;
148-    case poppler::page::seascape: rotate=270;
149-     break;
150-     default:rotate=0;
151-  }
152+  FPDF_PAGE current_page = FPDF_LoadPage(doc, pageNo - 1);
153+  FS_RECTF mediaBox;
154+  FPDFPage_GetMediaBox(current_page, &mediaBox.left, &mediaBox.bottom, &mediaBox.right, &mediaBox.top);
155+  rotate = FPDFPage_GetRotation(current_page);
156   fprintf(stderr, "DEBUG: mediaBox = [ %f %f %f %f ]; rotate = %d\n",
157-	  mediaBox.left(), mediaBox.top(), mediaBox.right(), mediaBox.bottom(), rotate);
158-  l = mediaBox.width();
159+	  mediaBox.left, mediaBox.top, mediaBox.right, mediaBox.bottom, rotate);
160+  l = mediaBox.right - mediaBox.left;
161   if (l < 0) l = -l;
162   if (rotate == 90 || rotate == 270)
163     header.PageSize[1] = (unsigned)l;
164   else
165     header.PageSize[0] = (unsigned)l;
166-  l = mediaBox.height();
167+  l = mediaBox.bottom - mediaBox.top;
168   if (l < 0) l = -l;
169   if (rotate == 90 || rotate == 270)
170     header.PageSize[0] = (unsigned)l;
171@@ -1831,8 +1832,8 @@ static void outPage(poppler::document *doc, int pageNo,
172   }
173
174   if (imageable_area_fit == 0) {
175-    bitmapoffset[0] = margins[0] / 72.0 * header.HWResolution[0];
176-    bitmapoffset[1] = margins[3] / 72.0 * header.HWResolution[1];
177+    bitmapoffset[0] = margins[0];
178+    bitmapoffset[1] = margins[3];
179   } else {
180     bitmapoffset[0] = 0;
181     bitmapoffset[1] = 0;
182@@ -1884,6 +1885,7 @@ static void outPage(poppler::document *doc, int pageNo,
183
184   /* write page image */
185   writePageImage(raster,doc,pageNo);
186+  FPDF_ClosePage(current_page);
187 }
188
189 static void setPopplerColorProfile()
190@@ -1972,11 +1974,12 @@ static void setPopplerColorProfile()
191 }
192
193 int main(int argc, char *argv[]) {
194-  poppler::document *doc;
195+  FPDF_DOCUMENT doc;
196   int i;
197   int npages=0;
198   cups_raster_t *raster;
199
200+  FPDF_InitLibrary();
201   cmsSetLogErrorHandler(lcmsErrorHandler);
202   parseOpts(argc, argv);
203
204@@ -2002,7 +2005,7 @@ int main(int argc, char *argv[]) {
205       }
206     }
207     close(fd);
208-    doc=poppler::document::load_from_file(name,"","");
209+    doc = FPDF_LoadDocument(name, NULL);
210     /* remove name */
211     unlink(name);
212   } else {
213@@ -2015,14 +2018,21 @@ int main(int argc, char *argv[]) {
214     }
215     parsePDFTOPDFComment(fp);
216     fclose(fp);
217-    doc=poppler::document::load_from_file(argv[6],"","");
218+    doc = FPDF_LoadDocument(argv[6], NULL);
219   }
220
221   if(doc != NULL)
222-    npages = doc->pages();
223-
224+    npages = FPDF_GetPageCount(doc);
225+  fprintf(stderr, "ERROR: npages: %d\n", npages);
226   /* fix NumCopies, Collate ccording to PDFTOPDFComments */
227-  header.NumCopies = deviceCopies;
228+  if (cupsManualCopies) {
229+    fprintf(stderr, "ERROR: Using cups to handle the number of printed copies\n");
230+    header.NumCopies = atoi(argv[4]);
231+  } else {
232+    fprintf(stderr, "ERROR: The printer handles the number of printed copies\n");
233+    header.NumCopies = 1;
234+  }
235+  fprintf(stderr, "ERROR: NumCopies: %d\n", header.NumCopies);
236   header.Collate = deviceCollate ? CUPS_TRUE : CUPS_FALSE;
237   /* fixed other values that pdftopdf handles */
238   header.MirrorPrint = CUPS_FALSE;
239@@ -2119,15 +2129,16 @@ int main(int argc, char *argv[]) {
240   }
241   selectConvertFunc(raster);
242   if(doc != NULL){
243-    for (i = 1;i <= npages;i++) {
244-      outPage(doc,i,raster);
245+    for (int j = 0; j < header.NumCopies; j++) {
246+      for (i = 1;i <= npages;i++) {
247+        outPage(doc,i,raster);
248+      }
249     }
250   } else
251     fprintf(stderr, "DEBUG: Input is empty, outputting empty file.\n");
252
253   cupsRasterClose(raster);
254
255-  delete doc;
256   if (ppd != NULL) {
257     ppdClose(ppd);
258   }
259@@ -2140,33 +2151,9 @@ int main(int argc, char *argv[]) {
260   if (colorTransform != NULL) {
261     cmsDeleteTransform(colorTransform);
262   }
263-
264+  if (doc != nullptr) {
265+    FPDF_CloseDocument(doc);
266+  }
267+  FPDF_DestroyLibrary();
268   return exitCode;
269 }
270-
271-/* replace memory allocation methods for memory check */
272-/* For compatibility with g++ >= 4.7 compilers _GLIBCXX_THROW
273- *  should be used as a guard, otherwise use traditional definition */
274-#ifndef _GLIBCXX_THROW
275-#define _GLIBCXX_THROW throw
276-#endif
277-
278-void * operator new(size_t size) _GLIBCXX_THROW (std::bad_alloc)
279-{
280-  return malloc(size);
281-}
282-
283-void operator delete(void *p) throw ()
284-{
285-  free(p);
286-}
287-
288-void * operator new[](size_t size) _GLIBCXX_THROW (std::bad_alloc)
289-{
290-  return malloc(size);
291-}
292-
293-void operator delete[](void *p) throw ()
294-{
295-  free(p);
296-}
297