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;
75 static char* filters = 0;
83 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
86 static int system_quiet=0;
88 int systemf(const char* format, ...)
93 va_start(arglist, format);
94 vsnprintf(buf, sizeof(buf)-1, format, arglist);
103 fprintf(stderr, "system() returned %d\n", ret);
110 void sigalarm(int signal)
112 msg("<fatal> Aborting rendering after %d seconds", max_time);
113 #if 0 && defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRUSAGE)
115 getrusage(RUSAGE_CHILDREN, &usage);
116 msg("<fatal> Memory used: %d,%d,%d", usage.ru_maxrss, usage.ru_idrss, usage.ru_isrss);
118 #if defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_H)
119 struct mallinfo info = mallinfo();
120 msg("<fatal> Memory used: %d Mb (%d bytes)", info.uordblks/1048576, info.uordblks);
126 typedef struct _parameter {
127 struct _parameter*next;
132 static parameter_t* device_config = 0;
133 static parameter_t* device_config_next = 0;
134 static void store_parameter(const char*name, const char*value)
136 parameter_t*o = device_config;
138 if(!strcmp(name, o->name)) {
139 /* overwrite old value */
140 free((void*)o->value);
141 o->value = strdup(value);
146 parameter_t*p = (parameter_t*)malloc(sizeof(parameter_t));
147 p->name = strdup(name);
148 p->value = strdup(value);
151 if(device_config_next) {
152 device_config_next->next = p;
153 device_config_next = p;
156 device_config_next = p;
160 int args_callback_option(char*name,char*val) {
161 if (!strcmp(name, "o"))
166 else if (!strcmp(name, "v"))
169 setConsoleLogging(loglevel);
172 else if (!strcmp(name, "2"))
178 else if (!strcmp(name, "4"))
184 else if (!strcmp(name, "9"))
190 else if (!strcmp(name, "X"))
192 maxwidth = atoi(val);
195 else if (!strcmp(name, "Y"))
197 maxheight = atoi(val);
200 else if (!strcmp(name, "q"))
203 setConsoleLogging(loglevel);
207 else if (name[0]=='p')
209 /* check whether the page range follows the p directly, like
213 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
222 else if (!strcmp(name, "P"))
227 else if (!strcmp(name, "c"))
229 char*s = strdup(val);
230 char*x1 = strtok(s, ":");
231 char*y1 = strtok(0, ":");
232 char*x2 = strtok(0, ":");
233 char*y2 = strtok(0, ":");
234 if(!(x1 && y1 && x2 && y2)) {
235 fprintf(stderr, "-c option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
246 else if (!strcmp(name, "m"))
248 char*s = strdup(val);
249 char*c = strchr(s, ':');
251 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
261 else if (!strcmp(name, "s"))
264 char*c = strchr(s, '=');
265 if(c && *c && c[1]) {
268 store_parameter(s,c);
269 } else if(!strcmp(s,"help")) {
270 printf("PDF Parameters:\n");
271 gfxsource_t*pdf = gfxsource_pdf_create();
272 pdf->setparameter(pdf, "help", "");
274 gfxdevice_swf_init(&swf);
275 printf("SWF Parameters:\n");
276 swf.setparameter(&swf, "help", "");
279 store_parameter(s,"1");
283 else if (!strcmp(name, "S"))
285 store_parameter("drawonlyshapes", "1");
288 else if (!strcmp(name, "i"))
290 store_parameter("ignoredraworder", "1");
294 else if (!strcmp(name, "Q"))
296 max_time = atoi(val);
298 # ifdef HAVE_SIGNAL_H
299 signal(SIGALRM, sigalarm);
304 else if (!strcmp(name, "z"))
306 store_parameter("enablezlib", "1");
310 else if (!strcmp(name, "n"))
312 store_parameter("opennewwindow", "1");
315 else if (!strcmp(name, "I"))
320 else if (!strcmp(name, "t"))
322 store_parameter("insertstop", "1");
325 else if (!strcmp(name, "T"))
327 if(!strcasecmp(val, "mx"))
328 store_parameter("flashversion", "6");
330 store_parameter("flashversion", val);
334 else if (!strcmp(name, "f"))
336 store_parameter("storeallcharacters", "1");
337 store_parameter("extrafontdata", "1");
340 else if (!strcmp(name, "ff"))
343 // append this to the current filter expression (we allow more than one --filter)
344 int l = strlen(filters);
345 int new_len = l + strlen(val) + 2;
346 filters = (char*)realloc(filters, new_len);
348 strcpy(filters+l+1, val);
350 filters = strdup(val);
354 else if (!strcmp(name, "w"))
356 store_parameter("linksopennewwindow", "0");
359 else if (!strcmp(name, "O"))
363 if(val&& val[0] && val[1]==0 && isdigit(val[0])) {
368 store_parameter("poly2bitmap", "1");
370 store_parameter("bitmapfonts", "1");
372 store_parameter("ignoredraworder", "1");
375 else if (!strcmp(name, "G"))
377 //store_parameter("optimize_polygons", "1");
381 else if (!strcmp(name, "F"))
383 char *s = strdup(val);
385 while(l && s[l-1]=='/') {
389 fontpaths[fontpathpos++] = s;
392 else if (!strcmp(name, "l"))
395 sprintf(buf, "%s/default_loader.swf", SWFDIR);
396 preloader = strdup(buf);
399 else if (!strcmp(name, "b"))
402 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
403 viewer = strdup(buf);
406 else if (!strcmp(name, "L"))
414 systemf("ls %s/*_loader.swf", SWFDIR);
421 else if (!strcmp(name, "B"))
429 systemf("ls %s/*_viewer.swf", SWFDIR);
436 else if (!strcmp(name, "j"))
439 store_parameter("jpegquality", &name[1]);
442 store_parameter("jpegquality", val);
446 else if (!strcmp(name, "V"))
448 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
453 fprintf(stderr, "Unknown option: -%s\n", name);
459 static struct options_t options[] = {
468 {"j", "jpegquality"},
472 {"T", "flashversion"},
475 {"b", "defaultviewer"},
476 {"l", "defaultloader"},
490 int args_callback_longoption(char*name,char*val) {
491 return args_long2shortoption(options, name, val);
494 int args_callback_command(char*name, char*val) {
500 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
509 void args_callback_usage(char *name)
512 printf("Usage: %s [-options] file.pdf -o file.swf\n", name);
514 printf("-h , --help Print short help message and exit\n");
515 printf("-V , --version Print version info and exit\n");
516 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");
517 printf("-p , --pages range Convert only pages in range with range e.g. 1-20 or 1,4,6,9-11 or\n");
518 printf("-P , --password password Use password for deciphering the pdf.\n");
519 printf("-v , --verbose Be verbose. Use more than one -v for greater effect.\n");
520 printf("-z , --zlib Use Flash 6 (MX) zlib compression.\n");
521 printf("-i , --ignore Allows pdf2swf to change the draw order of the pdf. This may make the generated\n");
522 printf("-j , --jpegquality quality Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)\n");
523 printf("-s , --set param=value Set a SWF encoder specific parameter. See pdf2swf -s help for more information.\n");
524 printf("-w , --samewindow When converting pdf hyperlinks, don't make the links open a new window. \n");
525 printf("-t , --stop Insert a stop() command in each page. \n");
526 printf("-T , --flashversion num Set Flash Version in the SWF header to num.\n");
527 printf("-F , --fontdir directory Add directory to the font search path.\n");
528 printf("-b , --defaultviewer Link a standard viewer to the swf file. \n");
529 printf("-l , --defaultloader Link a standard preloader to the swf file which will be displayed while the main swf is loading.\n");
530 printf("-B , --viewer filename Link viewer filename to the swf file. \n");
531 printf("-L , --preloader filename Link preloader filename to the swf file. \n");
532 printf("-q , --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
533 printf("-S , --shapes Don't use SWF Fonts, but store everything as shape.\n");
534 printf("-f , --fonts Store full fonts in SWF. (Don't reduce to used characters).\n");
535 printf("-G , --flatten Remove as many clip layers from file as possible. \n");
536 printf("-I , --info Don't do actual conversion, just display a list of all pages in the PDF.\n");
537 printf("-Q , --maxtime n Abort conversion after n seconds. Only available on Unix.\n");
541 float getRate(char*filename)
545 fi = open(filename,O_RDONLY|O_BINARY);
548 sprintf(buffer, "Couldn't open %s", filename);
552 if(swf_ReadSWF(fi,&swf) < 0)
554 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
559 return swf.frameRate / 256.0;
562 void show_info(gfxsource_t*driver, char*filename)
564 gfxdocument_t* pdf = driver->open(driver, filename);
568 msg("<error> Couldn't open %s", filename);
572 fo = fopen(outputname, "wb");
574 perror(outputname);exit(1);;
580 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
582 gfxpage_t*page = pdf->getpage(pdf,pagenr);
583 if(is_in_range(pagenr, pagerange)) {
584 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
591 static gfxdevice_t swf,wrap,rescale;
592 gfxdevice_t*create_output_device()
594 gfxdevice_swf_init(&swf);
596 /* set up filter chain */
600 gfxdevice_removeclippings_init(&wrap, &swf);
604 if(maxwidth || maxheight) {
605 gfxdevice_rescale_init(&rescale, out, maxwidth, maxheight, 0);
610 gfxfilterchain_t*chain = gfxfilterchain_parse(filters);
612 fprintf(stderr, "Unable to parse filters: %s\n", filters);
615 out = gfxfilterchain_apply(chain, out);
616 gfxfilterchain_destroy(chain);
619 /* pass global parameters to output device */
620 parameter_t*p = device_config;
622 out->setparameter(out, p->name, p->value);
628 int main(int argn, char *argv[])
634 char t1searchpath[1024];
637 int one_file_per_page = 0;
639 initLog(0,-1,0,0,-1,loglevel);
641 /* not needed anymore since fonts are embedded
643 fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
654 processargs(argn, argv);
656 driver = gfxsource_pdf_create();
658 /* pass global parameters to PDF driver*/
659 parameter_t*p = device_config;
661 driver->setparameter(driver, p->name, p->value);
667 fprintf(stderr, "Please specify an input file\n");
675 outputname = stripFilename(filename, ".swf");
676 msg("<notice> Output filename not given. Writing to %s", outputname);
682 fprintf(stderr, "Please use -o to specify an output file\n");
687 // test if the page range is o.k.
688 is_in_range(0x7fffffff, pagerange);
691 args_callback_usage(argv[0]);
696 if(password && *password) {
697 sprintf(fullname, "%s|%s", filename, password);
702 driver->setparameter(driver, "pages", pagerange);
705 for(t=0;t<fontpathpos;t++) {
706 driver->setparameter(driver, "fontdir", fontpaths[t]);
710 show_info(driver, filename);
715 if((u = strchr(outputname, '%'))) {
716 if(strchr(u+1, '%') ||
717 strchr(outputname, '%')!=u) {
718 msg("<error> only one %% allowed in filename\n");
721 if(preloader || viewer) {
722 msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
725 msg("<notice> outputting one file per page");
726 one_file_per_page = 1;
727 char*pattern = (char*)malloc(strlen(outputname)+2);
728 /* convert % to %d */
729 int l = u-outputname+1;
730 memcpy(pattern, outputname, l);
732 strcpy(pattern+l+1, outputname+l);
733 outputname = pattern;
736 gfxdocument_t* pdf = driver->open(driver, filename);
738 msg("<error> Couldn't open %s", filename);
741 /* pass global parameters document */
744 pdf->setparameter(pdf, p->name, p->value);
758 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
760 if(is_in_range(pagenr, pagerange)) {
762 sprintf(mapping, "%d:%d", pagenr, frame);
763 pdf->setparameter(pdf, "pagemap", mapping);
766 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
771 if(pagerange && !pagenum && frame==1) {
772 fprintf(stderr, "No pages in range %s", pagerange);
778 gfxdevice_t*out = create_output_device();;
779 pdf->prepare(pdf, out);
781 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
783 if(is_in_range(pagenr, pagerange)) {
784 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
785 pages[pagenum].x = 0;
786 pages[pagenum].y = 0;
787 pages[pagenum].page = page;
790 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
793 int xmax[xnup], ymax[xnup];
795 int width=0, height=0;
797 memset(xmax, 0, xnup*sizeof(int));
798 memset(ymax, 0, ynup*sizeof(int));
801 for(x=0;x<xnup;x++) {
804 if(pages[t].page->width > xmax[x])
805 xmax[x] = (int)pages[t].page->width;
806 if(pages[t].page->height > ymax[y])
807 ymax[y] = (int)pages[t].page->height;
809 for(x=0;x<xnup;x++) {
813 for(y=0;y<ynup;y++) {
818 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
820 out->startpage(out,width,height);
822 for(t=0;t<pagenum;t++) {
825 int xpos = x>0?xmax[x-1]:0;
826 int ypos = y>0?ymax[y-1]:0;
827 msg("<verbose> Render (%d,%d) move:%d/%d\n",
828 (int)(pages[t].page->width + xpos),
829 (int)(pages[t].page->height + ypos), xpos, ypos);
830 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
831 custom_move? move_y : ypos,
832 custom_clip? clip_x1 : 0 + xpos,
833 custom_clip? clip_y1 : 0 + ypos,
834 custom_clip? clip_x2 : pages[t].page->width + xpos,
835 custom_clip? clip_y2 : pages[t].page->height + ypos);
838 for(t=0;t<pagenum;t++) {
839 pages[t].page->destroy(pages[t].page);
843 if(one_file_per_page) {
844 gfxresult_t*result = out->finish(out);out=0;
846 sprintf(buf, outputname, pagenr);
847 if(result->save(result, buf) < 0) {
850 result->destroy(result);result=0;
851 out = create_output_device();;
852 msg("<notice> Writing SWF file %s", buf);
857 if(one_file_per_page) {
858 // remove empty device
859 gfxresult_t*result = out->finish(out);out=0;
860 result->destroy(result);result=0;
862 gfxresult_t*result = out->finish(out);
863 msg("<notice> Writing SWF file %s", outputname);
864 if(result->save(result, outputname) < 0) {
867 int width = (int)(ptroff_t)result->get(result, "width");
868 int height = (int)(ptroff_t)result->get(result, "height");
869 result->destroy(result);result=0;
871 if(preloader || viewer) {
876 if(!preloader && viewer) {
877 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
878 viewer, outputname, outputname);
882 if(preloader && !viewer) {
883 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
884 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
885 SWFDIR, preloader, outputname, outputname);
889 if(preloader && viewer) {
891 char tmpname[] = "__swf__XXXXXX";
894 char*tmpname = "__tmp__.swf";
896 systemf("swfcombine \"%s\" viewport=%s -o %s",
897 viewer, outputname, tmpname);
898 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
899 getRate(preloader), SWFDIR, preloader, tmpname, outputname);
900 systemf("rm %s", tmpname);
906 driver->destroy(driver);
909 /* free global parameters */
912 parameter_t*next = p->next;
913 if(p->name) free((void*)p->name);p->name = 0;
914 if(p->value) free((void*)p->value);p->value =0;