1diff --git a/filter/pdftoraster.cxx b/filter/pdftoraster.cxx 2index e8af184f..528181ee 100755 3--- a/filter/pdftoraster.cxx 4+++ b/filter/pdftoraster.cxx 5@@ -49,12 +49,8 @@ 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 <vector> 16+#include "pdfservice_inner.h" 17 #ifdef USE_LCMS1 18 #include <lcms.h> 19 #define cmsColorSpaceSignature icColorSpaceSignature 20@@ -120,6 +116,7 @@ namespace { 21 bool swap_margin_x = false; 22 bool swap_margin_y = false; 23 bool allocLineBuf = false; 24+ static int g_pdfSdkCount = 0; 25 ConvertLineFunc convertLineOdd; 26 ConvertLineFunc convertLineEven; 27 ConvertCSpaceFunc convertCSpace; 28@@ -191,6 +188,10 @@ namespace { 29 int renderingIntent = INTENT_PERCEPTUAL; 30 int cm_disabled = 0; 31 cm_calibration_t cm_calibrate; 32+ 33+ static const char* ignoreCupsBackSidePrinterList[] = { 34+ "_PixLab_X1", "_CV81", "_PixLab_B5" 35+ }; 36 } 37 38 cmsCIExyY adobergb_wp() 39@@ -382,8 +383,28 @@ static void parseOpts(int argc, char **argv) 40 FM_NO, FM_FALSE, FM_TRUE 41 } flippedMargin = FM_NO; 42 43+ bool isIgnoreCupsBackSidePrinter = false; 44+ int listSize = sizeof(ignoreCupsBackSidePrinterList) / sizeof(ignoreCupsBackSidePrinterList[0]); 45+ const char *printerName = getenv("PRINTER"); 46+ for (int i = 0; i < listSize; i++) { 47+ if (strstr(printerName, ignoreCupsBackSidePrinterList[i]) != NULL) { 48+ fprintf(stderr, "DEBUG: ignoreCupsBackSide Printer found!\n"); 49+ isIgnoreCupsBackSidePrinter = true; 50+ break; 51+ } 52+ } 53+ 54+ bool isBsUniPrinter = false; 55+ attr = ppdFindAttr(ppd, "PCFileName", NULL); 56+ if (attr != NULL && attr->value != NULL && !strcasecmp(attr->value, "BSUNIPRT.PPD")) { 57+ fprintf(stderr, "DEBUG: BsUni Printer found!\n"); 58+ isBsUniPrinter = true; 59+ } 60+ 61 attr = ppdFindAttr(ppd,"cupsBackSide",NULL); 62- if (attr != NULL && attr->value != NULL) { 63+ if (isIgnoreCupsBackSidePrinter && isBsUniPrinter) { 64+ fprintf(stderr, "DEBUG: ignoreCupsBackSide Get!\n"); 65+ } else if (attr != NULL && attr->value != NULL) { 66 ppd->flip_duplex = 0; 67 backside = attr->value; 68 } else if (ppd->flip_duplex) { 69@@ -1521,12 +1542,14 @@ static unsigned char *onebitpixel(unsigned char *src, unsigned char *dst, unsign 70 unsigned char tem=0; 71 for(int k=0;k<8;k++){ 72 cnt++; 73- tem <<=1; 74+ tem <<=1; 75+ if (j + k < width) { 76 unsigned int var=*src; 77 if(var > dither1[i & 0xf][(j+k) & 0xf]){ 78 tem |= 0x1; 79 } 80 src +=1; 81+ } 82 } 83 *dst=tem; 84 dst+=1; 85@@ -1551,7 +1574,7 @@ static unsigned char *removeAlpha(unsigned char *src, unsigned char *dst, unsign 86 return temp; 87 } 88 89-static void writePageImage(cups_raster_t *raster, poppler::document *doc, 90+static void writePageImage(cups_raster_t *raster, PDFInner::PDFDocument* doc, 91 int pageNo) 92 { 93 ConvertLineFunc convertLine; 94@@ -1559,36 +1582,38 @@ static void writePageImage(cups_raster_t *raster, poppler::document *doc, 95 unsigned char *dp; 96 unsigned int rowsize; 97 98- poppler::page *current_page =doc->create_page(pageNo-1); 99- poppler::page_renderer pr; 100- pr.set_render_hint(poppler::page_renderer::antialiasing, true); 101- pr.set_render_hint(poppler::page_renderer::text_antialiasing, true); 102- 103 unsigned char *colordata,*newdata,*graydata,*onebitdata; 104 unsigned int pixel_count; 105- poppler::image im; 106+ std::vector<unsigned char>* buffer; 107+ unsigned char* newBuffer; 108+ unsigned int fullPageBitmapWidth = (unsigned int)(header.cupsPageSize[0] / 72 * header.HWResolution[0]); 109+ unsigned int fullPageBitmapHeight = (unsigned int)(header.cupsPageSize[1] / 72 * header.HWResolution[1]); 110+ fprintf(stderr, "paperdimesion0=%u, paperdimesion1=%u\n", fullPageBitmapWidth, fullPageBitmapHeight); 111 //render the page according to the colourspace and generate the requried data 112 switch (header.cupsColorSpace) { 113 case CUPS_CSPACE_W://gray 114 case CUPS_CSPACE_K://black 115 case CUPS_CSPACE_SW://sgray 116 if(header.cupsBitsPerColor==1){ //special case for 1-bit colorspaces 117- im = pr.render_page(current_page,header.HWResolution[0],header.HWResolution[1],bitmapoffset[0],bitmapoffset[1],bytesPerLine*8,header.cupsHeight); 118- newdata = (unsigned char *)malloc(sizeof(char)*3*im.width()*im.height()); 119- newdata = removeAlpha((unsigned char *)im.const_data(),newdata,im.width(),im.height()); 120- graydata=(unsigned char *)malloc(sizeof(char)*im.width()*im.height()); 121- cupsImageRGBToWhite(newdata,graydata,im.width()*im.height()); 122- onebitdata=(unsigned char *)malloc(sizeof(char)*bytesPerLine*im.height()); 123- onebitpixel(graydata,onebitdata,im.width(),im.height()); 124+ buffer = doc->GetPageBitMapBuffer(pageNo-1, -bitmapoffset[0], -bitmapoffset[1], fullPageBitmapWidth, 125+ fullPageBitmapHeight, 3, header.cupsWidth, header.cupsHeight, 0, 0, 0xFFFFFFFF); 126+ newBuffer = &((*buffer)[0]); 127+ newdata = (unsigned char *)malloc(sizeof(char)*3*header.cupsWidth*header.cupsHeight); 128+ newdata = removeAlpha(newBuffer, newdata, header.cupsWidth, header.cupsHeight); 129+ graydata=(unsigned char *)malloc(sizeof(char)*header.cupsWidth*header.cupsHeight); 130+ cupsImageRGBToWhite(newdata,graydata,header.cupsWidth*header.cupsHeight); 131+ onebitdata=(unsigned char *)malloc(sizeof(char)*bytesPerLine*header.cupsHeight); 132+ onebitpixel(graydata,onebitdata,header.cupsWidth, header.cupsHeight); 133 colordata=onebitdata; 134 rowsize=bytesPerLine; 135- } 136- else{ 137- im = pr.render_page(current_page,header.HWResolution[0],header.HWResolution[1],bitmapoffset[0],bitmapoffset[1],header.cupsWidth,header.cupsHeight); 138- newdata = (unsigned char *)malloc(sizeof(char)*3*im.width()*im.height()); 139- newdata = removeAlpha((unsigned char *)im.const_data(),newdata,im.width(),im.height()); 140- pixel_count=im.width()*im.height(); 141- graydata=(unsigned char *)malloc(sizeof(char)*im.width()*im.height()); 142+ } else { 143+ buffer = doc->GetPageBitMapBuffer(pageNo-1, -bitmapoffset[0], -bitmapoffset[1], fullPageBitmapWidth, 144+ fullPageBitmapHeight, 3, header.cupsWidth, header.cupsHeight, 0, 0, 0xFFFFFFFF); 145+ newBuffer = &((*buffer)[0]); 146+ newdata = (unsigned char *)malloc(sizeof(char)*3*header.cupsWidth*header.cupsHeight); 147+ newdata = removeAlpha(newBuffer, newdata, header.cupsWidth, header.cupsHeight); 148+ pixel_count=header.cupsWidth * header.cupsHeight; 149+ graydata=(unsigned char *)malloc(sizeof(char)*header.cupsWidth*header.cupsHeight); 150 cupsImageRGBToWhite(newdata,graydata,pixel_count); 151 colordata=graydata; 152 rowsize=header.cupsWidth; 153@@ -1602,10 +1627,12 @@ static void writePageImage(cups_raster_t *raster, poppler::document *doc, 154 case CUPS_CSPACE_CMY: 155 case CUPS_CSPACE_RGBW: 156 default: 157- im = pr.render_page(current_page,header.HWResolution[0],header.HWResolution[1],bitmapoffset[0],bitmapoffset[1],header.cupsWidth,header.cupsHeight); 158- newdata = (unsigned char *)malloc(sizeof(char)*3*im.width()*im.height()); 159- newdata = removeAlpha((unsigned char *)im.const_data(),newdata,im.width(),im.height()); 160- pixel_count=im.width()*im.height(); 161+ buffer = doc->GetPageBitMapBuffer(pageNo-1, -bitmapoffset[0], -bitmapoffset[1], fullPageBitmapWidth, 162+ fullPageBitmapHeight, 3, header.cupsWidth, header.cupsHeight, 0, 0, 0xFFFFFFFF); 163+ newBuffer = &((*buffer)[0]); 164+ newdata = (unsigned char *)malloc(sizeof(char)*3*header.cupsWidth*header.cupsHeight); 165+ newdata = removeAlpha(newBuffer, newdata, header.cupsWidth, header.cupsHeight); 166+ pixel_count=header.cupsWidth * header.cupsHeight; 167 rowsize=header.cupsWidth*3; 168 colordata=newdata; 169 break; 170@@ -1645,11 +1672,18 @@ static void writePageImage(cups_raster_t *raster, poppler::document *doc, 171 } 172 } 173 } 174- free(colordata); 175+ if (colordata == newdata) { 176+ if (colordata) free(colordata); 177+ } else { 178+ if (colordata) free(colordata); 179+ if (newdata) free(newdata); 180+ } 181+ if (buffer) delete(buffer); 182+ newBuffer = NULL; 183 if (allocLineBuf) delete[] lineBuf; 184 } 185 186-static void outPage(poppler::document *doc, int pageNo, 187+static void outPage(PDFInner::PDFDocument* doc, int pageNo, 188 cups_raster_t *raster) 189 { 190 int rotate = 0; 191@@ -1661,28 +1695,21 @@ static void outPage(poppler::document *doc, int pageNo, 192 int imageable_area_fit = 0; 193 int i; 194 195- poppler::page *current_page =doc->create_page(pageNo-1); 196- poppler::page_box_enum box = poppler::page_box_enum::media_box; 197- poppler::rectf mediaBox = current_page->page_rect(box); 198- poppler::page::orientation_enum orient = current_page->orientation(); 199- switch (orient) { 200- case poppler::page::landscape: rotate=90; 201- break; 202- case poppler::page::upside_down: rotate=180; 203- break; 204- case poppler::page::seascape: rotate=270; 205- break; 206- default:rotate=0; 207- } 208+ float left = 0; 209+ float bottom = 0; 210+ float right = 0; 211+ float top = 0; 212+ doc->GetMediaBox(pageNo-1, &left, &bottom, &right, &top); 213+ rotate = doc->GetPageRotation(pageNo-1); 214 fprintf(stderr, "DEBUG: mediaBox = [ %f %f %f %f ]; rotate = %d\n", 215- mediaBox.left(), mediaBox.top(), mediaBox.right(), mediaBox.bottom(), rotate); 216- l = mediaBox.width(); 217+ left, top, right, bottom, rotate); 218+ l = right - left; 219 if (l < 0) l = -l; 220 if (rotate == 90 || rotate == 270) 221 header.PageSize[1] = (unsigned)l; 222 else 223 header.PageSize[0] = (unsigned)l; 224- l = mediaBox.height(); 225+ l = bottom - top; 226 if (l < 0) l = -l; 227 if (rotate == 90 || rotate == 270) 228 header.PageSize[0] = (unsigned)l; 229@@ -1882,6 +1909,9 @@ static void outPage(poppler::document *doc, int pageNo, 230 exit(1); 231 } 232 233+ fprintf(stderr, "margins=[%.0f %.0f %.0f %.0f], pwgraster=%d\n", 234+ margins[0], margins[1], margins[2], margins[3], pwgraster); 235+ 236 /* write page image */ 237 writePageImage(raster,doc,pageNo); 238 } 239@@ -1972,7 +2002,9 @@ static void setPopplerColorProfile() 240 } 241 242 int main(int argc, char *argv[]) { 243- poppler::document *doc; 244+ g_pdfSdkCount++; 245+ PDFInner::PDFSdk sdk = PDFInner::PDFSdk::GetSdkInstance(); 246+ PDFInner::PDFDocument* doc = sdk.GetDocInstance(); 247 int i; 248 int npages=0; 249 cups_raster_t *raster; 250@@ -2002,7 +2034,20 @@ int main(int argc, char *argv[]) { 251 } 252 } 253 close(fd); 254- doc=poppler::document::load_from_file(name,"",""); 255+ if (doc != nullptr) { 256+ size_t len = mbstowcs(NULL, name, 0) + 1; 257+ wchar_t *wName = new wchar_t[len]; 258+ mbstowcs(wName, name, len); 259+ doc->LoadDocument(wName, ""); 260+ delete[] wName; 261+ } 262+ FILE* fp; 263+ if ((fp = fopen(name, "rb")) == nullptr) { 264+ fprintf(stderr, "ERROR: Can't open input stdin file\n"); 265+ exit(1); 266+ } 267+ parsePDFTOPDFComment(fp); 268+ fclose(fp); 269 /* remove name */ 270 unlink(name); 271 } else { 272@@ -2015,14 +2060,21 @@ int main(int argc, char *argv[]) { 273 } 274 parsePDFTOPDFComment(fp); 275 fclose(fp); 276- doc=poppler::document::load_from_file(argv[6],"",""); 277+ if (doc != nullptr) { 278+ size_t len = strlen(argv[6]) + 1; 279+ wchar_t *wstr = new wchar_t[len]; 280+ mbstowcs(wstr, argv[6], len); 281+ doc->LoadDocument(wstr, ""); 282+ delete[] wstr; 283+ } 284 } 285 286 if(doc != NULL) 287- npages = doc->pages(); 288+ npages = doc->GetPageCount(); 289 290 /* fix NumCopies, Collate ccording to PDFTOPDFComments */ 291 header.NumCopies = deviceCopies; 292+ fprintf(stderr, "ERROR: NumCopies: %d\n", header.NumCopies); 293 header.Collate = deviceCollate ? CUPS_TRUE : CUPS_FALSE; 294 /* fixed other values that pdftopdf handles */ 295 header.MirrorPrint = CUPS_FALSE; 296@@ -2127,7 +2179,13 @@ int main(int argc, char *argv[]) { 297 298 cupsRasterClose(raster); 299 300- delete doc; 301+ if (doc != nullptr) { 302+ sdk.ReleaseDocInstance(doc); 303+ } 304+ g_pdfSdkCount--; 305+ if (g_pdfSdkCount == 0) { 306+ sdk.Release(); 307+ } 308 if (ppd != NULL) { 309 ppdClose(ppd); 310 } 311@@ -2143,30 +2201,3 @@ int main(int argc, char *argv[]) { 312 313 return exitCode; 314 } 315- 316-/* replace memory allocation methods for memory check */ 317-/* For compatibility with g++ >= 4.7 compilers _GLIBCXX_THROW 318- * should be used as a guard, otherwise use traditional definition */ 319-#ifndef _GLIBCXX_THROW 320-#define _GLIBCXX_THROW throw 321-#endif 322- 323-void * operator new(size_t size) _GLIBCXX_THROW (std::bad_alloc) 324-{ 325- return malloc(size); 326-} 327- 328-void operator delete(void *p) throw () 329-{ 330- free(p); 331-} 332- 333-void * operator new[](size_t size) _GLIBCXX_THROW (std::bad_alloc) 334-{ 335- return malloc(size); 336-} 337- 338-void operator delete[](void *p) throw () 339-{ 340- free(p); 341-} 342