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