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 */
28 #include "../config.h"
39 #include "../lib/args.h"
40 #include "../lib/os.h"
41 #include "../lib/rfxswf.h"
42 #include "../lib/devices/swf.h"
43 #include "../lib/devices/polyops.h"
44 #include "../lib/devices/record.h"
45 #include "../lib/pdf/pdf.h"
46 #include "../lib/log.h"
48 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
50 static gfxsource_t*driver = 0;
51 static gfxdevice_t*out = 0;
53 static char * outputname = 0;
54 static int loglevel = 3;
55 static char * pagerange = 0;
56 static char * filename = 0;
57 static char * password = 0;
60 static char * preloader = 0;
61 static char * viewer = 0;
65 static int info_only = 0;
67 static int max_time = 0;
69 static int flatten = 0;
77 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
80 static int system_quiet=0;
82 int systemf(const char* format, ...)
87 va_start(arglist, format);
88 vsprintf(buf, format, arglist);
97 fprintf(stderr, "system() returned %d\n", ret);
104 void sigalarm(int signal)
106 msg("<fatal> Aborting rendering after %d seconds", max_time);
107 #if 0 && defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRUSAGE)
109 getrusage(RUSAGE_CHILDREN, &usage);
110 msg("<fatal> Memory used: %d,%d,%d", usage.ru_maxrss, usage.ru_idrss, usage.ru_isrss);
112 #if defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_H)
113 struct mallinfo info = mallinfo();
114 msg("<fatal> Memory used: %d Mb (%d bytes)", info.uordblks/1048576, info.uordblks);
120 typedef struct _parameter {
121 struct _parameter*next;
126 static parameter_t* device_config = 0;
127 static parameter_t* device_config_next = 0;
128 static void store_parameter(const char*name, const char*value)
130 parameter_t*o = device_config;
132 if(!strcmp(name, o->name)) {
133 /* overwrite old value */
134 free((void*)o->value);
135 o->value = strdup(value);
140 parameter_t*p = (parameter_t*)malloc(sizeof(parameter_t));
141 p->name = strdup(name);
142 p->value = strdup(value);
145 if(device_config_next) {
146 device_config_next->next = p;
147 device_config_next = p;
150 device_config_next = p;
154 int args_callback_option(char*name,char*val) {
155 if (!strcmp(name, "o"))
160 else if (!strcmp(name, "v"))
163 setConsoleLogging(loglevel);
166 else if (!strcmp(name, "2"))
172 else if (!strcmp(name, "4"))
178 else if (!strcmp(name, "9"))
184 else if (!strcmp(name, "q"))
187 setConsoleLogging(loglevel);
191 else if (name[0]=='p')
193 /* check whether the page range follows the p directly, like
197 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
206 else if (!strcmp(name, "P"))
211 else if (!strcmp(name, "c"))
213 char*s = strdup(val);
214 char*x1 = strtok(s, ":");
215 char*y1 = strtok(0, ":");
216 char*x2 = strtok(0, ":");
217 char*y2 = strtok(0, ":");
218 if(!(x1 && y1 && x2 && y2)) {
219 fprintf(stderr, "-c option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
230 else if (!strcmp(name, "m"))
232 char*s = strdup(val);
233 char*c = strchr(s, ':');
235 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
245 else if (!strcmp(name, "s"))
247 char*s = strdup(val);
248 char*c = strchr(s, '=');
249 if(c && *c && c[1]) {
252 store_parameter(s,c);
253 } else if(!strcmp(s,"help")) {
254 printf("PDF Parameters:\n");
255 gfxsource_t*pdf = gfxsource_pdf_create();
256 pdf->set_parameter(pdf, "help", "");
258 gfxdevice_swf_init(&swf);
259 printf("SWF Parameters:\n");
260 swf.setparameter(&swf, "help", "");
263 store_parameter(s,"1");
267 else if (!strcmp(name, "S"))
269 store_parameter("drawonlyshapes", "1");
272 else if (!strcmp(name, "i"))
274 store_parameter("ignoredraworder", "1");
278 else if (!strcmp(name, "Q"))
280 max_time = atoi(val);
282 # ifdef HAVE_SIGNAL_H
283 signal(SIGALRM, sigalarm);
288 else if (!strcmp(name, "z"))
290 store_parameter("enablezlib", "1");
294 else if (!strcmp(name, "n"))
296 store_parameter("opennewwindow", "1");
299 else if (!strcmp(name, "I"))
304 else if (!strcmp(name, "t"))
306 store_parameter("insertstop", "1");
309 else if (!strcmp(name, "T"))
311 if(!strcasecmp(val, "mx"))
312 store_parameter("flashversion", "6");
314 store_parameter("flashversion", val);
318 else if (!strcmp(name, "f"))
320 store_parameter("storeallcharacters", "1");
321 store_parameter("extrafontdata", "1");
324 else if (!strcmp(name, "w"))
326 store_parameter("linksopennewwindow", "0");
329 else if (!strcmp(name, "O"))
333 if(val&& val[0] && val[1]==0 && isdigit(val[0])) {
338 store_parameter("poly2bitmap", "1");
340 store_parameter("bitmapfonts", "1");
342 store_parameter("ignoredraworder", "1");
345 else if (!strcmp(name, "G"))
347 //store_parameter("optimize_polygons", "1");
351 else if (!strcmp(name, "F"))
353 char *s = strdup(val);
355 while(l && s[l-1]=='/') {
359 fontpaths[fontpathpos++] = s;
362 else if (!strcmp(name, "l"))
365 sprintf(buf, "%s/default_loader.swf", SWFDIR);
366 preloader = strdup(buf);
369 else if (!strcmp(name, "b"))
372 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
373 viewer = strdup(buf);
376 else if (!strcmp(name, "L"))
384 systemf("ls %s/*_loader.swf", SWFDIR);
391 else if (!strcmp(name, "B"))
399 systemf("ls %s/*_viewer.swf", SWFDIR);
406 else if (!strcmp(name, "j"))
409 store_parameter("jpegquality", &name[1]);
412 store_parameter("jpegquality", val);
416 else if (!strcmp(name, "V"))
418 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
423 fprintf(stderr, "Unknown option: -%s\n", name);
429 /*struct docoptions_t options[] =
430 {{"o","output","filename::Specify output file"},
431 {"V","version","Print program version"},
432 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
433 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
434 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
435 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
436 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
437 {"w","samewindow","Don't open a new browser window for links in the SWF"},
438 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
439 {"F","fontpath","path::Add directory to font search path"},
440 {"B","viewer","name::Link viewer \"name\" to the pdf"},
441 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
442 {"b","defaultviewer","Link default viewer to the pdf"},
443 {"l","defaultpreloader","Link default preloader to the pdf"}
446 static struct options_t options[] = {
455 {"j", "jpegquality"},
459 {"T", "flashversion"},
461 {"b", "defaultviewer"},
462 {"l", "defaultloader"},
474 int args_callback_longoption(char*name,char*val) {
475 return args_long2shortoption(options, name, val);
478 int args_callback_command(char*name, char*val) {
484 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
493 void args_callback_usage(char *name)
496 printf("Usage: %s [-options] file.pdf -o file.swf\n", name);
498 printf("-h , --help Print short help message and exit\n");
499 printf("-V , --version Print version info and exit\n");
500 printf("-o , --output file.swf Direct output to file.swf. If file.swf contains '%d' (file%d.swf), then each page \n");
501 printf("-p , --pages range Convert only pages in range with range e.g. 1-20 or 1,4,6,9-11 or\n");
502 printf("-P , --password password Use password for deciphering the pdf.\n");
503 printf("-v , --verbose Be verbose. Use more than one -v for greater effect.\n");
504 printf("-z , --zlib Use Flash 6 (MX) zlib compression.\n");
505 printf("-i , --ignore Allows pdf2swf to change the draw order of the pdf. This may make the generated\n");
506 printf("-j , --jpegquality quality Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)\n");
507 printf("-s , --set param=value Set a SWF encoder specific parameter. See pdf2swf -s help for more information.\n");
508 printf("-w , --samewindow When converting pdf hyperlinks, don't make the links open a new window. \n");
509 printf("-t , --stop Insert a stop() command in each page. \n");
510 printf("-T , --flashversion num Set Flash Version in the SWF header to num.\n");
511 printf("-F , --fontdir directory Add directory to the font search path.\n");
512 printf("-b , --defaultviewer Link a standard viewer to the swf file. \n");
513 printf("-l , --defaultloader Link a standard preloader to the swf file which will be displayed while the main swf is loading.\n");
514 printf("-B , --viewer filename Link viewer filename to the swf file. \n");
515 printf("-L , --preloader filename Link preloader filename to the swf file. \n");
516 printf("-q , --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
517 printf("-S , --shapes Don't use SWF Fonts, but store everything as shape.\n");
518 printf("-f , --fonts Store full fonts in SWF. (Don't reduce to used characters).\n");
519 printf("-G , --flatten Remove as many clip layers from file as possible. \n");
520 printf("-I , --info Don't do actual conversion, just display a list of all pages in the PDF.\n");
521 printf("-Q , --maxtime n Abort conversion after n seconds. Only available on Unix.\n");
525 float getRate(char*filename)
529 fi = open(filename,O_RDONLY|O_BINARY);
532 sprintf(buffer, "Couldn't open %s", filename);
536 if(swf_ReadSWF(fi,&swf) < 0)
538 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
543 return swf.frameRate / 256.0;
546 void show_info(gfxsource_t*driver, char*filename)
548 gfxdocument_t* pdf = driver->open(driver, filename);
552 msg("<error> Couldn't open %s", filename);
556 fo = fopen(outputname, "wb");
558 perror(outputname);exit(1);;
564 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
566 gfxpage_t*page = pdf->getpage(pdf,pagenr);
567 if(is_in_range(pagenr, pagerange)) {
568 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
575 static gfxdevice_t swf,wrap;
576 gfxdevice_t*create_output_device()
578 gfxdevice_swf_init(&swf);
579 gfxdevice_removeclippings_init(&wrap, &swf);
584 /* pass global parameters to output device */
585 parameter_t*p = device_config;
587 out->setparameter(out, p->name, p->value);
593 int main(int argn, char *argv[])
599 char t1searchpath[1024];
602 char* installPath = getInstallationPath();
603 int one_file_per_page = 0;
605 initLog(0,-1,0,0,-1,loglevel);
607 /* not needed anymore since fonts are embedded
609 fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
620 processargs(argn, argv);
622 driver = gfxsource_pdf_create();
624 /* pass global parameters to PDF driver*/
625 parameter_t*p = device_config;
627 driver->set_parameter(driver, p->name, p->value);
633 fprintf(stderr, "Please specify an input file\n");
638 show_info(driver, filename);
645 outputname = stripFilename(filename, ".swf");
646 msg("<notice> Output filename not given. Writing to %s", outputname);
652 fprintf(stderr, "Please use -o to specify an output file\n");
656 // test if the page range is o.k.
657 is_in_range(0x7fffffff, pagerange);
660 driver->set_parameter(driver, "pages", pagerange);
663 args_callback_usage(argv[0]);
668 for(t=0;t<fontpathpos;t++) {
669 driver->set_parameter(driver, "fontdir", fontpaths[t]);
673 if(password && *password) {
674 sprintf(fullname, "%s|%s", filename, password);
679 if((u = strchr(outputname, '%'))) {
680 if(strchr(u+1, '%') ||
681 strchr(outputname, '%')!=u) {
682 msg("<error> only one %% allowed in filename\n");
685 if(preloader || viewer) {
686 msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
689 msg("<notice> outputting one file per page");
690 one_file_per_page = 1;
691 char*pattern = (char*)malloc(strlen(outputname)+2);
692 /* convert % to %d */
693 int l = u-outputname+1;
694 memcpy(pattern, outputname, l);
696 strcpy(pattern+l+1, outputname+l);
697 outputname = pattern;
700 gfxdocument_t* pdf = driver->open(driver, filename);
702 msg("<error> Couldn't open %s", filename);
705 /* pass global parameters document */
708 pdf->set_parameter(pdf, p->name, p->value);
722 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
724 if(is_in_range(pagenr, pagerange)) {
726 sprintf(mapping, "%d:%d", pagenr, frame);
727 pdf->set_parameter(pdf, "pagemap", mapping);
730 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
738 gfxdevice_t*out = create_output_device();;
739 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
741 if(is_in_range(pagenr, pagerange)) {
742 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
743 pages[pagenum].x = 0;
744 pages[pagenum].y = 0;
745 pages[pagenum].page = page;
748 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
751 int xmax[xnup], ymax[xnup];
753 int width=0, height=0;
755 memset(xmax, 0, xnup*sizeof(int));
756 memset(ymax, 0, ynup*sizeof(int));
759 for(x=0;x<xnup;x++) {
762 if(pages[t].page->width > xmax[x])
763 xmax[x] = (int)pages[t].page->width;
764 if(pages[t].page->height > ymax[y])
765 ymax[y] = (int)pages[t].page->height;
767 for(x=0;x<xnup;x++) {
771 for(y=0;y<ynup;y++) {
776 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
778 out->startpage(out,width,height);
780 for(t=0;t<pagenum;t++) {
783 int xpos = x>0?xmax[x-1]:0;
784 int ypos = y>0?ymax[y-1]:0;
785 msg("<verbose> Render (%d,%d) move:%d/%d\n",
786 (int)(pages[t].page->width + xpos),
787 (int)(pages[t].page->height + ypos), xpos, ypos);
788 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
789 custom_move? move_y : ypos,
790 custom_clip? clip_x1 : 0 + xpos,
791 custom_clip? clip_y1 : 0 + ypos,
792 custom_clip? clip_x2 : pages[t].page->width + xpos,
793 custom_clip? clip_y2 : pages[t].page->height + ypos);
796 for(t=0;t<pagenum;t++) {
797 pages[t].page->destroy(pages[t].page);
801 if(one_file_per_page) {
802 gfxresult_t*result = out->finish(out);out=0;
804 sprintf(buf, outputname, one_file_per_page++);
805 if(result->save(result, buf) < 0) {
808 result->destroy(result);result=0;
809 out = create_output_device();;
810 msg("<notice> Writing SWF file %s", buf);
815 if(one_file_per_page) {
816 // remove empty device
817 gfxresult_t*result = out->finish(out);out=0;
818 result->destroy(result);result=0;
820 gfxresult_t*result = out->finish(out);
821 msg("<notice> Writing SWF file %s", outputname);
822 if(result->save(result, outputname) < 0) {
825 int width = (int)result->get(result, "width");
826 int height = (int)result->get(result, "height");
827 result->destroy(result);result=0;
829 if(preloader || viewer) {
834 if(!preloader && viewer) {
835 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
836 viewer, outputname, outputname);
840 if(preloader && !viewer) {
841 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
842 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
843 SWFDIR, preloader, outputname, outputname);
847 if(preloader && viewer) {
849 char tmpname[] = "__swf__XXXXXX";
852 char*tmpname = "__tmp__.swf";
854 systemf("swfcombine \"%s\" viewport=%s -o %s",
855 viewer, outputname, tmpname);
856 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
857 getRate(preloader), SWFDIR, preloader, tmpname, outputname);
858 systemf("rm %s", tmpname);
864 driver->destroy(driver);
867 /* free global parameters */
870 parameter_t*next = p->next;
871 if(p->name) free((void*)p->name);p->name = 0;
872 if(p->value) free((void*)p->value);p->value =0;