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