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 "../lib/os.h"
36 #include "../lib/rfxswf.h"
37 #include "SWFOutputDev.h"
40 #define FONTDIR concatPaths(getInstallationPath(), "fonts")
41 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
43 static char * outputname = 0;
44 static int loglevel = 3;
45 static char * pagerange = 0;
46 static char * filename = 0;
47 static char * password = 0;
50 static char * preloader = 0;
51 static char * viewer = 0;
58 static int system_quiet=0;
60 int systemf(const char* format, ...)
65 va_start(arglist, format);
66 vsprintf(buf, format, arglist);
75 fprintf(stderr, "system() returned %d\n", ret);
81 int args_callback_option(char*name,char*val) {
82 if (!strcmp(name, "o"))
87 else if (!strcmp(name, "v"))
90 setConsoleLogging(loglevel);
93 else if (!strcmp(name, "2"))
99 else if (!strcmp(name, "4"))
105 else if (!strcmp(name, "9"))
111 else if (!strcmp(name, "q"))
114 setConsoleLogging(loglevel);
118 else if (name[0]=='p')
120 /* check whether the page range follows the p directly, like
124 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
133 else if (!strcmp(name, "P"))
138 else if (!strcmp(name, "s"))
140 char*s = strdup(val);
141 char*c = strchr(s, '=');
142 if(c && *c && c[1]) {
145 pdfswf_setparameter(s,c);
148 pdfswf_setparameter(s,"1");
151 else if (!strcmp(name, "S"))
153 pdfswf_setparameter("drawonlyshapes", "1");
156 else if (!strcmp(name, "i"))
158 pdfswf_setparameter("ignoredraworder", "1");
161 else if (!strcmp(name, "z"))
163 pdfswf_setparameter("enablezlib", "1");
167 else if (!strcmp(name, "n"))
169 pdfswf_setparameter("opennewwindow", "1");
172 else if (!strcmp(name, "t"))
174 pdfswf_setparameter("insertstop", "1");
177 else if (!strcmp(name, "T"))
179 if(!strcasecmp(val, "mx"))
180 pdfswf_setparameter("flashversion", "6");
182 pdfswf_setparameter("flashversion", val);
186 else if (!strcmp(name, "f"))
188 pdfswf_setparameter("storeallcharacters", "1");
191 else if (!strcmp(name, "w"))
193 pdfswf_setparameter("linksopennewwindow", "0");
196 else if (!strcmp(name, "F"))
198 char *s = strdup(val);
200 while(l && s[l-1]=='/') {
204 fontpaths[fontpathpos++] = s;
207 else if (!strcmp(name, "l"))
210 sprintf(buf, "%s/default_loader.swf", SWFDIR);
211 preloader = strdup(buf);
214 else if (!strcmp(name, "b"))
217 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
218 viewer = strdup(buf);
221 else if (!strcmp(name, "L"))
229 systemf("ls %s/*_loader.swf", SWFDIR);
236 else if (!strcmp(name, "B"))
244 systemf("ls %s/*_viewer.swf", SWFDIR);
251 else if (!strcmp(name, "j"))
254 pdfswf_setparameter("jpegquality", &name[1]);
257 pdfswf_setparameter("jpegquality", val);
261 else if (!strcmp(name, "V"))
263 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
268 fprintf(stderr, "Unknown option: -%s\n", name);
274 /*struct docoptions_t options[] =
275 {{"o","output","filename::Specify output file"},
276 {"V","version","Print program version"},
277 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
278 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
279 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
280 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
281 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
282 {"w","samewindow","Don't open a new browser window for links in the SWF"},
283 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
284 {"F","fontpath","path::Add directory to font search path"},
285 {"B","viewer","name::Link viewer \"name\" to the pdf"},
286 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
287 {"b","defaultviewer","Link default viewer to the pdf"},
288 {"l","defaultpreloader","Link default preloader to the pdf"}
291 struct options_t options[] =
306 {"b","defaultviewer"},
307 {"l","defaultpreloader"},
309 {"T","flashversion"},
313 int args_callback_longoption(char*name,char*val) {
314 return args_long2shortoption(options, name, val);
317 int args_callback_command(char*name, char*val) {
323 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
332 void args_callback_usage(char*name)
334 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
335 printf("\nBasic options:\n");
336 printf("-p --pages=range Convert only pages in range\n");
337 printf("-P --password=password Use password for deciphering the pdf\n");
338 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
339 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
341 printf("-F --fontdir directory Add directory to font search path\n");
343 printf("-V --version Print program version\n");
344 printf("\nEnhanced conversion options:\n");
345 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
346 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
347 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
348 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
349 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
350 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
351 printf("-s zoom=factor Scale result, default: 72\n");
352 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
353 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
354 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
355 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
356 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
357 " graphic errors)\n");
358 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
359 " other out. (Needed for some Powerpoint PDFs)\n");
360 //deliberately undocumented (for now)
361 //printf("-2 Put 2 pages into each frame.\n");
362 //printf("-4 Put 4 pages into each frame.\n");
363 printf("Postprocessing options:\n");
364 #ifndef SYSTEM_BACKTICKS
365 printf("(They might not work because your system call doesn't support command substitution)\n");
367 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
368 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
369 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
370 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
373 float getRate(char*filename)
377 fi = open(filename,O_RDONLY|O_BINARY);
380 sprintf(buffer, "Couldn't open %s", filename);
384 if(swf_ReadSWF(fi,&swf) < 0)
386 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
391 return swf.frameRate / 256.0;
394 int main(int argn, char *argv[])
400 char t1searchpath[1024];
404 initLog(0,-1,0,0,-1,loglevel);
406 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
407 FILE*test = fopen(concatPaths(FONTDIR,"\\d050000l.afm"), "rb");
409 fprintf(stderr, "Couldn't find file %s - pdf2swf not installed properly? OS says:\n", concatPaths(FONTDIR, "\\d050000l.afm"));
423 processargs(argn, argv);
427 fprintf(stderr, "Please specify an input file\n");
434 outputname = stripFilename(filename, ".swf");
435 msg("<notice> Output filename not given. Writing to %s", outputname);
441 fprintf(stderr, "Please use -o to specify an output file\n");
445 // test if the page range is o.k.
446 is_in_range(0x7fffffff, pagerange);
449 args_callback_usage(argv[0]);
454 pdfswf_addfontdir(FONTDIR);
455 for(t=0;t<fontpathpos;t++) {
456 pdfswf_addfontdir(fontpaths[t]);
459 pdf_doc_t* pdf = pdf_init(filename, password);
461 msg("<error> Couldn't open %s", filename);
465 swf_output_t* swf = swf_output_init();
471 pdf_page_info_t*info;
477 for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
479 if(is_in_range(pagenr, pagerange)) {
480 swf_output_preparepage(swf, pagenr, frame);
483 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
491 for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
493 if(is_in_range(pagenr, pagerange)) {
494 pdf_page_t* page = pages[pagenum].page = pdf_getpage(pdf, pagenr);
495 pdf_page_info_t* info = pdf_page_getinfo(page);
496 pages[pagenum].x = 0;
497 pages[pagenum].y = 0;
498 pages[pagenum].info = info;
499 pages[pagenum].page = page;
502 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
505 int xmax[xnup], ymax[xnup];
507 int width=0, height=0;
509 memset(xmax, 0, xnup*sizeof(int));
510 memset(ymax, 0, ynup*sizeof(int));
513 for(x=0;x<xnup;x++) {
516 if(pages[t].info->xMax-pages[t].info->xMin > xmax[x])
517 xmax[x] = pages[t].info->xMax-pages[t].info->xMin;
518 if(pages[t].info->yMax-pages[t].info->yMin > ymax[y])
519 ymax[y] = pages[t].info->yMax-pages[t].info->yMin;
521 for(x=0;x<xnup;x++) {
525 for(y=0;y<ynup;y++) {
529 swf_output_startframe(swf, width, height);
530 for(t=0;t<pagenum;t++) {
533 int xpos = x>0?xmax[x-1]:0;
534 int ypos = y>0?ymax[y-1]:0;
535 pdf_page_info_t*info = pages[t].info;
538 msg("<verbose> Render (%d,%d)-(%d,%d) move:%d/%d\n",
542 info->yMax + ypos, xpos, ypos);
543 pdf_page_rendersection(pages[t].page, swf, xpos,
550 swf_output_endframe(swf);
551 for(t=0;t<pagenum;t++) {
552 pdf_page_info_destroy(pages[t].info);
553 pdf_page_destroy(pages[t].page);
558 if(swf_output_save(swf, outputname) < 0) {
561 int width = (int)swf_output_get(swf, "width");
562 int height = (int)swf_output_get(swf, "height");
563 msg("<notice> SWF written");
564 swf_output_destroy(swf);
571 #undef SYSTEM_BACKTICKS
572 if(viewer && !preloader) {
573 #ifdef SYSTEM_BACKTICKS
574 systemf("swfcombine %s `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",zip,
575 viewer, outputname, outputname);
577 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
578 viewer, outputname, outputname);
583 if(preloader && !viewer) {
584 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
585 #ifdef SYSTEM_BACKTICKS
586 ret = systemf("swfcombine %s `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,
587 preloader, SWFDIR, preloader, outputname, outputname);
589 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
590 SWFDIR, preloader, outputname, outputname);
595 if(preloader && viewer) {
596 systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
597 viewer, outputname, outputname);
598 #ifdef SYSTEM_BACKTICKS
599 systemf("swfcombine %s `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,
600 outputname, preloader, SWFDIR, preloader, outputname);
602 /* TODO: read out rate */
603 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,width,height,
604 getRate(preloader), preloader, SWFDIR, preloader, outputname);
606 systemf("rm __tmp__.swf");