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");
647 outputname = stripFilename(filename, ".swf");
648 msg("<notice> Output filename not given. Writing to %s", outputname);
654 fprintf(stderr, "Please use -o to specify an output file\n");
659 // test if the page range is o.k.
660 is_in_range(0x7fffffff, pagerange);
663 args_callback_usage(argv[0]);
668 if(password && *password) {
669 sprintf(fullname, "%s|%s", filename, password);
674 driver->set_parameter(driver, "pages", pagerange);
677 show_info(driver, filename);
682 for(t=0;t<fontpathpos;t++) {
683 driver->set_parameter(driver, "fontdir", fontpaths[t]);
687 if((u = strchr(outputname, '%'))) {
688 if(strchr(u+1, '%') ||
689 strchr(outputname, '%')!=u) {
690 msg("<error> only one %% allowed in filename\n");
693 if(preloader || viewer) {
694 msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
697 msg("<notice> outputting one file per page");
698 one_file_per_page = 1;
699 char*pattern = (char*)malloc(strlen(outputname)+2);
700 /* convert % to %d */
701 int l = u-outputname+1;
702 memcpy(pattern, outputname, l);
704 strcpy(pattern+l+1, outputname+l);
705 outputname = pattern;
708 gfxdocument_t* pdf = driver->open(driver, filename);
710 msg("<error> Couldn't open %s", filename);
713 /* pass global parameters document */
716 pdf->set_parameter(pdf, p->name, p->value);
730 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
732 if(is_in_range(pagenr, pagerange)) {
734 sprintf(mapping, "%d:%d", pagenr, frame);
735 pdf->set_parameter(pdf, "pagemap", mapping);
738 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
743 if(pagerange && !pagenum && frame==1) {
744 fprintf(stderr, "No pages in range %s", pagerange);
750 gfxdevice_t*out = create_output_device();;
751 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
753 if(is_in_range(pagenr, pagerange)) {
754 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
755 pages[pagenum].x = 0;
756 pages[pagenum].y = 0;
757 pages[pagenum].page = page;
760 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
763 int xmax[xnup], ymax[xnup];
765 int width=0, height=0;
767 memset(xmax, 0, xnup*sizeof(int));
768 memset(ymax, 0, ynup*sizeof(int));
771 for(x=0;x<xnup;x++) {
774 if(pages[t].page->width > xmax[x])
775 xmax[x] = (int)pages[t].page->width;
776 if(pages[t].page->height > ymax[y])
777 ymax[y] = (int)pages[t].page->height;
779 for(x=0;x<xnup;x++) {
783 for(y=0;y<ynup;y++) {
788 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
790 out->startpage(out,width,height);
792 for(t=0;t<pagenum;t++) {
795 int xpos = x>0?xmax[x-1]:0;
796 int ypos = y>0?ymax[y-1]:0;
797 msg("<verbose> Render (%d,%d) move:%d/%d\n",
798 (int)(pages[t].page->width + xpos),
799 (int)(pages[t].page->height + ypos), xpos, ypos);
800 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
801 custom_move? move_y : ypos,
802 custom_clip? clip_x1 : 0 + xpos,
803 custom_clip? clip_y1 : 0 + ypos,
804 custom_clip? clip_x2 : pages[t].page->width + xpos,
805 custom_clip? clip_y2 : pages[t].page->height + ypos);
808 for(t=0;t<pagenum;t++) {
809 pages[t].page->destroy(pages[t].page);
813 if(one_file_per_page) {
814 gfxresult_t*result = out->finish(out);out=0;
816 sprintf(buf, outputname, pagenr);
817 if(result->save(result, buf) < 0) {
820 result->destroy(result);result=0;
821 out = create_output_device();;
822 msg("<notice> Writing SWF file %s", buf);
827 if(one_file_per_page) {
828 // remove empty device
829 gfxresult_t*result = out->finish(out);out=0;
830 result->destroy(result);result=0;
832 gfxresult_t*result = out->finish(out);
833 msg("<notice> Writing SWF file %s", outputname);
834 if(result->save(result, outputname) < 0) {
837 int width = (int)(ptroff_t)result->get(result, "width");
838 int height = (int)(ptroff_t)result->get(result, "height");
839 result->destroy(result);result=0;
841 if(preloader || viewer) {
846 if(!preloader && viewer) {
847 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
848 viewer, outputname, outputname);
852 if(preloader && !viewer) {
853 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
854 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
855 SWFDIR, preloader, outputname, outputname);
859 if(preloader && viewer) {
861 char tmpname[] = "__swf__XXXXXX";
864 char*tmpname = "__tmp__.swf";
866 systemf("swfcombine \"%s\" viewport=%s -o %s",
867 viewer, outputname, tmpname);
868 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
869 getRate(preloader), SWFDIR, preloader, tmpname, outputname);
870 systemf("rm %s", tmpname);
876 driver->destroy(driver);
879 /* free global parameters */
882 parameter_t*next = p->next;
883 if(p->name) free((void*)p->name);p->name = 0;
884 if(p->value) free((void*)p->value);p->value =0;