2 main routine for pdf2swf(1)
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This file is distributed under the GPL, see file COPYING for details */
15 #include "../config.h"
19 #include "../lib/args.h"
20 #include "SWFOutputDev.h"
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;
32 static char * preloader = 0;
33 static char * viewer = 0;
38 static int system_quiet=0;
40 int systemf(const char* format, ...)
45 va_start(arglist, format);
46 vsprintf(buf, format, arglist);
55 fprintf(stderr, "system() returned %d\n", ret);
61 int args_callback_option(char*name,char*val) {
62 if (!strcmp(name, "o"))
67 else if (!strcmp(name, "v"))
72 else if (!strcmp(name, "q"))
78 else if (name[0]=='p')
80 /* check whether the page range follows the p directly, like
84 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
93 else if (!strcmp(name, "P"))
98 else if (!strcmp(name, "s"))
100 pdfswf_drawonlyshapes();
103 else if (!strcmp(name, "i"))
105 pdfswf_ignoredraworder();
108 else if (!strcmp(name, "z"))
113 else if (!strcmp(name, "n"))
115 pdfswf_linksopennewwindow();
118 else if (!strcmp(name, "t"))
123 else if (!strcmp(name, "T"))
126 if(!strcasecmp(val, "mx"))
128 pdfswf_setversion(i);
131 else if (!strcmp(name, "f"))
133 pdfswf_storeallcharacters();
136 else if (!strcmp(name, "F"))
138 char *s = strdup(val);
140 while(l && s[l-1]=='/') {
144 fontpaths[fontpathpos++] = s;
147 else if (!strcmp(name, "l"))
150 sprintf(buf, "%s/swfs/default_loader.swf", SWFTOOLS_DATADIR);
151 preloader = strdup(buf);
154 else if (!strcmp(name, "b"))
157 sprintf(buf, "%s/swfs/default_viewer.swf", SWFTOOLS_DATADIR);
158 viewer = strdup(buf);
161 else if (!strcmp(name, "L"))
169 systemf("ls %s/swfs/*_loader.swf", SWFTOOLS_DATADIR);
176 else if (!strcmp(name, "B"))
184 systemf("ls %s/swfs/*_viewer.swf", SWFTOOLS_DATADIR);
191 else if (!strcmp(name, "j"))
194 pdfswf_jpegquality(atoi(&name[1]));
197 pdfswf_jpegquality(atoi(val));
201 else if (!strcmp(name, "V"))
203 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
208 fprintf(stderr, "Unknown option: -%s\n", name);
214 /*struct docoptions_t options[] =
215 {{"o","output","filename::Specify output file"},
216 {"V","version","Print program version"},
217 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
218 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
219 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
220 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
221 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
222 {"w","samewindow","Don't open a new browser window for links in the SWF"},
223 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
224 {"F","fontpath","path::Add directory to font search path"},
225 {"B","viewer","name::Link viewer \"name\" to the pdf"},
226 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
227 {"b","defaultviewer","Link default viewer to the pdf"},
228 {"l","defaultpreloader","Link default preloader to the pdf"}
231 struct options_t options[] =
245 {"b","defaultviewer"},
246 {"l","defaultpreloader"},
248 {"T","flashversion"},
252 int args_callback_longoption(char*name,char*val) {
253 return args_long2shortoption(options, name, val);
256 int args_callback_command(char*name, char*val) {
262 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
271 void args_callback_usage(char*name)
273 printf("Usage: %s [-si] [-j quality] [-p range] [-P password] input.pdf [-o output.swf]\n", name);
275 printf("-p --pages=range Convert only pages in range\n");
276 printf("-P --password=password Use password for deciphering the pdf\n");
277 printf("-s --shapes Don't use SWF Fonts, but store everything as shape\n");
278 printf("-i --ignore Ignore draw order (makes the SWF file smaller, but may produce\n");
279 printf(" graphic errors)\n");
280 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
281 printf("-j --jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
282 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
283 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
284 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
286 printf("-F --fontdir directory Add directory to font search path\n");
288 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
289 printf("-V --version Print program version\n");
290 printf("-t --stop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
291 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
292 #ifndef SYSTEM_BACKTICKS
293 printf("The following might not work because your system call doesn't support command substitution:\n");
295 printf("-b --defaultviewer Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", SWFTOOLS_DATADIR);
296 printf("-l --defaultpreloader Link default preloader the pdf (%s/swfs/default_loader.swf)\n", SWFTOOLS_DATADIR);
297 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
298 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
302 void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath)
306 strcat(searchpath, ":");
307 strcat(searchpath, dirname);
309 logf("<verbose> Adding %s to search path\n", dirname);
311 DIR*dir = opendir(dirname);
313 logf("<warning> Couldn't open directory %s\n", dirname);
322 char*name = ent->d_name;
328 if(!strncasecmp(&name[l-4], ".afm", 4))
330 if(!strncasecmp(&name[l-4], ".ttf", 4))
334 if(database && type==1) {
337 sprintf(buf, "%s/%s", dirname,name);
338 fi = fopen(buf, "rb");
339 if(!fi || !fread(&a,1,1,fi)) {
340 logf("<warning> Couldn't read from %s", buf);
342 fprintf(database, "%s\n", buf);
343 logf("<verbose> Found font %s\n", buf);
354 char* stripfilename(char*filename, char*newext)
356 char*last1 = strrchr(filename, '/');
357 char*last2 = strrchr(filename, '\\');
361 if(last1>pos) pos = last1 + 1;
362 if(last2>pos) pos = last2 + 1;
363 name = (char*)malloc(strlen(pos)+5);
365 dot = strrchr(name, '.');
369 strcat(name, newext);
373 int main(int argn, char *argv[])
379 char t1searchpath[1024];
387 processargs(argn, argv);
388 initLog(0,-1,0,0,-1,loglevel);
392 outputname = stripfilename(filename, ".swf");
393 logf("<notice> Output filename not given. Writing to %s", outputname);
399 fprintf(stderr, "Please use -o to specify an output file\n");
403 // test if the page range is o.k.
404 is_in_range(0x7fffffff, pagerange);
407 args_callback_usage(argv[0]);
411 logf("<verbose> reading font files from %s/fonts\n", SWFTOOLS_DATADIR);
412 //TODO: use tempnam here. Check if environment already contains a
414 putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
415 FILE*db = fopen("/tmp/FontDataBase", "wb");
416 FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
418 fprintf(stderr, "Couldn't create temporary file in /tmp/\n");
423 sprintf(buf, "%s/fonts",SWFTOOLS_DATADIR);
425 addfontdir(0, buf, &numfonts, 0);
426 for(t=0;t<fontpathpos;t++) {
427 addfontdir(0, fontpaths[t], &numfonts,0);
429 fprintf(db, "%d\n", numfonts);
431 addfontdir(db, buf, 0, t1searchpath);
432 for(t=0;t<fontpathpos;t++) {
433 addfontdir(db, fontpaths[t], 0, t1searchpath);
436 /* This is a workaround. The correct way would be to
437 get directory listings working on all systems.
439 strcpy(t1searchpath, SWFTOOLS_DATADIR);
440 strcat(t1searchpath, "/fonts");
442 fprintf(db, "n021003l.afm\n");
443 fprintf(db, "n021023l.afm\n");
444 fprintf(db, "n021004l.afm\n");
445 fprintf(db, "n021024l.afm\n");
446 fprintf(db, "n019003l.afm\n");
447 fprintf(db, "n019023l.afm\n");
448 fprintf(db, "n019004l.afm\n");
449 fprintf(db, "n019024l.afm\n");
450 fprintf(db, "n022003l.afm\n");
451 fprintf(db, "n022023l.afm\n");
452 fprintf(db, "n022004l.afm\n");
453 fprintf(db, "n022024l.afm\n");
454 fprintf(db, "s050000l.afm\n");
455 fprintf(db, "d050000l.afm\n");
458 fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
459 fprintf(fi, "ENCODING=%s:.\n", t1searchpath);
460 fprintf(fi, "AFM=%s:.\n", t1searchpath);
461 fprintf(fi, "TYPE1=%s:.\n", t1searchpath);
464 /* initialize t1lib */
465 T1_SetBitmapPad( 16);
466 if ((T1_InitLib(NO_LOGFILE)==NULL)){
467 fprintf(stderr, "Initialization of t1lib failed\n");
470 unlink("/tmp/t1lib.config.tmp");
472 pdfswf_init(filename, password);
473 pdfswf_setoutputfilename(outputname);
475 int pages = pdfswf_numpages();
476 for(t = 1; t <= pages; t++)
478 if(is_in_range(t, pagerange))
479 pdfswf_convertpage(t);
481 pdfswf_performconversion();
485 if(viewer || preloader) {
486 #ifndef SYSTEM_BACKTICKS
487 logf("<warning> Not sure whether system() can handle command substitution");
488 logf("<warning> (According to config.h, it can't)");
494 if(viewer && !preloader) {
495 systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
496 outputname, viewer, outputname, outputname);
500 if(preloader && !viewer) {
501 logf("<warning> --preloader option without --viewer option doesn't make very much sense.");
502 ret = systemf("swfcombine `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
503 preloader, SWFTOOLS_DATADIR, preloader, outputname, outputname);
507 if(preloader && viewer) {
508 systemf("swfcombine %s viewport=%s -o __tmp__.swf",
509 viewer, outputname, outputname);
510 systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
511 outputname, preloader, SWFTOOLS_DATADIR, preloader, outputname);
512 systemf("rm __tmp__.swf");
515 unlink("/tmp/FontDataBase");