added documentation about zoom option.
[swftools.git] / pdf2swf / pdf2swf.cc
1 /* pdf2swf.cc
2    main routine for pdf2swf(1)
3
4    Part of the swftools package.
5    
6    Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org> 
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include "../config.h"
28 #ifdef HAVE_DIRENT_H
29 #include <dirent.h>
30 #endif
31 #ifdef HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34 #include "../lib/args.h"
35 #include "SWFOutputDev.h"
36 extern "C" {
37 #include "log.h"
38 }
39
40 #ifndef WIN32
41 #define FONTDIR SWFTOOLS_DATADIR "/fonts"
42 #define SWFDIR SWFTOOLS_DATADIR "/swfs"
43 #else
44 #define FONTDIR "C:\\swftools\\fonts"
45 #define SWFDIR "C:\\swftools\\swfs"
46 #endif
47
48 static char * outputname = 0;
49 static int loglevel = 3;
50 static char * pagerange = 0;
51 static char * filename = 0;
52 static char * password = 0;
53
54 static char * preloader = 0;
55 static char * viewer = 0;
56
57 char* fontpaths[256];
58 int fontpathpos = 0;
59
60 static int system_quiet=0;
61
62 int systemf(const char* format, ...)
63 {
64     char buf[1024];
65     int ret;
66     va_list arglist;
67     va_start(arglist, format);
68     vsprintf(buf, format, arglist);
69     va_end(arglist);
70
71     if(!system_quiet) {
72         printf("%s\n", buf);
73         fflush(stdout);
74     }
75     ret = system(buf);
76     if(ret) {
77         fprintf(stderr, "system() returned %d\n", ret);
78         exit(ret);
79     }
80     return ret;
81 }
82
83 int args_callback_option(char*name,char*val) {
84     if (!strcmp(name, "o"))
85     {
86         outputname = val;
87         return 1;
88     }
89     else if (!strcmp(name, "v"))
90     {
91         loglevel ++;
92         return 0;
93     }
94     else if (!strcmp(name, "q"))
95     {
96         loglevel --;
97         system_quiet = 1;
98         return 0;
99     }
100     else if (name[0]=='p')
101     {
102         /* check whether the page range follows the p directly, like 
103            in -p1,2 */
104         do {
105             name++;
106         } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
107
108         if(*name) {
109             pagerange = name;
110             return 0;
111         } 
112         pagerange = val;        
113         return 1;
114     }
115     else if (!strcmp(name, "P"))
116     {
117         password = val;
118         return 1;
119     }
120     else if (!strcmp(name, "s"))
121     {
122         char*s = strdup(val);
123         char*c = strchr(s, '=');
124         if(c && *c && c[1])  {
125             *c = 0;
126             c++;
127             pdfswf_setparameter(s,c);
128         }
129         else
130             pdfswf_setparameter(s,"1");
131         return 1;
132     }
133     else if (!strcmp(name, "S"))
134     {
135         pdfswf_drawonlyshapes();
136         return 0;
137     }
138     else if (!strcmp(name, "i"))
139     {
140         pdfswf_ignoredraworder();
141         return 0;
142     }
143     else if (!strcmp(name, "z"))
144     {
145         pdfswf_enablezlib();
146         return 0;
147     }
148     else if (!strcmp(name, "n"))
149     {
150         pdfswf_linksopennewwindow();
151         return 0;
152     }
153     else if (!strcmp(name, "t"))
154     {
155         pdfswf_insertstop();
156         return 0;
157     }
158     else if (!strcmp(name, "T"))
159     {
160         int i = atoi(val);
161         if(!strcasecmp(val, "mx"))
162             i = 6;
163         pdfswf_setversion(i);
164         return 1;
165     }
166     else if (!strcmp(name, "f"))
167     {
168         pdfswf_storeallcharacters();
169         return 0;
170     }
171     else if (!strcmp(name, "F"))
172     {
173         char *s = strdup(val);
174         int l = strlen(s);
175         while(l && s[l-1]=='/') {
176             s[l-1] = 0;
177             l--;
178         }
179         fontpaths[fontpathpos++] = s;
180         return 1;
181     }
182     else if (!strcmp(name, "l"))
183     {
184         char buf[256];
185         sprintf(buf, "%s/default_loader.swf", SWFDIR);
186         preloader = strdup(buf);
187         return 0;
188     }
189     else if (!strcmp(name, "b"))
190     {
191         char buf[256];
192         sprintf(buf, "%s/default_viewer.swf", SWFDIR);
193         viewer = strdup(buf);
194         return 0;
195     }
196     else if (!strcmp(name, "L"))
197     {
198         if(val)
199         {
200             preloader = val;
201         }
202         else
203         {
204             systemf("ls %s/*_loader.swf", SWFDIR);
205             if(!system_quiet)
206                 printf("\n");
207             exit(1);
208         }
209         return 1;
210     }
211     else if (!strcmp(name, "B"))
212     {
213         if(val)
214         {
215             viewer = val;
216         }
217         else
218         {
219             systemf("ls %s/*_viewer.swf", SWFDIR);
220             if(!system_quiet)
221                 printf("\n");
222             exit(1);
223         }
224         return 1;
225     }
226     else if (!strcmp(name, "j"))
227     {
228         if(name[1]) {
229             pdfswf_jpegquality(atoi(&name[1]));
230             return 0;
231         } else {
232             pdfswf_jpegquality(atoi(val));
233             return 1;
234         }
235     }
236     else if (!strcmp(name, "V"))
237     {   
238         printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
239         exit(0);
240     }
241     else 
242     {
243         fprintf(stderr, "Unknown option: -%s\n", name);
244         exit(1);
245     }
246     return 0;
247 }
248
249 /*struct docoptions_t options[] =
250 {{"o","output","filename::Specify output file"},
251  {"V","version","Print program version"},
252  {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
253  {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
254  {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
255  {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
256  {"p","pages","Convert only pages in range. (E.g. 3-85)"},
257  {"w","samewindow","Don't open a new browser window for links in the SWF"},
258  {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
259  {"F","fontpath","path::Add directory to font search path"},
260  {"B","viewer","name::Link viewer \"name\" to the pdf"},
261  {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
262  {"b","defaultviewer","Link default viewer to the pdf"},
263  {"l","defaultpreloader","Link default preloader to the pdf"}
264  {0,0}
265 };*/
266 struct options_t options[] =
267 {{"o","output"},
268  {"q","quiet"},
269  {"V","version"},
270  {"i","ignore"},
271  {"z","zlib"},
272  {"s","set"},
273  {"S","shapes"},
274  {"j","jpegquality"},
275  {"p","pages"},
276  {"w","samewindow"},
277  {"f","fonts"},
278  {"F","fontdir"},
279  {"B","viewer"},
280  {"L","preloader"},
281  {"b","defaultviewer"},
282  {"l","defaultpreloader"},
283  {"t","stop"},
284  {"T","flashversion"},
285  {0,0}
286 };
287
288 int args_callback_longoption(char*name,char*val) {
289     return args_long2shortoption(options, name, val);
290 }
291
292 int args_callback_command(char*name, char*val) {
293     if (!filename) 
294         filename = name;
295     else {
296         if(outputname)
297         {
298              fprintf(stderr, "Error: Do you want the output to go to %s or to %s?", 
299                      outputname, name);
300              exit(1);
301         }
302         outputname = name;
303     }
304     return 0;
305 }
306
307 void args_callback_usage(char*name)
308 {
309     printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
310     printf("\nBasic options:\n");
311     printf("-p  --pages=range          Convert only pages in range\n");
312     printf("-P  --password=password    Use password for deciphering the pdf\n");
313     printf("-v  --verbose              Be verbose. Use more than one -v for greater effect\n");
314     printf("-q  --quiet                Suppress normal messages. Use -qq to suppress warnings, also.\n");
315 #ifdef HAVE_DIRENT_H
316     printf("-F  --fontdir directory    Add directory to font search path\n");
317 #endif
318     printf("-V  --version              Print program version\n");
319     printf("\nEnhanced conversion options:\n");
320     printf("-S  --shapes               Don't use SWF Fonts, but store everything as shape\n");
321     printf("-z  --zlib                 Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
322     printf("-w  --samewindow           Don't open a new Browser Window for Links in the SWF\n");
323     printf("-f  --fonts                Store full fonts in SWF. (Don't reduce to used characters)\n");
324     printf("-T  --flashversion=num     Set the flash version in the header to num (default: 4)\n");
325     printf("-s insertstop              Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
326     printf("-s zoom=factor             Scale result, default: 100 (before 0.6.2: 72)\n");
327     printf("-s jpegquality=quality     Set quality of embedded jpeg pictures (default:85)\n");
328     printf("-s caplinewidth=value      Set the minimum line width to trigger cap style handling to value. (3)\n");
329     printf("-s splinequality=value     Set the quality of spline convertion to value (0-100, default: 100).\n");
330     printf("-s fontquality=value       Set the quality of font convertion to value (0-100, default: 100).\n");
331     printf("-s ignoredraworder         Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
332            "                           graphic errors)\n");
333     printf("-s filloverlap             Make intersecting shapes overlap, instead of canceling each\n"
334            "                           other out. (Needed for some Powerpoint PDFs)\n");
335     printf("Postprocessing options:\n");
336 #ifndef SYSTEM_BACKTICKS
337     printf("(They might not work because your system call doesn't support command substitution)\n");
338 #endif
339     printf("-b  --defaultviewer        Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", SWFTOOLS_DATADIR);
340     printf("-l  --defaultpreloader     Link default preloader the pdf (%s/swfs/default_loader.swf)\n", SWFTOOLS_DATADIR);
341     printf("-B  --viewer=filename      Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
342     printf("-L  --preloader=filename   Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
343 }
344
345 #ifdef HAVE_DIRENT_H
346 void addfontdir(char* dirname, int*numfonts)
347 {
348     if(!numfonts)
349         msg("<verbose> Adding %s to search path\n", dirname);
350
351     DIR*dir = opendir(dirname);
352     if(!dir) {
353         msg("<warning> Couldn't open directory %s\n", dirname);
354         return;
355     }
356     dirent*ent;
357     while(1) {
358         ent = readdir (dir);
359         if (!ent) 
360             break;
361         int l;
362         char*name = ent->d_name;
363         char type = 0;
364         if(!name) continue;
365         l=strlen(name);
366         if(l<4)
367             continue;
368         if(!strncasecmp(&name[l-4], ".pfa", 4)) 
369             type=1;
370         if(!strncasecmp(&name[l-4], ".pfb", 4)) 
371             type=3;
372         if(!strncasecmp(&name[l-4], ".ttf", 4)) 
373             type=2;
374         if(type)
375         {
376             char*fontname = (char*)malloc(strlen(dirname)+strlen(name)+2);
377             strcpy(fontname, dirname);
378 #ifdef WIN32
379                 strcat(fontname, "\\");
380 #else
381                 strcat(fontname, "/");
382 #endif
383             strcat(fontname, name);
384             if(!numfonts)
385                 msg("<debug> Adding %s to fonts", fontname);
386             pdfswf_addfont(fontname);
387             if(numfonts)
388                 (*numfonts)++;
389         }
390     }
391     closedir(dir);
392 }
393 #endif
394
395 char* stripfilename(char*filename, char*newext)
396 {
397     char*last1 = strrchr(filename, '/');
398     char*last2 = strrchr(filename, '\\');
399     char*pos = filename;
400     char*name;
401     char*dot;
402     if(last1>pos) pos = last1 + 1;
403     if(last2>pos) pos = last2 + 1;
404     name = (char*)malloc(strlen(pos)+5);
405     strcpy(name, pos);
406     dot = strrchr(name, '.');
407     if(dot) {
408         *dot = 0;
409     }
410     strcat(name, newext);
411     return name;
412 }
413
414 int main(int argn, char *argv[])
415 {
416     int ret;
417     char buf[256];
418     int numfonts = 0;
419     int t;
420     char t1searchpath[1024];
421
422 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
423     FILE*test = fopen(FONTDIR "\\d050000l.afm", "rb");
424     if(!test) {
425         fprintf(stderr, "Couldn't find file " FONTDIR "\\d050000l.afm- pdf2swf not installed properly? OS says:\n");
426         perror("open");
427         exit(1);
428     }
429     fclose(test);
430 #endif
431
432 #ifdef HAVE_SRAND48
433     srand48(time(0));
434 #else
435 #ifdef HAVE_SRAND
436     srand(time(0));
437 #endif
438 #endif
439     processargs(argn, argv);
440     initLog(0,-1,0,0,-1,loglevel);
441
442     if(!filename)
443     {
444         fprintf(stderr, "Please specify an input file\n");
445         exit(1);
446     }
447
448     if(!outputname)
449     {
450         if(filename) {
451             outputname = stripfilename(filename, ".swf");
452             msg("<notice> Output filename not given. Writing to %s", outputname);
453         } 
454     }
455         
456     if(!outputname)
457     {
458         fprintf(stderr, "Please use -o to specify an output file\n");
459         exit(1);
460     }
461
462     // test if the page range is o.k.
463     is_in_range(0x7fffffff, pagerange);
464
465     if (!filename) {
466         args_callback_usage(argv[0]);
467         exit(0);
468     }
469
470 #ifdef HAVE_DIRENT_H
471     // pass 1
472     addfontdir(FONTDIR, &numfonts);
473     for(t=0;t<fontpathpos;t++) {
474         addfontdir(fontpaths[t], &numfonts);
475     }
476     // pass 2
477     addfontdir(FONTDIR, 0);
478     for(t=0;t<fontpathpos;t++) {
479         addfontdir(fontpaths[t], 0);
480     }
481 #else
482     msg("<error> Couldn't find any fonts!");
483 #endif
484
485     pdfswf_init(filename, password);
486     pdfswf_setoutputfilename(outputname);
487
488     int pages = pdfswf_numpages();
489     for(t = 1; t <= pages; t++) 
490     {
491         if(is_in_range(t, pagerange))
492         pdfswf_convertpage(t);
493     }
494     pdfswf_performconversion();
495
496     pdfswf_close();
497
498     if(viewer || preloader) {
499 #ifndef SYSTEM_BACKTICKS
500         msg("<warning> Not sure whether system() can handle command substitution");
501         msg("<warning> (According to config.h, it can't)");
502 #endif
503         if(!system_quiet)
504             printf("\n");
505     }
506
507     if(viewer && !preloader) {
508         systemf("swfcombine `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",
509                 outputname, viewer, outputname, outputname);
510         if(!system_quiet)
511             printf("\n");
512     }
513     if(preloader && !viewer) {
514         msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
515         ret = systemf("swfcombine `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",
516                 preloader, SWFDIR, preloader, outputname, outputname);
517         if(!system_quiet)
518             printf("\n");
519     }
520     if(preloader && viewer) {
521         systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
522                 viewer, outputname, outputname);
523         systemf("swfcombine `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",
524                 outputname, preloader, SWFDIR, preloader, outputname);
525         systemf("rm __tmp__.swf");
526     }
527
528     return 0;
529 }
530
531