~ubuntu-branches/ubuntu/lucid/swftools/lucid

« back to all changes in this revision

Viewing changes to lib/pdf/pdf.cc

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2009-04-30 05:22:19 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090430052219-l1n64qofzeq5pej8
Tags: 0.9.0-0ubuntu1
* New upstream release (LP: #369931)
  - patches/01_manpages: edited to match updated version of src/pdf2swf.1 and
    src/wav2swf.1
  - patches/02_faq: edited to match updated version of FAQ
  - patches/04_makefile: edited to delete the patch on lib/Makefile.in and 
    src/Makefile.in (applied upstream)
  - deleted patch 99_configure_for_python2.5_and_2.6 (applied upstream)
  - debian/swftools.doc: deleted installation of TODO and 
    pdf2swf/HOWTO_pdf2swf as they don't exist anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "../gfxdevice.h"
2
2
#include "../gfxsource.h"
 
3
#include "../devices/rescale.h"
3
4
#include "../log.h"
4
5
#include "config.h"
5
6
#include "GlobalParams.h"
6
7
#include "InfoOutputDev.h"
7
8
#include "GFXOutputDev.h"
 
9
#include "FullBitmapOutputDev.h"
 
10
#include "BitmapOutputDev.h"
8
11
#include "../mem.h"
9
12
#include "pdf.h"
10
13
#define NO_ARGPARSER
11
14
#include "../args.h"
12
15
 
13
 
static parameter_t* device_config = 0;
14
 
static parameter_t* device_config_next = 0;
15
 
 
16
 
int jpeg_dpi = 0;
17
 
int ppm_dpi = 0;
18
 
 
19
16
static double zoom = 72; /* xpdf: 86 */
20
 
 
 
17
static int jpeg_dpi = 0;
 
18
static int ppm_dpi = 0;
 
19
static int multiply = 1;
21
20
static char* global_page_range = 0;
22
21
 
 
22
static int globalparams_count=0;
 
23
 
 
24
typedef struct _parameter
 
25
{
 
26
    struct _parameter *next;
 
27
    const char*name;
 
28
    const char*value;
 
29
} parameter_t;
 
30
typedef struct _parameterlist
 
31
{
 
32
    parameter_t* device_config;
 
33
    parameter_t* device_config_next;
 
34
} parameterlist_t;
 
35
 
23
36
typedef struct _pdf_page_info
24
37
{
25
38
    int xMin, yMin, xMax, yMax;
32
45
 
33
46
typedef struct _pdf_doc_internal
34
47
{
 
48
    char config_bitmap_optimizing;
 
49
    char config_full_bitmap_optimizing;
 
50
    char config_print;
 
51
    parameterlist_t parameters;
 
52
 
35
53
    int protect;
 
54
    int nocopy;
 
55
    int noprint;
 
56
    
36
57
    PDFDoc*doc;
 
58
    Object docinfo;
37
59
    InfoOutputDev*info;
38
 
    GFXOutputDev*outputDev;
 
60
 
39
61
    pdf_page_info_t*pages;
 
62
    char*filename;
 
63
 
 
64
    /* page map */
 
65
    int*pagemap;
 
66
    int pagemap_size;
 
67
    int pagemap_pos;
 
68
 
 
69
    gfxsource_t*parent;
40
70
} pdf_doc_internal_t;
41
71
 
42
72
typedef struct _pdf_page_internal
45
75
 
46
76
typedef struct _dev_output_internal
47
77
{
48
 
    GFXOutputDev*outputDev;
 
78
    CommonOutputDev*outputDev;
49
79
} dev_output_internal_t;
50
80
 
51
81
 
52
 
static char* dirseparator()
 
82
typedef struct _gfxsource_internal
 
83
{
 
84
    parameterlist_t parameters;
 
85
} gfxsource_internal_t;
 
86
 
 
87
 
 
88
static const char* dirseparator()
53
89
{
54
90
#ifdef WIN32
55
91
    return "\\";
58
94
#endif
59
95
}
60
96
 
 
97
static void storeDeviceParameter(parameterlist_t*i, const char*name, const char*value)
 
98
{
 
99
    parameter_t*o = i->device_config;
 
100
    while(o) {
 
101
        if(!strcmp(name, o->name)) {
 
102
            /* overwrite old value */
 
103
            free((void*)o->value);
 
104
            o->value = strdup(value);
 
105
            return;
 
106
        }
 
107
        o = o->next;
 
108
    }
 
109
    parameter_t*p = new parameter_t();
 
110
    p->name = strdup(name);
 
111
    p->value = strdup(value);
 
112
    p->next = 0;
 
113
 
 
114
    if(i->device_config_next) {
 
115
        i->device_config_next->next = p;
 
116
        i->device_config_next = p;
 
117
    } else {
 
118
        i->device_config = p;
 
119
        i->device_config_next = p;
 
120
    }
 
121
}
 
122
 
 
123
 
61
124
 
62
125
void pdfpage_destroy(gfxpage_t*pdf_page)
63
126
{
66
129
    free(pdf_page);pdf_page=0;
67
130
}
68
131
 
69
 
void render2(gfxpage_t*page, gfxdevice_t*output)
 
132
static void render2(gfxpage_t*page, gfxdevice_t*dev, int x,int y, int x1,int y1,int x2,int y2)
70
133
{
71
134
    pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
72
 
 
 
135
    gfxsource_internal_t*i = (gfxsource_internal_t*)pi->parent->internal;
 
136
 
 
137
    if(!pi->config_print && pi->nocopy) {msg("<fatal> PDF disallows copying");exit(0);}
 
138
    if(pi->config_print && pi->noprint) {msg("<fatal> PDF disallows printing");exit(0);}
 
139
 
 
140
    CommonOutputDev*outputDev = 0;
 
141
    if(pi->config_full_bitmap_optimizing) {
 
142
        FullBitmapOutputDev*d = new FullBitmapOutputDev(pi->info, pi->doc);
 
143
        outputDev = (CommonOutputDev*)d;
 
144
    } else if(pi->config_bitmap_optimizing) {
 
145
        BitmapOutputDev*d = new BitmapOutputDev(pi->info, pi->doc);
 
146
        outputDev = (CommonOutputDev*)d;
 
147
    } else {
 
148
        GFXOutputDev*d = new GFXOutputDev(pi->info, pi->doc);
 
149
        outputDev = (CommonOutputDev*)d;
 
150
    }
 
151
    /* pass global parameters to PDF driver*/
 
152
    parameter_t*p = i->parameters.device_config;
 
153
    while(p) {
 
154
        outputDev->setParameter(p->name, p->value);
 
155
        p = p->next;
 
156
    }
 
157
    p = pi->parameters.device_config;
 
158
    while(p) {
 
159
        outputDev->setParameter(p->name, p->value);
 
160
        p = p->next;
 
161
    }
 
162
 
 
163
    outputDev->setPageMap(pi->pagemap, pi->pagemap_pos);
 
164
    outputDev->setMove(x,y);
 
165
    outputDev->setClip(x1,y1,x2,y2);
 
166
 
 
167
    gfxdevice_t* middev=0;
 
168
    if(multiply>1) {
 
169
        middev = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
 
170
        gfxdevice_rescale_init(middev, 0x00000000, 0, 0, 1.0 / multiply);
 
171
        gfxdevice_rescale_setdevice(middev, dev);
 
172
        middev->setparameter(middev, "protect", "1");
 
173
        dev = middev;
 
174
    } 
 
175
        
73
176
    if(!pi) {
74
177
        msg("<fatal> pdf_page_render: Parent PDF this page belongs to doesn't exist yet/anymore");
75
178
        return;
81
184
    }
82
185
 
83
186
    if(pi->protect) {
84
 
        gfxdevice_t*dev = pi->outputDev->device;
85
187
        dev->setparameter(dev, "protect", "1");
86
188
    }
87
 
    pi->outputDev->setInfo(pi->info);
88
 
    pi->outputDev->setXRef(pi->doc, pi->doc->getXRef());
89
 
    pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
90
 
    pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
 
189
 
 
190
    outputDev->setDevice(dev);
 
191
    pi->doc->displayPage((OutputDev*)outputDev, page->nr, zoom*multiply, zoom*multiply, /*rotate*/0, true, true, pi->config_print);
 
192
    pi->doc->processLinks((OutputDev*)outputDev, page->nr);
 
193
    outputDev->finishPage();
 
194
    outputDev->setDevice(0);
 
195
    delete outputDev;
 
196
 
 
197
    if(middev) {
 
198
        gfxdevice_rescale_setdevice(middev, 0x00000000);
 
199
        middev->finish(middev);
 
200
    }
 
201
 
91
202
}
92
203
 
93
204
    
94
205
void pdfpage_render(gfxpage_t*page, gfxdevice_t*output)
95
206
{
96
207
    pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
97
 
    pi->outputDev->setDevice(output);
98
 
    pi->outputDev->setMove(0,0);
99
 
    pi->outputDev->setClip(0,0,0,0);
100
 
    render2(page, output);
101
 
    pi->outputDev->setDevice(0);
 
208
    render2(page, output, 0,0, 0,0,0,0);
102
209
}
103
210
 
104
211
void pdfpage_rendersection(gfxpage_t*page, gfxdevice_t*output, gfxcoord_t x, gfxcoord_t y, gfxcoord_t _x1, gfxcoord_t _y1, gfxcoord_t _x2, gfxcoord_t _y2)
105
212
{
 
213
    pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
 
214
 
106
215
    int x1=(int)_x1,y1=(int)_y1,x2=(int)_x2,y2=(int)_y2;
107
 
    pdf_doc_internal_t*pi = (pdf_doc_internal_t*)page->parent->internal;
108
 
    pi->outputDev->setDevice(output);
109
 
    pi->outputDev->setMove((int)x,(int)y);
110
216
    if((x1|y1|x2|y2)==0) x2++;
111
 
    pi->outputDev->setClip((int)x1,(int)y1,(int)x2,(int)y2);
112
 
    render2(page, output);
113
 
    pi->outputDev->setDevice(0);
 
217
 
 
218
    render2(page, output, (int)x*multiply,(int)y*multiply,
 
219
                          (int)x1*multiply,(int)y1*multiply,(int)x2*multiply,(int)y2*multiply);
114
220
}
115
221
 
116
222
void pdf_doc_destroy(gfxdocument_t*gfx)
119
225
 
120
226
    delete i->doc; i->doc=0;
121
227
    free(i->pages); i->pages = 0;
 
228
 
 
229
    i->docinfo.free();
 
230
 
 
231
    if(i->filename) {
 
232
        free(i->filename);i->filename=0;
 
233
    }
122
234
    
123
235
    if(i->info) {
124
236
        delete i->info;i->info=0;
131
243
        free(global_page_range);
132
244
        global_page_range = 0;
133
245
    }
134
 
}
135
 
 
136
 
void pdf_doc_set_parameter(gfxdocument_t*gfx, char*name, char*value)
 
246
    
 
247
    /*globalparams_count--;
 
248
    if(!globalparams_count) {
 
249
        delete globalParams;
 
250
        globalParams = 0;
 
251
        globalparams_count = 0;
 
252
    }*/
 
253
}
 
