Fixed the masked bitmap handling. (converting the "pdfscreen" manual
[swftools.git] / pdf2swf / SWFOutputDev.cc
1 /* pdfswf.cc
2    implements a pdf output device (OutputDev).
3
4    This file is part of swftools.
5
6    Swftools is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    Swftools is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with swftools; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <unistd.h>
25 //xpdf header files
26 #include "GString.h"
27 #include "gmem.h"
28 #include "Object.h"
29 #include "Stream.h"
30 #include "Array.h"
31 #include "Dict.h"
32 #include "XRef.h"
33 #include "Catalog.h"
34 #include "Page.h"
35 #include "PDFDoc.h"
36 #include "Params.h"
37 #include "Error.h"
38 #include "config.h"
39 #include "OutputDev.h"
40 #include "GfxState.h"
41 #include "GfxFont.h"
42 #include "FontFile.h"
43 //swftools header files
44 #include "swfoutput.h"
45 extern "C" {
46 #include "../lib/log.h"
47 }
48
49 static PDFDoc*doc = 0;
50 static char* swffilename = 0;
51 int numpages;
52 int currentpage;
53
54 // swf <-> pdf pages
55 int*pages = 0;
56 int pagebuflen = 0;
57 int pagepos = 0;
58
59 static void printInfoString(Dict *infoDict, char *key, char *fmt);
60 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
61
62 double fontsizes[] = 
63 {
64  0.833,0.833,0.889,0.889,0.788,0.722,0.833,0.778,0.600,0.600,0.600,0.600,0.576,0.576,0.576,0.576
65 };
66 char*fontnames[]={
67 "Helvetica",             
68 "Helvetica-Bold",        
69 "Helvetica-BoldOblique", 
70 "Helvetica-Oblique",     
71 "Times-Roman",           
72 "Times-Bold",            
73 "Times-BoldItalic",      
74 "Times-Italic",          
75 "Courier",               
76 "Courier-Bold",          
77 "Courier-BoldOblique",   
78 "Courier-Oblique",       
79 "Symbol",                
80 "Symbol",                
81 "Symbol",                
82 "Symbol",
83 "ZapfDingBats"
84 };
85
86 struct mapping {
87     char*pdffont;
88     char*filename;
89     int id;
90 } pdf2t1map[] ={
91 {"Times-Roman",           "n021003l.pfb"},
92 {"Times-Italic",          "n021023l.pfb"},
93 {"Times-Bold",            "n021004l.pfb"},
94 {"Times-BoldItalic",      "n021024l.pfb"},
95 {"Helvetica",             "n019003l.pfb"},
96 {"Helvetica-Oblique",     "n019023l.pfb"},
97 {"Helvetica-Bold",        "n019004l.pfb"},
98 {"Helvetica-BoldOblique", "n019024l.pfb"},
99 {"Courier",               "n022003l.pfb"},
100 {"Courier-Oblique",       "n022023l.pfb"},
101 {"Courier-Bold",          "n022004l.pfb"},
102 {"Courier-BoldOblique",   "n022024l.pfb"},
103 {"Symbol",                "s050000l.pfb"},
104 {"ZapfDingbats",          "d050000l.pfb"}};
105
106 class GfxState;
107 class GfxImageColorMap;
108
109 class SWFOutputDev:  public OutputDev {
110   struct swfoutput output;
111   int outputstarted;
112 public:
113
114   // Constructor.
115   SWFOutputDev();
116
117   // Destructor.
118   virtual ~SWFOutputDev() ;
119
120   //----- get info about output device
121
122   // Does this device use upside-down coordinates?
123   // (Upside-down means (0,0) is the top left corner of the page.)
124   virtual GBool upsideDown();
125
126   // Does this device use drawChar() or drawString()?
127   virtual GBool useDrawChar();
128
129   //----- initialization and control
130
131   // Start a page.
132   virtual void startPage(int pageNum, GfxState *state) ;
133
134   //----- link borders
135   virtual void drawLink(Link *link, Catalog *catalog) ;
136
137   //----- save/restore graphics state
138   virtual void saveState(GfxState *state) ;
139   virtual void restoreState(GfxState *state) ;
140
141   //----- update graphics state
142
143   virtual void updateFont(GfxState *state);
144   virtual void updateFillColor(GfxState *state);
145   virtual void updateStrokeColor(GfxState *state);
146   virtual void updateLineWidth(GfxState *state);
147   
148   virtual void updateAll(GfxState *state) 
149   {
150       updateFont(state);
151       updateFillColor(state);
152       updateStrokeColor(state);
153       updateLineWidth(state);
154   };
155
156   //----- path painting
157   virtual void stroke(GfxState *state) ;
158   virtual void fill(GfxState *state) ;
159   virtual void eoFill(GfxState *state) ;
160
161   //----- path clipping
162   virtual void clip(GfxState *state) ;
163   virtual void eoClip(GfxState *state) ;
164
165   //----- text drawing
166   virtual void beginString(GfxState *state, GString *s) ;
167   virtual void endString(GfxState *state) ;
168   virtual void drawChar(GfxState *state, double x, double y,
169                         double dx, double dy, Guchar c) ;
170   virtual void drawChar16(GfxState *state, double x, double y,
171                           double dx, double dy, int c) ;
172
173   //----- image drawing
174   virtual void drawImageMask(GfxState *state, Object *ref, Stream *str,
175                              int width, int height, GBool invert,
176                              GBool inlineImg);
177   virtual void drawImage(GfxState *state, Object *ref, Stream *str,
178                          int width, int height, GfxImageColorMap *colorMap,
179                          GBool inlineImg);
180
181   private:
182   void drawGeneralImage(GfxState *state, Object *ref, Stream *str,
183                                    int width, int height, GfxImageColorMap*colorMap, GBool invert,
184                                    GBool inlineImg, int mask);
185   int clipping[32];
186   int clippos;
187
188   int setT1Font(char*name,FontEncoding*enc);
189   int t1id;
190   int jpeginfo; // did we write "File contains jpegs" yet?
191   int pbminfo; // did we write "File contains jpegs" yet?
192   int linkinfo; // did we write "File contains links" yet?
193
194   GfxState *laststate;
195 };
196
197 char mybuf[1024];
198 char* gfxstate2str(GfxState *state)
199 {
200   char*bufpos = mybuf;
201   GfxRGB rgb;
202   bufpos+=sprintf(bufpos,"CTM[%.3f/%.3f/%.3f/%.3f/%.3f/%.3f] ",
203                                     state->getCTM()[0],
204                                     state->getCTM()[1],
205                                     state->getCTM()[2],
206                                     state->getCTM()[3],
207                                     state->getCTM()[4],
208                                     state->getCTM()[5]);
209   if(state->getX1()!=0.0)
210   bufpos+=sprintf(bufpos,"X1-%.1f ",state->getX1());
211   if(state->getY1()!=0.0)
212   bufpos+=sprintf(bufpos,"Y1-%.1f ",state->getY1());
213   bufpos+=sprintf(bufpos,"X2-%.1f ",state->getX2());
214   bufpos+=sprintf(bufpos,"Y2-%.1f ",state->getY2());
215   bufpos+=sprintf(bufpos,"PW%.1f ",state->getPageWidth());
216   bufpos+=sprintf(bufpos,"PH%.1f ",state->getPageHeight());
217   /*bufpos+=sprintf(bufpos,"FC[%.1f/%.1f] ",
218           state->getFillColor()->c[0], state->getFillColor()->c[1]);
219   bufpos+=sprintf(bufpos,"SC[%.1f/%.1f] ",
220           state->getStrokeColor()->c[0], state->getFillColor()->c[1]);*/
221 /*  bufpos+=sprintf(bufpos,"FC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
222           state->getFillColor()->c[0], state->getFillColor()->c[1],
223           state->getFillColor()->c[2], state->getFillColor()->c[3],
224           state->getFillColor()->c[4], state->getFillColor()->c[5],
225           state->getFillColor()->c[6], state->getFillColor()->c[7]);
226   bufpos+=sprintf(bufpos,"SC[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f/%.1f]",
227           state->getStrokeColor()->c[0], state->getFillColor()->c[1],
228           state->getStrokeColor()->c[2], state->getFillColor()->c[3],
229           state->getStrokeColor()->c[4], state->getFillColor()->c[5],
230           state->getStrokeColor()->c[6], state->getFillColor()->c[7]);*/
231   state->getFillRGB(&rgb);
232   if(rgb.r || rgb.g || rgb.b)
233   bufpos+=sprintf(bufpos,"FR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
234   state->getStrokeRGB(&rgb);
235   if(rgb.r || rgb.g || rgb.b)
236   bufpos+=sprintf(bufpos,"SR[%.1f/%.1f/%.1f] ", rgb.r,rgb.g,rgb.b);
237   if(state->getFillColorSpace()->getNComps()>1)
238   bufpos+=sprintf(bufpos,"CS[[%d]] ",state->getFillColorSpace()->getNComps());
239   if(state->getStrokeColorSpace()->getNComps()>1)
240   bufpos+=sprintf(bufpos,"SS[[%d]] ",state->getStrokeColorSpace()->getNComps());
241   if(state->getFillPattern())
242   bufpos+=sprintf(bufpos,"FP%08x ", state->getFillPattern());
243   if(state->getStrokePattern())
244   bufpos+=sprintf(bufpos,"SP%08x ", state->getStrokePattern());
245  
246   if(state->getFillOpacity()!=1.0)
247   bufpos+=sprintf(bufpos,"FO%.1f ", state->getFillOpacity());
248   if(state->getStrokeOpacity()!=1.0)
249   bufpos+=sprintf(bufpos,"SO%.1f ", state->getStrokeOpacity());
250
251   bufpos+=sprintf(bufpos,"LW%.1f ", state->getLineWidth());
252  
253   double * dash;
254   int length;
255   double start;
256   state->getLineDash(&dash, &length, &start);
257   int t;
258   if(length)
259   {
260       bufpos+=sprintf(bufpos,"DASH%.1f[",start);
261       for(t=0;t<length;t++) {
262           bufpos+=sprintf(bufpos,"D%.1f",dash[t]);
263       }
264       bufpos+=sprintf(bufpos,"]");
265   }
266
267   if(state->getFlatness()!=1)
268   bufpos+=sprintf(bufpos,"F%d ", state->getFlatness());
269   if(state->getLineJoin()!=0)
270   bufpos+=sprintf(bufpos,"J%d ", state->getLineJoin());
271   if(state->getLineJoin()!=0)
272   bufpos+=sprintf(bufpos,"C%d ", state->getLineCap());
273   if(state->getLineJoin()!=0)
274   bufpos+=sprintf(bufpos,"ML%d ", state->getMiterLimit());
275
276   if(state->getFont() && state->getFont()->getName() && state->getFont()->getName()->getCString())
277   bufpos+=sprintf(bufpos,"F\"%s\" ",((state->getFont())->getName())->getCString());
278   bufpos+=sprintf(bufpos,"FS%.1f ", state->getFontSize());
279   bufpos+=sprintf(bufpos,"MAT[%.1f/%.1f/%.1f/%.1f/%.1f/%.1f] ", state->getTextMat()[0],state->getTextMat()[1],state->getTextMat()[2],
280                                    state->getTextMat()[3],state->getTextMat()[4],state->getTextMat()[5]);
281   if(state->getCharSpace())
282   bufpos+=sprintf(bufpos,"CS%.5f ", state->getCharSpace());
283   if(state->getWordSpace())
284   bufpos+=sprintf(bufpos,"WS%.5f ", state->getWordSpace());
285   if(state->getHorizScaling()!=1.0)
286   bufpos+=sprintf(bufpos,"SC%.1f ", state->getHorizScaling());
287   if(state->getLeading())
288   bufpos+=sprintf(bufpos,"L%.1f ", state->getLeading());
289   if(state->getRise())
290   bufpos+=sprintf(bufpos,"R%.1f ", state->getRise());
291   if(state->getRender())
292   bufpos+=sprintf(bufpos,"R%d ", state->getRender());
293   bufpos+=sprintf(bufpos,"P%08x ", state->getPath());
294   bufpos+=sprintf(bufpos,"CX%.1f ", state->getCurX());
295   bufpos+=sprintf(bufpos,"CY%.1f ", state->getCurY());
296   if(state->getLineX())
297   bufpos+=sprintf(bufpos,"LX%.1f ", state->getLineX());
298   if(state->getLineY())
299   bufpos+=sprintf(bufpos,"LY%.1f ", state->getLineY());
300   bufpos+=sprintf(bufpos," ");
301   return mybuf;
302 }
303
304 void dumpFontInfo(char*loglevel, GfxFont*font);
305 int lastdumps[1024];
306 int lastdumppos = 0;
307 /* nr = 0  unknown
308    nr = 1  substituting
309    nr = 2  type 3
310  */
311 void showFontError(GfxFont*font, int nr) 
312 {  
313     Ref r=font->getID();
314     int t;
315     for(t=0;t<lastdumppos;t++)
316         if(lastdumps[t] == r.num)
317             break;
318     if(t < lastdumppos)
319       return;
320     if(lastdumppos<sizeof(lastdumps)/sizeof(int))
321     lastdumps[lastdumppos++] = r.num;
322     if(nr == 0)
323       logf("<warning> The following font caused problems:");
324     else if(nr == 1)
325       logf("<warning> The following font caused problems (substituting):");
326     else if(nr == 2)
327       logf("<warning> This document contains Type 3 Fonts: (some text may be incorrectly displayed)");
328
329     dumpFontInfo("<warning>", font);
330 }
331
332 void dumpFontInfo(char*loglevel, GfxFont*font)
333 {
334   GString *gstr;
335   char*name;
336   gstr = font->getName();
337   Ref r=font->getID();
338   logf("%s=========== %s (ID:%d,%d) ==========\n", loglevel, gstr?gstr->getCString():"(unknown font)", r.num,r.gen);
339
340   gstr  = font->getTag();
341   if(gstr) 
342    logf("%sTag: %s\n", loglevel, gstr->getCString());
343   if(font->is16Bit()) logf("%sis 16 bit\n", loglevel);
344
345   GfxFontType type=font->getType();
346   switch(type) {
347     case fontUnknownType:
348      logf("%sType: unknown\n",loglevel);
349     break;
350     case fontType0:
351      logf("%sType: 0\n",loglevel);
352     break;
353     case fontType1:
354      logf("%sType: 1\n",loglevel);
355     break;
356     case fontType1C:
357      logf("%sType: 1C\n",loglevel);
358     break;
359     case fontType3:
360      logf("%sType: 3\n",loglevel);
361     break;
362     case fontTrueType:
363      logf("%sType: TrueType\n",loglevel);
364     break;
365   }
366   
367   Ref embRef;
368   GBool embedded = font->getEmbeddedFontID(&embRef);
369   name = font->getEmbeddedFontName();
370   if(embedded)
371    logf("%sEmbedded name: %s id: %d\n",loglevel, name, embRef.num);
372
373   gstr = font->getExtFontFile();
374   if(gstr)
375    logf("%sExternal Font file: %s\n", loglevel, gstr->getCString());
376
377   // Get font descriptor flags.
378   if(font->isFixedWidth()) logf("%sis fixed width\n", loglevel);
379   if(font->isSerif()) logf("%sis serif\n", loglevel);
380   if(font->isSymbolic()) logf("%sis symbolic\n", loglevel);
381   if(font->isItalic()) logf("%sis italic\n", loglevel);
382   if(font->isBold()) logf("%sis bold\n", loglevel);
383 }
384
385 //void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, int width, int height, GBool invert, GBool inlineImg) {printf("void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str, int width, int height, GBool invert, GBool inlineImg) \n");}
386 //void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, GBool inlineImg) {printf("void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str, int width, int height, GfxImageColorMap *colorMap, GBool inlineImg) \n");}
387
388 SWFOutputDev::SWFOutputDev() 
389 {
390     jpeginfo = 0;
391     linkinfo = 0;
392     pbminfo = 0;
393     clippos = 0;
394     clipping[clippos] = 0;
395     outputstarted = 0;
396 //    printf("SWFOutputDev::SWFOutputDev() \n");
397 };
398
399 T1_OUTLINE* gfxPath_to_T1_OUTLINE(GfxState*state, GfxPath*path)
400 {
401     int num = path->getNumSubpaths();
402     int s,t;
403     bezierpathsegment*start,*last;
404     bezierpathsegment*outline = start = new bezierpathsegment();
405     int cpos = 0;
406     double lastx=0,lasty=0;
407     for(t = 0; t < num; t++) {
408         GfxSubpath *subpath = path->getSubpath(t);
409         int subnum = subpath->getNumPoints();
410
411         for(s=0;s<subnum;s++) {
412            double nx,ny;
413            state->transform(subpath->getX(s),subpath->getY(s),&nx,&ny);
414            int x = (int)((nx-lastx)*0xffff);
415            int y = (int)((ny-lasty)*0xffff);
416            if(s==0) 
417            {
418                 last = outline;
419                 outline->type = T1_PATHTYPE_MOVE;
420                 outline->dest.x = x;
421                 outline->dest.y = y;
422                 outline->link = (T1_OUTLINE*)new bezierpathsegment();
423                 outline = (bezierpathsegment*)outline->link;
424                 cpos = 0;
425                 lastx = nx;
426                 lasty = ny;
427            }
428            else if(subpath->getCurve(s) && !cpos)
429            {
430                 outline->B.x = x;
431                 outline->B.y = y;
432                 cpos = 1;
433            } 
434            else if(subpath->getCurve(s) && cpos)
435            {
436                 outline->C.x = x;
437                 outline->C.y = y;
438                 cpos = 2;
439            }
440            else
441            {
442                 last = outline;
443                 outline->dest.x = x;
444                 outline->dest.y = y;
445                 outline->type = cpos?T1_PATHTYPE_BEZIER:T1_PATHTYPE_LINE;
446                 outline->link = 0;
447                 outline->link = (T1_OUTLINE*)new bezierpathsegment();
448                 outline = (bezierpathsegment*)outline->link;
449                 cpos = 0;
450                 lastx = nx;
451                 lasty = ny;
452            }
453         }
454     }
455     last->link = 0;
456     return (T1_OUTLINE*)start;
457 }
458 /*----------------------------------------------------------------------------
459  * Primitive Graphic routines
460  *----------------------------------------------------------------------------*/
461
462 void SWFOutputDev::stroke(GfxState *state) 
463 {
464     logf("<debug> stroke\n");
465     GfxPath * path = state->getPath();
466     struct swfmatrix m;
467     m.m11 = 1; m.m21 = 0; m.m22 = 1;
468     m.m12 = 0; m.m13 = 0; m.m23 = 0;
469     T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
470     swfoutput_setdrawmode(&output, DRAWMODE_STROKE);
471     swfoutput_drawpath(&output, outline, &m);
472 }
473 void SWFOutputDev::fill(GfxState *state) 
474 {
475     logf("<debug> fill\n");
476     GfxPath * path = state->getPath();
477     struct swfmatrix m;
478     m.m11 = 1; m.m21 = 0; m.m22 = 1;
479     m.m12 = 0; m.m13 = 0; m.m23 = 0;
480     T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
481     swfoutput_setdrawmode(&output, DRAWMODE_FILL);
482     swfoutput_drawpath(&output, outline, &m);
483 }
484 void SWFOutputDev::eoFill(GfxState *state) 
485 {
486     logf("<debug> eofill\n");
487     GfxPath * path = state->getPath();
488     struct swfmatrix m;
489     m.m11 = 1; m.m21 = 0; m.m22 = 1;
490     m.m12 = 0; m.m13 = 0; m.m23 = 0;
491     T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
492     swfoutput_setdrawmode(&output, DRAWMODE_EOFILL);
493     swfoutput_drawpath(&output, outline, &m);
494 }
495 void SWFOutputDev::clip(GfxState *state) 
496 {
497     logf("<debug> clip\n");
498     GfxPath * path = state->getPath();
499     struct swfmatrix m;
500     m.m11 = 1; m.m22 = 1;
501     m.m12 = 0; m.m21 = 0; 
502     m.m13 = 0; m.m23 = 0;
503     T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
504     swfoutput_startclip(&output, outline, &m);
505     clipping[clippos] = 1;
506 }
507 void SWFOutputDev::eoClip(GfxState *state) 
508 {
509     logf("<debug> eoclip\n");
510     GfxPath * path = state->getPath();
511     struct swfmatrix m;
512     m.m11 = 1; m.m21 = 0; m.m22 = 1;
513     m.m12 = 0; m.m13 = 0; m.m23 = 0;
514     T1_OUTLINE*outline = gfxPath_to_T1_OUTLINE(state, path);
515     swfoutput_startclip(&output, outline, &m);
516     clipping[clippos] = 1;
517 }
518
519 SWFOutputDev::~SWFOutputDev() 
520 {
521     swfoutput_destroy(&output);
522     outputstarted = 0;
523 };
524 GBool SWFOutputDev::upsideDown() 
525 {
526     logf("<debug> upsidedown?");
527     return gTrue;
528 };
529 GBool SWFOutputDev::useDrawChar() 
530 {
531     logf("<debug> usedrawchar?");
532     return gTrue;
533 }
534
535 void SWFOutputDev::beginString(GfxState *state, GString *s) 
536
537     double m11,m21,m12,m22;
538 //    logf("<debug> %s beginstring \"%s\"\n", gfxstate2str(state), s->getCString());
539     state->getFontTransMat(&m11, &m12, &m21, &m22);
540     m11 *= state->getHorizScaling();
541     m21 *= state->getHorizScaling();
542     swfoutput_setfontmatrix(&output, m11, -m12, m21, -m22);
543 }
544
545 int charcounter = 0;
546 void SWFOutputDev::drawChar(GfxState *state, double x, double y, double dx, double dy, Guchar c) 
547 {
548     logf("<debug> drawChar(%f,%f,%f,%f,'%c')\n",x,y,dx,dy,c);
549     // check for invisible text -- this is used by Acrobat Capture
550     if ((state->getRender() & 3) != 3)
551     {
552        FontEncoding*enc=state->getFont()->getEncoding();
553
554        double x1,y1;
555        x1 = x;
556        y1 = y;
557        state->transform(x, y, &x1, &y1);
558
559        if(enc->getCharName(c))
560           swfoutput_drawchar(&output, x1, y1, enc->getCharName(c));
561        else
562           logf("<warning> couldn't get name for character %02x from Encoding", c);
563     }
564 }
565
566 void SWFOutputDev::drawChar16(GfxState *state, double x, double y, double dx, double dy, int c) 
567 {
568     printf("<error> drawChar16(%f,%f,%f,%f,%08x)\n",x,y,dx,dy,c);
569     exit(1);
570 }
571
572 void SWFOutputDev::endString(GfxState *state) 
573
574     logf("<debug> endstring\n");
575 }    
576
577 void SWFOutputDev::startPage(int pageNum, GfxState *state) 
578 {
579   double x1,y1,x2,y2;
580   laststate = state;
581   logf("<debug> startPage %d\n", pageNum);
582   logf("<notice> processing page %d", pageNum);
583
584   state->transform(state->getX1(),state->getY1(),&x1,&y1);
585   state->transform(state->getX2(),state->getY2(),&x2,&y2);
586   if(!outputstarted) {
587     swfoutput_init(&output, swffilename, abs((int)(x2-x1)),abs((int)(y2-y1)));
588     outputstarted = 1;
589   }
590   else
591     swfoutput_newpage(&output);
592 }
593
594 void SWFOutputDev::drawLink(Link *link, Catalog *catalog) 
595 {
596   double x1, y1, x2, y2, w;
597   GfxRGB rgb;
598   swfcoord points[5];
599   int x, y;
600
601   link->getBorder(&x1, &y1, &x2, &y2, &w);
602 //  if (w > 0) 
603   {
604     rgb.r = 0;
605     rgb.g = 0;
606     rgb.b = 1;
607     cvtUserToDev(x1, y1, &x, &y);
608     points[0].x = points[4].x = (int)x;
609     points[0].y = points[4].y = (int)y;
610     cvtUserToDev(x2, y1, &x, &y);
611     points[1].x = (int)x;
612     points[1].y = (int)y;
613     cvtUserToDev(x2, y2, &x, &y);
614     points[2].x = (int)x;
615     points[2].y = (int)y;
616     cvtUserToDev(x1, y2, &x, &y);
617     points[3].x = (int)x;
618     points[3].y = (int)y;
619
620     LinkAction*action=link->getAction();
621     char buf[128];
622     char*s = "-?-";
623     char*type = "-?-";
624     char*url = 0;
625     int page = -1;
626     switch(action->getKind())
627     {
628         case actionGoTo: {
629             type = "GoTo";
630             LinkGoTo *ha=(LinkGoTo *)link->getAction();
631             LinkDest *dest=NULL;
632             if (ha->getDest()==NULL) 
633                 dest=catalog->findDest(ha->getNamedDest());
634             else dest=ha->getDest();
635             if (dest){ 
636               if (dest->isPageRef()){
637                 Ref pageref=dest->getPageRef();
638                 page=catalog->findPage(pageref.num,pageref.gen);
639               }
640               else  page=dest->getPageNum();
641               sprintf(buf, "%d", page);
642               s = buf;
643             }
644         }
645         break;
646         case actionGoToR: {
647             type = "GoToR";
648             LinkGoToR*l = (LinkGoToR*)action;
649             GString*g = l->getNamedDest();
650             if(g)
651              s = g->getCString();
652         }
653         break;
654         case actionNamed: {
655             type = "Named";
656             LinkNamed*l = (LinkNamed*)action;
657             GString*name = l->getName();
658             if(name) {
659               s = name->lowerCase()->getCString();
660               if(strstr(s, "next") || strstr(s, "forward"))
661               {
662                   page = currentpage + 1;
663               }
664               else if(strstr(s, "prev") || strstr(s, "back"))
665               {
666                   page = currentpage - 1;
667               }
668               else if(strstr(s, "last") || strstr(s, "end"))
669               {
670                   page = pages[pagepos-1]; //:)
671               }
672               else if(strstr(s, "first") || strstr(s, "top"))
673               {
674                   page = 1;
675               }
676             }
677         }
678         break;
679         case actionLaunch: {
680             type = "Launch";
681             LinkLaunch*l = (LinkLaunch*)action;
682             GString * str = new GString(l->getFileName());
683             str->append(l->getParams());
684             s = str->getCString();
685         }
686         break;
687         case actionURI: {
688             type = "URI";
689             LinkURI*l = (LinkURI*)action;
690             GString*g = l->getURI();
691             if(g) {
692              url = g->getCString();
693              s = url;
694             }
695         }
696         break;
697         case actionUnknown: {
698             type = "Unknown";
699             LinkUnknown*l = (LinkUnknown*)action;
700             s = "";
701         }
702         break;
703         default: {
704             logf("<error> Unknown link type!\n");
705             break;
706         }
707     }
708     if(!linkinfo && (page || url))
709     {
710         logf("<notice> File contains links");
711         linkinfo = 1;
712     }
713     if(page>0)
714     {
715         int t;
716         for(t=0;t<pagepos;t++)
717             if(pages[t]==page)
718                 break;
719         if(t!=pagepos)
720         swfoutput_linktopage(&output, t, points);
721     }
722     else if(url)
723     {
724         swfoutput_linktourl(&output, url, points);
725     }
726     logf("<verbose> \"%s\" link to \"%s\" (%d)\n", type, s, page);
727   }
728 }
729
730 void SWFOutputDev::saveState(GfxState *state) {
731   logf("<debug> saveState\n");
732   updateAll(state);
733   clippos ++;
734   clipping[clippos] = 0;
735 };
736
737 void SWFOutputDev::restoreState(GfxState *state) {
738   logf("<debug> restoreState\n");
739   updateAll(state);
740   if(clipping[clippos])
741       swfoutput_endclip(&output);
742   clippos--;
743 }
744
745 char type3Warning=0;
746
747 int SWFOutputDev::setT1Font(char*name, FontEncoding*encoding) 
748 {       
749     int i;
750     
751     int id=-1;
752     int mapid=-1;
753     char*filename=0;
754     for(i=0;i<sizeof(pdf2t1map)/sizeof(mapping);i++) 
755     {
756         if(!strcmp(name, pdf2t1map[i].pdffont))
757         {
758             filename = pdf2t1map[i].filename;
759             mapid = i;
760         }
761     }
762     if(filename)
763     for(i=0; i<T1_Get_no_fonts(); i++)
764     {
765         char*fontfilename = T1_GetFontFileName (i);
766         if(strstr(fontfilename, filename))
767         {
768                 id = i;
769                 pdf2t1map[i].id = mapid;
770         }
771     }
772     if(id<0)
773      return 0;
774
775     this->t1id = id;
776     return 1;
777 }
778
779 void SWFOutputDev::updateLineWidth(GfxState *state)
780 {
781     double width = state->getTransformedLineWidth();
782     swfoutput_setlinewidth(&output, width);
783 }
784
785 void SWFOutputDev::updateFillColor(GfxState *state) 
786 {
787     GfxRGB rgb;
788     double opaq = state->getFillOpacity();
789     state->getFillRGB(&rgb);
790
791     swfoutput_setfillcolor(&output, (char)(rgb.r*255), (char)(rgb.g*255), 
792                                     (char)(rgb.b*255), (char)(opaq*255));
793 }
794
795 void SWFOutputDev::updateStrokeColor(GfxState *state) 
796 {
797     GfxRGB rgb;
798     double opaq = state->getStrokeOpacity();
799     state->getStrokeRGB(&rgb);
800
801     swfoutput_setstrokecolor(&output, (char)(rgb.r*255), (char)(rgb.g*255), 
802                                       (char)(rgb.b*255), (char)(opaq*255));
803 }
804
805 char*writeEmbeddedFontToFile(GfxFont*font)
806 {
807       char*tmpFileName = NULL;
808       char*fileName = NULL;
809       FILE *f;
810       int c;
811       char *fontBuf;
812       int fontLen;
813       Type1CFontConverter *cvt;
814       Ref embRef;
815       Object refObj, strObj;
816       tmpFileName = "/tmp/tmpfont";
817       font->getEmbeddedFontID(&embRef);
818
819       f = fopen(tmpFileName, "wb");
820       if (!f) {
821         logf("<error> Couldn't create temporary Type 1 font file");
822         return 0;
823       }
824       if (font->getType() == fontType1C) {
825         if (!(fontBuf = font->readEmbFontFile(&fontLen))) {
826           fclose(f);
827           logf("<error> Couldn't read embedded font file");
828           return 0;
829         }
830         cvt = new Type1CFontConverter(fontBuf, fontLen, f);
831         cvt->convert();
832         delete cvt;
833         gfree(fontBuf);
834       } else {
835         font->getEmbeddedFontID(&embRef);
836         refObj.initRef(embRef.num, embRef.gen);
837         refObj.fetch(&strObj);
838         refObj.free();
839         strObj.streamReset();
840         while ((c = strObj.streamGetChar()) != EOF) {
841           fputc(c, f);
842         }
843         strObj.streamClose();
844         strObj.free();
845       }
846       fclose(f);
847       fileName = tmpFileName;
848       if(!fileName) {
849           logf("<error> Embedded font writer didn't create a file");
850           return 0;
851       }
852       return fileName;
853 }
854
855 char* gfxFontName(GfxFont* gfxFont)
856 {
857       GString *gstr;
858       gstr = gfxFont->getName();
859       if(gstr) {
860           return gstr->getCString();
861       }
862       else {
863           char buf[32];
864           Ref r=gfxFont->getID();
865           sprintf(buf, "UFONT%d", r.num);
866           return strdup(buf);
867       }
868 }
869
870 void SWFOutputDev::updateFont(GfxState *state) 
871 {
872   double m11, m12, m21, m22;
873   char * fontname = 0;
874   GfxFont*gfxFont = state->getFont();
875   char * fileName = 0;
876     
877 //  logf("<debug> %s updateFont\n", gfxstate2str(state));
878
879   if (!gfxFont) {
880     return;
881   }  
882
883   if(swfoutput_queryfont(&output, gfxFontName(gfxFont)))
884   {
885       swfoutput_setfont(&output, gfxFontName(gfxFont), -1, 0);
886       return;
887   }
888
889   // look for Type 3 font
890   if (!type3Warning && gfxFont->getType() == fontType3) {
891     type3Warning = gTrue;
892     showFontError(gfxFont, 2);
893   }
894   //dumpFontInfo ("<notice>", gfxFont);
895
896   Ref embRef;
897   GBool embedded = gfxFont->getEmbeddedFontID(&embRef);
898   if(embedded) {
899     if (!gfxFont->is16Bit() &&
900         (gfxFont->getType() == fontType1 ||
901          gfxFont->getType() == fontType1C)) {
902         
903         fileName = writeEmbeddedFontToFile(gfxFont);
904         if(!fileName)
905           return ;
906     }
907     else {
908         showFontError(gfxFont,0);
909         return ;
910     }
911     
912     t1id = T1_AddFont(fileName);
913   } else {
914     fontname = NULL;
915     if(gfxFont->getName()) {
916       fontname = gfxFont->getName()->getCString();
917       //logf("<notice> Processing font %s", fontname);
918     }
919     if(!fontname || !setT1Font(state->getFont()->getName()->getCString(), gfxFont->getEncoding()))
920     { //substitute font
921       int index;
922       int code;
923       double w,w1,w2;
924       double*fm;
925       double v;
926       showFontError(gfxFont, 1);
927       if (!gfxFont->is16Bit()) {
928         if (gfxFont->isFixedWidth()) {
929           index = 8;
930         } else if (gfxFont->isSerif()) {
931           index = 4;
932         } else {
933           index = 0;
934         }
935         if (gfxFont->isBold())
936           index += 2;
937         if (gfxFont->isItalic())
938           index += 1;
939         fontname = fontnames[index];
940         // get width of 'm' in real font and substituted font
941         if ((code = gfxFont->getCharCode("m")) >= 0)
942           w1 = gfxFont->getWidth(code);
943         else
944           w1 = 0;
945         w2 = fontsizes[index];
946         if (gfxFont->getType() == fontType3) {
947           // This is a hack which makes it possible to substitute for some
948           // Type 3 fonts.  The problem is that it's impossible to know what
949           // the base coordinate system used in the font is without actually
950           // rendering the font.  This code tries to guess by looking at the
951           // width of the character 'm' (which breaks if the font is a
952           // subset that doesn't contain 'm').
953           if (w1 > 0 && (w1 > 1.1 * w2 || w1 < 0.9 * w2)) {
954             w1 /= w2;
955             m11 *= w1;
956             m12 *= w1;
957             m21 *= w1;
958             m22 *= w1;
959           }
960           fm = gfxFont->getFontMatrix();
961           v = (fm[0] == 0) ? 1 : (fm[3] / fm[0]);
962           m21 *= v;
963           m22 *= v;
964         } else if (!gfxFont->isSymbolic()) {
965           // if real font is substantially narrower than substituted
966           // font, reduce the font size accordingly
967           if (w1 > 0.01 && w1 < 0.9 * w2) {
968             w1 /= w2;
969             if (w1 < 0.8) {
970               w1 = 0.8;
971             }
972             m11 *= w1;
973             m12 *= w1;
974             m21 *= w1;
975             m22 *= w1;
976           }
977         }
978       }
979       if(fontname)
980         setT1Font(fontname, gfxFont->getEncoding());
981     }
982   }
983
984   swfoutput_setfont(&output,gfxFontName(gfxFont),t1id, fileName);
985   if(fileName)
986       unlink(fileName);
987 }
988
989 int pic_xids[1024];
990 int pic_yids[1024];
991 int pic_ids[1024];
992 int picpos = 0;
993 int pic_id = 0;
994
995 void SWFOutputDev::drawGeneralImage(GfxState *state, Object *ref, Stream *str,
996                                    int width, int height, GfxImageColorMap*colorMap, GBool invert,
997                                    GBool inlineImg, int mask)
998 {
999   FILE *fi;
1000   int c;
1001   char fileName[128];
1002   double x1,y1,x2,y2,x3,y3,x4,y4;
1003   ImageStream *imgStr;
1004   Guchar pixBuf[4];
1005   GfxRGB rgb;
1006   if(!width || !height || (height<=1 && width<=1))
1007   {
1008       logf("<verbose> Ignoring %d by %d image", width, height);
1009       int i,j;
1010       if (inlineImg) {
1011         j = height * ((width + 7) / 8);
1012         str->reset();
1013         for (i = 0; i < j; ++i) {
1014           str->getChar();
1015         }
1016       }
1017       return;
1018   }
1019   
1020   state->transform(0, 1, &x1, &y1);
1021   state->transform(0, 0, &x2, &y2);
1022   state->transform(1, 0, &x3, &y3);
1023   state->transform(1, 1, &x4, &y4);
1024
1025   if (str->getKind() == strDCT &&
1026       (colorMap->getNumPixelComps() == 3 || !mask) )
1027   {
1028     sprintf(fileName, "/tmp/tmp%08x.jpg",lrand48());
1029     logf("<verbose> Found jpeg. Temporary storage is %s", fileName);
1030     if(!jpeginfo)
1031     {
1032         logf("<notice> file contains jpeg pictures");
1033         jpeginfo = 1;
1034     }
1035     if (!(fi = fopen(fileName, "wb"))) {
1036       logf("<error> Couldn't open temporary image file '%s'", fileName);
1037       return;
1038     }
1039     str = ((DCTStream *)str)->getRawStream();
1040     str->reset();
1041     int xid = 0;
1042     int yid = 0;
1043     int count = 0;
1044     while ((c = str->getChar()) != EOF)
1045     {
1046       fputc(c, fi);
1047       xid += count*c;
1048       yid += (~count)*c;
1049       count++;
1050     }
1051     fclose(fi);
1052     
1053     int t,found = -1;
1054     for(t=0;t<picpos;t++)
1055     {
1056         if(pic_xids[t] == xid &&
1057            pic_yids[t] == yid) {
1058             found = t;break;
1059         }
1060     }
1061     if(found<0) {
1062         pic_ids[picpos] = swfoutput_drawimagejpeg(&output, fileName, width, height, 
1063                 x1,y1,x2,y2,x3,y3,x4,y4);
1064         pic_xids[picpos] = xid;
1065         pic_yids[picpos] = yid;
1066         if(picpos<1024)
1067             picpos++;
1068     } else {
1069         swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1070                 x1,y1,x2,y2,x3,y3,x4,y4);
1071     }
1072     unlink(fileName);
1073   } else {
1074
1075     if(!pbminfo) {
1076         logf("<notice> file contains pbm pictures %s",mask?"(masked)":"");
1077         if(mask)
1078         logf("<verbose> drawing %d by %d masked picture\n", width, height);
1079         pbminfo = 1;
1080     }
1081
1082     if(mask) {
1083         imgStr = new ImageStream(str, width, 1, 1);
1084         imgStr->reset();
1085         return;
1086         int yes=0,i,j;
1087         unsigned char buf[8];
1088         int xid = 0;
1089         int yid = 0;
1090         int x,y;
1091         int width2 = (width+3)&(~3);
1092         unsigned char*pic = new unsigned char[width2*height];
1093         RGBA pal[256];
1094         GfxRGB rgb;
1095         state->getFillRGB(&rgb);
1096         pal[0].r = (int)(rgb.r*255); pal[0].g = (int)(rgb.g*255); 
1097         pal[0].b = (int)(rgb.b*255); pal[0].a = 255;
1098         pal[1].r = 0; pal[1].g = 0; pal[1].b = 0; pal[1].a = 0;
1099         xid += pal[1].r*3 + pal[1].g*11 + pal[1].b*17;
1100         yid += pal[1].r*7 + pal[1].g*5 + pal[1].b*23;
1101         for (y = 0; y < height; ++y)
1102         for (x = 0; x < width; ++x)
1103         {
1104               imgStr->getPixel(buf);
1105               pic[width*y+x] = buf[0];
1106               xid+=x*buf[0]+1;
1107               yid+=y*buf[0]+1;
1108         }
1109         int t,found = -1;
1110         for(t=0;t<picpos;t++)
1111         {
1112             if(pic_xids[t] == xid &&
1113                pic_yids[t] == yid) {
1114                 found = t;break;
1115             }
1116         }
1117         if(found<0) {
1118             pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height, 
1119                     x1,y1,x2,y2,x3,y3,x4,y4);
1120             pic_xids[picpos] = xid;
1121             pic_yids[picpos] = yid;
1122             if(picpos<1024)
1123                 picpos++;
1124         } else {
1125             swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1126                     x1,y1,x2,y2,x3,y3,x4,y4);
1127         }
1128         free(pic);
1129     } else {
1130         int x,y;
1131         int width2 = (width+3)&(~3);
1132         imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
1133                                    colorMap->getBits());
1134         imgStr->reset();
1135
1136         if(colorMap->getNumPixelComps()!=1)
1137         {
1138             RGBA*pic=new RGBA[width*height];
1139             int xid = 0;
1140             int yid = 0;
1141             for (y = 0; y < height; ++y) {
1142               for (x = 0; x < width; ++x) {
1143                 int r,g,b,a;
1144                 imgStr->getPixel(pixBuf);
1145                 colorMap->getRGB(pixBuf, &rgb);
1146                 pic[width*y+x].r = r = (U8)(rgb.r * 255 + 0.5);
1147                 pic[width*y+x].g = g = (U8)(rgb.g * 255 + 0.5);
1148                 pic[width*y+x].b = b = (U8)(rgb.b * 255 + 0.5);
1149                 pic[width*y+x].a = a = 255;//(U8)(rgb.a * 255 + 0.5);
1150                 xid += x*r+x*b*3+x*g*7+x*a*11;
1151                 yid += y*r*3+y*b*17+y*g*19+y*a*11;
1152               }
1153             }
1154             int t,found = -1;
1155             for(t=0;t<picpos;t++)
1156             {
1157                 if(pic_xids[t] == xid &&
1158                    pic_yids[t] == yid) {
1159                     found = t;break;
1160                 }
1161             }
1162             if(found<0) {
1163                 pic_ids[picpos] = swfoutput_drawimagelossless(&output, pic, width, height, 
1164                         x1,y1,x2,y2,x3,y3,x4,y4);
1165                 pic_xids[picpos] = xid;
1166                 pic_yids[picpos] = yid;
1167                 if(picpos<1024)
1168                     picpos++;
1169             } else {
1170                 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1171                         x1,y1,x2,y2,x3,y3,x4,y4);
1172             }
1173             delete pic;
1174         }
1175         else
1176         {
1177             U8*pic = new U8[width2*height];
1178             RGBA pal[256];
1179             int t;
1180             int xid=0,yid=0;
1181             for(t=0;t<256;t++)
1182             {
1183                 int r,g,b,a;
1184                 pixBuf[0] = t;
1185                 colorMap->getRGB(pixBuf, &rgb);
1186                 pal[t].r = r = (U8)(rgb.r * 255 + 0.5);
1187                 pal[t].g = g = (U8)(rgb.g * 255 + 0.5);
1188                 pal[t].b = b = (U8)(rgb.b * 255 + 0.5);
1189                 pal[t].a = a = 255;//(U8)(rgb.b * 255 + 0.5);
1190                 xid += t*r+t*b*3+t*g*7+t*a*11;
1191                 xid += (~t)*r+t*b*3+t*g*7+t*a*11;
1192             }
1193             for (y = 0; y < height; ++y) {
1194               for (x = 0; x < width; ++x) {
1195                 imgStr->getPixel(pixBuf);
1196                 pic[width2*y+x] = pixBuf[0];
1197                 xid += x*pixBuf[0]*7;
1198                 yid += y*pixBuf[0]*3;
1199               }
1200             }
1201             int found = -1;
1202             for(t=0;t<picpos;t++)
1203             {
1204                 if(pic_xids[t] == xid &&
1205                    pic_yids[t] == yid) {
1206                     found = t;break;
1207                 }
1208             }
1209             if(found<0) {
1210                 pic_ids[picpos] = swfoutput_drawimagelossless256(&output, pic, pal, width, height, 
1211                         x1,y1,x2,y2,x3,y3,x4,y4);
1212                 pic_xids[picpos] = xid;
1213                 pic_yids[picpos] = yid;
1214                 if(picpos<1024)
1215                     picpos++;
1216             } else {
1217                 swfoutput_drawimageagain(&output, pic_ids[found], width, height,
1218                         x1,y1,x2,y2,x3,y3,x4,y4);
1219             }
1220             delete pic;
1221         }
1222         delete imgStr;
1223     }
1224
1225   }
1226 }
1227
1228 void SWFOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1229                                    int width, int height, GBool invert,
1230                                    GBool inlineImg) 
1231 {
1232   drawGeneralImage(state,ref,str,width,height,0,invert,inlineImg,1);
1233 }
1234
1235 void SWFOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1236                                int width, int height,
1237                                GfxImageColorMap *colorMap, GBool inlineImg) 
1238 {
1239   drawGeneralImage(state,ref,str,width,height,colorMap,0,inlineImg,0);
1240 }
1241
1242 SWFOutputDev*output = 0; 
1243
1244 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
1245   Object obj;
1246   GString *s1, *s2;
1247   int i;
1248
1249   if (infoDict->lookup(key, &obj)->isString()) {
1250     s1 = obj.getString();
1251     if ((s1->getChar(0) & 0xff) == 0xfe &&
1252         (s1->getChar(1) & 0xff) == 0xff) {
1253       s2 = new GString();
1254       for (i = 2; i < obj.getString()->getLength(); i += 2) {
1255         if (s1->getChar(i) == '\0') {
1256           s2->append(s1->getChar(i+1));
1257         } else {
1258           delete s2;
1259           s2 = new GString("<unicode>");
1260           break;
1261         }
1262       }
1263       printf(fmt, s2->getCString());
1264       delete s2;
1265     } else {
1266       printf(fmt, s1->getCString());
1267     }
1268   }
1269   obj.free();
1270 }
1271
1272 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
1273   Object obj;
1274   char *s;
1275
1276   if (infoDict->lookup(key, &obj)->isString()) {
1277     s = obj.getString()->getCString();
1278     if (s[0] == 'D' && s[1] == ':') {
1279       s += 2;
1280     }
1281     printf(fmt, s);
1282   }
1283   obj.free();
1284 }
1285
1286 void pdfswf_init(char*filename, char*userPassword) 
1287 {
1288   GString *fileName = new GString(filename);
1289   GString *userPW;
1290   Object info;
1291   // init error file
1292   errorInit();
1293
1294   // read config file
1295   initParams(xpdfConfigFile);
1296
1297   // open PDF file
1298   xref = NULL;
1299   if (userPassword && userPassword[0]) {
1300     userPW = new GString(userPassword);
1301   } else {
1302     userPW = NULL;
1303   }
1304   doc = new PDFDoc(fileName, userPW);
1305   if (userPW) {
1306     delete userPW;
1307   }
1308   if (!doc->isOk()) {
1309     exit(1);
1310   }
1311
1312   // print doc info
1313   doc->getDocInfo(&info);
1314   if (info.isDict()) {
1315     printInfoString(info.getDict(), "Title",        "Title:        %s\n");
1316     printInfoString(info.getDict(), "Subject",      "Subject:      %s\n");
1317     printInfoString(info.getDict(), "Keywords",     "Keywords:     %s\n");
1318     printInfoString(info.getDict(), "Author",       "Author:       %s\n");
1319     printInfoString(info.getDict(), "Creator",      "Creator:      %s\n");
1320     printInfoString(info.getDict(), "Producer",     "Producer:     %s\n");
1321     printInfoDate(info.getDict(),   "CreationDate", "CreationDate: %s\n");
1322     printInfoDate(info.getDict(),   "ModDate",      "ModDate:      %s\n");
1323   }
1324   info.free();
1325
1326   // print page count
1327   printf("Pages:        %d\n", doc->getNumPages());
1328   numpages = doc->getNumPages();
1329   
1330   // print linearization info
1331   printf("Linearized:   %s\n", doc->isLinearized() ? "yes" : "no");
1332
1333   // print encryption info
1334   printf("Encrypted:    ");
1335   if (doc->isEncrypted()) {
1336     printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
1337            doc->okToPrint() ? "yes" : "no",
1338            doc->okToCopy() ? "yes" : "no",
1339            doc->okToChange() ? "yes" : "no",
1340            doc->okToAddNotes() ? "yes" : "no");
1341         /*ERROR: This pdf is encrypted, and disallows copying.
1342           Due to the DMCA, paragraph 1201, (2) A-C, circumventing
1343           a technological measure that efficively controls access to
1344           a protected work is violating American law. 
1345           See www.eff.org for more information about DMCA issues.
1346          */
1347         if(!doc->okToCopy()) {
1348             printf("PDF disallows copying. Bailing out.\n");
1349             exit(1); //bail out
1350         }
1351         if(!doc->okToChange() || !doc->okToAddNotes())
1352             swfoutput_setprotected();
1353     }
1354   else {
1355     printf("no\n");
1356   }
1357
1358
1359   output = new SWFOutputDev();
1360 }
1361
1362 void pdfswf_drawonlyshapes()
1363 {
1364     drawonlyshapes = 1;
1365 }
1366
1367 void pdfswf_ignoredraworder()
1368 {
1369     ignoredraworder = 1;
1370 }
1371
1372 void pdfswf_linksopennewwindow()
1373 {
1374     opennewwindow = 1;
1375 }
1376
1377 void pdfswf_storeallcharacters()
1378 {
1379     storeallcharacters = 1;
1380 }
1381
1382 void pdfswf_jpegquality(int val)
1383 {
1384     if(val<0) val=0;
1385     if(val>100) val=100;
1386     jpegquality = val;
1387 }
1388
1389 void pdfswf_setoutputfilename(char*_filename)
1390 {
1391     swffilename = _filename;
1392 }
1393
1394
1395 void pdfswf_convertpage(int page)
1396 {
1397     if(!pages)
1398     {
1399         pages = (int*)malloc(1024*sizeof(int));
1400         pagebuflen = 1024;
1401     } else {
1402         if(pagepos == pagebuflen)
1403         {
1404             pagebuflen+=1024;
1405             pages = (int*)realloc(pages, pagebuflen);
1406         }
1407     }
1408     pages[pagepos++] = page;
1409 }
1410
1411 void pdfswf_performconversion()
1412 {
1413     int t;
1414     for(t=0;t<pagepos;t++)
1415     {
1416        currentpage = pages[t];
1417        doc->displayPage((OutputDev*)output, currentpage, /*zoom*/100, /*rotate*/0, /*doLinks*/(int)1);
1418     }
1419 }
1420
1421 int pdfswf_numpages()
1422 {
1423   return doc->getNumPages();
1424 }
1425
1426 int closed=0;
1427 void pdfswf_close()
1428 {
1429     logf("<debug> pdfswf.cc: pdfswf_close()");
1430     delete output;
1431     delete doc;
1432     freeParams();
1433     // check for memory leaks
1434     Object::memCheck(stderr);
1435     gMemReport(stderr);
1436 }
1437