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"
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 int systemf(const char* format, ...)
43 va_start(arglist, format);
44 vsprintf(buf, format, arglist);
51 fprintf(stderr, "system() returned %d\n", ret);
57 int args_callback_option(char*name,char*val) {
58 if (!strcmp(name, "o"))
63 else if (!strcmp(name, "v"))
68 else if (name[0]=='p')
70 /* check whether the page range follows the p directly, like
74 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
83 else if (!strcmp(name, "P"))
88 else if (!strcmp(name, "s"))
90 pdfswf_drawonlyshapes();
93 else if (!strcmp(name, "i"))
95 pdfswf_ignoredraworder();
98 else if (!strcmp(name, "n"))
100 pdfswf_linksopennewwindow();
103 else if (!strcmp(name, "f"))
105 pdfswf_storeallcharacters();
108 else if (!strcmp(name, "F"))
110 char *s = strdup(val);
112 while(l && s[l-1]=='/') {
116 fontpaths[fontpathpos++] = s;
119 else if (!strcmp(name, "l"))
122 sprintf(buf, "%s/swfs/default_loader.swf", DATADIR);
123 preloader = strdup(buf);
126 else if (!strcmp(name, "b"))
129 sprintf(buf, "%s/swfs/default_viewer.swf", DATADIR);
130 viewer = strdup(buf);
133 else if (!strcmp(name, "L"))
141 systemf("ls %s/swfs/*_loader.swf", DATADIR);
147 else if (!strcmp(name, "B"))
155 systemf("ls %s/swfs/*_viewer.swf", DATADIR);
161 else if (name[0]=='j')
164 pdfswf_jpegquality(atoi(&name[1]));
167 pdfswf_jpegquality(atoi(val));
171 else if (!strcmp(name, "V"))
173 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
178 fprintf(stderr, "Unknown option: -%s\n", name);
184 struct options_t options[] =
196 {"b","defaultviewer"},
197 {"l","defaultpreloader"},
201 int args_callback_longoption(char*name,char*val) {
202 return args_long2shortoption(options, name, val);
205 int args_callback_command(char*name, char*val) {
211 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
220 void args_callback_usage(char*name)
222 printf("Usage: %s [-si] [-j quality] [-p range] [-P password] input.pdf [output.swf]\n", name);
224 printf("-p --pages=range Convert only pages in range\n");
225 printf("-P --password=password Use password for deciphering the pdf\n");
226 printf("-s --shapes Don't use SWF Fonts, but store everything as shape\n");
227 printf("-i --ignore Ignore draw order (makes the SWF file smaller)\n");
228 printf("-j --jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
229 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
230 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
232 printf("-F --fontdir directory Add directory to font search path\n");
234 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
235 printf("-V --version Print program version\n");
236 #ifndef SYSTEM_BACKTICKS
237 printf("The following might not work because your system call doesn't support command substitution:\n");
239 printf("-b --defaultviewer Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", DATADIR);
240 printf("-l --defaultpreloader Link preloader \"name\" to the pdf (%s/swfs/default_loader.swf)\n", DATADIR);
241 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
242 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
246 void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath)
250 strcat(searchpath, ":");
251 strcat(searchpath, dirname);
253 logf("<verbose> Adding %s to search path\n", dirname);
255 DIR*dir = opendir(dirname);
257 logf("<warning> Couldn't open directory %s\n", dirname);
266 char*name = ent->d_name;
272 if(!strncasecmp(&name[l-4], ".afm", 4))
274 if(!strncasecmp(&name[l-4], ".ttf", 4))
278 if(database && type==1) {
281 sprintf(buf, "%s/%s", dirname,name);
282 fi = fopen(buf, "rb");
283 if(!fi || !fread(&a,1,1,fi)) {
284 logf("<warning> Couldn't read from %s", buf);
286 fprintf(database, "%s\n", buf);
287 logf("<verbose> Found font %s\n", buf);
297 int main(int argn, char *argv[])
303 char t1searchpath[1024];
311 processargs(argn, argv);
312 initLog(0,-1,0,0,-1,loglevel);
315 fprintf(stderr, "Please use -o to specify an output file\n");
319 // test if the page range is o.k.
320 is_in_range(0x7fffffff, pagerange);
323 args_callback_usage(argv[0]);
327 logf("<verbose> reading font files from %s/fonts\n", DATADIR);
328 //TODO: use tempnam here. Check if environment already contains a
330 putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
331 FILE*db = fopen("/tmp/FontDataBase", "wb");
332 FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
334 fprintf(stderr, "Couldn't create temporary file in /tmp/\n");
339 sprintf(buf, "%s/fonts",DATADIR);
341 addfontdir(0, buf, &numfonts, 0);
342 for(t=0;t<fontpathpos;t++) {
343 addfontdir(0, fontpaths[t], &numfonts,0);
345 fprintf(db, "%d\n", numfonts);
347 addfontdir(db, buf, 0, t1searchpath);
348 for(t=0;t<fontpathpos;t++) {
349 addfontdir(db, fontpaths[t], 0, t1searchpath);
352 /* This is a workaround. The correct way would be to
353 get directory listings working on all systems.
355 strcpy(t1searchpath, DATADIR);
356 strcat(t1searchpath, "/fonts");
358 fprintf(db, "n021003l.afm\n");
359 fprintf(db, "n021023l.afm\n");
360 fprintf(db, "n021004l.afm\n");
361 fprintf(db, "n021024l.afm\n");
362 fprintf(db, "n019003l.afm\n");
363 fprintf(db, "n019023l.afm\n");
364 fprintf(db, "n019004l.afm\n");
365 fprintf(db, "n019024l.afm\n");
366 fprintf(db, "n022003l.afm\n");
367 fprintf(db, "n022023l.afm\n");
368 fprintf(db, "n022004l.afm\n");
369 fprintf(db, "n022024l.afm\n");
370 fprintf(db, "s050000l.afm\n");
371 fprintf(db, "d050000l.afm\n");
374 fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
375 fprintf(fi, "ENCODING=%s:.\n", t1searchpath);
376 fprintf(fi, "AFM=%s:.\n", t1searchpath);
377 fprintf(fi, "TYPE1=%s:.\n", t1searchpath);
380 /* initialize t1lib */
381 T1_SetBitmapPad( 16);
382 if ((T1_InitLib(NO_LOGFILE)==NULL)){
383 fprintf(stderr, "Initialization of t1lib failed\n");
386 unlink("/tmp/t1lib.config.tmp");
388 pdfswf_init(filename, password);
389 pdfswf_setoutputfilename(outputname);
391 int pages = pdfswf_numpages();
392 for(t = 1; t <= pages; t++)
394 if(is_in_range(t, pagerange))
395 pdfswf_convertpage(t);
397 pdfswf_performconversion();
401 if(viewer || preloader) {
402 #ifndef SYSTEM_BACKTICKS
403 logf("<warning> Not sure whether system() can handle command substitution");
404 logf("<warning> (According to config.h, it can't)");
409 if(viewer && !preloader) {
410 systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
411 outputname, viewer, outputname, outputname);
414 if(preloader && !viewer) {
415 logf("<warning> --preloader option without --viewer option doesn't make very much sense.");
416 ret = systemf("swfcombine `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
417 preloader, DATADIR, preloader, outputname, outputname);
420 if(preloader && viewer) {
421 systemf("swfcombine %s viewport=%s -o __tmp__.swf",
422 viewer, outputname, outputname);
423 systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
424 outputname, preloader, DATADIR, preloader, outputname);
425 systemf("rm __tmp__.swf");
428 unlink("/tmp/FontDataBase");