254
 
 
255
static void add_page_to_map(gfxdocument_t*gfx, int pdfpage, int outputpage)
 
256
{
 
257
    pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
 
258
    if(pdfpage < 0)
 
259
        return;
 
260
    if(pdfpage >= i->pagemap_size) {
 
261
        int oldlen = i->pagemap_size;
 
262
        i->pagemap_size = oldlen + 1024;
 
263
        if(pdfpage > i->pagemap_size)
 
264
            i->pagemap_size = pdfpage+1;
 
265
 
 
266
        if(i->pages) {
 
267
            i->pagemap = (int*)malloc(i->pagemap_size*sizeof(int));
 
268
        } else {
 
269
            i->pagemap = (int*)realloc(i->pages, i->pagemap_size*sizeof(int));
 
270
        }
 
271
        memset(&i->pagemap[oldlen], -1, (i->pagemap_size-oldlen)*sizeof(int));
 
272
    }
 
273
    i->pagemap[pdfpage] = outputpage;
 
274
    if(pdfpage > i->pagemap_pos)
 
275
        i->pagemap_pos = pdfpage;
 
276
}
 
277
 
 
278
void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
137
279
{
138
280
    pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
139
281
    if(!strcmp(name, "pagemap")) {
140
 
        GFXOutputDev*o = i->outputDev;
141
282
        int pdfpage=0, outputpage=0;
142
283
        sscanf(value,"%d:%d", &pdfpage, &outputpage);
143
 
        o->preparePage(pdfpage, outputpage);
 
284
        add_page_to_map(gfx, pdfpage, outputpage);
 
285
    } else if(!strcmp(name, "poly2bitmap")) {
 
286
        i->config_bitmap_optimizing = atoi(value);
 
287
    } else if(!strcmp(name, "bitmapfonts") || !strcmp(name, "bitmap")) {
 
288
        i->config_full_bitmap_optimizing = atoi(value);
 
289
    } else if(!strcmp(name, "asprint")) {
 
290
        i->config_print = 1;
144
291
    } else {
145
 
        msg("<warning> Ignored parameter: %s=%s", name, value);
 
292
        storeDeviceParameter(&i->parameters, name, value);
146
293
    }
147
294
}
148
295
 
