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 */
27 #include "../config.h"
31 #ifdef HAVE_SYS_STAT_H
34 #include "../lib/args.h"
35 #include "../lib/os.h"
36 #include "../lib/rfxswf.h"
37 #include "../lib/devices/swf.h"
38 //#include "../lib/devices/render.h"
39 #include "SWFOutputDev.h"
42 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
44 static char * outputname = 0;
45 static int loglevel = 3;
46 static char * pagerange = 0;
47 static char * filename = 0;
48 static char * password = 0;
51 static char * preloader = 0;
52 static char * viewer = 0;
62 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
65 static int system_quiet=0;
67 int systemf(const char* format, ...)
72 va_start(arglist, format);
73 vsprintf(buf, format, arglist);
82 fprintf(stderr, "system() returned %d\n", ret);
88 int args_callback_option(char*name,char*val) {
89 if (!strcmp(name, "o"))
94 else if (!strcmp(name, "v"))
97 setConsoleLogging(loglevel);
100 else if (!strcmp(name, "2"))
106 else if (!strcmp(name, "4"))
112 else if (!strcmp(name, "9"))
118 else if (!strcmp(name, "q"))
121 setConsoleLogging(loglevel);
125 else if (name[0]=='p')
127 /* check whether the page range follows the p directly, like
131 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
140 else if (!strcmp(name, "P"))
145 else if (!strcmp(name, "c"))
147 char*s = strdup(val);
148 char*x1 = strtok(s, ":");
149 char*y1 = strtok(0, ":");
150 char*x2 = strtok(0, ":");
151 char*y2 = strtok(0, ":");
152 if(!(x1 && y1 && x2 && y2)) {
153 fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
164 else if (!strcmp(name, "m"))
166 char*s = strdup(val);
167 char*c = strchr(s, ':');
169 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
179 else if (!strcmp(name, "s"))
181 char*s = strdup(val);
182 char*c = strchr(s, '=');
183 if(c && *c && c[1]) {
186 pdfswf_setparameter(s,c);
189 pdfswf_setparameter(s,"1");
192 else if (!strcmp(name, "S"))
194 pdfswf_setparameter("drawonlyshapes", "1");
197 else if (!strcmp(name, "i"))
199 pdfswf_setparameter("ignoredraworder", "1");
202 else if (!strcmp(name, "z"))
204 pdfswf_setparameter("enablezlib", "1");
208 else if (!strcmp(name, "n"))
210 pdfswf_setparameter("opennewwindow", "1");
213 else if (!strcmp(name, "t"))
215 pdfswf_setparameter("insertstop", "1");
218 else if (!strcmp(name, "T"))
220 if(!strcasecmp(val, "mx"))
221 pdfswf_setparameter("flashversion", "6");
223 pdfswf_setparameter("flashversion", val);
227 else if (!strcmp(name, "f"))
229 pdfswf_setparameter("storeallcharacters", "1");
232 else if (!strcmp(name, "w"))
234 pdfswf_setparameter("linksopennewwindow", "0");
237 else if (!strcmp(name, "F"))
239 char *s = strdup(val);
241 while(l && s[l-1]=='/') {
245 fontpaths[fontpathpos++] = s;
248 else if (!strcmp(name, "l"))
251 sprintf(buf, "%s/default_loader.swf", SWFDIR);
252 preloader = strdup(buf);
255 else if (!strcmp(name, "b"))
258 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
259 viewer = strdup(buf);
262 else if (!strcmp(name, "L"))
270 systemf("ls %s/*_loader.swf", SWFDIR);
277 else if (!strcmp(name, "B"))
285 systemf("ls %s/*_viewer.swf", SWFDIR);
292 else if (!strcmp(name, "j"))
295 pdfswf_setparameter("jpegquality", &name[1]);
298 pdfswf_setparameter("jpegquality", val);
302 else if (!strcmp(name, "V"))
304 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
309 fprintf(stderr, "Unknown option: -%s\n", name);
315 /*struct docoptions_t options[] =
316 {{"o","output","filename::Specify output file"},
317 {"V","version","Print program version"},
318 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
319 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
320 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
321 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
322 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
323 {"w","samewindow","Don't open a new browser window for links in the SWF"},
324 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
325 {"F","fontpath","path::Add directory to font search path"},
326 {"B","viewer","name::Link viewer \"name\" to the pdf"},
327 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
328 {"b","defaultviewer","Link default viewer to the pdf"},
329 {"l","defaultpreloader","Link default preloader to the pdf"}
332 struct options_t options[] =
347 {"b","defaultviewer"},
348 {"l","defaultpreloader"},
350 {"T","flashversion"},
354 int args_callback_longoption(char*name,char*val) {
355 return args_long2shortoption(options, name, val);
358 int args_callback_command(char*name, char*val) {
364 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
373 void args_callback_usage(char*name)
375 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
376 printf("\nBasic options:\n");
377 printf("-p --pages=range Convert only pages in range\n");
378 printf("-P --password=password Use password for deciphering the pdf\n");
379 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
380 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
382 printf("-F --fontdir directory Add directory to font search path\n");
384 printf("-V --version Print program version\n");
385 printf("\nEnhanced conversion options:\n");
386 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
387 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
388 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
389 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
390 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
391 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
392 printf("-s zoom=factor Scale result, default: 72\n");
393 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
394 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
395 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
396 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
397 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
398 " graphic errors)\n");
399 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
400 " other out. (Needed for some Powerpoint PDFs)\n");
401 //deliberately undocumented (for now)
402 //printf("-2 Put 2 pages into each frame.\n");
403 //printf("-4 Put 4 pages into each frame.\n");
404 printf("Postprocessing options:\n");
405 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
406 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
407 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
408 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
411 float getRate(char*filename)
415 fi = open(filename,O_RDONLY|O_BINARY);
418 sprintf(buffer, "Couldn't open %s", filename);
422 if(swf_ReadSWF(fi,&swf) < 0)
424 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
429 return swf.frameRate / 256.0;
432 int main(int argn, char *argv[])
438 char t1searchpath[1024];
441 char* installPath = getInstallationPath();
444 initLog(0,-1,0,0,-1,loglevel);
446 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
448 fontdir = concatPaths(getInstallationPath(), "fonts");
449 FILE*test = fopen(concatPaths(fontdir,"\\d050000l.afm"), "rb");
451 fprintf(stderr, "Couldn't find file %s - pdf2swf not installed properly? OS says:\n", concatPaths(fontdir, "\\d050000l.afm"));
466 processargs(argn, argv);
470 fprintf(stderr, "Please specify an input file\n");
477 outputname = stripFilename(filename, ".swf");
478 msg("<notice> Output filename not given. Writing to %s", outputname);
484 fprintf(stderr, "Please use -o to specify an output file\n");
488 // test if the page range is o.k.
489 is_in_range(0x7fffffff, pagerange);
492 args_callback_usage(argv[0]);
498 pdfswf_addfontdir(fontdir);
500 for(t=0;t<fontpathpos;t++) {
501 pdfswf_addfontdir(fontpaths[t]);
504 pdf_doc_t* pdf = pdf_init(filename, password);
506 msg("<error> Couldn't open %s", filename);
511 gfxdevice_swf_init(&swfdev);
512 //gfxdevice_render_init(&swfdev);
513 dev_output_t* swf = dev_output_init(&swfdev);
519 pdf_page_info_t*info;
525 for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
527 if(is_in_range(pagenr, pagerange)) {
528 dev_output_preparepage(swf, pagenr, frame);
531 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
539 for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
541 if(is_in_range(pagenr, pagerange)) {
542 pdf_page_t* page = pages[pagenum].page = pdf_getpage(pdf, pagenr);
543 pdf_page_info_t* info = pdf_page_getinfo(page);
544 pages[pagenum].x = 0;
545 pages[pagenum].y = 0;
546 pages[pagenum].info = info;
547 pages[pagenum].page = page;
550 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
553 int xmax[xnup], ymax[xnup];
555 int width=0, height=0;
557 memset(xmax, 0, xnup*sizeof(int));
558 memset(ymax, 0, ynup*sizeof(int));
561 for(x=0;x<xnup;x++) {
564 if(pages[t].info->xMax-pages[t].info->xMin > xmax[x])
565 xmax[x] = pages[t].info->xMax-pages[t].info->xMin;
566 if(pages[t].info->yMax-pages[t].info->yMin > ymax[y])
567 ymax[y] = pages[t].info->yMax-pages[t].info->yMin;
569 for(x=0;x<xnup;x++) {
573 for(y=0;y<ynup;y++) {
578 dev_output_startframe(swf, clip_x2 - clip_x1, clip_y2 - clip_y1);
580 dev_output_startframe(swf, width, height);
582 for(t=0;t<pagenum;t++) {
585 int xpos = x>0?xmax[x-1]:0;
586 int ypos = y>0?ymax[y-1]:0;
587 pdf_page_info_t*info = pages[t].info;
590 msg("<verbose> Render (%d,%d)-(%d,%d) move:%d/%d\n",
594 info->yMax + ypos, xpos, ypos);
595 pdf_page_rendersection(pages[t].page, swf, custom_move? move_x : xpos,
596 custom_move? move_y : ypos,
597 custom_clip? clip_x1 : info->xMin + xpos,
598 custom_clip? clip_y1 : info->yMin + ypos,
599 custom_clip? clip_x2 : info->xMax + xpos,
600 custom_clip? clip_y2 : info->yMax + ypos);
602 dev_output_endframe(swf);
603 for(t=0;t<pagenum;t++) {
604 pdf_page_info_destroy(pages[t].info);
605 pdf_page_destroy(pages[t].page);
610 dev_output_finish(swf);
612 gfxresult_t*result = swfdev.finish(&swfdev);
613 if(result->save(result, outputname) < 0) {
617 int width = (int)result->get(result, "width");
618 int height = (int)result->get(result, "height");
619 msg("<notice> SWF written");
621 result->destroy(result);
623 dev_output_destroy(swf);
630 #undef SYSTEM_BACKTICKS
631 if(viewer && !preloader) {
632 #ifdef SYSTEM_BACKTICKS
633 systemf("swfcombine %s `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",zip,
634 viewer, outputname, outputname);
636 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
637 viewer, outputname, outputname);
642 if(preloader && !viewer) {
643 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
644 #ifdef SYSTEM_BACKTICKS
645 ret = systemf("swfcombine %s `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,
646 preloader, SWFDIR, preloader, outputname, outputname);
648 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
649 SWFDIR, preloader, outputname, outputname);
654 if(preloader && viewer) {
655 systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
656 viewer, outputname, outputname);
657 #ifdef SYSTEM_BACKTICKS
658 systemf("swfcombine %s `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,
659 outputname, preloader, SWFDIR, preloader, outputname);
661 /* TODO: read out rate */
662 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,width,height,
663 getRate(preloader), preloader, SWFDIR, preloader, outputname);
665 systemf("rm __tmp__.swf");