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