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 FONTDIR concatPaths(getInstallationPath(), "fonts")
43 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
45 static char * outputname = 0;
46 static int loglevel = 3;
47 static char * pagerange = 0;
48 static char * filename = 0;
49 static char * password = 0;
52 static char * preloader = 0;
53 static char * viewer = 0;
63 int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
66 static int system_quiet=0;
68 int systemf(const char* format, ...)
73 va_start(arglist, format);
74 vsprintf(buf, format, arglist);
83 fprintf(stderr, "system() returned %d\n", ret);
89 int args_callback_option(char*name,char*val) {
90 if (!strcmp(name, "o"))
95 else if (!strcmp(name, "v"))
98 setConsoleLogging(loglevel);
101 else if (!strcmp(name, "2"))
107 else if (!strcmp(name, "4"))
113 else if (!strcmp(name, "9"))
119 else if (!strcmp(name, "q"))
122 setConsoleLogging(loglevel);
126 else if (name[0]=='p')
128 /* check whether the page range follows the p directly, like
132 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
141 else if (!strcmp(name, "P"))
146 else if (!strcmp(name, "c"))
148 char*s = strdup(val);
149 char*x1 = strtok(s, ":");
150 char*y1 = strtok(0, ":");
151 char*x2 = strtok(0, ":");
152 char*y2 = strtok(0, ":");
153 if(!(x1 && y1 && x2 && y2)) {
154 fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
165 else if (!strcmp(name, "m"))
167 char*s = strdup(val);
168 char*c = strchr(s, ':');
170 fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
180 else if (!strcmp(name, "s"))
182 char*s = strdup(val);
183 char*c = strchr(s, '=');
184 if(c && *c && c[1]) {
187 pdfswf_setparameter(s,c);
190 pdfswf_setparameter(s,"1");
193 else if (!strcmp(name, "S"))
195 pdfswf_setparameter("drawonlyshapes", "1");
198 else if (!strcmp(name, "i"))
200 pdfswf_setparameter("ignoredraworder", "1");
203 else if (!strcmp(name, "z"))
205 pdfswf_setparameter("enablezlib", "1");
209 else if (!strcmp(name, "n"))
211 pdfswf_setparameter("opennewwindow", "1");
214 else if (!strcmp(name, "t"))
216 pdfswf_setparameter("insertstop", "1");
219 else if (!strcmp(name, "T"))
221 if(!strcasecmp(val, "mx"))
222 pdfswf_setparameter("flashversion", "6");
224 pdfswf_setparameter("flashversion", val);
228 else if (!strcmp(name, "f"))
230 pdfswf_setparameter("storeallcharacters", "1");
233 else if (!strcmp(name, "w"))
235 pdfswf_setparameter("linksopennewwindow", "0");
238 else if (!strcmp(name, "F"))
240 char *s = strdup(val);
242 while(l && s[l-1]=='/') {
246 fontpaths[fontpathpos++] = s;
249 else if (!strcmp(name, "l"))
252 sprintf(buf, "%s/default_loader.swf", SWFDIR);
253 preloader = strdup(buf);
256 else if (!strcmp(name, "b"))
259 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
260 viewer = strdup(buf);
263 else if (!strcmp(name, "L"))
271 systemf("ls %s/*_loader.swf", SWFDIR);
278 else if (!strcmp(name, "B"))
286 systemf("ls %s/*_viewer.swf", SWFDIR);
293 else if (!strcmp(name, "j"))
296 pdfswf_setparameter("jpegquality", &name[1]);
299 pdfswf_setparameter("jpegquality", val);
303 else if (!strcmp(name, "V"))
305 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
310 fprintf(stderr, "Unknown option: -%s\n", name);
316 /*struct docoptions_t options[] =
317 {{"o","output","filename::Specify output file"},
318 {"V","version","Print program version"},
319 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
320 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
321 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
322 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
323 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
324 {"w","samewindow","Don't open a new browser window for links in the SWF"},
325 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
326 {"F","fontpath","path::Add directory to font search path"},
327 {"B","viewer","name::Link viewer \"name\" to the pdf"},
328 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
329 {"b","defaultviewer","Link default viewer to the pdf"},
330 {"l","defaultpreloader","Link default preloader to the pdf"}
333 struct options_t options[] =
348 {"b","defaultviewer"},
349 {"l","defaultpreloader"},
351 {"T","flashversion"},
355 int args_callback_longoption(char*name,char*val) {
356 return args_long2shortoption(options, name, val);
359 int args_callback_command(char*name, char*val) {
365 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
374 void args_callback_usage(char*name)
376 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
377 printf("\nBasic options:\n");
378 printf("-p --pages=range Convert only pages in range\n");
379 printf("-P --password=password Use password for deciphering the pdf\n");
380 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
381 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
383 printf("-F --fontdir directory Add directory to font search path\n");
385 printf("-V --version Print program version\n");
386 printf("\nEnhanced conversion options:\n");
387 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
388 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
389 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
390 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
391 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
392 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
393 printf("-s zoom=factor Scale result, default: 72\n");
394 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
395 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
396 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
397 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
398 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
399 " graphic errors)\n");
400 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
401 " other out. (Needed for some Powerpoint PDFs)\n");
402 //deliberately undocumented (for now)
403 //printf("-2 Put 2 pages into each frame.\n");
404 //printf("-4 Put 4 pages into each frame.\n");
405 printf("Postprocessing options:\n");
406 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
407 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
408 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
409 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
412 float getRate(char*filename)
416 fi = open(filename,O_RDONLY|O_BINARY);
419 sprintf(buffer, "Couldn't open %s", filename);
423 if(swf_ReadSWF(fi,&swf) < 0)
425 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
430 return swf.frameRate / 256.0;
433 int main(int argn, char *argv[])
439 char t1searchpath[1024];
443 initLog(0,-1,0,0,-1,loglevel);
445 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
446 FILE*test = fopen(concatPaths(FONTDIR,"\\d050000l.afm"), "rb");
448 fprintf(stderr, "Couldn't find file %s - pdf2swf not installed properly? OS says:\n", concatPaths(FONTDIR, "\\d050000l.afm"));
462 processargs(argn, argv);
466 fprintf(stderr, "Please specify an input file\n");
473 outputname = stripFilename(filename, ".swf");
474 msg("<notice> Output filename not given. Writing to %s", outputname);
480 fprintf(stderr, "Please use -o to specify an output file\n");
484 // test if the page range is o.k.
485 is_in_range(0x7fffffff, pagerange);
488 args_callback_usage(argv[0]);
493 pdfswf_addfontdir(FONTDIR);
494 for(t=0;t<fontpathpos;t++) {
495 pdfswf_addfontdir(fontpaths[t]);
498 pdf_doc_t* pdf = pdf_init(filename, password);
500 msg("<error> Couldn't open %s", filename);
505 gfxdevice_swf_init(&swfdev);
506 //gfxdevice_render_init(&swfdev);
507 dev_output_t* swf = dev_output_init(&swfdev);
513 pdf_page_info_t*info;
519 for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
521 if(is_in_range(pagenr, pagerange)) {
522 dev_output_preparepage(swf, pagenr, frame);
525 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
533 for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
535 if(is_in_range(pagenr, pagerange)) {
536 pdf_page_t* page = pages[pagenum].page = pdf_getpage(pdf, pagenr);
537 pdf_page_info_t* info = pdf_page_getinfo(page);
538 pages[pagenum].x = 0;
539 pages[pagenum].y = 0;
540 pages[pagenum].info = info;
541 pages[pagenum].page = page;
544 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
547 int xmax[xnup], ymax[xnup];
549 int width=0, height=0;
551 memset(xmax, 0, xnup*sizeof(int));
552 memset(ymax, 0, ynup*sizeof(int));
555 for(x=0;x<xnup;x++) {
558 if(pages[t].info->xMax-pages[t].info->xMin > xmax[x])
559 xmax[x] = pages[t].info->xMax-pages[t].info->xMin;
560 if(pages[t].info->yMax-pages[t].info->yMin > ymax[y])
561 ymax[y] = pages[t].info->yMax-pages[t].info->yMin;
563 for(x=0;x<xnup;x++) {
567 for(y=0;y<ynup;y++) {
571 dev_output_startframe(swf, width, height);
572 for(t=0;t<pagenum;t++) {
575 int xpos = x>0?xmax[x-1]:0;
576 int ypos = y>0?ymax[y-1]:0;
577 pdf_page_info_t*info = pages[t].info;
580 msg("<verbose> Render (%d,%d)-(%d,%d) move:%d/%d\n",
584 info->yMax + ypos, xpos, ypos);
585 pdf_page_rendersection(pages[t].page, swf, custom_move? move_x : xpos,
586 custom_move? move_y : ypos,
587 custom_clip? clip_x1 : info->xMin + xpos,
588 custom_clip? clip_y1 : info->yMin + ypos,
589 custom_clip? clip_x2 : info->xMax + xpos,
590 custom_clip? clip_y2 : info->yMax + ypos);
592 dev_output_endframe(swf);
593 for(t=0;t<pagenum;t++) {
594 pdf_page_info_destroy(pages[t].info);
595 pdf_page_destroy(pages[t].page);
600 dev_output_finish(swf);
602 gfxresult_t*result = swfdev.finish(&swfdev);
603 if(result->save(result, outputname) < 0) {
607 int width = (int)result->get(result, "width");
608 int height = (int)result->get(result, "height");
609 msg("<notice> SWF written");
611 result->destroy(result);
613 dev_output_destroy(swf);
620 #undef SYSTEM_BACKTICKS
621 if(viewer && !preloader) {
622 #ifdef SYSTEM_BACKTICKS
623 systemf("swfcombine %s `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",zip,
624 viewer, outputname, outputname);
626 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
627 viewer, outputname, outputname);
632 if(preloader && !viewer) {
633 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
634 #ifdef SYSTEM_BACKTICKS
635 ret = systemf("swfcombine %s `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,
636 preloader, SWFDIR, preloader, outputname, outputname);
638 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
639 SWFDIR, preloader, outputname, outputname);
644 if(preloader && viewer) {
645 systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
646 viewer, outputname, outputname);
647 #ifdef SYSTEM_BACKTICKS
648 systemf("swfcombine %s `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,
649 outputname, preloader, SWFDIR, preloader, outputname);
651 /* TODO: read out rate */
652 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,width,height,
653 getRate(preloader), preloader, SWFDIR, preloader, outputname);
655 systemf("rm __tmp__.swf");