169
316
    return pdf_page;
170
317
}
171
318
 
172
 
void storeDeviceParameter(char*name, char*value)
173
 
{
174
 
    parameter_t*p = new parameter_t();
175
 
    p->name = strdup(name);
176
 
    p->value = strdup(value);
177
 
    p->next = 0;
178
 
    if(device_config_next) {
179
 
        device_config_next->next = p;
180
 
        device_config_next = p;
181
 
    } else {
182
 
        device_config = p;
183
 
        device_config_next = p;
184
 
    }
185
 
}
186
 
 
187
 
void pdf_set_parameter(char*name, char*value)
188
 
{
 
319
static char*getInfoString(Dict *infoDict, const char *key)
 
320
{
 
321
    Object obj;
 
322
    GString *s1, *s2;
 
323
    int i;
 
324
 
 
325
    if (infoDict && infoDict->lookup((char*)key, &obj)->isString()) {
 
326
        s1 = obj.getString();
 
327
        if ((s1->getChar(0) & 0xff) == 0xfe &&
 
328
            (s1->getChar(1) & 0xff) == 0xff) {
 
329
            s2 = new GString();
 
330
            for (i = 2; i < obj.getString()->getLength(); i += 2) {
 
331
              if (s1->getChar(i) == '\0') {
 
332
                s2->append(s1->getChar(i+1));
 
333
              } else {
 
334
                delete s2;
 
335
                s2 = new GString("<unicode>");
 
336
                break;
 
337
              }
 
338
            }
 
339
            char*ret = strdup(s2->getCString());
 
340
            delete s2;
 
341
            obj.free();
 
342
            return ret;
 
343
        } else {
 
344
            char*ret = strdup(s1->getCString());
 
345
            obj.free();
 
346
            return ret;
 
347
        }
 
348
    }
 
349
    return strdup("");
 
350
}
 
351
 
 
352
static char*getInfoDate(Dict *infoDict, const char *key) 
 
353
{
 
354
    Object obj;
 
355
    char *s;
 
356
 
 
357
    if (infoDict && infoDict->lookup((char*)key, &obj)->isString()) {
 
358
        s = obj.getString()->getCString();
 
359
        if (s[0] == 'D' && s[1] == ':') {
 
360
          s += 2;
 
361
        }
 
362
        char*ret = strdup(s);
 
363
        obj.free();
 
364
        return ret;
 
365
    }
 
366
    return strdup("");
 
367
}
 
368
 
 
369
char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
 
370
{
 
371
    pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
 
372
    if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
 
373
    else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
 
374
    else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
 
375
    else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
 
376
    else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
 
377
    else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
 
378
    else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
 
379
    else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
 
380
    else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
 
381
    else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
 
382
    else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
 
383
    else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
 
384
    else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
 
385
    else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
 
386
    else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
 
387
    else if(!strcmp(name, "version")) { 
 
388
        char buf[32];
 
389
        sprintf(buf, "%.1f", i->doc->getPDFVersion());
 
390
        return strdup(buf);
 
391
    }
 
392
    return 0;
 
393
}
 
394
 
 
395
 
 
396
static void pdf_set_parameter(gfxsource_t*src, const char*name, const char*value)
 
397
{
 
398
    gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
 
399
    parameterlist_t*p = &i->parameters;
189
400
    msg("<verbose> setting parameter %s to \"%s\"", name, value);
190
401
    if(!strncmp(name, "fontdir", strlen("fontdir"))) {
191
402
        addGlobalFontDir(value);
192
403
    } else if(!strcmp(name, "pages")) {
193
404
        global_page_range = strdup(value);
194
 
    } else if(!strncmp(name, "font", strlen("font"))) {
 
405
    } else if(!strncmp(name, "font", strlen("font")) && name[4]!='q') {
195
406
        addGlobalFont(value);
196
407
    } else if(!strncmp(name, "languagedir", strlen("languagedir"))) {
197
408
        addGlobalLanguageDir(value);
199
410
        char buf[80];
200
411
        zoom = atof(value);
201
412
        sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
202
 
        storeDeviceParameter("jpegsubpixels", buf);
 
413
        storeDeviceParameter(p, "jpegsubpixels", buf);
203
414
        sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
204
 
        storeDeviceParameter("ppmsubpixels", buf);
 
415
        storeDeviceParameter(p, "ppmsubpixels", buf);
205
416
    } else if(!strcmp(name, "jpegdpi")) {
206
417
        char buf[80];
207
418
        jpeg_dpi = atoi(value);
208
419
        sprintf(buf, "%f", (double)jpeg_dpi/(double)zoom);
209
 
        storeDeviceParameter("jpegsubpixels", buf);
 
420
        storeDeviceParameter(p, "jpegsubpixels", buf);
210
421
    } else if(!strcmp(name, "ppmdpi")) {
211
422
        char buf[80];
212
423
        ppm_dpi = atoi(value);
213
424
        sprintf(buf, "%f", (double)ppm_dpi/(double)zoom);
214
 
        storeDeviceParameter("ppmsubpixels", buf);
215
 
    } else {
216
 
        storeDeviceParameter(name,value);
217
 
    }
218
 
}
219
 
 
220
 
static void printInfoString(Dict *infoDict, char *key, char *fmt) {
221
 
  Object obj;
222
 
  GString *s1, *s2;
223
 
  int i;
224
 
 
225
 
  if (infoDict->lookup(key, &obj)->isString()) {
226
 
    s1 = obj.getString();
227
 
    if ((s1->getChar(0) & 0xff) == 0xfe &&
228
 
        (s1->getChar(1) & 0xff) == 0xff) {
229
 
      s2 = new GString();
230
 
      for (i = 2; i < obj.getString()->getLength(); i += 2) {
231
 
        if (s1->getChar(i) == '\0') {
232
 
          s2->append(s1->getChar(i+1));
233
 
        } else {
234
 
          delete s2;
235
 
          s2 = new GString("<unicode>");
236
 
          break;
237
 
        }
238
 
      }
239
 
      printf(fmt, s2->getCString());
240
 
      delete s2;
241
 
    } else {
242
 
      printf(fmt, s1->getCString());
243
 
    }
244
 
  }
245
 
  obj.free();
246
 
}
247
 
 
248
 
static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
249
 
  Object obj;
250
 
  char *s;
251
 
 
252
 
  if (infoDict->lookup(key, &obj)->isString()) {
253
 
    s = obj.getString()->getCString();
254
 
    if (s[0] == 'D' && s[1] == ':') {
255
 
      s += 2;
256
 
    }
257
 
    printf(fmt, s);
258
 
  }
259
 
  obj.free();
260
 
}
261
 
 
262
 
 
263
 
 
264
 
