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