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