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 #ifdef HAVE_SYS_STAT_H
34 #include "../lib/args.h"
35 #include "SWFOutputDev.h"
40 #include "xpdf/gfile.h"
43 #define FONTDIR SWFTOOLS_DATADIR "/fonts"
44 #define SWFDIR SWFTOOLS_DATADIR "/swfs"
46 #define FONTDIR "C:\\swftools\\fonts"
47 #define SWFDIR "C:\\swftools\\swfs"
50 static char * outputname = 0;
51 static int loglevel = 3;
52 static char * pagerange = 0;
53 static char * filename = 0;
54 static char * password = 0;
56 static char * preloader = 0;
57 static char * viewer = 0;
62 static int system_quiet=0;
64 int systemf(const char* format, ...)
69 va_start(arglist, format);
70 vsprintf(buf, format, arglist);
79 fprintf(stderr, "system() returned %d\n", ret);
85 int args_callback_option(char*name,char*val) {
86 if (!strcmp(name, "o"))
91 else if (!strcmp(name, "v"))
96 else if (!strcmp(name, "q"))
102 else if (name[0]=='p')
104 /* check whether the page range follows the p directly, like
108 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
117 else if (!strcmp(name, "P"))
122 else if (!strcmp(name, "s"))
124 char*s = strdup(val);
125 char*c = strchr(s, '=');
126 if(c && *c && c[1]) {
129 pdfswf_setparameter(s,c);
132 pdfswf_setparameter(s,"1");
135 else if (!strcmp(name, "S"))
137 pdfswf_drawonlyshapes();
140 else if (!strcmp(name, "i"))
142 pdfswf_ignoredraworder();
145 else if (!strcmp(name, "z"))
150 else if (!strcmp(name, "n"))
152 pdfswf_linksopennewwindow();
155 else if (!strcmp(name, "t"))
160 else if (!strcmp(name, "T"))
163 if(!strcasecmp(val, "mx"))
165 pdfswf_setversion(i);
168 else if (!strcmp(name, "f"))
170 pdfswf_storeallcharacters();
173 else if (!strcmp(name, "F"))
175 char *s = strdup(val);
177 while(l && s[l-1]=='/') {
181 fontpaths[fontpathpos++] = s;
184 else if (!strcmp(name, "l"))
187 sprintf(buf, "%s/default_loader.swf", SWFDIR);
188 preloader = strdup(buf);
191 else if (!strcmp(name, "b"))
194 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
195 viewer = strdup(buf);
198 else if (!strcmp(name, "L"))
206 systemf("ls %s/*_loader.swf", SWFDIR);
213 else if (!strcmp(name, "B"))
221 systemf("ls %s/*_viewer.swf", SWFDIR);
228 else if (!strcmp(name, "j"))
231 pdfswf_jpegquality(atoi(&name[1]));
234 pdfswf_jpegquality(atoi(val));
238 else if (!strcmp(name, "V"))
240 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
245 fprintf(stderr, "Unknown option: -%s\n", name);
251 /*struct docoptions_t options[] =
252 {{"o","output","filename::Specify output file"},
253 {"V","version","Print program version"},
254 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
255 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
256 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
257 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
258 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
259 {"w","samewindow","Don't open a new browser window for links in the SWF"},
260 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
261 {"F","fontpath","path::Add directory to font search path"},
262 {"B","viewer","name::Link viewer \"name\" to the pdf"},
263 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
264 {"b","defaultviewer","Link default viewer to the pdf"},
265 {"l","defaultpreloader","Link default preloader to the pdf"}
268 struct options_t options[] =
283 {"b","defaultviewer"},
284 {"l","defaultpreloader"},
286 {"T","flashversion"},
290 int args_callback_longoption(char*name,char*val) {
291 return args_long2shortoption(options, name, val);
294 int args_callback_command(char*name, char*val) {
300 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
309 void args_callback_usage(char*name)
311 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
312 printf("\nBasic options:\n");
313 printf("-p --pages=range Convert only pages in range\n");
314 printf("-P --password=password Use password for deciphering the pdf\n");
315 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
316 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
318 printf("-F --fontdir directory Add directory to font search path\n");
320 printf("-V --version Print program version\n");
321 printf("\nEnhanced conversion options:\n");
322 printf("-t --stop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
323 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
324 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
325 printf("-j --jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
326 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
327 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
328 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
329 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
330 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
331 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
332 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller, but may produce\n"
333 " graphic errors)\n");
334 printf("Postprocessing options:\n");
335 #ifndef SYSTEM_BACKTICKS
336 printf("(They might not work because your system call doesn't support command substitution)\n");
338 printf("-b --defaultviewer Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", SWFTOOLS_DATADIR);
339 printf("-l --defaultpreloader Link default preloader the pdf (%s/swfs/default_loader.swf)\n", SWFTOOLS_DATADIR);
340 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
341 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
345 void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath)
349 strcat(searchpath, ":");
350 strcat(searchpath, dirname);
353 msg("<verbose> Adding %s to search path\n", dirname);
355 DIR*dir = opendir(dirname);
357 msg("<warning> Couldn't open directory %s\n", dirname);
366 char*name = ent->d_name;
372 if(!strncasecmp(&name[l-4], ".afm", 4))
374 if(!strncasecmp(&name[l-4], ".ttf", 4))
378 if(database && type==1) {
382 sprintf(buf, "%s\\%s", dirname,name);
384 sprintf(buf, "%s/%s", dirname,name);
386 fi = fopen(buf, "rb");
387 if(!fi || !fread(&a,1,1,fi)) {
388 msg("<warning> Couldn't read from %s", buf);
392 /* might also work for all other systems, but we *need* it for win32 */
393 if(!strncmp(buf, FONTDIR, strlen(FONTDIR)))
394 fprintf(database, "%s\n", buf+strlen(FONTDIR));
397 fprintf(database, "%s\n", buf);
399 msg("<verbose> Found font %s\n", buf);
410 char* stripfilename(char*filename, char*newext)
412 char*last1 = strrchr(filename, '/');
413 char*last2 = strrchr(filename, '\\');
417 if(last1>pos) pos = last1 + 1;
418 if(last2>pos) pos = last2 + 1;
419 name = (char*)malloc(strlen(pos)+5);
421 dot = strrchr(name, '.');
425 strcat(name, newext);
429 int main(int argn, char *argv[])
435 char t1searchpath[1024];
437 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
438 FILE*test = fopen(FONTDIR "\\d050000l.afm", "rb");
440 fprintf(stderr, "Couldn't find file " FONTDIR "\\d050000l.afm- pdf2swf not installed properly? OS says:\n");
454 processargs(argn, argv);
455 initLog(0,-1,0,0,-1,loglevel);
459 fprintf(stderr, "Please specify an input file\n");
466 outputname = stripfilename(filename, ".swf");
467 msg("<notice> Output filename not given. Writing to %s", outputname);
473 fprintf(stderr, "Please use -o to specify an output file\n");
477 // test if the page range is o.k.
478 is_in_range(0x7fffffff, pagerange);
481 args_callback_usage(argv[0]);
485 msg("<verbose> reading font files from %s\n", FONTDIR);
486 //TODO: use tempnam here. Check if environment already contains a
488 char buf1[256],buf2[256],buf3[256];
489 char* t1lib_config = mktmpname(buf1);
490 char* fontdatabase = mktmpname(buf2);
492 sprintf(buf3, "T1LIB_CONFIG=%s", t1lib_config);
495 FILE*db = fopen(fontdatabase, "wb");
496 FILE*fi = fopen(t1lib_config, "wb");
498 if(!db) fprintf(stderr, "Couldn't create temporary file %s\n",fontdatabase);
499 if(!fi) fprintf(stderr, "Couldn't create temporary file %s\n",t1lib_config);
505 addfontdir(0, FONTDIR, &numfonts, 0);
506 for(t=0;t<fontpathpos;t++) {
507 addfontdir(0, fontpaths[t], &numfonts,0);
509 fprintf(db, "%d\n", numfonts);
511 addfontdir(db, FONTDIR, 0, t1searchpath);
512 for(t=0;t<fontpathpos;t++) {
513 addfontdir(db, fontpaths[t], 0, t1searchpath);
517 #error Win32 version requires dirent.h
519 /* This is a workaround. The correct way would be to
520 get directory listings working on all systems.
522 strcpy(t1searchpath, SWFTOOLS_DATADIR);
523 strcat(t1searchpath, "/fonts");
525 fprintf(db, "n021003l.afm\n");
526 fprintf(db, "n021023l.afm\n");
527 fprintf(db, "n021004l.afm\n");
528 fprintf(db, "n021024l.afm\n");
529 fprintf(db, "n019003l.afm\n");
530 fprintf(db, "n019023l.afm\n");
531 fprintf(db, "n019004l.afm\n");
532 fprintf(db, "n019024l.afm\n");
533 fprintf(db, "n022003l.afm\n");
534 fprintf(db, "n022023l.afm\n");
535 fprintf(db, "n022004l.afm\n");
536 fprintf(db, "n022024l.afm\n");
537 fprintf(db, "s050000l.afm\n");
538 fprintf(db, "d050000l.afm\n");
541 fprintf(fi, "FONTDATABASE=%s\n",fontdatabase);
543 fprintf(fi, "ENCODING=%s:.\n", t1searchpath);
544 fprintf(fi, "AFM=%s:.\n", t1searchpath);
545 fprintf(fi, "TYPE1=%s:.\n", t1searchpath);
547 fprintf(fi, "ENCODING=%s\n", FONTDIR);
548 fprintf(fi, "AFM=%s\n", FONTDIR);
549 fprintf(fi, "TYPE1=%s\n", FONTDIR);
553 /* initialize t1lib */
554 T1_SetBitmapPad( 16);
555 if ((T1_InitLib(LOGFILE)==NULL)){
556 fprintf(stderr, "Initialization of t1lib failed\n");
559 unlink(t1lib_config);
561 pdfswf_init(filename, password);
562 pdfswf_setoutputfilename(outputname);
564 int pages = pdfswf_numpages();
565 for(t = 1; t <= pages; t++)
567 if(is_in_range(t, pagerange))
568 pdfswf_convertpage(t);
570 pdfswf_performconversion();
574 if(viewer || preloader) {
575 #ifndef SYSTEM_BACKTICKS
576 msg("<warning> Not sure whether system() can handle command substitution");
577 msg("<warning> (According to config.h, it can't)");
583 if(viewer && !preloader) {
584 systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
585 outputname, viewer, outputname, outputname);
589 if(preloader && !viewer) {
590 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
591 ret = systemf("swfcombine `swfdump -r %s` %s/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
592 preloader, SWFDIR, preloader, outputname, outputname);
596 if(preloader && viewer) {
597 systemf("swfcombine %s viewport=%s -o __tmp__.swf",
598 viewer, outputname, outputname);
599 systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
600 outputname, preloader, SWFDIR, preloader, outputname);
601 systemf("rm __tmp__.swf");
604 unlink(fontdatabase);