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 int maxwidth=0, maxheight=0;
55 static char * outputname = 0;
56 static int loglevel = 3;
57 static char * pagerange = 0;
58 static char * filename = 0;
59 static char * password = 0;
62 static char * preloader = 0;
63 static char * viewer = 0;
67 static int info_only = 0;
69 static int max_time = 0;
71 static int flatten = 0;
79 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
82 static int system_quiet=0;
84 int systemf(const char* format, ...)
89 va_start(arglist, format);
90 vsprintf(buf, format, arglist);
99 fprintf(stderr, "system() returned %d\n", ret);
106 void sigalarm(int signal)
108 msg("<fatal> Aborting rendering after %d seconds", max_time);
109 #if 0 && defined(HAVE_SYS_TIME_H) && defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRUSAGE)
111 getrusage(RUSAGE_CHILDREN, &usage);
112 msg("<fatal> Memory used: %d,%d,%d", usage.ru_maxrss, usage.ru_idrss, usage.ru_isrss);
114 #if defined(HAVE_MALLINFO) && defined(HAVE_MALLOC_H)
115 struct mallinfo info = mallinfo();
116 msg("<fatal> Memory used: %d Mb (%d bytes)", info.uordblks/1048576, info.uordblks);
122 typedef struct _parameter {
123 struct _parameter*next;
128 static parameter_t* device_config = 0;
129 static parameter_t* device_config_next = 0;
130 static void store_parameter(const char*name, const char*value)
132 parameter_t*o = device_config;
134 if(!strcmp(name, o->name)) {
135 /* overwrite old value */
136 free((void*)o->value);
137 o->value = strdup(value);
142 parameter_t*p = (parameter_t*)malloc(sizeof(parameter_t));
143 p->name = strdup(name);
144 p->value = strdup(value);
147 if(device_config_next) {
148 device_config_next->next = p;
149 device_config_next = p;
152 device_config_next = p;
156 int args_callback_option(char*name,char*val) {
157 if (!strcmp(name, "o"))
162 else if (!strcmp(name, "v"))
165 setConsoleLogging(loglevel);
168 else if (!strcmp(name, "2"))
174 else if (!strcmp(name, "4"))
180 else if (!strcmp(name, "9"))
186 else if (!strcmp(name, "X"))
188 maxwidth = atoi(value);
191 else if (!strcmp(name, "Y"))
193 maxheight = atoi(value);
196 else if (!strcmp(name, "q"))
199 setConsoleLogging(loglevel);
203 else if (name[0]=='p')
205 /* check whether the page range follows the p directly, like
209 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
218 else if (!strcmp(name, "P"))
223 else if (!strcmp(name, "c"))
225 char*s = strdup(val);
226 char*x1 = strtok(s, ":");
227 char*y1 = strtok(0, ":");
228 char*x2 = strtok(0, ":");
229 char*y2 = strtok(0, ":");
230 if(!(x1 && y1 && x2 && y2)) {
231 fprintf(stderr, "-c option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
242 else if (!strcmp(name, "m"))
244 char*s = strdup(val);
245 char*c = strchr(s, ':');
247 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
257 else if (!strcmp(name, "s"))
259 char*s = strdup(val);
260 char*c = strchr(s, '=');
261 if(c && *c && c[1]) {
264 store_parameter(s,c);
265 } else if(!strcmp(s,"help")) {
266 printf("PDF Parameters:\n");
267 gfxsource_t*pdf = gfxsource_pdf_create();
268 pdf->set_parameter(pdf, "help", "");
270 gfxdevice_swf_init(&swf);
271 printf("SWF Parameters:\n");
272 swf.setparameter(&swf, "help", "");
275 store_parameter(s,"1");
279 else if (!strcmp(name, "S"))
281 store_parameter("drawonlyshapes", "1");
284 else if (!strcmp(name, "i"))
286 store_parameter("ignoredraworder", "1");
290 else if (!strcmp(name, "Q"))
292 max_time = atoi(val);
294 # ifdef HAVE_SIGNAL_H
295 signal(SIGALRM, sigalarm);
300 else if (!strcmp(name, "z"))
302 store_parameter("enablezlib", "1");
306 else if (!strcmp(name, "n"))
308 store_parameter("opennewwindow", "1");
311 else if (!strcmp(name, "I"))
316 else if (!strcmp(name, "t"))
318 store_parameter("insertstop", "1");
321 else if (!strcmp(name, "T"))
323 if(!strcasecmp(val, "mx"))
324 store_parameter("flashversion", "6");
326 store_parameter("flashversion", val);
330 else if (!strcmp(name, "f"))
332 store_parameter("storeallcharacters", "1");
333 store_parameter("extrafontdata", "1");
336 else if (!strcmp(name, "w"))
338 store_parameter("linksopennewwindow", "0");
341 else if (!strcmp(name, "O"))
345 if(val&& val[0] && val[1]==0 && isdigit(val[0])) {
350 store_parameter("poly2bitmap", "1");
352 store_parameter("bitmapfonts", "1");
354 store_parameter("ignoredraworder", "1");
357 else if (!strcmp(name, "G"))
359 //store_parameter("optimize_polygons", "1");
363 else if (!strcmp(name, "F"))
365 char *s = strdup(val);
367 while(l && s[l-1]=='/') {
371 fontpaths[fontpathpos++] = s;
374 else if (!strcmp(name, "l"))
377 sprintf(buf, "%s/default_loader.swf", SWFDIR);
378 preloader = strdup(buf);
381 else if (!strcmp(name, "b"))
384 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
385 viewer = strdup(buf);
388 else if (!strcmp(name, "L"))
396 systemf("ls %s/*_loader.swf", SWFDIR);
403 else if (!strcmp(name, "B"))
411 systemf("ls %s/*_viewer.swf", SWFDIR);
418 else if (!strcmp(name, "j"))
421 store_parameter("jpegquality", &name[1]);
424 store_parameter("jpegquality", val);
428 else if (!strcmp(name, "V"))
430 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
435 fprintf(stderr, "Unknown option: -%s\n", name);
441 /*struct docoptions_t options[] =
442 {{"o","output","filename::Specify output file"},
443 {"V","version","Print program version"},
444 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
445 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
446 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
447 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
448 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
449 {"w","samewindow","Don't open a new browser window for links in the SWF"},
450 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
451 {"F","fontpath","path::Add directory to font search path"},
452 {"B","viewer","name::Link viewer \"name\" to the pdf"},
453 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
454 {"b","defaultviewer","Link default viewer to the pdf"},
455 {"l","defaultpreloader","Link default preloader to the pdf"}
458 static struct options_t options[] = {
467 {"j", "jpegquality"},
471 {"T", "flashversion"},
473 {"b", "defaultviewer"},
474 {"l", "defaultloader"},
488 int args_callback_longoption(char*name,char*val) {
489 return args_long2shortoption(options, name, val);
492 int args_callback_command(char*name, char*val) {
498 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
507 void args_callback_usage(char *name)
510 printf("Usage: %s [-options] file.pdf -o file.swf\n", name);
512 printf("-h , --help Print short help message and exit\n");
513 printf("-V , --version Print version info and exit\n");
514 printf("-o , --output file.swf Direct output to file.swf. If file.swf contains '%d' (file%d.swf), then each page \n");
515 printf("-p , --pages range Convert only pages in range with range e.g. 1-20 or 1,4,6,9-11 or\n");
516 printf("-P , --password password Use password for deciphering the pdf.\n");
517 printf("-v , --verbose Be verbose. Use more than one -v for greater effect.\n");
518 printf("-z , --zlib Use Flash 6 (MX) zlib compression.\n");
519 printf("-i , --ignore Allows pdf2swf to change the draw order of the pdf. This may make the generated\n");
520 printf("-j , --jpegquality quality Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)\n");
521 printf("-s , --set param=value Set a SWF encoder specific parameter. See pdf2swf -s help for more information.\n");
522 printf("-w , --samewindow When converting pdf hyperlinks, don't make the links open a new window. \n");
523 printf("-t , --stop Insert a stop() command in each page. \n");
524 printf("-T , --flashversion num Set Flash Version in the SWF header to num.\n");
525 printf("-F , --fontdir directory Add directory to the font search path.\n");
526 printf("-b , --defaultviewer Link a standard viewer to the swf file. \n");
527 printf("-l , --defaultloader Link a standard preloader to the swf file which will be displayed while the main swf is loading.\n");
528 printf("-B , --viewer filename Link viewer filename to the swf file. \n");
529 printf("-L , --preloader filename Link preloader filename to the swf file. \n");
530 printf("-q , --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
531 printf("-S , --shapes Don't use SWF Fonts, but store everything as shape.\n");
532 printf("-f , --fonts Store full fonts in SWF. (Don't reduce to used characters).\n");
533 printf("-G , --flatten Remove as many clip layers from file as possible. \n");
534 printf("-I , --info Don't do actual conversion, just display a list of all pages in the PDF.\n");
535 printf("-Q , --maxtime n Abort conversion after n seconds. Only available on Unix.\n");
539 float getRate(char*filename)
543 fi = open(filename,O_RDONLY|O_BINARY);
546 sprintf(buffer, "Couldn't open %s", filename);
550 if(swf_ReadSWF(fi,&swf) < 0)
552 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
557 return swf.frameRate / 256.0;
560 void show_info(gfxsource_t*driver, char*filename)
562 gfxdocument_t* pdf = driver->open(driver, filename);
566 msg("<error> Couldn't open %s", filename);
570 fo = fopen(outputname, "wb");
572 perror(outputname);exit(1);;
578 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
580 gfxpage_t*page = pdf->getpage(pdf,pagenr);
581 if(is_in_range(pagenr, pagerange)) {
582 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
589 static gfxdevice_t swf,wrap,rescale;
590 gfxdevice_t*create_output_device()
592 gfxdevice_swf_init(&swf);
594 /* set up filter chain */
598 gfxdevice_removeclippings_init(&wrap, &swf);
602 if(maxwidth || maxheight) {
603 gfxdevice_rescale_init(&rescale, out, maxwidth, maxheight);
607 /* pass global parameters to output device */
608 parameter_t*p = device_config;
610 out->setparameter(out, p->name, p->value);
616 int main(int argn, char *argv[])
622 char t1searchpath[1024];
625 char* installPath = getInstallationPath();
626 int one_file_per_page = 0;
628 initLog(0,-1,0,0,-1,loglevel);
630 /* not needed anymore since fonts are embedded
632 fontpaths[fontpathpos++] = concatPaths(installPath, "fonts");
643 processargs(argn, argv);
645 driver = gfxsource_pdf_create();
647 /* pass global parameters to PDF driver*/
648 parameter_t*p = device_config;
650 driver->set_parameter(driver, p->name, p->value);
656 fprintf(stderr, "Please specify an input file\n");
661 show_info(driver, filename);
668 outputname = stripFilename(filename, ".swf");
669 msg("<notice> Output filename not given. Writing to %s", outputname);
675 fprintf(stderr, "Please use -o to specify an output file\n");
679 // test if the page range is o.k.
680 is_in_range(0x7fffffff, pagerange);
683 driver->set_parameter(driver, "pages", pagerange);
686 args_callback_usage(argv[0]);
691 for(t=0;t<fontpathpos;t++) {
692 driver->set_parameter(driver, "fontdir", fontpaths[t]);
696 if(password && *password) {
697 sprintf(fullname, "%s|%s", filename, password);
702 if((u = strchr(outputname, '%'))) {
703 if(strchr(u+1, '%') ||
704 strchr(outputname, '%')!=u) {
705 msg("<error> only one %% allowed in filename\n");
708 if(preloader || viewer) {
709 msg("<error> -b/-l/-B/-L not supported together with %% in filename\n");
712 msg("<notice> outputting one file per page");
713 one_file_per_page = 1;
714 char*pattern = (char*)malloc(strlen(outputname)+2);
715 /* convert % to %d */
716 int l = u-outputname+1;
717 memcpy(pattern, outputname, l);
719 strcpy(pattern+l+1, outputname+l);
720 outputname = pattern;
723 gfxdocument_t* pdf = driver->open(driver, filename);
725 msg("<error> Couldn't open %s", filename);
728 /* pass global parameters document */
731 pdf->set_parameter(pdf, p->name, p->value);
745 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
747 if(is_in_range(pagenr, pagerange)) {
749 sprintf(mapping, "%d:%d", pagenr, frame);
750 pdf->set_parameter(pdf, "pagemap", mapping);
753 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
761 gfxdevice_t*out = create_output_device();;
762 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
764 if(is_in_range(pagenr, pagerange)) {
765 gfxpage_t* page = pages[pagenum].page = pdf->getpage(pdf, pagenr);
766 pages[pagenum].x = 0;
767 pages[pagenum].y = 0;
768 pages[pagenum].page = page;
771 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
774 int xmax[xnup], ymax[xnup];
776 int width=0, height=0;
778 memset(xmax, 0, xnup*sizeof(int));
779 memset(ymax, 0, ynup*sizeof(int));
782 for(x=0;x<xnup;x++) {
785 if(pages[t].page->width > xmax[x])
786 xmax[x] = (int)pages[t].page->width;
787 if(pages[t].page->height > ymax[y])
788 ymax[y] = (int)pages[t].page->height;
790 for(x=0;x<xnup;x++) {
794 for(y=0;y<ynup;y++) {
799 out->startpage(out,clip_x2 - clip_x1, clip_y2 - clip_y1);
801 out->startpage(out,width,height);
803 for(t=0;t<pagenum;t++) {
806 int xpos = x>0?xmax[x-1]:0;
807 int ypos = y>0?ymax[y-1]:0;
808 msg("<verbose> Render (%d,%d) move:%d/%d\n",
809 (int)(pages[t].page->width + xpos),
810 (int)(pages[t].page->height + ypos), xpos, ypos);
811 pages[t].page->rendersection(pages[t].page, out, custom_move? move_x : xpos,
812 custom_move? move_y : ypos,
813 custom_clip? clip_x1 : 0 + xpos,
814 custom_clip? clip_y1 : 0 + ypos,
815 custom_clip? clip_x2 : pages[t].page->width + xpos,
816 custom_clip? clip_y2 : pages[t].page->height + ypos);
819 for(t=0;t<pagenum;t++) {
820 pages[t].page->destroy(pages[t].page);
824 if(one_file_per_page) {
825 gfxresult_t*result = out->finish(out);out=0;
827 sprintf(buf, outputname, one_file_per_page++);
828 if(result->save(result, buf) < 0) {
831 result->destroy(result);result=0;
832 out = create_output_device();;
833 msg("<notice> Writing SWF file %s", buf);
838 if(one_file_per_page) {
839 // remove empty device
840 gfxresult_t*result = out->finish(out);out=0;
841 result->destroy(result);result=0;
843 gfxresult_t*result = out->finish(out);
844 msg("<notice> Writing SWF file %s", outputname);
845 if(result->save(result, outputname) < 0) {
848 int width = (int)result->get(result, "width");
849 int height = (int)result->get(result, "height");
850 result->destroy(result);result=0;
852 if(preloader || viewer) {
857 if(!preloader && viewer) {
858 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
859 viewer, outputname, outputname);
863 if(preloader && !viewer) {
864 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
865 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
866 SWFDIR, preloader, outputname, outputname);
870 if(preloader && viewer) {
872 char tmpname[] = "__swf__XXXXXX";
875 char*tmpname = "__tmp__.swf";
877 systemf("swfcombine \"%s\" viewport=%s -o %s",
878 viewer, outputname, tmpname);
879 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=%s -o \"%s\"",zip,width,height,
880 getRate(preloader), SWFDIR, preloader, tmpname, outputname);
881 systemf("rm %s", tmpname);
887 driver->destroy(driver);
890 /* free global parameters */
893 parameter_t*next = p->next;
894 if(p->name) free((void*)p->name);p->name = 0;
895 if(p->value) free((void*)p->value);p->value =0;