2 main routine for pdf2swf(1)
4 Part of the swftools package.
6 Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
27 #include "../config.h"
31 #include "../lib/args.h"
32 #include "SWFOutputDev.h"
38 static char * outputname = 0;
39 static int loglevel = 3;
40 static char * pagerange = 0;
41 static char * filename = 0;
42 static char * password = 0;
44 static char * preloader = 0;
45 static char * viewer = 0;
50 static int system_quiet=0;
52 int systemf(const char* format, ...)
57 va_start(arglist, format);
58 vsprintf(buf, format, arglist);
67 fprintf(stderr, "system() returned %d\n", ret);
73 int args_callback_option(char*name,char*val) {
74 if (!strcmp(name, "o"))
79 else if (!strcmp(name, "v"))
84 else if (!strcmp(name, "q"))
90 else if (name[0]=='p')
92 /* check whether the page range follows the p directly, like
96 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
105 else if (!strcmp(name, "P"))
110 else if (!strcmp(name, "s"))
112 char*s = strdup(val);
113 char*c = strchr(s, '=');
114 if(c && *c && c[1]) {
117 pdfswf_setparameter(s,c);
120 pdfswf_setparameter(s,"1");
123 else if (!strcmp(name, "S"))
125 pdfswf_drawonlyshapes();
128 else if (!strcmp(name, "i"))
130 pdfswf_ignoredraworder();
133 else if (!strcmp(name, "z"))
138 else if (!strcmp(name, "n"))
140 pdfswf_linksopennewwindow();
143 else if (!strcmp(name, "t"))
148 else if (!strcmp(name, "T"))
151 if(!strcasecmp(val, "mx"))
153 pdfswf_setversion(i);
156 else if (!strcmp(name, "f"))
158 pdfswf_storeallcharacters();
161 else if (!strcmp(name, "F"))
163 char *s = strdup(val);
165 while(l && s[l-1]=='/') {
169 fontpaths[fontpathpos++] = s;
172 else if (!strcmp(name, "l"))
175 sprintf(buf, "%s/swfs/default_loader.swf", SWFTOOLS_DATADIR);
176 preloader = strdup(buf);
179 else if (!strcmp(name, "b"))
182 sprintf(buf, "%s/swfs/default_viewer.swf", SWFTOOLS_DATADIR);
183 viewer = strdup(buf);
186 else if (!strcmp(name, "L"))
194 systemf("ls %s/swfs/*_loader.swf", SWFTOOLS_DATADIR);
201 else if (!strcmp(name, "B"))
209 systemf("ls %s/swfs/*_viewer.swf", SWFTOOLS_DATADIR);
216 else if (!strcmp(name, "j"))
219 pdfswf_jpegquality(atoi(&name[1]));
222 pdfswf_jpegquality(atoi(val));
226 else if (!strcmp(name, "V"))
228 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
233 fprintf(stderr, "Unknown option: -%s\n", name);
239 /*struct docoptions_t options[] =
240 {{"o","output","filename::Specify output file"},
241 {"V","version","Print program version"},
242 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
243 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
244 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
245 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
246 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
247 {"w","samewindow","Don't open a new browser window for links in the SWF"},
248 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
249 {"F","fontpath","path::Add directory to font search path"},
250 {"B","viewer","name::Link viewer \"name\" to the pdf"},
251 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
252 {"b","defaultviewer","Link default viewer to the pdf"},
253 {"l","defaultpreloader","Link default preloader to the pdf"}
256 struct options_t options[] =
271 {"b","defaultviewer"},
272 {"l","defaultpreloader"},
274 {"T","flashversion"},
278 int args_callback_longoption(char*name,char*val) {
279 return args_long2shortoption(options, name, val);
282 int args_callback_command(char*name, char*val) {
288 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
297 void args_callback_usage(char*name)
299 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
300 printf("\nBasic options:\n");
301 printf("-p --pages=range Convert only pages in range\n");
302 printf("-P --password=password Use password for deciphering the pdf\n");
303 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
304 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
306 printf("-F --fontdir directory Add directory to font search path\n");
308 printf("-V --version Print program version\n");
309 printf("\nEnhanced conversion options:\n");
310 printf("-t --stop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
311 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
312 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
313 printf("-j --jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
314 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
315 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
316 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
317 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
318 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
319 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
320 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller, but may produce\n"
321 " graphic errors)\n");
322 printf("Postprocessing options:\n");
323 #ifndef SYSTEM_BACKTICKS
324 printf("(They might not work because your system call doesn't support command substitution)\n");
326 printf("-b --defaultviewer Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", SWFTOOLS_DATADIR);
327 printf("-l --defaultpreloader Link default preloader the pdf (%s/swfs/default_loader.swf)\n", SWFTOOLS_DATADIR);
328 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
329 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
333 void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath)
337 strcat(searchpath, ":");
338 strcat(searchpath, dirname);
340 msg("<verbose> Adding %s to search path\n", dirname);
342 DIR*dir = opendir(dirname);
344 msg("<warning> Couldn't open directory %s\n", dirname);
353 char*name = ent->d_name;
359 if(!strncasecmp(&name[l-4], ".afm", 4))
361 if(!strncasecmp(&name[l-4], ".ttf", 4))
365 if(database && type==1) {
368 sprintf(buf, "%s/%s", dirname,name);
369 fi = fopen(buf, "rb");
370 if(!fi || !fread(&a,1,1,fi)) {
371 msg("<warning> Couldn't read from %s", buf);
373 fprintf(database, "%s\n", buf);
374 msg("<verbose> Found font %s\n", buf);
385 char* stripfilename(char*filename, char*newext)
387 char*last1 = strrchr(filename, '/');
388 char*last2 = strrchr(filename, '\\');
392 if(last1>pos) pos = last1 + 1;
393 if(last2>pos) pos = last2 + 1;
394 name = (char*)malloc(strlen(pos)+5);
396 dot = strrchr(name, '.');
400 strcat(name, newext);
404 int main(int argn, char *argv[])
410 char t1searchpath[1024];
418 processargs(argn, argv);
419 initLog(0,-1,0,0,-1,loglevel);
423 fprintf(stderr, "Please specify an input file\n");
430 outputname = stripfilename(filename, ".swf");
431 msg("<notice> Output filename not given. Writing to %s", outputname);
437 fprintf(stderr, "Please use -o to specify an output file\n");
441 // test if the page range is o.k.
442 is_in_range(0x7fffffff, pagerange);
445 args_callback_usage(argv[0]);
449 msg("<verbose> reading font files from %s/fonts\n", SWFTOOLS_DATADIR);
450 //TODO: use tempnam here. Check if environment already contains a
452 putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
453 FILE*db = fopen("/tmp/FontDataBase", "wb");
454 FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
456 fprintf(stderr, "Couldn't create temporary file in /tmp/\n");
461 sprintf(buf, "%s/fonts",SWFTOOLS_DATADIR);
463 addfontdir(0, buf, &numfonts, 0);
464 for(t=0;t<fontpathpos;t++) {
465 addfontdir(0, fontpaths[t], &numfonts,0);
467 fprintf(db, "%d\n", numfonts);
469 addfontdir(db, buf, 0, t1searchpath);
470 for(t=0;t<fontpathpos;t++) {
471 addfontdir(db, fontpaths[t], 0, t1searchpath);
474 /* This is a workaround. The correct way would be to
475 get directory listings working on all systems.
477 strcpy(t1searchpath, SWFTOOLS_DATADIR);
478 strcat(t1searchpath, "/fonts");
480 fprintf(db, "n021003l.afm\n");
481 fprintf(db, "n021023l.afm\n");
482 fprintf(db, "n021004l.afm\n");
483 fprintf(db, "n021024l.afm\n");
484 fprintf(db, "n019003l.afm\n");
485 fprintf(db, "n019023l.afm\n");
486 fprintf(db, "n019004l.afm\n");
487 fprintf(db, "n019024l.afm\n");
488 fprintf(db, "n022003l.afm\n");
489 fprintf(db, "n022023l.afm\n");
490 fprintf(db, "n022004l.afm\n");
491 fprintf(db, "n022024l.afm\n");
492 fprintf(db, "s050000l.afm\n");
493 fprintf(db, "d050000l.afm\n");
496 fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
497 fprintf(fi, "ENCODING=%s:.\n", t1searchpath);
498 fprintf(fi, "AFM=%s:.\n", t1searchpath);
499 fprintf(fi, "TYPE1=%s:.\n", t1searchpath);
502 /* initialize t1lib */
503 T1_SetBitmapPad( 16);
504 if ((T1_InitLib(NO_LOGFILE)==NULL)){
505 fprintf(stderr, "Initialization of t1lib failed\n");
508 unlink("/tmp/t1lib.config.tmp");
510 pdfswf_init(filename, password);
511 pdfswf_setoutputfilename(outputname);
513 int pages = pdfswf_numpages();
514 for(t = 1; t <= pages; t++)
516 if(is_in_range(t, pagerange))
517 pdfswf_convertpage(t);
519 pdfswf_performconversion();
523 if(viewer || preloader) {
524 #ifndef SYSTEM_BACKTICKS
525 msg("<warning> Not sure whether system() can handle command substitution");
526 msg("<warning> (According to config.h, it can't)");
532 if(viewer && !preloader) {
533 systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
534 outputname, viewer, outputname, outputname);
538 if(preloader && !viewer) {
539 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
540 ret = systemf("swfcombine `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
541 preloader, SWFTOOLS_DATADIR, preloader, outputname, outputname);
545 if(preloader && viewer) {
546 systemf("swfcombine %s viewport=%s -o __tmp__.swf",
547 viewer, outputname, outputname);
548 systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
549 outputname, preloader, SWFTOOLS_DATADIR, preloader, outputname);
550 systemf("rm __tmp__.swf");
553 unlink("/tmp/FontDataBase");