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/pdf/pdf.h"
47 #include "../lib/log.h"
49 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
51 static gfxsource_t*driver = 0;
52 static gfxdevice_t*out = 0;
54 static int maxwidth=0, maxheight=0;
56 static char * outputname = 0;
57 static int loglevel = 3;
58 static char * pagerange = 0;
59 static char * filename = 0;
60 static char * password = 0;
63 static char * preloader = 0;
64 static char * viewer = 0;
68 static int info_only = 0;
70 static int max_time = 0;
72 static int flatten = 0;
80 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
83 static int system_quiet=0;
85 int systemf(const char* format, ...)
90 va_start(arglist, format);
91 vsnprintf(buf, sizeof(buf)-1, format, arglist);
100 fprintf(stderr, "system() returned %d\n", ret);
107 void sigalarm(int signal)
109 msg("<fatal> Aborting rendering after %d seconds", max_time);
110 #if 0 && defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRUSAGE)
112 getrusage(RUSAGE_CHILDREN, &usage);
113 msg("<fatal> Memory used: %d,%d,%d", usage.ru_maxrss, usage.ru_idrss, usage.ru_isrss);
115 #if defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_H)
116 struct mallinfo info = mallinfo();
117 msg("<fatal> Memory used: %d Mb (%d bytes)", info.uordblks/1048576, info.uordblks);
123 typedef struct _parameter {
124 struct _parameter*next;
129 static parameter_t* device_config = 0;
130 static parameter_t* device_config_next = 0;
131 static void store_parameter(const char*name, const char*value)
133 parameter_t*o = device_config;
135 if(!strcmp(name, o->name)) {
136 /* overwrite old value */
137 free((void*)o->value);
138 o->value = strdup(value);
143 parameter_t*p = (parameter_t*)malloc(sizeof(parameter_t));
144 p->name = strdup(name);
145 p->value = strdup(value);
148 if(device_config_next) {
149 device_config_next->next = p;
150 device_config_next = p;
153 device_config_next = p;
157 int args_callback_option(char*name,char*val) {
158 if (!strcmp(name, "o"))
163 else if (!strcmp(name, "v"))
166 setConsoleLogging(loglevel);
169 else if (!strcmp(name, "2"))
175 else if (!strcmp(name, "4"))
181 else if (!strcmp(name, "9"))
187 else if (!strcmp(name, "X"))
189 maxwidth = atoi(val);
192 else if (!strcmp(name, "Y"))
194 maxheight = atoi(val);
197 else if (!strcmp(name, "q"))
200 setConsoleLogging(loglevel);
204 else if (name[0]=='p')
206 /* check whether the page range follows the p directly, like
210 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
219 else if (!strcmp(name, "P"))
224 else if (!strcmp(name, "c"))
226 char*s = strdup(val);
227 char*x1 = strtok(s, ":");
228 char*y1 = strtok(0, ":");
229 char*x2 = strtok(0, ":");
230 char*y2 = strtok(0, ":");
231 if(!(x1 && y1 && x2 && y2)) {
232 fprintf(stderr, "-c option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
243 else if (!strcmp(name, "m"))
245 char*s = strdup(val);
246 char*c = strchr(s, ':');
248 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
258 else if (!strcmp(name, "s"))
260 char*s = strdup(val);
261 char*c = strchr(s, '=');
262 if(c && *c && c[1]) {
265 store_parameter(s,c);
266 } else if(!strcmp(s,"help")) {
267 printf("PDF Parameters:\n");
268 gfxsource_t*pdf = gfxsource_pdf_create();
269 pdf->set_parameter(pdf, "help", "");
271 gfxdevice_swf_init(&swf);
272 printf("SWF Parameters:\n");
273 swf.setparameter(&swf, "help", "");
276 store_parameter(s,"1");
280 else if (!strcmp(name, "S"))
282 store_parameter("drawonlyshapes", "1");
285 else if (!strcmp(name, "i"))
287 store_parameter("ignoredraworder", "1");
291 else if (!strcmp(name, "Q"))
293 max_time = atoi(val);
295 # ifdef HAVE_SIGNAL_H
296 signal(SIGALRM, sigalarm);
301 else if (!strcmp(name, "z"))
303 store_parameter("enablezlib", "1");
307 else if (!strcmp(name, "n"))
309 store_parameter("opennewwindow", "1");
312 else if (!strcmp(name, "I"))
317 else if (!strcmp(name, "t"))
319 store_parameter("insertstop", "1");
322 else if (!strcmp(name, "T"))
324 if(!strcasecmp(val, "mx"))
325 store_parameter("flashversion", "6");
327 store_parameter("flashversion", val);
331 else if (!strcmp(name, "f"))
333 store_parameter("storeallcharacters", "1");
334 store_parameter("extrafontdata", "1");
337 else if (!strcmp(name, "w"))
339 store_parameter("linksopennewwindow", "0");
342 else if (!strcmp(name, "O"))
346 if(val&& val[0] && val[1]==0 && isdigit(val[0])) {
351 store_parameter("poly2bitmap", "1");
353 store_parameter("bitmapfonts", "1");
355 store_parameter("ignoredraworder", "1");
358 else if (!strcmp(name, "G"))
360 //store_parameter("optimize_polygons", "1");
364 else if (!strcmp(name, "F"))
366 char *s = strdup(val);
368 while(l && s[l-1]=='/') {
372 fontpaths[fontpathpos++] = s;
375 else if (!strcmp(name, "l"))
378 sprintf(buf, "%s/default_loader.swf", SWFDIR);
379 preloader = strdup(buf);
382 else if (!strcmp(name, "b"))
385 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
386 viewer = strdup(buf);
389 else if (!strcmp(name, "L"))
397 systemf("ls %s/*_loader.swf", SWFDIR);
404 else if (!strcmp(name, "B"))
412 systemf("ls %s/*_viewer.swf", SWFDIR);
419 else if (!strcmp(name, "j"))
422 store_parameter("jpegquality", &name[1]);
425 store_parameter("jpegquality", val);
429 else if (!strcmp(name, "V"))
431 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
436 fprintf(stderr, "Unknown option: -%s\n", name);
442 static struct options_t options[] = {
451 {"j", "jpegquality"},
455 {"T", "flashversion"},
457 {"b", "defaultviewer"},
458 {"l", "defaultloader"},
472 int args_callback_longoption(char*name,char*val) {
473 return args_long2shortoption(options, name, val);
476 int args_callback_command(char*name, char*val) {
482 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
491 void args_callback_usage(char *name)
494 printf("Usage: %s [-options] file.pdf -o file.swf\n", name);
496 printf("-h , --help Print short help message and exit\n");
497 printf("-V , --version Print version info and exit\n");
498 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");
499 printf("-p , --pages range Convert only pages in range with range e.g. 1-20 or 1,4,6,9-11 or\n");
500 printf("-P , --password password Use password for deciphering the pdf.\n");
501 printf("-v , --verbose Be verbose. Use more than one -v for greater effect.\n");
502 printf("-z , --zlib Use Flash 6 (MX) zlib compression.\n");
503 printf("-i , --ignore Allows pdf2swf to change the draw order of the pdf. This may make the generated\n");
504 printf("-j , --jpegquality quality Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)\n");
505 printf("-s , --set param=value Set a SWF encoder specific parameter. See pdf2swf -s help for more information.\n");
506 printf("-w , --samewindow When converting pdf hyperlinks, don't make the links open a new window. \n");
507 printf("-t , --stop Insert a stop() command in each page. \n");
508 printf("-T , --flashversion num Set Flash Version in the SWF header to num.\n");
509 printf("-F , --fontdir directory Add directory to the font search path.\n");
510 printf("-b , --defaultviewer Link a standard viewer to the swf file. \n");
511 printf("-l , --defaultloader Link a standard preloader to the swf file which will be displayed while the main swf is loading.\n");
512 printf("-B , --viewer filename Link viewer filename to the swf file. \n");
513 printf("-L , --preloader filename Link preloader filename to the swf file. \n");
514 printf("-q , --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
515 printf("-S , --shapes Don't use SWF Fonts, but store everything as shape.\n");
516 printf("-f , --fonts Store full fonts in SWF. (Don't reduce to used characters).\n");
517 printf("-G , --flatten Remove as many clip layers from file as possible. \n");
518 printf("-I , --info Don't do actual conversion, just display a list of all pages in the PDF.\n");
519 printf("-Q , --maxtime n Abort conversion after n seconds. Only available on Unix.\n");
523 float getRate(char*filename)
527 fi = open(filename,O_RDONLY|O_BINARY);
530 sprintf(buffer, "Couldn't open %s", filename);
534 if(swf_ReadSWF(fi,&swf) < 0)
536 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
541 return swf.frameRate / 256.0;
544 void show_info(gfxsource_t*driver, char*filename)
546 gfxdocument_t* pdf = driver->open(driver, filename);
550 msg("<error> Couldn't open %s", filename);
554 fo = fopen(outputname, "wb");
556 perror(outputname);exit(1);;
562 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
564 gfxpage_t*page = pdf->getpage(pdf,pagenr);
565 if(is_in_range(pagenr, pagerange)) {
566 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
573 static gfxdevice_t swf,wrap,rescale;
574 gfxdevice_t*create_output_device()
576 gfxdevice_swf_init(&swf);
578 /* set up filter chain */
582 gfxdevice_removeclippings_init(&wrap, &swf);
586 if(maxwidth || maxheight) {
587 gfxdevice_rescale_init(&rescale, out, maxwidth, maxheight, 0);
591 /* pass global parameters to output device */
592 parameter_t*p = device_config;
594 out->setparameter(out, p->name, p->value);
600 int main(int argn, char *argv[])
606 char t1searchpath[1024];
609 int one_file_per_page = 0;
611 initLog(0,-1,0,0,-1,loglevel);
613 /* not needed anymore since fonts are embedded
615 fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
626 processargs(argn, argv);
628 driver = gfxsource_pdf_create();
630 /* pass global parameters to PDF driver*/
631 parameter_t*p = device_config;
633 driver->set_parameter(driver, p->name, p->value);
639 fprintf(stderr, "Please specify an input file\n");
646 outputname = stripFilename(filename, ".swf");
647 msg("<notice> Output filename not given. Writing to %s", outputname);
653 fprintf(stderr, "Please use -o to specify an output file\n");
657 // test if the page range is o.k.
658 is_in_range(0x7fffffff, pagerange);
661 args_callback_usage(argv[0]);
666 if(password && *password) {
667 sprintf(fullname, "%s|%s", filename, password);
672 driver->set_parameter(driver, "pages", pagerange);
675 show_info(driver, filename);
680 for(t=0;t<fontpathpos;t++) {
681 driver->set_parameter(driver, "fontdir", fontpaths[t]);
685 if((u = strchr(outputname, '%'))) {
686 if(strchr(u+1, '%') ||
687 strchr(outputname, '%')!=u) {
688 msg("<error> only one %% allowed in filename\n");
691 if(preloader || viewer) {
692 msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
695 msg("<notice> outputting one file per page");
696 one_file_per_page = 1;
697 char*pattern = (char*)malloc(strlen(outputname)+2);
698 /* convert % to %d */
699 int l = u-outputname+1;
700 memcpy(pattern, outputname, l);
702 strcpy(pattern+l+1, outputname+l);
703 outputname = pattern;
706 gfxdocument_t* pdf = driver->open(driver, filename);
708 msg("<error> Couldn't open %s", filename);
711 /* pass global parameters document */
714 pdf->set_parameter(pdf, p->name, p->value);
728 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
730 if(is_in_range(pagenr, pagerange)) {
732 sprintf(mapping, "%d:%d", pagenr, frame);
733 pdf->set_parameter(pdf, "pagemap", mapping);
736 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
744 gfxdevice_t*out = create_output_device();;
745 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
747 if(is_in_range(pagenr, pagerange)) {
748 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
749 pages[pagenum].x = 0;
750 pages[pagenum].y = 0;
751 pages[pagenum].page = page;
754 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
757 int xmax[xnup], ymax[xnup];
759 int width=0, height=0;
761 memset(xmax, 0, xnup*sizeof(int));
762 memset(ymax, 0, ynup*sizeof(int));
765 for(x=0;x<xnup;x++) {
768 if(pages[t].page->width > xmax[x])
769 xmax[x] = (int)pages[t].page->width;
770 if(pages[t].page->height > ymax[y])
771 ymax[y] = (int)pages[t].page->height;
773 for(x=0;x<xnup;x++) {
777 for(y=0;y<ynup;y++) {
782 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
784 out->startpage(out,width,height);
786 for(t=0;t<pagenum;t++) {
789 int xpos = x>0?xmax[x-1]:0;
790 int ypos = y>0?ymax[y-1]:0;
791 msg("<verbose> Render (%d,%d) move:%d/%d\n",
792 (int)(pages[t].page->width + xpos),
793 (int)(pages[t].page->height + ypos), xpos, ypos);
794 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
795 custom_move? move_y : ypos,
796 custom_clip? clip_x1 : 0 + xpos,
797 custom_clip? clip_y1 : 0 + ypos,
798 custom_clip? clip_x2 : pages[t].page->width + xpos,
799 custom_clip? clip_y2 : pages[t].page->height + ypos);
802 for(t=0;t<pagenum;t++) {
803 pages[t].page->destroy(pages[t].page);
807 if(one_file_per_page) {
808 gfxresult_t*result = out->finish(out);out=0;
810 sprintf(buf, outputname, pagenr);
811 if(result->save(result, buf) < 0) {
814 result->destroy(result);result=0;
815 out = create_output_device();;
816 msg("<notice> Writing SWF file %s", buf);
821 if(one_file_per_page) {
822 // remove empty device
823 gfxresult_t*result = out->finish(out);out=0;
824 result->destroy(result);result=0;
826 gfxresult_t*result = out->finish(out);
827 msg("<notice> Writing SWF file %s", outputname);
828 if(result->save(result, outputname) < 0) {
831 int width = (int)(ptroff_t)result->get(result, "width");
832 int height = (int)(ptroff_t)result->get(result, "height");
833 result->destroy(result);result=0;
835 if(preloader || viewer) {
840 if(!preloader && viewer) {
841 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
842 viewer, outputname, outputname);
846 if(preloader && !viewer) {
847 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
848 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
849 SWFDIR, preloader, outputname, outputname);
853 if(preloader && viewer) {
855 char tmpname[] = "__swf__XXXXXX";
858 char*tmpname = "__tmp__.swf";
860 systemf("swfcombine \"%s\" viewport=%s -o %s",
861 viewer, outputname, tmpname);
862 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
863 getRate(preloader), SWFDIR, preloader, tmpname, outputname);
864 systemf("rm %s", tmpname);
870 driver->destroy(driver);
873 /* free global parameters */
876 parameter_t*next = p->next;
877 if(p->name) free((void*)p->name);p->name = 0;
878 if(p->value) free((void*)p->value);p->value =0;