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 "SWFOutputDev.h"
40 #define FONTDIR concatPaths(getInstallationPath(), "fonts")
41 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
43 static char * outputname = 0;
44 static int loglevel = 3;
45 static char * pagerange = 0;
46 static char * filename = 0;
47 static char * password = 0;
50 static char * preloader = 0;
51 static char * viewer = 0;
58 static int system_quiet=0;
60 int systemf(const char* format, ...)
65 va_start(arglist, format);
66 vsprintf(buf, format, arglist);
75 fprintf(stderr, "system() returned %d\n", ret);
81 int args_callback_option(char*name,char*val) {
82 if (!strcmp(name, "o"))
87 else if (!strcmp(name, "v"))
90 setConsoleLogging(loglevel);
93 else if (!strcmp(name, "2"))
99 else if (!strcmp(name, "4"))
105 else if (!strcmp(name, "9"))
111 else if (!strcmp(name, "q"))
114 setConsoleLogging(loglevel);
118 else if (name[0]=='p')
120 /* check whether the page range follows the p directly, like
124 } while(*name == 32 || *name == 13 || *name == 10 || *name == '\t');
133 else if (!strcmp(name, "P"))
138 else if (!strcmp(name, "s"))
140 char*s = strdup(val);
141 char*c = strchr(s, '=');
142 if(c && *c && c[1]) {
145 pdfswf_setparameter(s,c);
148 pdfswf_setparameter(s,"1");
151 else if (!strcmp(name, "S"))
153 pdfswf_setparameter("drawonlyshapes", "1");
156 else if (!strcmp(name, "i"))
158 pdfswf_setparameter("ignoredraworder", "1");
161 else if (!strcmp(name, "z"))
163 pdfswf_setparameter("enablezlib", "1");
167 else if (!strcmp(name, "n"))
169 pdfswf_setparameter("opennewwindow", "1");
172 else if (!strcmp(name, "t"))
174 pdfswf_setparameter("insertstop", "1");
177 else if (!strcmp(name, "T"))
179 if(!strcasecmp(val, "mx"))
180 pdfswf_setparameter("flashversion", "6");
182 pdfswf_setparameter("flashversion", val);
186 else if (!strcmp(name, "f"))
188 pdfswf_setparameter("storeallcharacters", "1");
191 else if (!strcmp(name, "w"))
193 pdfswf_setparameter("linksopennewwindow", "0");
196 else if (!strcmp(name, "F"))
198 char *s = strdup(val);
200 while(l && s[l-1]=='/') {
204 fontpaths[fontpathpos++] = s;
207 else if (!strcmp(name, "l"))
210 sprintf(buf, "%s/default_loader.swf", SWFDIR);
211 preloader = strdup(buf);
214 else if (!strcmp(name, "b"))
217 sprintf(buf, "%s/default_viewer.swf", SWFDIR);
218 viewer = strdup(buf);
221 else if (!strcmp(name, "L"))
229 systemf("ls %s/*_loader.swf", SWFDIR);
236 else if (!strcmp(name, "B"))
244 systemf("ls %s/*_viewer.swf", SWFDIR);
251 else if (!strcmp(name, "j"))
254 pdfswf_setparameter("jpegquality", &name[1]);
257 pdfswf_setparameter("jpegquality", val);
261 else if (!strcmp(name, "V"))
263 printf("pdf2swf - part of %s %s\n", PACKAGE, VERSION);
268 fprintf(stderr, "Unknown option: -%s\n", name);
274 /*struct docoptions_t options[] =
275 {{"o","output","filename::Specify output file"},
276 {"V","version","Print program version"},
277 {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"},
278 {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"},
279 {"s","shapes","Don't use SWF Fonts, but store everything as shape"},
280 {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"},
281 {"p","pages","Convert only pages in range. (E.g. 3-85)"},
282 {"w","samewindow","Don't open a new browser window for links in the SWF"},
283 {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"},
284 {"F","fontpath","path::Add directory to font search path"},
285 {"B","viewer","name::Link viewer \"name\" to the pdf"},
286 {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"},
287 {"b","defaultviewer","Link default viewer to the pdf"},
288 {"l","defaultpreloader","Link default preloader to the pdf"}
291 struct options_t options[] =
306 {"b","defaultviewer"},
307 {"l","defaultpreloader"},
309 {"T","flashversion"},
313 int args_callback_longoption(char*name,char*val) {
314 return args_long2shortoption(options, name, val);
317 int args_callback_command(char*name, char*val) {
323 fprintf(stderr, "Error: Do you want the output to go to %s or to %s?",
332 void args_callback_usage(char*name)
334 printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
335 printf("\nBasic options:\n");
336 printf("-p --pages=range Convert only pages in range\n");
337 printf("-P --password=password Use password for deciphering the pdf\n");
338 printf("-v --verbose Be verbose. Use more than one -v for greater effect\n");
339 printf("-q --quiet Suppress normal messages. Use -qq to suppress warnings, also.\n");
341 printf("-F --fontdir directory Add directory to font search path\n");
343 printf("-V --version Print program version\n");
344 printf("\nEnhanced conversion options:\n");
345 printf("-S --shapes Don't use SWF Fonts, but store everything as shape\n");
346 printf("-z --zlib Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
347 printf("-w --samewindow Don't open a new Browser Window for Links in the SWF\n");
348 printf("-f --fonts Store full fonts in SWF. (Don't reduce to used characters)\n");
349 printf("-T --flashversion=num Set the flash version in the header to num (default: 4)\n");
350 printf("-s insertstop Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
351 printf("-s zoom=factor Scale result, default: 72\n");
352 printf("-s jpegquality=quality Set quality of embedded jpeg pictures (default:85)\n");
353 printf("-s caplinewidth=value Set the minimum line width to trigger cap style handling to value. (3)\n");
354 printf("-s splinequality=value Set the quality of spline convertion to value (0-100, default: 100).\n");
355 printf("-s fontquality=value Set the quality of font convertion to value (0-100, default: 100).\n");
356 printf("-s ignoredraworder Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
357 " graphic errors)\n");
358 printf("-s filloverlap Make intersecting shapes overlap, instead of canceling each\n"
359 " other out. (Needed for some Powerpoint PDFs)\n");
360 //deliberately undocumented (for now)
361 //printf("-2 Put 2 pages into each frame.\n");
362 //printf("-4 Put 4 pages into each frame.\n");
363 printf("Postprocessing options:\n");
364 printf("-b --defaultviewer Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
365 printf("-l --defaultpreloader Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
366 printf("-B --viewer=filename Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
367 printf("-L --preloader=filename Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
370 float getRate(char*filename)
374 fi = open(filename,O_RDONLY|O_BINARY);
377 sprintf(buffer, "Couldn't open %s", filename);
381 if(swf_ReadSWF(fi,&swf) < 0)
383 fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
388 return swf.frameRate / 256.0;
391 int main(int argn, char *argv[])
397 char t1searchpath[1024];
401 initLog(0,-1,0,0,-1,loglevel);
403 #if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H)
404 FILE*test = fopen(concatPaths(FONTDIR,"\\d050000l.afm"), "rb");
406 fprintf(stderr, "Couldn't find file %s - pdf2swf not installed properly? OS says:\n", concatPaths(FONTDIR, "\\d050000l.afm"));
420 processargs(argn, argv);
424 fprintf(stderr, "Please specify an input file\n");
431 outputname = stripFilename(filename, ".swf");
432 msg("<notice> Output filename not given. Writing to %s", outputname);
438 fprintf(stderr, "Please use -o to specify an output file\n");
442 // test if the page range is o.k.
443 is_in_range(0x7fffffff, pagerange);
446 args_callback_usage(argv[0]);
451 pdfswf_addfontdir(FONTDIR);
452 for(t=0;t<fontpathpos;t++) {
453 pdfswf_addfontdir(fontpaths[t]);
456 pdf_doc_t* pdf = pdf_init(filename, password);
458 msg("<error> Couldn't open %s", filename);
462 swf_output_t* swf = swf_output_init();
468 pdf_page_info_t*info;
474 for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
476 if(is_in_range(pagenr, pagerange)) {
477 swf_output_preparepage(swf, pagenr, frame);
480 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
488 for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++)
490 if(is_in_range(pagenr, pagerange)) {
491 pdf_page_t* page = pages[pagenum].page = pdf_getpage(pdf, pagenr);
492 pdf_page_info_t* info = pdf_page_getinfo(page);
493 pages[pagenum].x = 0;
494 pages[pagenum].y = 0;
495 pages[pagenum].info = info;
496 pages[pagenum].page = page;
499 if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
502 int xmax[xnup], ymax[xnup];
504 int width=0, height=0;
506 memset(xmax, 0, xnup*sizeof(int));
507 memset(ymax, 0, ynup*sizeof(int));
510 for(x=0;x<xnup;x++) {
513 if(pages[t].info->xMax-pages[t].info->xMin > xmax[x])
514 xmax[x] = pages[t].info->xMax-pages[t].info->xMin;
515 if(pages[t].info->yMax-pages[t].info->yMin > ymax[y])
516 ymax[y] = pages[t].info->yMax-pages[t].info->yMin;
518 for(x=0;x<xnup;x++) {
522 for(y=0;y<ynup;y++) {
526 swf_output_startframe(swf, width, height);
527 for(t=0;t<pagenum;t++) {
530 int xpos = x>0?xmax[x-1]:0;
531 int ypos = y>0?ymax[y-1]:0;
532 pdf_page_info_t*info = pages[t].info;
535 msg("<verbose> Render (%d,%d)-(%d,%d) move:%d/%d\n",
539 info->yMax + ypos, xpos, ypos);
540 pdf_page_rendersection(pages[t].page, swf, xpos,
547 swf_output_endframe(swf);
548 for(t=0;t<pagenum;t++) {
549 pdf_page_info_destroy(pages[t].info);
550 pdf_page_destroy(pages[t].page);
555 if(swf_output_save(swf, outputname) < 0) {
558 int width = (int)swf_output_get(swf, "width");
559 int height = (int)swf_output_get(swf, "height");
560 msg("<notice> SWF written");
561 swf_output_destroy(swf);
568 #undef SYSTEM_BACKTICKS
569 if(viewer && !preloader) {
570 #ifdef SYSTEM_BACKTICKS
571 systemf("swfcombine %s `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",zip,
572 viewer, outputname, outputname);
574 systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
575 viewer, outputname, outputname);
580 if(preloader && !viewer) {
581 msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
582 #ifdef SYSTEM_BACKTICKS
583 ret = systemf("swfcombine %s `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,
584 preloader, SWFDIR, preloader, outputname, outputname);
586 ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
587 SWFDIR, preloader, outputname, outputname);
592 if(preloader && viewer) {
593 systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
594 viewer, outputname, outputname);
595 #ifdef SYSTEM_BACKTICKS
596 systemf("swfcombine %s `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,
597 outputname, preloader, SWFDIR, preloader, outputname);
599 /* TODO: read out rate */
600 systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,width,height,
601 getRate(preloader), preloader, SWFDIR, preloader, outputname);
603 systemf("rm __tmp__.swf");