add documentation about the -i 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 Matthias Kramm <kramm@quiss.org> 
7
8    This file is distributed under the GPL, see file COPYING for details */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include "../config.h"
16 #ifdef HAVE_DIRENT_H
17 #include <dirent.h>
18 #endif
19 #include "../lib/args.h"
20 #include "pdfswf.h"
21 #include "t1lib.h"
22 extern "C" {
23 #include "log.h"
24 }
25
26 static char * outputname = 0;
27 static int loglevel = 3;
28 static char * pagerange = 0;
29 static char * filename = 0;
30 static char * password = 0;
31
32 static char * preloader = 0;
33 static char * viewer = 0;
34
35 char* fontpaths[256];
36 int fontpathpos = 0;
37
38 int systemf(const char* format, ...)
39 {
40     char buf[1024];
41     int ret;
42     va_list arglist;
43     va_start(arglist, format);
44     vsprintf(buf, format, arglist);
45     va_end(arglist);
46
47     printf("%s\n", buf);
48     fflush(stdout);
49     ret = system(buf);
50     if(ret) {
51         fprintf(stderr, "system() returned %d\n", ret);
52         exit(ret);
53     }
54     return ret;
55 }
56
57 int args_callback_option(char*name,char*val) {
58     if (!strcmp(name, "o"))
59     {
60         outputname = val;
61         return 1;
62     }
63     else if (!strcmp(name, "v"))
64     {
65         loglevel ++;
66         return 0;
67     }
68     else if (name[0]=='p')
69     {
70         /* check whether the page range follows the p directly, like 
71            in -p1,2 */
72         do {
73             name++;
74         } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
75
76         if(*name) {
77             pagerange = name;
78             return 0;
79         } 
80         pagerange = val;        
81         return 1;
82     }
83     else if (!strcmp(name, "P"))
84     {
85         password = val;
86         return 1;
87     }
88     else if (!strcmp(name, "s"))
89     {
90         pdfswf_drawonlyshapes();
91         return 0;
92     }
93     else if (!strcmp(name, "i"))
94     {
95         pdfswf_ignoredraworder();
96         return 0;
97     }
98     else if (!strcmp(name, "n"))
99     {
100         pdfswf_linksopennewwindow();
101         return 0;
102     }
103     else if (!strcmp(name, "f"))
104     {
105         pdfswf_storeallcharacters();
106         return 0;
107     }
108     else if (!strcmp(name, "F"))
109     {
110         char *s = strdup(val);
111         int l = strlen(s);
112         while(l && s[l-1]=='/') {
113             s[l-1] = 0;
114             l--;
115         }
116         fontpaths[fontpathpos++] = s;
117         return 1;
118     }
119     else if (!strcmp(name, "l"))
120     {
121         char buf[256];
122         sprintf(buf, "%s/swfs/default_loader.swf", DATADIR);
123         preloader = strdup(buf);
124         return 0;
125     }
126     else if (!strcmp(name, "b"))
127     {
128         char buf[256];
129         sprintf(buf, "%s/swfs/default_viewer.swf", DATADIR);
130         viewer = strdup(buf);
131         return 0;
132     }
133     else if (!strcmp(name, "L"))
134     {
135         if(val)
136         {
137             preloader = val;
138         }
139         else
140         {
141             systemf("ls %s/swfs/*_loader.swf", DATADIR);
142             printf("\n");
143             exit(1);
144         }
145         return 1;
146     }
147     else if (!strcmp(name, "B"))
148     {
149         if(val)
150         {
151             viewer = val;
152         }
153         else
154         {
155             systemf("ls %s/swfs/*_viewer.swf", DATADIR);
156             printf("\n");
157             exit(1);
158         }
159         return 1;
160     }
161     else if (name[0]=='j')
162     {
163         if(name[1]) {
164             pdfswf_jpegquality(atoi(&name[1]));
165             return 0;
166         } else {
167             pdfswf_jpegquality(atoi(val));
168             return 1;
169         }
170     }
171     else if (!strcmp(name, "V"))
172     {   
173         printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
174         exit(0);
175     }
176     else 
177     {
178         fprintf(stderr, "Unknown option: -%s\n", name);
179         exit(1);
180     }
181     return 0;
182 }
183
184 struct options_t options[] =
185 {{"o","output"},
186  {"V","version"},
187  {"i","ignore"},
188  {"s","shapes"},
189  {"j","jpegquality"},
190  {"p","pages"},
191  {"w","samewindow"},
192  {"f","fonts"},
193  {"F","fontpath"},
194  {"B","viewer"},
195  {"L","preloader"},
196  {"b","defaultviewer"},
197  {"l","defaultpreloader"},
198  {0,0}
199 };
200
201 int args_callback_longoption(char*name,char*val) {
202     return args_long2shortoption(options, name, val);
203 }
204
205 int args_callback_command(char*name, char*val) {
206     if (!filename) 
207         filename = name;
208     else {
209         if(outputname)
210         {
211              fprintf(stderr, "Error: Do you want the output to go to %s or to %s?", 
212                      outputname, name);
213              exit(1);
214         }
215         outputname = name;
216     }
217     return 0;
218 }
219
220 void args_callback_usage(char*name)
221 {
222     printf("Usage: %s [-si] [-j quality] [-p range] [-P password] input.pdf [output.swf]\n", name);
223     printf("\n");
224     printf("-p  --pages=range          Convert only pages in range\n");
225     printf("-P  --password=password    Use password for deciphering the pdf\n");
226     printf("-s  --shapes               Don't use SWF Fonts, but store everything as shape\n");
227     printf("-i  --ignore               Ignore draw order (makes the SWF file smaller, but may produce\n");
228     printf("                           graphic errors)\n");
229     printf("-j  --jpegquality=quality  Set quality of embedded jpeg pictures (default:85)\n");
230     printf("-v  --verbose              Be verbose. Use more than one -v for greater effect\n");
231     printf("-w  --samewindow           Don't open a new Browser Window for Links in the SWF\n");
232 #ifdef HAVE_DIRENT
233     printf("-F  --fontdir directory    Add directory to font search path\n");
234 #endif
235     printf("-f  --fonts                Store full fonts in SWF. (Don't reduce to used characters)\n");
236     printf("-V  --version              Print program version\n");
237 #ifndef SYSTEM_BACKTICKS
238     printf("The following might not work because your system call doesn't support command substitution:\n");
239 #endif
240     printf("-b  --defaultviewer        Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", DATADIR);
241     printf("-l  --defaultpreloader     Link preloader \"name\" to the pdf (%s/swfs/default_loader.swf)\n", DATADIR);
242     printf("-B  --viewer=filename      Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
243     printf("-L  --preloader=filename   Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
244 }
245
246 #ifdef HAVE_DIRENT_H
247 void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath) 
248 {
249     if(searchpath) {
250         if(searchpath[0])
251             strcat(searchpath, ":");
252         strcat(searchpath, dirname);
253     }
254     logf("<verbose> Adding %s to search path\n", dirname);
255
256     DIR*dir = opendir(dirname);
257     if(!dir) {
258         logf("<warning> Couldn't open directory %s\n", dirname);
259         return;
260     }
261     dirent*ent;
262     while(1) {
263         ent = readdir (dir);
264         if (!ent) 
265             break;
266         int l;
267         char*name = ent->d_name;
268         char type = 0;
269         if(!name) continue;
270         l=strlen(name);
271         if(l<4)
272             continue;
273         if(!strncasecmp(&name[l-4], ".afm", 4)) 
274             type=1;
275         if(!strncasecmp(&name[l-4], ".ttf", 4)) 
276             type=2;
277         if(type)
278         {
279             if(database && type==1) {
280                 char buf[256],a;
281                 FILE*fi;
282                 sprintf(buf, "%s/%s", dirname,name);
283                 fi = fopen(buf, "rb");
284                 if(!fi || !fread(&a,1,1,fi)) {
285                     logf("<warning> Couldn't read from %s", buf);
286                 }
287                 fprintf(database, "%s\n", buf);
288                 logf("<verbose> Found font %s\n", buf);
289             } 
290             if(numfonts)
291                 (*numfonts)++;
292         }
293     }
294     closedir(dir);
295 }
296 #endif
297
298 int main(int argn, char *argv[])
299 {
300     int ret;
301     char buf[256];
302     int numfonts = 0;
303     int t;
304     char t1searchpath[1024];
305 #ifdef HAVE_SRAND48
306     srand48(time(0));
307 #else
308 #ifdef HAVE_SRAND
309     srand(time(0));
310 #endif
311 #endif
312     processargs(argn, argv);
313     initLog(0,-1,0,0,-1,loglevel);
314     if(!outputname)
315     {
316         fprintf(stderr, "Please use -o to specify an output file\n");
317         exit(1);
318     }
319
320     // test if the page range is o.k.
321     is_in_range(0x7fffffff, pagerange);
322
323     if (!filename) {
324         args_callback_usage(argv[0]);
325         exit(0);
326     }
327
328     logf("<verbose> reading font files from %s/fonts\n", DATADIR);
329     //TODO: use tempnam here. Check if environment already contains a
330     //T1LIB_CONFIG.
331     putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
332     FILE*db = fopen("/tmp/FontDataBase", "wb");
333     FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
334     if(!db || !fi) {
335         fprintf(stderr, "Couldn't create temporary file in /tmp/\n");
336         exit(1);
337     }
338     t1searchpath[0] = 0;
339 #ifdef HAVE_DIRENT_H
340     sprintf(buf, "%s/fonts",DATADIR);
341     // pass 1
342     addfontdir(0, buf, &numfonts, 0);
343     for(t=0;t<fontpathpos;t++) {
344         addfontdir(0, fontpaths[t], &numfonts,0);
345     }
346     fprintf(db, "%d\n", numfonts);
347     // pass 2
348     addfontdir(db, buf, 0, t1searchpath);
349     for(t=0;t<fontpathpos;t++) {
350         addfontdir(db, fontpaths[t], 0, t1searchpath);
351     }
352 #else
353 /* This is a workaround. The correct way would be to
354    get directory listings working on all systems.
355 */
356     strcpy(t1searchpath, DATADIR);
357     strcat(t1searchpath, "/fonts");
358     fprintf(db, "14\n");
359     fprintf(db, "n021003l.afm\n");
360     fprintf(db, "n021023l.afm\n");
361     fprintf(db, "n021004l.afm\n");
362     fprintf(db, "n021024l.afm\n");
363     fprintf(db, "n019003l.afm\n");
364     fprintf(db, "n019023l.afm\n");
365     fprintf(db, "n019004l.afm\n");
366     fprintf(db, "n019024l.afm\n");
367     fprintf(db, "n022003l.afm\n");
368     fprintf(db, "n022023l.afm\n");
369     fprintf(db, "n022004l.afm\n");
370     fprintf(db, "n022024l.afm\n");
371     fprintf(db, "s050000l.afm\n");
372     fprintf(db, "d050000l.afm\n");
373 #endif
374
375     fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
376     fprintf(fi, "ENCODING=%s:.\n", t1searchpath);
377     fprintf(fi, "AFM=%s:.\n", t1searchpath);
378     fprintf(fi, "TYPE1=%s:.\n", t1searchpath);
379     fclose(fi);
380     fclose(db);
381     /* initialize t1lib */
382     T1_SetBitmapPad( 16);
383     if ((T1_InitLib(NO_LOGFILE)==NULL)){
384         fprintf(stderr, "Initialization of t1lib failed\n");
385         exit(1);
386     }
387     unlink("/tmp/t1lib.config.tmp");
388
389     pdfswf_init(filename, password);
390     pdfswf_setoutputfilename(outputname);
391
392     int pages = pdfswf_numpages();
393     for(t = 1; t <= pages; t++) 
394     {
395         if(is_in_range(t, pagerange))
396         pdfswf_convertpage(t);
397     }
398     pdfswf_performconversion();
399
400     pdfswf_close();
401
402     if(viewer || preloader) {
403 #ifndef SYSTEM_BACKTICKS
404         logf("<warning> Not sure whether system() can handle command substitution");
405         logf("<warning> (According to config.h, it can't)");
406 #endif
407         printf("\n");
408     }
409
410     if(viewer && !preloader) {
411         systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
412                 outputname, viewer, outputname, outputname);
413         printf("\n");
414     }
415     if(preloader && !viewer) {
416         logf("<warning> --preloader option without --viewer option doesn't make very much sense.");
417         ret = systemf("swfcombine `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
418                 preloader, DATADIR, preloader, outputname, outputname);
419         printf("\n");
420     }
421     if(preloader && viewer) {
422         systemf("swfcombine %s viewport=%s -o __tmp__.swf",
423                 viewer, outputname, outputname);
424         systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
425                 outputname, preloader, DATADIR, preloader, outputname);
426         systemf("rm __tmp__.swf");
427     }
428
429     unlink("/tmp/FontDataBase");
430     return 0;
431 }
432
433