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"
39 #include "xpdf/gfile.h"
42 #define FONTDIR SWFTOOLS_DATADIR "/fonts"
43 #define SWFDIR SWFTOOLS_DATADIR "/swfs"
45 #define FONTDIR "C:\\swftools\\fonts"
46 #define SWFDIR "C:\\swftools\\swfs"
49 static char * outputname = 0;
50 static int loglevel = 3;
51 static char * pagerange = 0;
52 static char * filename = 0;
53 static char * password = 0;
55 static char * preloader = 0;
56 static char * viewer = 0;
61 static int system_quiet=0;
63 int systemf(const char* format, ...)
68 va_start(arglist, format);
69 vsprintf(buf, format, arglist);
78 fprintf(stderr, "system() returned %d\n", ret);
84 int args_callback_option(char*name,char*val) {
85 if (!strcmp(name, "o"))
90 else if (!strcmp(name, "v"))
95 else if (!strcmp(name, "q"))
101 else if (name[0]=='p')
103 /* check whether the page range follows the p directly, like
107 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
116 else if (!strcmp(name, "P"))
121 else if (!strcmp(name, "s"))
123 char*s = strdup(val);
124 char*c = strchr(s, '=');
125 if(c && *c && c[1]) {
128 pdfswf_setparameter(s,c);
131 pdfswf_setparameter(s,"1");
134 else if (!strcmp(name, "S"))
136 pdfswf_drawonlyshapes();
139 else if (!strcmp(name, "i"))
141 pdfswf_ignoredraworder();
144 else if (!strcmp(name, "z"))
149 else if (!strcmp(name, "n"))
151 pdfswf_linksopennewwindow();
154 else if (!strcmp(name, "t"))
159 else if (!strcmp(name, "T"))
162 if(!strcasecmp(val, "mx"))
164 pdfswf_setversion(i);
167 else if (!strcmp(name, "f"))
169 pdfswf_storeallcharacters();
172 else if (!strcmp(name, "F"))
174 char *s = strdup(val);
176 while(l && s[l-1]=='/') {
180 fontpaths[fontpathpos++] = s;
183 else if (!strcmp(name, "l"))
186 sprintf(buf, "%s/default_loader.swf", SWFDIR);
187 preloader = strdup(buf);
190 else if (!strcmp(name, "b"))
193 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
194 viewer = strdup(buf);
197 else if (!strcmp(name, "L"))
205 systemf("ls %s/*_loader.swf", SWFDIR);
212 else if (!strcmp(name, "B"))
220 systemf("ls %s/*_viewer.swf", SWFDIR);
227 else if (!strcmp(name, "j"))
230 pdfswf_jpegquality(atoi(&name[1]));
233 pdfswf_jpegquality(atoi(val));
237 else if (!strcmp(name, "V"))
239 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
244 fprintf(stderr, "Unknown option: -%s\n", name);
250 /*struct docoptions_t options[] =
251 {{"o","output","filename::Specify output file"},
252 {"V","version","Print program version"},
253 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
254 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
255 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
256 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
257 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
258 {"w","samewindow","Don't open a new browser window for links in the SWF"},
259 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
260 {"F","fontpath","path::Add directory to font search path"},
261 {"B","viewer","name::Link viewer \"name\" to the pdf"},
262 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
263 {"b","defaultviewer","Link default viewer to the pdf"},
264 {"l","defaultpreloader","Link default preloader to the pdf"}
267 struct options_t options[] =
282 {"b","defaultviewer"},
283 {"l","defaultpreloader"},
285 {"T","flashversion"},
289 int args_callback_longoption(char*name,char*val) {
290 return args_long2shortoption(options, name, val);
293 int args_callback_command(char*name, char*val) {
299 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
308 void args_callback_usage(char*name)
310 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
311 printf("\nBasic options:\n");
312 printf("-p --pages=range Convert only pages in range\n");
313 printf("-P --password=password Use password for deciphering the pdf\n");
314 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
315 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
317 printf("-F --fontdir directory Add directory to font search path\n");
319 printf("-V --version Print program version\n");
320 printf("\nEnhanced conversion options:\n");
321 printf("-t --stop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
322 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
323 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
324 printf("-j --jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
325 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
326 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
327 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
328 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
329 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
330 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
331 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller, but may produce\n"
332 " graphic errors)\n");
333 printf("Postprocessing options:\n");
334 #ifndef SYSTEM_BACKTICKS
335 printf("(They might not work because your system call doesn't support command substitution)\n");
337 printf("-b --defaultviewer Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", SWFTOOLS_DATADIR);
338 printf("-l --defaultpreloader Link default preloader the pdf (%s/swfs/default_loader.swf)\n", SWFTOOLS_DATADIR);
339 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
340 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
344 void addfontdir(char* dirname, int*numfonts)
347 msg("<verbose> Adding %s to search path\n", dirname);
349 DIR*dir = opendir(dirname);
351 msg("<warning> Couldn't open directory %s\n", dirname);
360 char*name = ent->d_name;
366 if(!strncasecmp(&name[l-4], ".pfa", 4))
368 if(!strncasecmp(&name[l-4], ".pfb", 4))
370 if(!strncasecmp(&name[l-4], ".ttf", 4))
374 char*fontname = (char*)malloc(strlen(dirname)+strlen(name)+2);
375 strcpy(fontname, dirname);
377 strcat(fontname, "\\");
379 strcat(fontname, "/");
381 strcat(fontname, name);
383 msg("<debug> Adding %s to fonts", fontname);
384 pdfswf_addfont(fontname);
393 char* stripfilename(char*filename, char*newext)
395 char*last1 = strrchr(filename, '/');
396 char*last2 = strrchr(filename, '\\');
400 if(last1>pos) pos = last1 + 1;
401 if(last2>pos) pos = last2 + 1;
402 name = (char*)malloc(strlen(pos)+5);
404 dot = strrchr(name, '.');
408 strcat(name, newext);
412 int main(int argn, char *argv[])
418 char t1searchpath[1024];
420 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
421 FILE*test = fopen(FONTDIR "\\d050000l.afm", "rb");
423 fprintf(stderr, "Couldn't find file " FONTDIR "\\d050000l.afm- pdf2swf not installed properly? OS says:\n");
437 processargs(argn, argv);
438 initLog(0,-1,0,0,-1,loglevel);
442 fprintf(stderr, "Please specify an input file\n");
449 outputname = stripfilename(filename, ".swf");
450 msg("<notice> Output filename not given. Writing to %s", outputname);
456 fprintf(stderr, "Please use -o to specify an output file\n");
460 // test if the page range is o.k.
461 is_in_range(0x7fffffff, pagerange);
464 args_callback_usage(argv[0]);
470 addfontdir(FONTDIR, &numfonts);
471 for(t=0;t<fontpathpos;t++) {
472 addfontdir(fontpaths[t], &numfonts);
475 addfontdir(FONTDIR, 0);
476 for(t=0;t<fontpathpos;t++) {
477 addfontdir(fontpaths[t], 0);
480 msg("<error> Couldn't find any fonts!");
483 pdfswf_init(filename, password);
484 pdfswf_setoutputfilename(outputname);
486 int pages = pdfswf_numpages();
487 for(t = 1; t <= pages; t++)
489 if(is_in_range(t, pagerange))
490 pdfswf_convertpage(t);
492 pdfswf_performconversion();
496 if(viewer || preloader) {
497 #ifndef SYSTEM_BACKTICKS
498 msg("<warning> Not sure whether system() can handle command substitution");
499 msg("<warning> (According to config.h, it can't)");
505 if(viewer && !preloader) {
506 systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
507 outputname, viewer, outputname, outputname);
511 if(preloader && !viewer) {
512 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
513 ret = systemf("swfcombine `swfdump -r %s` %s/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
514 preloader, SWFDIR, preloader, outputname, outputname);
518 if(preloader && viewer) {
519 systemf("swfcombine %s viewport=%s -o __tmp__.swf",
520 viewer, outputname, outputname);
521 systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
522 outputname, preloader, SWFDIR, preloader, outputname);
523 systemf("rm __tmp__.swf");