gfxdocument_t*pdf_open(char*filename)
265
 
{
 
425
        storeDeviceParameter(p, "ppmsubpixels", buf);
 
426
    } else if(!strcmp(name, "multiply")) {
 
427
        multiply = atoi(value);
 
428
    } else if(!strcmp(name, "help")) {
 
429
        printf("\nPDF device global parameters:\n");
 
430
        printf("fontdir=<dir>     a directory with additional fonts\n");
 
431
        printf("font=<filename>   an additional font filename\n");
 
432
        printf("pages=<range>     the range of pages to convert (example: pages=1-100,210-)\n");
 
433
        printf("zoom=<dpi>        the resultion (default: 72)\n");
 
434
        printf("languagedir=<dir> Add an xpdf language directory\n");
 
435
        printf("multiply=<times>  Render everything at <times> the resolution\n");
 
436
        printf("poly2bitmap       Convert graphics to bitmaps\n");
 
437
        printf("bitmap            Convert everything to bitmaps\n");
 
438
    }   
 
439
}
 
440
 
 
441
void pdf_doc_prepare(gfxdocument_t*doc, gfxdevice_t*dev)
 
442
{
 
443
    pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
 
444
    i->info->dumpfonts(dev);
 
445
}
 
446
 
 
447
static gfxdocument_t*pdf_open(gfxsource_t*src, const char*filename)
 
