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"
38 #include "../lib/args.h"
39 #include "../lib/os.h"
40 #include "../lib/rfxswf.h"
41 #include "../lib/devices/swf.h"
42 #include "../lib/devices/polyops.h"
43 #include "../lib/devices/record.h"
44 #include "../lib/pdf/pdf.h"
45 #include "../lib/log.h"
47 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
49 static gfxsource_t*driver = 0;
50 static gfxdevice_t*out = 0;
52 static char * outputname = 0;
53 static int loglevel = 3;
54 static char * pagerange = 0;
55 static char * filename = 0;
56 static char * password = 0;
59 static char * preloader = 0;
60 static char * viewer = 0;
64 static int info_only = 0;
66 static int max_time = 0;
68 static int flatten = 0;
76 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
79 static int system_quiet=0;
81 int systemf(const char* format, ...)
86 va_start(arglist, format);
87 vsprintf(buf, format, arglist);
96 fprintf(stderr, "system() returned %d\n", ret);
103 void sigalarm(int signal)
105 msg("<fatal> Aborting rendering after %d seconds", max_time);
106 #if 0 && defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRUSAGE)
108 getrusage(RUSAGE_CHILDREN, &usage);
109 msg("<fatal> Memory used: %d,%d,%d", usage.ru_maxrss, usage.ru_idrss, usage.ru_isrss);
111 #if defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_H)
112 struct mallinfo info = mallinfo();
113 msg("<fatal> Memory used: %d Mb (%d bytes)", info.uordblks/1048576, info.uordblks);
119 int args_callback_option(char*name,char*val) {
120 if (!strcmp(name, "o"))
125 else if (!strcmp(name, "v"))
128 setConsoleLogging(loglevel);
131 else if (!strcmp(name, "2"))
137 else if (!strcmp(name, "4"))
143 else if (!strcmp(name, "9"))
149 else if (!strcmp(name, "q"))
152 setConsoleLogging(loglevel);
156 else if (name[0]=='p')
158 /* check whether the page range follows the p directly, like
162 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
171 else if (!strcmp(name, "P"))
176 else if (!strcmp(name, "c"))
178 char*s = strdup(val);
179 char*x1 = strtok(s, ":");
180 char*y1 = strtok(0, ":");
181 char*x2 = strtok(0, ":");
182 char*y2 = strtok(0, ":");
183 if(!(x1 && y1 && x2 && y2)) {
184 fprintf(stderr, "-c option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
195 else if (!strcmp(name, "m"))
197 char*s = strdup(val);
198 char*c = strchr(s, ':');
200 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
210 else if (!strcmp(name, "s"))
212 char*s = strdup(val);
213 char*c = strchr(s, '=');
214 if(c && *c && c[1]) {
217 driver->set_parameter(driver, s,c);
218 out->setparameter(out, s,c);
220 driver->set_parameter(driver, s,"1");
221 out->setparameter(out, s,"1");
225 else if (!strcmp(name, "S"))
227 driver->set_parameter(driver, "drawonlyshapes", "1");
230 else if (!strcmp(name, "i"))
232 driver->set_parameter(driver, "ignoredraworder", "1");
236 else if (!strcmp(name, "Q"))
238 max_time = atoi(val);
240 # ifdef HAVE_SIGNAL_H
241 signal(SIGALRM, sigalarm);
246 else if (!strcmp(name, "z"))
248 driver->set_parameter(driver, "enablezlib", "1");
252 else if (!strcmp(name, "n"))
254 driver->set_parameter(driver, "opennewwindow", "1");
257 else if (!strcmp(name, "I"))
262 else if (!strcmp(name, "t"))
264 driver->set_parameter(driver, "insertstop", "1");
267 else if (!strcmp(name, "T"))
269 if(!strcasecmp(val, "mx"))
270 driver->set_parameter(driver, "flashversion", "6");
272 driver->set_parameter(driver, "flashversion", val);
276 else if (!strcmp(name, "f"))
278 driver->set_parameter(driver, "storeallcharacters", "1");
279 driver->set_parameter(driver, "extrafontdata", "1");
282 else if (!strcmp(name, "w"))
284 driver->set_parameter(driver, "linksopennewwindow", "0");
287 else if (!strcmp(name, "O"))
291 if(val&& val[0] && val[1]==0 && isdigit(val[0])) {
296 driver->set_parameter(driver, "poly2bitmap", "1");
298 driver->set_parameter(driver, "bitmapfonts", "1");
300 driver->set_parameter(driver, "ignoredraworder", "1");
303 else if (!strcmp(name, "G"))
305 //driver->set_parameter(driver, "optimize_polygons", "1");
309 else if (!strcmp(name, "F"))
311 char *s = strdup(val);
313 while(l && s[l-1]=='/') {
317 fontpaths[fontpathpos++] = s;
320 else if (!strcmp(name, "l"))
323 sprintf(buf, "%s/default_loader.swf", SWFDIR);
324 preloader = strdup(buf);
327 else if (!strcmp(name, "b"))
330 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
331 viewer = strdup(buf);
334 else if (!strcmp(name, "L"))
342 systemf("ls %s/*_loader.swf", SWFDIR);
349 else if (!strcmp(name, "B"))
357 systemf("ls %s/*_viewer.swf", SWFDIR);
364 else if (!strcmp(name, "j"))
367 driver->set_parameter(driver, "jpegquality", &name[1]);
370 driver->set_parameter(driver, "jpegquality", val);
374 else if (!strcmp(name, "V"))
376 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
381 fprintf(stderr, "Unknown option: -%s\n", name);
387 /*struct docoptions_t options[] =
388 {{"o","output","filename::Specify output file"},
389 {"V","version","Print program version"},
390 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
391 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
392 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
393 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
394 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
395 {"w","samewindow","Don't open a new browser window for links in the SWF"},
396 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
397 {"F","fontpath","path::Add directory to font search path"},
398 {"B","viewer","name::Link viewer \"name\" to the pdf"},
399 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
400 {"b","defaultviewer","Link default viewer to the pdf"},
401 {"l","defaultpreloader","Link default preloader to the pdf"}
404 struct options_t options[] =
422 {"b","defaultviewer"},
423 {"l","defaultpreloader"},
425 {"T","flashversion"},
429 int args_callback_longoption(char*name,char*val) {
430 return args_long2shortoption(options, name, val);
433 int args_callback_command(char*name, char*val) {
439 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
448 void args_callback_usage(char*name)
450 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
451 printf("\nBasic options:\n");
452 printf("-p --pages=range Convert only pages in range\n");
453 printf("-P --password=password Use password for deciphering the pdf\n");
454 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
455 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
457 printf("-F --fontdir directory Add directory to font search path\n");
459 printf("-V --version Print program version\n");
460 printf("\nEnhanced conversion options:\n");
461 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
462 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
463 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
464 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
465 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
466 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
467 printf("-s zoom=factor Scale result, default: 72\n");
468 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
469 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
470 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
471 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
472 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
473 " graphic errors)\n");
474 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
475 " other out. (Needed for some Powerpoint PDFs)\n");
476 printf("-s transparent Make the SWF transparent\n");
477 //deliberately undocumented (for now)
478 //printf("-2 Put 2 pages into each frame.\n");
479 //printf("-4 Put 4 pages into each frame.\n");
480 printf("Postprocessing options:\n");
481 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
482 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
483 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
484 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
487 float getRate(char*filename)
491 fi = open(filename,O_RDONLY|O_BINARY);
494 sprintf(buffer, "Couldn't open %s", filename);
498 if(swf_ReadSWF(fi,&swf) < 0)
500 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
505 return swf.frameRate / 256.0;
508 void show_info(gfxsource_t*driver, char*filename)
510 gfxdocument_t* pdf = driver->open(driver, filename);
514 msg("<error> Couldn't open %s", filename);
518 fo = fopen(outputname, "wb");
520 perror(outputname);exit(1);;
526 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
528 gfxpage_t*page = pdf->getpage(pdf,pagenr);
529 if(is_in_range(pagenr, pagerange)) {
530 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
536 int main(int argn, char *argv[])
542 char t1searchpath[1024];
545 char* installPath = getInstallationPath();
547 initLog(0,-1,0,0,-1,loglevel);
549 /* not needed anymore since fonts are embedded
551 fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
561 driver = gfxsource_pdf_create();
563 gfxdevice_t swf,wrap;
564 gfxdevice_swf_init(&swf);
566 gfxdevice_removeclippings_init(&wrap, &swf);
569 processargs(argn, argv);
577 fprintf(stderr, "Please specify an input file\n");
582 show_info(driver, filename);
589 outputname = stripFilename(filename, ".swf");
590 msg("<notice> Output filename not given. Writing to %s", outputname);
596 fprintf(stderr, "Please use -o to specify an output file\n");
600 // test if the page range is o.k.
601 is_in_range(0x7fffffff, pagerange);
604 driver->set_parameter(driver, "pages", pagerange);
607 args_callback_usage(argv[0]);
612 for(t=0;t<fontpathpos;t++) {
613 driver->set_parameter(driver, "fontdir", fontpaths[t]);
617 if(password && *password) {
618 sprintf(fullname, "%s|%s", filename, password);
622 gfxdocument_t* pdf = driver->open(driver, filename);
624 msg("<error> Couldn't open %s", filename);
638 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
640 if(is_in_range(pagenr, pagerange)) {
642 sprintf(mapping, "%d:%d", pagenr, frame);
643 pdf->set_parameter(pdf, "pagemap", mapping);
646 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
654 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
656 if(is_in_range(pagenr, pagerange)) {
657 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
658 pages[pagenum].x = 0;
659 pages[pagenum].y = 0;
660 pages[pagenum].page = page;
663 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
666 int xmax[xnup], ymax[xnup];
668 int width=0, height=0;
670 memset(xmax, 0, xnup*sizeof(int));
671 memset(ymax, 0, ynup*sizeof(int));
674 for(x=0;x<xnup;x++) {
677 if(pages[t].page->width > xmax[x])
678 xmax[x] = (int)pages[t].page->width;
679 if(pages[t].page->height > ymax[y])
680 ymax[y] = (int)pages[t].page->height;
682 for(x=0;x<xnup;x++) {
686 for(y=0;y<ynup;y++) {
691 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
693 out->startpage(out,width,height);
695 for(t=0;t<pagenum;t++) {
698 int xpos = x>0?xmax[x-1]:0;
699 int ypos = y>0?ymax[y-1]:0;
700 msg("<verbose> Render (%d,%d) move:%d/%d\n",
701 (int)(pages[t].page->width + xpos),
702 (int)(pages[t].page->height + ypos), xpos, ypos);
703 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
704 custom_move? move_y : ypos,
705 custom_clip? clip_x1 : 0 + xpos,
706 custom_clip? clip_y1 : 0 + ypos,
707 custom_clip? clip_x2 : pages[t].page->width + xpos,
708 custom_clip? clip_y2 : pages[t].page->height + ypos);
711 for(t=0;t<pagenum;t++) {
712 pages[t].page->destroy(pages[t].page);
718 gfxresult_t*result = out->finish(out);
720 if(result->save(result, outputname) < 0) {
724 int width = (int)result->get(result, "width");
725 int height = (int)result->get(result, "height");
726 msg("<notice> SWF written");
728 result->destroy(result);
731 driver->destroy(driver);
737 if(!preloader && viewer) {
738 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
739 viewer, outputname, outputname);
743 if(preloader && !viewer) {
744 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
745 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
746 SWFDIR, preloader, outputname, outputname);
750 if(preloader && viewer) {
752 char tmpname[] = "__swf__XXXXXX";
755 char*tmpname = "__tmp__.swf";
757 systemf("swfcombine \"%s\" viewport=%s -o %s",
758 viewer, outputname, tmpname);
759 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
760 getRate(preloader), SWFDIR, preloader, tmpname, outputname);
761 systemf("rm %s", tmpname);