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/devices/rescale.h"
46 #include "../lib/gfxfilter.h"
47 #include "../lib/pdf/pdf.h"
48 #include "../lib/log.h"
50 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
52 static gfxsource_t*driver = 0;
53 static gfxdevice_t*out = 0;
55 static int maxwidth=0, maxheight=0;
57 static char * outputname = 0;
58 static int loglevel = 3;
59 static char * pagerange = 0;
60 static char * filename = 0;
61 static char * password = 0;
64 static char * preloader = 0;
65 static char * viewer = 0;
69 static int info_only = 0;
71 static int max_time = 0;
73 static int flatten = 0;
81 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
84 static int system_quiet=0;
86 int systemf(const char* format, ...)
91 va_start(arglist, format);
92 vsnprintf(buf, sizeof(buf)-1, format, arglist);
101 fprintf(stderr, "system() returned %d\n", ret);
108 void sigalarm(int signal)
110 msg("<fatal> Aborting rendering after %d seconds", max_time);
111 #if 0 && defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRUSAGE)
113 getrusage(RUSAGE_CHILDREN, &usage);
114 msg("<fatal> Memory used: %d,%d,%d", usage.ru_maxrss, usage.ru_idrss, usage.ru_isrss);
116 #if defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_H)
117 struct mallinfo info = mallinfo();
118 msg("<fatal> Memory used: %d Mb (%d bytes)", info.uordblks/1048576, info.uordblks);
124 typedef struct _parameter {
125 struct _parameter*next;
130 static parameter_t* device_config = 0;
131 static parameter_t* device_config_next = 0;
132 static void store_parameter(const char*name, const char*value)
134 parameter_t*o = device_config;
136 if(!strcmp(name, o->name)) {
137 /* overwrite old value */
138 free((void*)o->value);
139 o->value = strdup(value);
144 parameter_t*p = (parameter_t*)malloc(sizeof(parameter_t));
145 p->name = strdup(name);
146 p->value = strdup(value);
149 if(device_config_next) {
150 device_config_next->next = p;
151 device_config_next = p;
154 device_config_next = p;
158 int args_callback_option(char*name,char*val) {
159 if (!strcmp(name, "o"))
164 else if (!strcmp(name, "v"))
167 setConsoleLogging(loglevel);
170 else if (!strcmp(name, "2"))
176 else if (!strcmp(name, "4"))
182 else if (!strcmp(name, "9"))
188 else if (!strcmp(name, "X"))
190 maxwidth = atoi(val);
193 else if (!strcmp(name, "Y"))
195 maxheight = atoi(val);
198 else if (!strcmp(name, "q"))
201 setConsoleLogging(loglevel);
205 else if (name[0]=='p')
207 /* check whether the page range follows the p directly, like
211 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
220 else if (!strcmp(name, "P"))
225 else if (!strcmp(name, "c"))
227 char*s = strdup(val);
228 char*x1 = strtok(s, ":");
229 char*y1 = strtok(0, ":");
230 char*x2 = strtok(0, ":");
231 char*y2 = strtok(0, ":");
232 if(!(x1 && y1 && x2 && y2)) {
233 fprintf(stderr, "-c option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
244 else if (!strcmp(name, "m"))
246 char*s = strdup(val);
247 char*c = strchr(s, ':');
249 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
259 else if (!strcmp(name, "s"))
261 char*s = strdup(val);
262 char*c = strchr(s, '=');
263 if(c && *c && c[1]) {
266 store_parameter(s,c);
267 } else if(!strcmp(s,"help")) {
268 printf("PDF Parameters:\n");
269 gfxsource_t*pdf = gfxsource_pdf_create();
270 pdf->setparameter(pdf, "help", "");
272 gfxdevice_swf_init(&swf);
273 printf("SWF Parameters:\n");
274 swf.setparameter(&swf, "help", "");
277 store_parameter(s,"1");
281 else if (!strcmp(name, "S"))
283 store_parameter("drawonlyshapes", "1");
286 else if (!strcmp(name, "i"))
288 store_parameter("ignoredraworder", "1");
292 else if (!strcmp(name, "Q"))
294 max_time = atoi(val);
296 # ifdef HAVE_SIGNAL_H
297 signal(SIGALRM, sigalarm);
302 else if (!strcmp(name, "z"))
304 store_parameter("enablezlib", "1");
308 else if (!strcmp(name, "n"))
310 store_parameter("opennewwindow", "1");
313 else if (!strcmp(name, "I"))
318 else if (!strcmp(name, "t"))
320 store_parameter("insertstop", "1");
323 else if (!strcmp(name, "T"))
325 if(!strcasecmp(val, "mx"))
326 store_parameter("flashversion", "6");
328 store_parameter("flashversion", val);
332 else if (!strcmp(name, "f"))
334 store_parameter("storeallcharacters", "1");
335 store_parameter("extrafontdata", "1");
338 else if (!strcmp(name, "w"))
340 store_parameter("linksopennewwindow", "0");
343 else if (!strcmp(name, "O"))
347 if(val&& val[0] && val[1]==0 && isdigit(val[0])) {
352 store_parameter("poly2bitmap", "1");
354 store_parameter("bitmapfonts", "1");
356 store_parameter("ignoredraworder", "1");
359 else if (!strcmp(name, "G"))
361 //store_parameter("optimize_polygons", "1");
365 else if (!strcmp(name, "F"))
367 char *s = strdup(val);
369 while(l && s[l-1]=='/') {
373 fontpaths[fontpathpos++] = s;
376 else if (!strcmp(name, "l"))
379 sprintf(buf, "%s/default_loader.swf", SWFDIR);
380 preloader = strdup(buf);
383 else if (!strcmp(name, "b"))
386 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
387 viewer = strdup(buf);
390 else if (!strcmp(name, "L"))
398 systemf("ls %s/*_loader.swf", SWFDIR);
405 else if (!strcmp(name, "B"))
413 systemf("ls %s/*_viewer.swf", SWFDIR);
420 else if (!strcmp(name, "j"))
423 store_parameter("jpegquality", &name[1]);
426 store_parameter("jpegquality", val);
430 else if (!strcmp(name, "V"))
432 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
437 fprintf(stderr, "Unknown option: -%s\n", name);
443 static struct options_t options[] = {
452 {"j", "jpegquality"},
456 {"T", "flashversion"},
458 {"b", "defaultviewer"},
459 {"l", "defaultloader"},
473 int args_callback_longoption(char*name,char*val) {
474 return args_long2shortoption(options, name, val);
477 int args_callback_command(char*name, char*val) {
483 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
492 void args_callback_usage(char *name)
495 printf("Usage: %s [-options] file.pdf -o file.swf\n", name);
497 printf("-h , --help Print short help message and exit\n");
498 printf("-V , --version Print version info and exit\n");
499 printf("-o , --output file.swf Direct output to file.swf. If file.swf contains '%%' (file%%.swf), then each page goes to a seperate file.\n");
500 printf("-p , --pages range Convert only pages in range with range e.g. 1-20 or 1,4,6,9-11 or\n");
501 printf("-P , --password password Use password for deciphering the pdf.\n");
502 printf("-v , --verbose Be verbose. Use more than one -v for greater effect.\n");
503 printf("-z , --zlib Use Flash 6 (MX) zlib compression.\n");
504 printf("-i , --ignore Allows pdf2swf to change the draw order of the pdf. This may make the generated\n");
505 printf("-j , --jpegquality quality Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)\n");
506 printf("-s , --set param=value Set a SWF encoder specific parameter. See pdf2swf -s help for more information.\n");
507 printf("-w , --samewindow When converting pdf hyperlinks, don't make the links open a new window. \n");
508 printf("-t , --stop Insert a stop() command in each page. \n");
509 printf("-T , --flashversion num Set Flash Version in the SWF header to num.\n");
510 printf("-F , --fontdir directory Add directory to the font search path.\n");
511 printf("-b , --defaultviewer Link a standard viewer to the swf file. \n");
512 printf("-l , --defaultloader Link a standard preloader to the swf file which will be displayed while the main swf is loading.\n");
513 printf("-B , --viewer filename Link viewer filename to the swf file. \n");
514 printf("-L , --preloader filename Link preloader filename to the swf file. \n");
515 printf("-q , --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
516 printf("-S , --shapes Don't use SWF Fonts, but store everything as shape.\n");
517 printf("-f , --fonts Store full fonts in SWF. (Don't reduce to used characters).\n");
518 printf("-G , --flatten Remove as many clip layers from file as possible. \n");
519 printf("-I , --info Don't do actual conversion, just display a list of all pages in the PDF.\n");
520 printf("-Q , --maxtime n Abort conversion after n seconds. Only available on Unix.\n");
524 float getRate(char*filename)
528 fi = open(filename,O_RDONLY|O_BINARY);
531 sprintf(buffer, "Couldn't open %s", filename);
535 if(swf_ReadSWF(fi,&swf) < 0)
537 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
542 return swf.frameRate / 256.0;
545 void show_info(gfxsource_t*driver, char*filename)
547 gfxdocument_t* pdf = driver->open(driver, filename);
551 msg("<error> Couldn't open %s", filename);
555 fo = fopen(outputname, "wb");
557 perror(outputname);exit(1);;
563 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
565 gfxpage_t*page = pdf->getpage(pdf,pagenr);
566 if(is_in_range(pagenr, pagerange)) {
567 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
574 static gfxdevice_t swf,wrap,rescale;
575 gfxdevice_t*create_output_device()
577 gfxdevice_swf_init(&swf);
579 /* set up filter chain */
583 gfxdevice_removeclippings_init(&wrap, &swf);
587 if(maxwidth || maxheight) {
588 gfxdevice_rescale_init(&rescale, out, maxwidth, maxheight, 0);
592 /* pass global parameters to output device */
593 parameter_t*p = device_config;
595 out->setparameter(out, p->name, p->value);
601 int main(int argn, char *argv[])
607 char t1searchpath[1024];
610 int one_file_per_page = 0;
612 initLog(0,-1,0,0,-1,loglevel);
614 /* not needed anymore since fonts are embedded
616 fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
627 processargs(argn, argv);
629 driver = gfxsource_pdf_create();
631 /* pass global parameters to PDF driver*/
632 parameter_t*p = device_config;
634 driver->setparameter(driver, p->name, p->value);
640 fprintf(stderr, "Please specify an input file\n");
648 outputname = stripFilename(filename, ".swf");
649 msg("<notice> Output filename not given. Writing to %s", outputname);
655 fprintf(stderr, "Please use -o to specify an output file\n");
660 // test if the page range is o.k.
661 is_in_range(0x7fffffff, pagerange);
664 args_callback_usage(argv[0]);
669 if(password && *password) {
670 sprintf(fullname, "%s|%s", filename, password);
675 driver->setparameter(driver, "pages", pagerange);
678 for(t=0;t<fontpathpos;t++) {
679 driver->setparameter(driver, "fontdir", fontpaths[t]);
683 show_info(driver, filename);
688 if((u = strchr(outputname, '%'))) {
689 if(strchr(u+1, '%') ||
690 strchr(outputname, '%')!=u) {
691 msg("<error> only one %% allowed in filename\n");
694 if(preloader || viewer) {
695 msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
698 msg("<notice> outputting one file per page");
699 one_file_per_page = 1;
700 char*pattern = (char*)malloc(strlen(outputname)+2);
701 /* convert % to %d */
702 int l = u-outputname+1;
703 memcpy(pattern, outputname, l);
705 strcpy(pattern+l+1, outputname+l);
706 outputname = pattern;
709 gfxdocument_t* pdf = driver->open(driver, filename);
711 msg("<error> Couldn't open %s", filename);
714 /* pass global parameters document */
717 pdf->setparameter(pdf, p->name, p->value);
731 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
733 if(is_in_range(pagenr, pagerange)) {
735 sprintf(mapping, "%d:%d", pagenr, frame);
736 pdf->setparameter(pdf, "pagemap", mapping);
739 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
744 if(pagerange && !pagenum && frame==1) {
745 fprintf(stderr, "No pages in range %s", pagerange);
751 gfxdevice_t*out = create_output_device();;
752 pdf->prepare(pdf, out);
754 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
756 if(is_in_range(pagenr, pagerange)) {
757 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
758 pages[pagenum].x = 0;
759 pages[pagenum].y = 0;
760 pages[pagenum].page = page;
763 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
766 int xmax[xnup], ymax[xnup];
768 int width=0, height=0;
770 memset(xmax, 0, xnup*sizeof(int));
771 memset(ymax, 0, ynup*sizeof(int));
774 for(x=0;x<xnup;x++) {
777 if(pages[t].page->width > xmax[x])
778 xmax[x] = (int)pages[t].page->width;
779 if(pages[t].page->height > ymax[y])
780 ymax[y] = (int)pages[t].page->height;
782 for(x=0;x<xnup;x++) {
786 for(y=0;y<ynup;y++) {
791 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
793 out->startpage(out,width,height);
795 for(t=0;t<pagenum;t++) {
798 int xpos = x>0?xmax[x-1]:0;
799 int ypos = y>0?ymax[y-1]:0;
800 msg("<verbose> Render (%d,%d) move:%d/%d\n",
801 (int)(pages[t].page->width + xpos),
802 (int)(pages[t].page->height + ypos), xpos, ypos);
803 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
804 custom_move? move_y : ypos,
805 custom_clip? clip_x1 : 0 + xpos,
806 custom_clip? clip_y1 : 0 + ypos,
807 custom_clip? clip_x2 : pages[t].page->width + xpos,
808 custom_clip? clip_y2 : pages[t].page->height + ypos);
811 for(t=0;t<pagenum;t++) {
812 pages[t].page->destroy(pages[t].page);
816 if(one_file_per_page) {
817 gfxresult_t*result = out->finish(out);out=0;
819 sprintf(buf, outputname, pagenr);
820 if(result->save(result, buf) < 0) {
823 result->destroy(result);result=0;
824 out = create_output_device();;
825 msg("<notice> Writing SWF file %s", buf);
830 if(one_file_per_page) {
831 // remove empty device
832 gfxresult_t*result = out->finish(out);out=0;
833 result->destroy(result);result=0;
835 gfxresult_t*result = out->finish(out);
836 msg("<notice> Writing SWF file %s", outputname);
837 if(result->save(result, outputname) < 0) {
840 int width = (int)(ptroff_t)result->get(result, "width");
841 int height = (int)(ptroff_t)result->get(result, "height");
842 result->destroy(result);result=0;
844 if(preloader || viewer) {
849 if(!preloader && viewer) {
850 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
851 viewer, outputname, outputname);
855 if(preloader && !viewer) {
856 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
857 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
858 SWFDIR, preloader, outputname, outputname);
862 if(preloader && viewer) {
864 char tmpname[] = "__swf__XXXXXX";
867 char*tmpname = "__tmp__.swf";
869 systemf("swfcombine \"%s\" viewport=%s -o %s",
870 viewer, outputname, tmpname);
871 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
872 getRate(preloader), SWFDIR, preloader, tmpname, outputname);
873 systemf("rm %s", tmpname);
879 driver->destroy(driver);
882 /* free global parameters */
885 parameter_t*next = p->next;
886 if(p->name) free((void*)p->name);p->name = 0;
887 if(p->value) free((void*)p->value);p->value =0;