448
{
 
449
    gfxsource_internal_t*isrc = (gfxsource_internal_t*)src->internal;
266
450
    gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
267
451
    memset(pdf_doc, 0, sizeof(gfxdocument_t));
268
452
    pdf_doc_internal_t*i= (pdf_doc_internal_t*)malloc(sizeof(pdf_doc_internal_t));
269
453
    memset(i, 0, sizeof(pdf_doc_internal_t));
 
454
    i->parent = src;
270
455
    pdf_doc->internal = i;
271
456
    char*userPassword=0;
272
457
    
273
 
    filename = strdup(filename);
 
458
    i->filename = strdup(filename);
274
459
 
275
460
    char*x = 0;
276
461
    if((x = strchr(filename, '|'))) {
280
465
    
281
466
    GString *fileName = new GString(filename);
282
467
    GString *userPW;
283
 
    Object info;
284
 
 
285
 
    // read config file
286
 
    if(!globalParams)
287
 
        globalParams = new GlobalParams("");
288
468
 
289
469
    // open PDF file
290
470
    if (userPassword && userPassword[0]) {
301
481
        return 0;
302
482
    }
303
483
 
304
 
    // print doc info
305
 
    i->doc->getDocInfo(&info);
306
 
    if (info.isDict() &&
307
 
      (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
308
 
      printInfoString(info.getDict(), "Title",        "Title:        %s\n");
309
 
      printInfoString(info.getDict(), "Subject",      "Subject:      %s\n");
310
 
      printInfoString(info.getDict(), "Keywords",     "Keywords:     %s\n");
311
 
      printInfoString(info.getDict(), "Author",       "Author:       %s\n");
312
 
      printInfoString(info.getDict(), "Creator",      "Creator:      %s\n");
313
 
      printInfoString(info.getDict(), "Producer",     "Producer:     %s\n");
314
 
      printInfoDate(info.getDict(),   "CreationDate", "CreationDate: %s\n");
315
 
      printInfoDate(info.getDict(),   "ModDate",      "ModDate:      %s\n");
316
 
      printf("Pages:        %d\n", i->doc->getNumPages());
317
 
      printf("Linearized:   %s\n", i->doc->isLinearized() ? "yes" : "no");
318
 
      printf("Encrypted:    ");
319
 
      if (i->doc->isEncrypted()) {
320
 
        printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
321
 
               i->doc->okToPrint() ? "yes" : "no",
322
 
               i->doc->okToCopy() ? "yes" : "no",
323
 
               i->doc->okToChange() ? "yes" : "no",
324
 
               i->doc->okToAddNotes() ? "yes" : "no");
325
 
      } else {
326
 
        printf("no\n");
327
 
      }
328
 
    }
329
 
    info.free();
330
 
                   
 
484
    // get doc info
 
485
    i->doc->getDocInfo(&i->docinfo);
 
486
    
331
487
    pdf_doc->num_pages = i->doc->getNumPages();
332
488
    i->protect = 0;
333
489
    if (i->doc->isEncrypted()) {
334
490
          if(!i->doc->okToCopy()) {
335
 
              printf("PDF disallows copying.\n");
336
 
              return 0;
 
491
              i->nocopy = 1;
 
492
          }
 
493
          if(!i->doc->okToPrint()) {
 
494
              i->noprint = 1;
337
495
          }
338
496
          if(!i->doc->okToChange() || !i->doc->okToAddNotes())
339
497
              i->protect = 1;
340
498
    }
341
499
 
342
 
    InfoOutputDev*io = new InfoOutputDev();
 
500
    i->info = new InfoOutputDev(i->doc->getXRef());
343
501
    int t;
344
502
    i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
345
503
    memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
346
504
    for(t=1;t<=pdf_doc->num_pages;t++) {
347
505
        if(!global_page_range || is_in_range(t, global_page_range)) {
348
 
            i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
349
 
#if xpdfUpdateVersion >= 16
350
 
            i->doc->processLinks((OutputDev*)io, t);
351
 
#endif
352
 
            i->pages[t-1].xMin = io->x1;
353
 
            i->pages[t-1].yMin = io->y1;
354
 
            i->pages[t-1].xMax = io->x2;
355
 
            i->pages[t-1].yMax = io->y2;
356
 
            i->pages[t-1].width = io->x2 - io->x1;
357
 
            i->pages[t-1].height = io->y2 - io->y1;
358
 
            i->pages[t-1].number_of_images = io->num_images;
359
 
            i->pages[t-1].number_of_links = io->num_links;
360
 
            i->pages[t-1].number_of_fonts = io->num_fonts;
 
506
            i->doc->displayPage((OutputDev*)i->info, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, i->config_print);
 
507
            i->doc->processLinks((OutputDev*)i->info, t);
 
508
            i->pages[t-1].xMin = i->info->x1;
 
509
            i->pages[t-1].yMin = i->info->y1;
 
510
            i->pages[t-1].xMax = i->info->x2;
 
511
            i->pages[t-1].yMax = i->info->y2;
 
512
            i->pages[t-1].width = i->info->x2 - i->info->x1;
 
513
            i->pages[t-1].height = i->info->y2 - i->info->y1;
 
514
            i->pages[t-1].number_of_images = i->info->num_ppm_images + i->info->num_jpeg_images;
 
515
            i->pages[t-1].number_of_links = i->info->num_links;
 
516
            i->pages[t-1].number_of_fonts = i->info->num_fonts;
361
517
            i->pages[t-1].has_info = 1;
362
518
        }
363
519
    }
364
 
    i->info = io;
365
 
    i->outputDev = new GFXOutputDev(device_config);
366
520
 
367
521
    pdf_doc->get = 0;
368
522
    pdf_doc->destroy = pdf_doc_destroy;
369
523
    pdf_doc->set_parameter = pdf_doc_set_parameter;
 
524
    pdf_doc->getinfo = pdf_doc_getinfo;
370
525
    pdf_doc->getpage = pdf_doc_getpage;
371
 
 
372
 
 
 
526
    pdf_doc->prepare = pdf_doc_prepare;
 
527
    
373
528
    return pdf_doc;
374
529
 
375
530
}
 
531
    
 
532
void pdf_destroy(gfxsource_t*src)
 
533
{
 
534
    if(!src->internal)
 
535
        return;
 
536
    gfxsource_internal_t*i = (gfxsource_internal_t*)src->internal;
 
537
    
 
538
    parameter_t*p = i->parameters.device_config;
 
539
    while(p) {
 
540
        parameter_t*next = p->next;
 
541
        if(p->name) free((void*)p->name);p->name = 0;
 
542
        if(p->value) free((void*)p->value);p->value =0;
 
543
        p->next = 0;delete p;
 
544
        p = next;
 
545
    }
 
546
    i->parameters.device_config=i->parameters.device_config_next=0;
 
547
    
 
548
    free(src->internal);src->internal=0;
 
549
 
 
550
    delete globalParams;globalParams = 0;
 
551
    free(src);
 
552
}
376
553
 
377
554
gfxsource_t*gfxsource_pdf_create()
378
555
{
380
557
    memset(src, 0, sizeof(gfxsource_t));
381
558
    src->set_parameter = pdf_set_parameter;
382
559
    src->open = pdf_open;
 
560
    src->destroy = pdf_destroy;
 
561
    src->internal = malloc(sizeof(gfxsource_internal_t));
 
562
    memset(src->internal, 0, sizeof(gfxsource_internal_t));
 
563
 
 
564
    if(!globalParams) {
 
565
        globalParams = new GFXGlobalParams();
 
566
        //globalparams_count++;
 
567
    }
 
568
    
 
569
 
383
570
    return src;
384
571
}