~ubuntu-branches/ubuntu/jaunty/cups/jaunty-proposed

« back to all changes in this revision

Viewing changes to debian/local/filters/pdf-filters/pdftopdf/pdf2pdf.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt, Till Kamppeter, Martin Pitt
  • Date: 2009-02-15 18:39:03 UTC
  • mfrom: (6.1.30 jaunty)
  • Revision ID: james.westby@ubuntu.com-20090215183903-i0nhvqyqj4vyn52a
Tags: 1.3.9-13
[ Till Kamppeter ]
* debian/local/filters/pdf-filters/filter/imagetopdf.c: Added support for
  the new "fit-to-page" option (new, more intuitive name for "fitplot").
* debian/filters/pstopdf: Only apply paper size if the "fitplot" or the
  "fit-to-page" option is set.
* debian/local/filters/cpdftocps: Only the last digit of the number of
  copies was used (LP: #309314).
* debian/local/filters/pdf-filters/pdftopdf/pdftopdf.cxx: Do not preceed the
  PDF output with a newline (LP: #303691). Only impose the page size from
  the PPD file to all pages if the "fitplot" or the "fit-to-page" option is 
  set. This prevented from automatic paper tray switching to the correct paper
  sizes when a multiple-page-size document is printed (partial fix for
  LP: #310575).
* debian/patches/pdftops-cups-1.4.dpatch: Updated from CUPS 1.4 SVN. Contains
  fixes for multiple-page-size document printing (partial fix for
  LP: #310575).
* debian/patches/pdftops-dont_fail_on_cancel.dpatch: Removed, should be
  fixed in the new upstream version of pdftops.

[ Martin Pitt ]
* debian/patches/pdftops-cups-1.4.dpatch: Add definition of
  HAVE_PDFTOPS and CUPS_PDFTOPS, so that the filter actually gets
  again built with pdftops support. (Fixes Till's change from above).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2007, BBR Inc.  All rights reserved.
 
3
 
 
4
Permission is hereby granted, free of charge, to any person obtaining
 
5
a copy of this software and associated documentation files (the
 
6
"Software"), to deal in the Software without restriction, including
 
7
without limitation the rights to use, copy, modify, merge, publish,
 
8
distribute, sublicense, and/or sell copies of the Software, and to
 
9
permit persons to whom the Software is furnished to do so, subject to
 
10
the following conditions:
 
11
 
 
12
The above copyright notice and this permission notice shall be included
 
13
in all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 
18
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 
19
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 
20
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 
21
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
22
 
 
23
*/
 
24
/*
 
25
 pdf2pdf.cc
 
26
 pdf to pdf filter, utilty version
 
27
*/
 
28
 
 
29
#include <config.h>
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include "goo/GooString.h"
 
33
#include "goo/gmem.h"
 
34
#include "Object.h"
 
35
#include "Stream.h"
 
36
#include "PDFDoc.h"
 
37
#include "P2PDoc.h"
 
38
#include "P2POutputStream.h"
 
39
#include <stdarg.h>
 
40
#include "Error.h"
 
41
#include "GlobalParams.h"
 
42
#include "parseargs.h"
 
43
#include "PDFFTrueTypeFont.h"
 
44
 
 
45
namespace {
 
46
  int exitCode = 0;
 
47
  GBool fitplot = gFalse;
 
48
  GBool mirror = gFalse;
 
49
  int numberUp = 1;
 
50
  char numberUpLayoutStr[5] = "";
 
51
  unsigned int numberUpLayout = PDFTOPDF_LAYOUT_LRTB;
 
52
  char pageBorderStr[15] = "";
 
53
  char pageSetStr[5] = "";
 
54
  char pageRangesStr[255] = "";
 
55
  unsigned int pageBorder = PDFTOPDF_BORDERNONE;
 
56
  int margin = 0;
 
57
  int aMargin = -1;
 
58
  double pageLeft = 0;
 
59
  double pageRight = 612.0;
 
60
  double pageBottom = 0;
 
61
  double pageTop = 792.0;
 
62
  double pageWidth = 612.0;
 
63
  double pageLength = 792.0;
 
64
  int paperWidth = 0;
 
65
  int paperHeight = 0;
 
66
  int xposition = 0;
 
67
  int yposition = 0;
 
68
  GBool position = gFalse;
 
69
  char positionStr[15] = "";
 
70
  int orientation = 0;
 
71
  double scaling = 1.0;
 
72
  int aScaling = -1;
 
73
  double naturalScaling = 1.0;
 
74
  int aNaturalScaling = -1;
 
75
  GBool printHelp = gFalse;
 
76
  GBool printVersion = gFalse;
 
77
  GBool copies = 1;
 
78
  GBool landscape = gFalse;
 
79
  char paperSize[15] = "";
 
80
  int leftMargin = -1;
 
81
  int rightMargin = -1;
 
82
  int bottomMargin = -1;
 
83
  int topMargin = -1;
 
84
  char ownerPassword[33] = "\001";
 
85
  char userPassword[33] = "\001";
 
86
  GBool reverseOrder = gFalse;
 
87
  GBool collate = gFalse;
 
88
  GBool fontEmbedding = gFalse;
 
89
  GBool fontNoCompress = gFalse;
 
90
  GBool contentNoCompress = gFalse;
 
91
  GooString *ownerPW = NULL;
 
92
  GooString *userPW = NULL;
 
93
};
 
94
 
 
95
struct Paper {
 
96
  const char *name;
 
97
  double width;
 
98
  double height;
 
99
} papers[] = {
 
100
  {"Letter", 612, 792},
 
101
  {"Legal", 612, 1008},
 
102
  {"Executive", 522, 757},
 
103
  {"A5", 421, 595},
 
104
  {"A4", 595, 842},
 
105
  {"A3", 842, 1191},
 
106
  {"A2", 1191, 1684},
 
107
  {"A1", 1684, 2384},
 
108
  {"A0", 2384, 3370},
 
109
  {"B5", 516, 729},
 
110
  {"B4", 729, 1032},
 
111
  {"B3", 1032, 1460},
 
112
  {"B2", 1460, 2064},
 
113
  {"B1", 2064, 2920},
 
114
  {"B0", 2920, 4127},
 
115
  {"Postcard", 283, 421},
 
116
  {NULL}
 
117
};
 
118
 
 
119
 
 
120
static ArgDesc argDesc[] = {
 
121
  {const_cast<char *>("-c"),    argInt,         &copies,        0,
 
122
    const_cast<char *>("number of copies")},
 
123
  {const_cast<char *>("-copies"),       argInt,         &copies,        0,
 
124
    const_cast<char *>("number of copies")},
 
125
  {const_cast<char *>("-fitplot"),argFlag,              &fitplot,       0,
 
126
    const_cast<char *>("fit original page to new page region")},
 
127
  {const_cast<char *>("-number-up"),argInt,             &numberUp,      0,
 
128
    const_cast<char *>("number up (1,2,4,6,8,9,16)")},
 
129
  {const_cast<char *>("-number-up-layout"),     argString,numberUpLayoutStr,sizeof(numberUpLayoutStr),
 
130
    const_cast<char *>("number up layout (lrtb,lrbt,rltb,rlbt,tblr,tbrl,btlr,btrl)")},
 
131
  {const_cast<char *>("-reverse"),      argFlag,        &reverseOrder,  0,
 
132
    const_cast<char *>("reverse output order")},
 
133
  {const_cast<char *>("-mirror"),       argFlag,        &mirror,        0,
 
134
    const_cast<char *>("mirror image")},
 
135
  {const_cast<char *>("-collate"),      argFlag,        &collate,       0,
 
136
    const_cast<char *>("collate")},
 
137
  {const_cast<char *>("-scaling"),argInt,       &aScaling,      0,
 
138
    const_cast<char *>("scaling, in %")},
 
139
  {const_cast<char *>("-natural-scaling"),argInt, &aNaturalScaling,     0,
 
140
    const_cast<char *>("natural scaling, in %")},
 
141
  {const_cast<char *>("-page-border"),argString, pageBorderStr, sizeof(pageBorderStr),
 
142
    const_cast<char *>("page border (none,single,single-thick,double,double-thick)")},
 
143
  {const_cast<char *>("-landscape"),argFlag,    &landscape,     0,
 
144
    const_cast<char *>("landscape")},
 
145
  {const_cast<char *>("-orientation"),argInt,   &orientation,   0,
 
146
    const_cast<char *>("orientation (0,1,2,3)")},
 
147
  {const_cast<char *>("-paper"),        argString,      paperSize,      sizeof(paperSize),
 
148
    const_cast<char *>("paper size (letter, legal, A0 ... A5, B0 ... B5)")},
 
149
  {const_cast<char *>("-paperw"),       argInt,         &paperWidth,    0,
 
150
    const_cast<char *>("paper width, in points")},
 
151
  {const_cast<char *>("-paperh"),       argInt,         &paperHeight,   0,
 
152
    const_cast<char *>("paper height, in points")},
 
153
  {const_cast<char *>("-margin"),       argInt,         &aMargin,       0,
 
154
    const_cast<char *>("paper margin, in points")},
 
155
  {const_cast<char *>("-page-left"),argInt,             &leftMargin,    0,
 
156
    const_cast<char *>("left margin, in points")},
 
157
  {const_cast<char *>("-page-right"),argInt,    &rightMargin,   0,
 
158
    const_cast<char *>("right margin, in points")},
 
159
  {const_cast<char *>("-page-bottom"),argInt,   &bottomMargin,  0,
 
160
    const_cast<char *>("bottom margin, in points")},
 
161
  {const_cast<char *>("-page-top"),     argInt,         &topMargin,     0,
 
162
    const_cast<char *>("top margin, in points")},
 
163
  {const_cast<char *>("-page-set"), argString,  pageSetStr, sizeof(pageSetStr),
 
164
    const_cast<char *>("page set (odd or even)")},
 
165
  {const_cast<char *>("-page-ranges"),argString, pageRangesStr, sizeof(pageRangesStr),
 
166
    const_cast<char *>("page ranges (Ex. 2-4,8)")},
 
167
  {const_cast<char *>("-font-embedding"),argFlag,       &fontEmbedding, 0,
 
168
    const_cast<char *>("embedding fonts")},
 
169
  {const_cast<char *>("-no-compressing-font"),argFlag,&fontNoCompress,0,
 
170
    const_cast<char *>("not compressing embedding fonts")},
 
171
  {const_cast<char *>("-no-compressing-contents"),argFlag,&contentNoCompress,0,
 
172
    const_cast<char *>("not compressing page contents")},
 
173
  {const_cast<char *>("-position"),argString, positionStr, sizeof(positionStr),
 
174
    const_cast<char *>("page position (center,top,left,right,top-left,top-right,bottom,bottom-left,bottom-right)\n")},
 
175
  {const_cast<char *>("-opw"),  argString, ownerPassword, sizeof(ownerPassword),
 
176
    const_cast<char *>("owner password (for encrypted files)")},
 
177
  {const_cast<char *>("-upw"), argString, userPassword, sizeof(userPassword),
 
178
    const_cast<char *>("user password (for encrypted files)")},
 
179
  {const_cast<char *>("-v"), argFlag, &printVersion, 0,
 
180
    const_cast<char *>("print version info")},
 
181
  {const_cast<char *>("-h"),    argFlag,                &printHelp,     0,
 
182
    const_cast<char *>("print usage information")},
 
183
  {const_cast<char *>("-help"), argFlag,                &printHelp,     0,
 
184
    const_cast<char *>("print usage information")},
 
185
  {const_cast<char *>("--help"), argFlag, &printHelp, 0,
 
186
    const_cast<char *>("print usage information")},
 
187
  {const_cast<char *>("--?"), argFlag, &printHelp, 0,
 
188
    const_cast<char *>("print usage information")},
 
189
  {NULL}
 
190
};
 
191
 
 
192
void CDECL myErrorFun(int pos, char *msg, va_list args)
 
193
{
 
194
  if (pos >= 0) {
 
195
    fprintf(stderr, "ERROR (%d): ", pos);
 
196
  } else {
 
197
    fprintf(stderr, "ERROR: ");
 
198
  }
 
199
  vfprintf(stderr, msg, args);
 
200
  fprintf(stderr, "\n");
 
201
  fflush(stderr);
 
202
}
 
203
 
 
204
Paper *getPaper(char *name)
 
205
{
 
206
  for (Paper *p = papers;p->name != NULL;p++) {
 
207
    if (strcasecmp(name,p->name) == 0) {
 
208
      return p;
 
209
    }
 
210
  }
 
211
  return NULL;
 
212
}
 
213
 
 
214
void parseOpts(int *argc, char **argv)
 
215
{
 
216
  GBool ok;
 
217
  Paper *ppaper;
 
218
 
 
219
  ok = parseArgs(argDesc,argc,argv);
 
220
  if (!ok || *argc < 1 || *argc > 3 || printVersion || printHelp) {
 
221
    fprintf(stderr, "pdf2pdf version %s\n", VERSION);
 
222
    if (!printVersion) {
 
223
      printUsage(argv[0],
 
224
        const_cast<char *>("[<input PDF-file> [<output PDF-file>]]"), argDesc);
 
225
    }
 
226
    exit(1);
 
227
  }
 
228
 
 
229
  if (copies <= 0) {
 
230
    fprintf(stderr,"number of copies should be plus number\n");
 
231
    exit(1);
 
232
  }
 
233
  P2PDoc::options.copies = copies;
 
234
 
 
235
  /* paper size */
 
236
  if (paperSize[0] != '\0') {
 
237
    if ((ppaper = getPaper(paperSize)) == NULL) {
 
238
      fprintf(stderr,"Unknown paper\n");
 
239
      exit(1);
 
240
    }
 
241
    pageWidth = ppaper->width;
 
242
    pageLength = ppaper->height;
 
243
    position = gTrue;
 
244
  }
 
245
  if (paperWidth < 0) {
 
246
    fprintf(stderr,"paper width should be plus\n");
 
247
    exit(1);
 
248
  }
 
249
  if (paperWidth > 0) {
 
250
    pageWidth = paperWidth;
 
251
    position = gTrue;
 
252
  }
 
253
  if (paperHeight < 0) {
 
254
    fprintf(stderr,"paper height should be plus\n");
 
255
    exit(1);
 
256
  }
 
257
  if (paperHeight > 0) {
 
258
    pageLength = paperHeight;
 
259
    position = gTrue;
 
260
  }
 
261
  /* paper margin */
 
262
  if (aMargin >= 0) {
 
263
    margin = aMargin;
 
264
    position = gTrue;
 
265
  }
 
266
  pageLeft = margin;
 
267
  pageRight = pageWidth - margin; 
 
268
  pageBottom = margin;
 
269
  pageTop = pageLength - margin;
 
270
  if (landscape) {
 
271
    orientation = 1;
 
272
  }
 
273
  if (orientation < 0 || orientation > 3) {
 
274
    fprintf(stderr,"orientation value should be one of (0,1,2,3)\n");
 
275
    exit(1);
 
276
  }
 
277
 
 
278
  if (leftMargin >= 0) {
 
279
    switch (orientation & 3) {
 
280
      case 0 :
 
281
          pageLeft = leftMargin;
 
282
          break;
 
283
      case 1 :
 
284
          pageBottom = leftMargin;
 
285
          break;
 
286
      case 2 :
 
287
          pageRight = pageWidth - leftMargin;
 
288
          break;
 
289
      case 3 :
 
290
          pageTop = pageLength - leftMargin;
 
291
          break;
 
292
    }
 
293
    position = gTrue;
 
294
  }
 
295
  if (rightMargin >= 0) {
 
296
    switch (orientation & 3) {
 
297
      case 0 :
 
298
          pageRight = pageWidth - rightMargin;
 
299
          break;
 
300
      case 1 :
 
301
          pageTop = pageLength - rightMargin;
 
302
          break;
 
303
      case 2 :
 
304
          pageLeft = rightMargin;
 
305
          break;
 
306
      case 3 :
 
307
          pageBottom = rightMargin;
 
308
          break;
 
309
    }
 
310
    position = gTrue;
 
311
  }
 
312
  if (bottomMargin >= 0) {
 
313
    switch (orientation & 3) {
 
314
      case 0 :
 
315
          pageBottom = bottomMargin;
 
316
          break;
 
317
      case 1 :
 
318
          pageLeft = bottomMargin;
 
319
          break;
 
320
      case 2 :
 
321
          pageTop = pageLength - bottomMargin;
 
322
          break;
 
323
      case 3 :
 
324
          pageRight = pageWidth - bottomMargin;
 
325
          break;
 
326
    }
 
327
    position = gTrue;
 
328
  }
 
329
  if (topMargin >= 0) {
 
330
    switch (orientation & 3) {
 
331
      case 0 :
 
332
          pageTop = pageLength - topMargin;
 
333
          break;
 
334
      case 1 :
 
335
          pageRight = pageWidth - topMargin;
 
336
          break;
 
337
      case 2 :
 
338
          pageBottom = topMargin;
 
339
          break;
 
340
      case 3 :
 
341
          pageLeft = topMargin;
 
342
          break;
 
343
    }
 
344
    position = gTrue;
 
345
  }
 
346
 
 
347
  switch (numberUp) {
 
348
    case 1 :
 
349
    case 2 :
 
350
    case 4 :
 
351
    case 6 :
 
352
    case 8 :
 
353
    case 9 :
 
354
    case 16 :
 
355
        break;
 
356
    default :
 
357
        fprintf(stderr,
 
358
                "Unsupported number-up value %d, should be one of (1,2,4,6,8,16)\n",
 
359
                numberUp);
 
360
        exit(1);
 
361
        break;
 
362
  }
 
363
  if (numberUpLayoutStr[0] != '\0') {
 
364
    if (!strcasecmp(numberUpLayoutStr,"lrtb")) {
 
365
      numberUpLayout = PDFTOPDF_LAYOUT_LRTB;
 
366
    } else if (!strcasecmp(numberUpLayoutStr,"lrbt")) {
 
367
      numberUpLayout = PDFTOPDF_LAYOUT_LRBT;
 
368
    } else if (!strcasecmp(numberUpLayoutStr,"rltb")) {
 
369
      numberUpLayout = PDFTOPDF_LAYOUT_RLTB;
 
370
    } else if (!strcasecmp(numberUpLayoutStr,"rlbt")) {
 
371
      numberUpLayout = PDFTOPDF_LAYOUT_RLBT;
 
372
    } else if (!strcasecmp(numberUpLayoutStr,"tblr")) {
 
373
      numberUpLayout = PDFTOPDF_LAYOUT_TBLR;
 
374
    } else if (!strcasecmp(numberUpLayoutStr,"tbrl")) {
 
375
      numberUpLayout = PDFTOPDF_LAYOUT_TBRL;
 
376
    } else if (!strcasecmp(numberUpLayoutStr,"btlr")) {
 
377
      numberUpLayout = PDFTOPDF_LAYOUT_BTLR;
 
378
    } else if (!strcasecmp(numberUpLayoutStr,"btrl")) {
 
379
      numberUpLayout = PDFTOPDF_LAYOUT_BTRL;
 
380
    } else {
 
381
      fprintf(stderr, "Unsupported number-up-layout value %s,\n"
 
382
       "  should be one of (lrtb,lrbt,rltb,rlbt,tblr,tbrl,btlr,btrl)\n",
 
383
       numberUpLayoutStr);
 
384
      exit(1);
 
385
    }
 
386
  }
 
387
  if (reverseOrder) {
 
388
    P2PDoc::options.reverse = gTrue;
 
389
  }
 
390
  if (pageBorderStr[0] != '\0') {
 
391
    if (!strcasecmp(pageBorderStr,"none")) {
 
392
      pageBorder = PDFTOPDF_BORDERNONE;
 
393
    } else if (!strcasecmp(pageBorderStr,"single")) {
 
394
      pageBorder = PDFTOPDF_BORDERHAIRLINE;
 
395
    } else if (!strcasecmp(pageBorderStr,"single-thick")) {
 
396
      pageBorder = PDFTOPDF_BORDERTHICK;
 
397
    } else if (!strcasecmp(pageBorderStr,"double")) {
 
398
      pageBorder = PDFTOPDF_BORDERDOUBLE | PDFTOPDF_BORDERHAIRLINE;
 
399
    } else if (!strcasecmp(pageBorderStr,"double-thick")) {
 
400
      pageBorder = PDFTOPDF_BORDERDOUBLE | PDFTOPDF_BORDERTHICK;
 
401
    } else {
 
402
      fprintf(stderr, "Unsupported page-border value %s,\n"
 
403
        "  should be one of (none,single,single-thick,double,double-thick)\n",
 
404
        pageBorderStr);
 
405
      exit(1);
 
406
    }
 
407
  }
 
408
  if (pageSetStr[0] != '\0') {
 
409
    if (strcasecmp(pageSetStr,"even") != 0 &&
 
410
       strcasecmp(pageSetStr,"odd") != 0) {
 
411
      fprintf(stderr, "Page set should be odd or even\n");
 
412
      exit(1);
 
413
    }
 
414
    P2PDoc::options.pageSet = pageSetStr;
 
415
  }
 
416
  if (pageRangesStr[0] != '\0') {
 
417
    P2PDoc::options.pageRanges = pageRangesStr;
 
418
  }
 
419
  if (positionStr[0] != '\0') {
 
420
    if (strcasecmp(positionStr,"center") == 0) {
 
421
      xposition = 0;
 
422
      yposition = 0;
 
423
    } else if (strcasecmp(positionStr,"top") == 0) {
 
424
      xposition = 0;
 
425
      yposition = 1;
 
426
    } else if (strcasecmp(positionStr,"left") == 0) {
 
427
      xposition = -1;
 
428
      yposition = 0;
 
429
    } else if (strcasecmp(positionStr,"right") == 0) {
 
430
      xposition = 1;
 
431
      yposition = 0;
 
432
    } else if (strcasecmp(positionStr,"top-left") == 0) {
 
433
      xposition = -1;
 
434
      yposition = 1;
 
435
    } else if (strcasecmp(positionStr,"top-right") == 0) {
 
436
      xposition = 1;
 
437
      yposition = 1;
 
438
    } else if (strcasecmp(positionStr,"bottom") == 0) {
 
439
      xposition = 0;
 
440
      yposition = -1;
 
441
    } else if (strcasecmp(positionStr,"bottom-left") == 0) {
 
442
      xposition = -1;
 
443
      yposition = -1;
 
444
    } else if (strcasecmp(positionStr,"bottom-right") == 0) {
 
445
      xposition = 1;
 
446
      yposition = -1;
 
447
    } else {
 
448
      fprintf(stderr, "Unsupported postion value %s,\n"
 
449
        "  should be one of (center,top,left,right,top-left,top-right,bottom,bottom-left,bottom-right).\n",
 
450
        positionStr);
 
451
      exit(1);
 
452
    }
 
453
    position = gTrue;
 
454
  }
 
455
 
 
456
  if (collate) {
 
457
    P2PDoc::options.collate = gTrue;
 
458
  }
 
459
 
 
460
  if (aScaling > 0) {
 
461
    scaling = aScaling * 0.01;
 
462
    fitplot = gTrue;
 
463
  } else if (fitplot) {
 
464
    scaling = 1.0;
 
465
  }
 
466
  if (aNaturalScaling > 0) {
 
467
    naturalScaling = aNaturalScaling * 0.01;
 
468
  }
 
469
 
 
470
  /* embedding fonts into output PDF */
 
471
  P2PDoc::options.fontEmbedding = fontEmbedding;
 
472
  P2PDoc::options.fontCompress = !fontNoCompress;
 
473
  P2PDoc::options.contentsCompress = !contentNoCompress;
 
474
 
 
475
  if (P2PDoc::options.copies == 1) {
 
476
    /* collate is not needed */
 
477
    P2PDoc::options.collate = gFalse;
 
478
  }
 
479
 
 
480
  if (ownerPassword[0] != '\001') {
 
481
    ownerPW = new GooString(ownerPassword);
 
482
  }
 
483
  if (userPassword[0] != '\001') {
 
484
    userPW = new GooString(userPassword);
 
485
  }
 
486
}
 
487
 
 
488
int main(int argc, char *argv[]) {
 
489
  PDFDoc *doc;
 
490
  P2PDoc *p2pdoc;
 
491
  P2POutputStream *str;
 
492
  FILE *outfp;
 
493
 
 
494
  setErrorFunction(::myErrorFun);
 
495
  parseOpts(&argc, argv);
 
496
#ifdef GLOBALPARAMS_HAS_A_ARG
 
497
  globalParams = new GlobalParams(0);
 
498
#else
 
499
  globalParams = new GlobalParams();
 
500
#endif
 
501
 
 
502
  PDFRectangle box(pageLeft,pageBottom,pageRight,pageTop);
 
503
  PDFRectangle mediaBox(0,0,pageWidth,pageLength);
 
504
 
 
505
  if (argc >=3 && strcmp(argv[2],"-") != 0) {
 
506
    if ((outfp = fopen(argv[2],"wb")) == NULL) {
 
507
      error(-1,const_cast<char *>("Can't open output file:%s"),argv[3]);
 
508
      exit(1);
 
509
    }
 
510
  } else {
 
511
    outfp = stdout;
 
512
  }
 
513
  if (argc <= 1 || strcmp(argv[1],"-") == 0) {
 
514
    /* stdin */
 
515
    Object obj;
 
516
    BaseStream *str;
 
517
    FILE *fp;
 
518
    char buf[BUFSIZ];
 
519
    unsigned int n;
 
520
 
 
521
    fp = tmpfile();
 
522
    if (fp == NULL) {
 
523
      error(-1,const_cast<char *>("Can't create temporary file"));
 
524
      exit(1);
 
525
    }
 
526
 
 
527
    /* copy stdin to the tmp file */
 
528
    while ((n = fread(buf,1,BUFSIZ,stdin)) > 0) {
 
529
      if (fwrite(buf,1,n,fp) != n) {
 
530
        error(-1,const_cast<char *>("Can't copy stdin to temporary file"));
 
531
        fclose(fp);
 
532
        exit(1);
 
533
      }
 
534
    }
 
535
    rewind(fp);
 
536
 
 
537
    obj.initNull();
 
538
    str = new FileStream(fp,0,gFalse,0,&obj);
 
539
    doc = new PDFDoc(str,ownerPW,userPW);
 
540
  } else {
 
541
    GooString *fileName = new GooString(argv[1]);
 
542
    /* input filenmae is specified */
 
543
    doc = new PDFDoc(fileName,ownerPW,userPW);
 
544
  }
 
545
  if (!doc->isOk()) {
 
546
    exitCode = 1;
 
547
    goto err1;
 
548
  }
 
549
  if (!doc->okToChange() && !doc->okToAssemble()) {
 
550
    error(-1,const_cast<char *>("Neither Changing, nor Assembling is allowed\n"));
 
551
    exit(1);
 
552
  }
 
553
  p2pdoc = new P2PDoc(doc);
 
554
  if (mirror) {
 
555
    p2pdoc->mirror();
 
556
  }
 
557
 
 
558
  if (orientation != 0) {
 
559
    p2pdoc->rotate(orientation);
 
560
  }
 
561
 
 
562
  if (naturalScaling != 1.0) {
 
563
    p2pdoc->scale(naturalScaling);
 
564
    p2pdoc->position(&box,xposition,yposition);
 
565
  }
 
566
 
 
567
  if (fitplot) {
 
568
    p2pdoc->fit(&box,scaling);
 
569
    p2pdoc->position(&box,xposition,yposition);
 
570
  }
 
571
  if (numberUp != 1) {
 
572
    p2pdoc->nup(numberUp,&box,pageBorder,numberUpLayout,
 
573
      xposition,yposition);
 
574
  } else if (position) {
 
575
    p2pdoc->position(&box,xposition,yposition);
 
576
  }
 
577
 
 
578
  if (orientation != 0 || naturalScaling != 1.0 || fitplot
 
579
      || numberUp != 1 || position) {
 
580
    /* When changing geometry, 
 
581
       set all pages's mediaBox to the target page size */
 
582
    p2pdoc->setMediaBox(&mediaBox);
 
583
  }
 
584
 
 
585
  if (P2PDoc::options.collate
 
586
      && p2pdoc->getNumberOfPages() == 1
 
587
      && !P2PDoc::options.even) {
 
588
    /* collate is not needed, disable it */
 
589
    /* Number of pages is changed by nup and page-ranges,
 
590
        so check this here */
 
591
    P2PDoc::options.collate = gFalse;
 
592
  }
 
593
 
 
594
  str = new P2POutputStream(outfp); /* PDF start here */
 
595
  p2pdoc->output(str);
 
596
 
 
597
  delete str;
 
598
  delete p2pdoc;
 
599
err1:
 
600
  delete doc;
 
601
 
 
602
  // Check for memory leaks
 
603
  Object::memCheck(stderr);
 
604
  gMemReport(stderr);
 
605
 
 
606
  return exitCode;
 
607
}
 
608
 
 
609
/* replace memory allocation methods for memory check */
 
610
 
 
611
void * operator new(size_t size)
 
612
{
 
613
  return gmalloc(size);
 
614
}
 
615
 
 
616
void operator delete(void *p)
 
617
{
 
618
  gfree(p);
 
619
}
 
620
 
 
621
void * operator new[](size_t size)
 
622
{
 
623
  return gmalloc(size);
 
624
}
 
625
 
 
626
void operator delete[](void *p)
 
627
{
 
628
  gfree(p);
 
629
}