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 vsprintf(buf, 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 /*struct docoptions_t options[] =
443 {{"o","output","filename::Specify output file"},
444 {"V","version","Print program version"},
445 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
446 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
447 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
448 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
449 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
450 {"w","samewindow","Don't open a new browser window for links in the SWF"},
451 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
452 {"F","fontpath","path::Add directory to font search path"},
453 {"B","viewer","name::Link viewer \"name\" to the pdf"},
454 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
455 {"b","defaultviewer","Link default viewer to the pdf"},
456 {"l","defaultpreloader","Link default preloader to the pdf"}
459 static struct options_t options[] = {
468 {"j", "jpegquality"},
472 {"T", "flashversion"},
474 {"b", "defaultviewer"},
475 {"l", "defaultloader"},
489 int args_callback_longoption(char*name,char*val) {
490 return args_long2shortoption(options, name, val);
493 int args_callback_command(char*name, char*val) {
499 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
508 void args_callback_usage(char *name)
511 printf("Usage: %s [-options] file.pdf -o file.swf\n", name);
513 printf("-h , --help Print short help message and exit\n");
514 printf("-V , --version Print version info and exit\n");
515 printf("-o , --output file.swf Direct output to file.swf. If file.swf contains '%d' (file%d.swf), then each page \n");
516 printf("-p , --pages range Convert only pages in range with range e.g. 1-20 or 1,4,6,9-11 or\n");
517 printf("-P , --password password Use password for deciphering the pdf.\n");
518 printf("-v , --verbose Be verbose. Use more than one -v for greater effect.\n");
519 printf("-z , --zlib Use Flash 6 (MX) zlib compression.\n");
520 printf("-i , --ignore Allows pdf2swf to change the draw order of the pdf. This may make the generated\n");
521 printf("-j , --jpegquality quality Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)\n");
522 printf("-s , --set param=value Set a SWF encoder specific parameter. See pdf2swf -s help for more information.\n");
523 printf("-w , --samewindow When converting pdf hyperlinks, don't make the links open a new window. \n");
524 printf("-t , --stop Insert a stop() command in each page. \n");
525 printf("-T , --flashversion num Set Flash Version in the SWF header to num.\n");
526 printf("-F , --fontdir directory Add directory to the font search path.\n");
527 printf("-b , --defaultviewer Link a standard viewer to the swf file. \n");
528 printf("-l , --defaultloader Link a standard preloader to the swf file which will be displayed while the main swf is loading.\n");
529 printf("-B , --viewer filename Link viewer filename to the swf file. \n");
530 printf("-L , --preloader filename Link preloader filename to the swf file. \n");
531 printf("-q , --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
532 printf("-S , --shapes Don't use SWF Fonts, but store everything as shape.\n");
533 printf("-f , --fonts Store full fonts in SWF. (Don't reduce to used characters).\n");
534 printf("-G , --flatten Remove as many clip layers from file as possible. \n");
535 printf("-I , --info Don't do actual conversion, just display a list of all pages in the PDF.\n");
536 printf("-Q , --maxtime n Abort conversion after n seconds. Only available on Unix.\n");
540 float getRate(char*filename)
544 fi = open(filename,O_RDONLY|O_BINARY);
547 sprintf(buffer, "Couldn't open %s", filename);
551 if(swf_ReadSWF(fi,&swf) < 0)
553 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
558 return swf.frameRate / 256.0;
561 void show_info(gfxsource_t*driver, char*filename)
563 gfxdocument_t* pdf = driver->open(driver, filename);
567 msg("<error> Couldn't open %s", filename);
571 fo = fopen(outputname, "wb");
573 perror(outputname);exit(1);;
579 for(pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
581 gfxpage_t*page = pdf->getpage(pdf,pagenr);
582 if(is_in_range(pagenr, pagerange)) {
583 fprintf(fo, "page=%d width=%.2f height=%.2f\n", pagenr, page->width, page->height);
590 static gfxdevice_t swf,wrap,rescale;
591 gfxdevice_t*create_output_device()
593 gfxdevice_swf_init(&swf);
595 /* set up filter chain */
599 gfxdevice_removeclippings_init(&wrap, &swf);
603 if(maxwidth || maxheight) {
604 gfxdevice_rescale_init(&rescale, out, maxwidth, maxheight, 0);
608 /* pass global parameters to output device */
609 parameter_t*p = device_config;
611 out->setparameter(out, p->name, p->value);
617 int main(int argn, char *argv[])
623 char t1searchpath[1024];
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, pagenr);
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;