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 char*s = strdup(val);
101 char*c = strchr(s, '=');
102 if(c && *c && c[1]) {
105 pdfswf_setparameter(s,c);
108 pdfswf_setparameter(s,"1");
111 else if (!strcmp(name, "S"))
113 pdfswf_drawonlyshapes();
116 else if (!strcmp(name, "i"))
118 pdfswf_ignoredraworder();
121 else if (!strcmp(name, "z"))
126 else if (!strcmp(name, "n"))
128 pdfswf_linksopennewwindow();
131 else if (!strcmp(name, "t"))
136 else if (!strcmp(name, "T"))
139 if(!strcasecmp(val, "mx"))
141 pdfswf_setversion(i);
144 else if (!strcmp(name, "f"))
146 pdfswf_storeallcharacters();
149 else if (!strcmp(name, "F"))
151 char *s = strdup(val);
153 while(l && s[l-1]=='/') {
157 fontpaths[fontpathpos++] = s;
160 else if (!strcmp(name, "l"))
163 sprintf(buf, "%s/swfs/default_loader.swf", SWFTOOLS_DATADIR);
164 preloader = strdup(buf);
167 else if (!strcmp(name, "b"))
170 sprintf(buf, "%s/swfs/default_viewer.swf", SWFTOOLS_DATADIR);
171 viewer = strdup(buf);
174 else if (!strcmp(name, "L"))
182 systemf("ls %s/swfs/*_loader.swf", SWFTOOLS_DATADIR);
189 else if (!strcmp(name, "B"))
197 systemf("ls %s/swfs/*_viewer.swf", SWFTOOLS_DATADIR);
204 else if (!strcmp(name, "j"))
207 pdfswf_jpegquality(atoi(&name[1]));
210 pdfswf_jpegquality(atoi(val));
214 else if (!strcmp(name, "V"))
216 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
221 fprintf(stderr, "Unknown option: -%s\n", name);
227 /*struct docoptions_t options[] =
228 {{"o","output","filename::Specify output file"},
229 {"V","version","Print program version"},
230 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
231 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
232 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
233 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
234 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
235 {"w","samewindow","Don't open a new browser window for links in the SWF"},
236 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
237 {"F","fontpath","path::Add directory to font search path"},
238 {"B","viewer","name::Link viewer \"name\" to the pdf"},
239 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
240 {"b","defaultviewer","Link default viewer to the pdf"},
241 {"l","defaultpreloader","Link default preloader to the pdf"}
244 struct options_t options[] =
259 {"b","defaultviewer"},
260 {"l","defaultpreloader"},
262 {"T","flashversion"},
266 int args_callback_longoption(char*name,char*val) {
267 return args_long2shortoption(options, name, val);
270 int args_callback_command(char*name, char*val) {
276 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
285 void args_callback_usage(char*name)
287 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
288 printf("\nBasic options:\n");
289 printf("-p --pages=range Convert only pages in range\n");
290 printf("-P --password=password Use password for deciphering the pdf\n");
291 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
292 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
294 printf("-F --fontdir directory Add directory to font search path\n");
296 printf("-V --version Print program version\n");
297 printf("\nEnhanced conversion options:\n");
298 printf("-t --stop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
299 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
300 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
301 printf("-j --jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
302 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
303 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
304 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
305 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
306 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
307 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
308 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller, but may produce\n"
309 " graphic errors)\n");
310 printf("Postprocessing options:\n");
311 #ifndef SYSTEM_BACKTICKS
312 printf("(They might not work because your system call doesn't support command substitution)\n");
314 printf("-b --defaultviewer Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", SWFTOOLS_DATADIR);
315 printf("-l --defaultpreloader Link default preloader the pdf (%s/swfs/default_loader.swf)\n", SWFTOOLS_DATADIR);
316 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
317 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
321 void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath)
325 strcat(searchpath, ":");
326 strcat(searchpath, dirname);
328 msg("<verbose> Adding %s to search path\n", dirname);
330 DIR*dir = opendir(dirname);
332 msg("<warning> Couldn't open directory %s\n", dirname);
341 char*name = ent->d_name;
347 if(!strncasecmp(&name[l-4], ".afm", 4))
349 if(!strncasecmp(&name[l-4], ".ttf", 4))
353 if(database && type==1) {
356 sprintf(buf, "%s/%s", dirname,name);
357 fi = fopen(buf, "rb");
358 if(!fi || !fread(&a,1,1,fi)) {
359 msg("<warning> Couldn't read from %s", buf);
361 fprintf(database, "%s\n", buf);
362 msg("<verbose> Found font %s\n", buf);
373 char* stripfilename(char*filename, char*newext)
375 char*last1 = strrchr(filename, '/');
376 char*last2 = strrchr(filename, '\\');
380 if(last1>pos) pos = last1 + 1;
381 if(last2>pos) pos = last2 + 1;
382 name = (char*)malloc(strlen(pos)+5);
384 dot = strrchr(name, '.');
388 strcat(name, newext);
392 int main(int argn, char *argv[])
398 char t1searchpath[1024];
406 processargs(argn, argv);
407 initLog(0,-1,0,0,-1,loglevel);
411 fprintf(stderr, "Please specify an input file\n");
418 outputname = stripfilename(filename, ".swf");
419 msg("<notice> Output filename not given. Writing to %s", outputname);
425 fprintf(stderr, "Please use -o to specify an output file\n");
429 // test if the page range is o.k.
430 is_in_range(0x7fffffff, pagerange);
433 args_callback_usage(argv[0]);
437 msg("<verbose> reading font files from %s/fonts\n", SWFTOOLS_DATADIR);
438 //TODO: use tempnam here. Check if environment already contains a
440 putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
441 FILE*db = fopen("/tmp/FontDataBase", "wb");
442 FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
444 fprintf(stderr, "Couldn't create temporary file in /tmp/\n");
449 sprintf(buf, "%s/fonts",SWFTOOLS_DATADIR);
451 addfontdir(0, buf, &numfonts, 0);
452 for(t=0;t<fontpathpos;t++) {
453 addfontdir(0, fontpaths[t], &numfonts,0);
455 fprintf(db, "%d\n", numfonts);
457 addfontdir(db, buf, 0, t1searchpath);
458 for(t=0;t<fontpathpos;t++) {
459 addfontdir(db, fontpaths[t], 0, t1searchpath);
462 /* This is a workaround. The correct way would be to
463 get directory listings working on all systems.
465 strcpy(t1searchpath, SWFTOOLS_DATADIR);
466 strcat(t1searchpath, "/fonts");
468 fprintf(db, "n021003l.afm\n");
469 fprintf(db, "n021023l.afm\n");
470 fprintf(db, "n021004l.afm\n");
471 fprintf(db, "n021024l.afm\n");
472 fprintf(db, "n019003l.afm\n");
473 fprintf(db, "n019023l.afm\n");
474 fprintf(db, "n019004l.afm\n");
475 fprintf(db, "n019024l.afm\n");
476 fprintf(db, "n022003l.afm\n");
477 fprintf(db, "n022023l.afm\n");
478 fprintf(db, "n022004l.afm\n");
479 fprintf(db, "n022024l.afm\n");
480 fprintf(db, "s050000l.afm\n");
481 fprintf(db, "d050000l.afm\n");
484 fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
485 fprintf(fi, "ENCODING=%s:.\n", t1searchpath);
486 fprintf(fi, "AFM=%s:.\n", t1searchpath);
487 fprintf(fi, "TYPE1=%s:.\n", t1searchpath);
490 /* initialize t1lib */
491 T1_SetBitmapPad( 16);
492 if ((T1_InitLib(NO_LOGFILE)==NULL)){
493 fprintf(stderr, "Initialization of t1lib failed\n");
496 unlink("/tmp/t1lib.config.tmp");
498 pdfswf_init(filename, password);
499 pdfswf_setoutputfilename(outputname);
501 int pages = pdfswf_numpages();
502 for(t = 1; t <= pages; t++)
504 if(is_in_range(t, pagerange))
505 pdfswf_convertpage(t);
507 pdfswf_performconversion();
511 if(viewer || preloader) {
512 #ifndef SYSTEM_BACKTICKS
513 msg("<warning> Not sure whether system() can handle command substitution");
514 msg("<warning> (According to config.h, it can't)");
520 if(viewer && !preloader) {
521 systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
522 outputname, viewer, outputname, outputname);
526 if(preloader && !viewer) {
527 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
528 ret = systemf("swfcombine `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
529 preloader, SWFTOOLS_DATADIR, preloader, outputname, outputname);
533 if(preloader && viewer) {
534 systemf("swfcombine %s viewport=%s -o __tmp__.swf",
535 viewer, outputname, outputname);
536 systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
537 outputname, preloader, SWFTOOLS_DATADIR, preloader, outputname);
538 systemf("rm __tmp__.swf");
541 unlink("/tmp/FontDataBase");