70a7620b13c2b269267f9ef6e6a028f84e22b3d4
[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)\n");
228     printf("-j  --jpegquality=quality  Set quality of embedded jpeg pictures (default:85)\n");
229     printf("-v  --verbose              Be verbose. Use more than one -v for greater effect\n");
230     printf("-w  --samewindow           Don't open a new Browser Window for Links in the SWF\n");
231 #ifdef HAVE_DIRENT
232     printf("-F  --fontdir directory    Add directory to font search path\n");
233 #endif
234     printf("-f  --fonts                Store full fonts in SWF. (Don't reduce to used characters)\n");
235     printf("-V  --version              Print program version\n");
236 #ifndef SYSTEM_BACKTICKS
237     printf("The following might not work because your system call doesn't support command substitution:\n");
238 #endif
239     printf("-b  --defaultviewer        Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", DATADIR);
240     printf("-l  --defaultpreloader     Link preloader \"name\" to the pdf (%s/swfs/default_loader.swf)\n", DATADIR);
241     printf("-B  --viewer=filename      Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
242     printf("-L  --preloader=filename   Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
243 }
244
245 #ifdef HAVE_DIRENT_H
246 void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath) 
247 {
248     if(searchpath) {
249         if(searchpath[0])
250             strcat(searchpath, ":");
251         strcat(searchpath, dirname);
252     }
253     logf("<verbose> Adding %s to search path\n", dirname);
254
255     DIR*dir = opendir(dirname);
256     if(!dir) {
257         logf("<warning> Couldn't open directory %s\n", dirname);
258         return;
259     }
260     dirent*ent;
261     while(1) {
262         ent = readdir (dir);
263         if (!ent) 
264             break;
265         int l;
266         char*name = ent->d_name;
267         char type = 0;
268         if(!name) continue;
269         l=strlen(name);
270         if(l<4)
271             continue;
272         if(!strncasecmp(&name[l-4], ".afm", 4)) 
273             type=1;
274         if(!strncasecmp(&name[l-4], ".ttf", 4)) 
275             type=2;
276         if(type)
277         {
278             if(database && type==1) {
279                 char buf[256],a;
280                 FILE*fi;
281                 sprintf(buf, "%s/%s", dirname,name);
282                 fi = fopen(buf, "rb");
283                 if(!fi || !fread(&a,1,1,fi)) {
284                     logf("<warning> Couldn't read from %s", buf);
285                 }
286                 fprintf(database, "%s\n", buf);
287                 logf("<verbose> Found font %s\n", buf);
288             } 
289             if(numfonts)
290                 (*numfonts)++;
291         }
292     }
293     closedir(dir);
294 }
295 #endif
296
297 int main(int argn, char *argv[])
298 {
299     int ret;
300     char buf[256];
301     int numfonts = 0;
302     int t;
303     char t1searchpath[1024];
304 #ifdef HAVE_SRAND48
305     srand48(time(0));
306 #else
307 #ifdef HAVE_SRAND
308     srand(time(0));
309 #endif
310 #endif
311     processargs(argn, argv);
312     initLog(0,-1,0,0,-1,loglevel);
313     if(!outputname)
314     {
315         fprintf(stderr, "Please use -o to specify an output file\n");
316         exit(1);
317     }
318
319     // test if the page range is o.k.
320     is_in_range(0x7fffffff, pagerange);
321
322     if (!filename) {
323         args_callback_usage(argv[0]);
324         exit(0);
325     }
326
327     logf("<verbose> reading font files from %s/fonts\n", DATADIR);
328     //TODO: use tempnam here. Check if environment already contains a
329     //T1LIB_CONFIG.
330     putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
331     FILE*db = fopen("/tmp/FontDataBase", "wb");
332     FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
333     if(!db || !fi) {
334         fprintf(stderr, "Couldn't create temporary file in /tmp/\n");
335         exit(1);
336     }
337     t1searchpath[0] = 0;
338 #ifdef HAVE_DIRENT_H
339     sprintf(buf, "%s/fonts",DATADIR);
340     // pass 1
341     addfontdir(0, buf, &numfonts, 0);
342     for(t=0;t<fontpathpos;t++) {
343         addfontdir(0, fontpaths[t], &numfonts,0);
344     }
345     fprintf(db, "%d\n", numfonts);
346     // pass 2
347     addfontdir(db, buf, 0, t1searchpath);
348     for(t=0;t<fontpathpos;t++) {
349         addfontdir(db, fontpaths[t], 0, t1searchpath);
350     }
351 #else
352 /* This is a workaround. The correct way would be to
353    get directory listings working on all systems.
354 */
355     strcpy(t1searchpath, DATADIR);
356     strcat(t1searchpath, "/fonts");
357     fprintf(db, "14\n");
358     fprintf(db, "n021003l.afm\n");
359     fprintf(db, "n021023l.afm\n");
360     fprintf(db, "n021004l.afm\n");
361     fprintf(db, "n021024l.afm\n");
362     fprintf(db, "n019003l.afm\n");
363     fprintf(db, "n019023l.afm\n");
364     fprintf(db, "n019004l.afm\n");
365     fprintf(db, "n019024l.afm\n");
366     fprintf(db, "n022003l.afm\n");
367     fprintf(db, "n022023l.afm\n");
368     fprintf(db, "n022004l.afm\n");
369     fprintf(db, "n022024l.afm\n");
370     fprintf(db, "s050000l.afm\n");
371     fprintf(db, "d050000l.afm\n");
372 #endif
373
374     fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
375     fprintf(fi, "ENCODING=%s:.\n", t1searchpath);
376     fprintf(fi, "AFM=%s:.\n", t1searchpath);
377     fprintf(fi, "TYPE1=%s:.\n", t1searchpath);
378     fclose(fi);
379     fclose(db);
380     /* initialize t1lib */
381     T1_SetBitmapPad( 16);
382     if ((T1_InitLib(NO_LOGFILE)==NULL)){
383         fprintf(stderr, "Initialization of t1lib failed\n");
384         exit(1);
385     }
386     unlink("/tmp/t1lib.config.tmp");
387
388     pdfswf_init(filename, password);
389     pdfswf_setoutputfilename(outputname);
390
391     int pages = pdfswf_numpages();
392     for(t = 1; t <= pages; t++) 
393     {
394         if(is_in_range(t, pagerange))
395         pdfswf_convertpage(t);
396     }
397     pdfswf_performconversion();
398
399     pdfswf_close();
400
401     if(viewer || preloader) {
402 #ifndef SYSTEM_BACKTICKS
403         logf("<warning> Not sure whether system() can handle command substitution");
404         logf("<warning> (According to config.h, it can't)");
405 #endif
406         printf("\n");
407     }
408
409     if(viewer && !preloader) {
410         systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
411                 outputname, viewer, outputname, outputname);
412         printf("\n");
413     }
414     if(preloader && !viewer) {
415         logf("<warning> --preloader option without --viewer option doesn't make very much sense.");
416         ret = systemf("swfcombine `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
417                 preloader, DATADIR, preloader, outputname, outputname);
418         printf("\n");
419     }
420     if(preloader && viewer) {
421         systemf("swfcombine %s viewport=%s -o __tmp__.swf",
422                 viewer, outputname, outputname);
423         systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
424                 outputname, preloader, DATADIR, preloader, outputname);
425         systemf("rm __tmp__.swf");
426     }
427
428     unlink("/tmp/FontDataBase");
429     return 0;
430 }
431
432