X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=pdf2swf%2Fpdf2swf.cc;h=96f125b8354351ba42fd7c3462f54feb72a3b644;hb=691052d481359cfb2ae48f6336a481f5117281db;hp=c784f805206d2b1b4fc9d123215af288d8b1dd73;hpb=6b23c562a5018a878151141624c0b6a6619a9e18;p=swftools.git diff --git a/pdf2swf/pdf2swf.cc b/pdf2swf/pdf2swf.cc index c784f80..96f125b 100644 --- a/pdf2swf/pdf2swf.cc +++ b/pdf2swf/pdf2swf.cc @@ -3,21 +3,41 @@ Part of the swftools package. - Copyright (c) 2001 Matthias Kramm + Copyright (c) 2001,2002,2003 Matthias Kramm + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This file is distributed under the GPL, see file COPYING for details */ + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include +#include #include #include #include "../config.h" -#include "args.h" -#include "pdfswf.h" -#include "t1lib.h" -extern "C" { +#ifdef HAVE_DIRENT_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#include "../lib/args.h" +#include "../lib/os.h" +#include "SWFOutputDev.h" #include "log.h" -} + +#define FONTDIR concatPaths(getInstallationPath(), "fonts") +#define SWFDIR concatPaths(getInstallationPath(), "swfs") static char * outputname = 0; static int loglevel = 3; @@ -25,6 +45,36 @@ static char * pagerange = 0; static char * filename = 0; static char * password = 0; +static char * preloader = 0; +static char * viewer = 0; +static int nup = 0; + +char* fontpaths[256]; +int fontpathpos = 0; + +static int system_quiet=0; + +int systemf(const char* format, ...) +{ + char buf[1024]; + int ret; + va_list arglist; + va_start(arglist, format); + vsprintf(buf, format, arglist); + va_end(arglist); + + if(!system_quiet) { + printf("%s\n", buf); + fflush(stdout); + } + ret = system(buf); + if(ret) { + fprintf(stderr, "system() returned %d\n", ret); + exit(ret); + } + return ret; +} + int args_callback_option(char*name,char*val) { if (!strcmp(name, "o")) { @@ -34,6 +84,24 @@ int args_callback_option(char*name,char*val) { else if (!strcmp(name, "v")) { loglevel ++; + setConsoleLogging(loglevel); + return 0; + } + else if (!strcmp(name, "2")) + { + nup = 2; + return 0; + } + else if (!strcmp(name, "4")) + { + nup = 4; + return 0; + } + else if (!strcmp(name, "q")) + { + loglevel --; + setConsoleLogging(loglevel); + system_quiet = 1; return 0; } else if (name[0]=='p') @@ -58,21 +126,118 @@ int args_callback_option(char*name,char*val) { } else if (!strcmp(name, "s")) { - pdfswf_drawonlyshapes(); + char*s = strdup(val); + char*c = strchr(s, '='); + if(c && *c && c[1]) { + *c = 0; + c++; + pdfswf_setparameter(s,c); + } + else + pdfswf_setparameter(s,"1"); + return 1; + } + else if (!strcmp(name, "S")) + { + pdfswf_setparameter("drawonlyshapes", "1"); return 0; } else if (!strcmp(name, "i")) { - pdfswf_ignoredraworder(); + pdfswf_setparameter("ignoredraworder", "1"); + return 0; + } + else if (!strcmp(name, "z")) + { + pdfswf_setparameter("enablezlib", "1"); + return 0; + } + else if (!strcmp(name, "n")) + { + pdfswf_setparameter("opennewwindow", "1"); + return 0; + } + else if (!strcmp(name, "t")) + { + pdfswf_setparameter("insertstop", "1"); + return 0; + } + else if (!strcmp(name, "T")) + { + if(!strcasecmp(val, "mx")) + pdfswf_setparameter("flashversion", "6"); + else + pdfswf_setparameter("flashversion", val); + + return 1; + } + else if (!strcmp(name, "f")) + { + pdfswf_setparameter("storeallcharacters", "1"); + return 0; + } + else if (!strcmp(name, "F")) + { + char *s = strdup(val); + int l = strlen(s); + while(l && s[l-1]=='/') { + s[l-1] = 0; + l--; + } + fontpaths[fontpathpos++] = s; + return 1; + } + else if (!strcmp(name, "l")) + { + char buf[256]; + sprintf(buf, "%s/default_loader.swf", SWFDIR); + preloader = strdup(buf); + return 0; + } + else if (!strcmp(name, "b")) + { + char buf[256]; + sprintf(buf, "%s/default_viewer.swf", SWFDIR); + viewer = strdup(buf); return 0; } - else if (name[0]=='j') + else if (!strcmp(name, "L")) + { + if(val) + { + preloader = val; + } + else + { + systemf("ls %s/*_loader.swf", SWFDIR); + if(!system_quiet) + printf("\n"); + exit(1); + } + return 1; + } + else if (!strcmp(name, "B")) + { + if(val) + { + viewer = val; + } + else + { + systemf("ls %s/*_viewer.swf", SWFDIR); + if(!system_quiet) + printf("\n"); + exit(1); + } + return 1; + } + else if (!strcmp(name, "j")) { if(name[1]) { - pdfswf_jpegquality(atoi(&name[1])); + pdfswf_setparameter("jpegquality", &name[1]); return 0; } else { - pdfswf_jpegquality(atoi(val)); + pdfswf_setparameter("jpegquality", val); return 1; } } @@ -89,39 +254,47 @@ int args_callback_option(char*name,char*val) { return 0; } -struct options_t -{ - char shortoption; - char*longoption; -} options[] = -{{'o',"output"}, - {'V',"version"}, - {'i',"ignore"}, - {'s',"shapes"}, - {'j',"jpegquality"}, - {'p',"pages"} +/*struct docoptions_t options[] = +{{"o","output","filename::Specify output file"}, + {"V","version","Print program version"}, + {"i","ignore","Ignore draw order (makes the SWF file smaller, but may produce graphic errors)"}, + {"z","zlib","Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)"}, + {"s","shapes","Don't use SWF Fonts, but store everything as shape"}, + {"j","jpegquality","Set quality of embedded jpeg pictures (default: 85)"}, + {"p","pages","Convert only pages in range. (E.g. 3-85)"}, + {"w","samewindow","Don't open a new browser window for links in the SWF"}, + {"f","fonts","Stroe full fonts in SWF. (Don't reduce to used characters)"}, + {"F","fontpath","path::Add directory to font search path"}, + {"B","viewer","name::Link viewer \"name\" to the pdf"}, + {"L","preloader","file.swf::Link preloader \"file.swf\" to the pdf"}, + {"b","defaultviewer","Link default viewer to the pdf"}, + {"l","defaultpreloader","Link default preloader to the pdf"} + {0,0} +};*/ +struct options_t options[] = +{{"o","output"}, + {"q","quiet"}, + {"V","version"}, + {"i","ignore"}, + {"z","zlib"}, + {"s","set"}, + {"S","shapes"}, + {"j","jpegquality"}, + {"p","pages"}, + {"w","samewindow"}, + {"f","fonts"}, + {"F","fontdir"}, + {"B","viewer"}, + {"L","preloader"}, + {"b","defaultviewer"}, + {"l","defaultpreloader"}, + {"t","stop"}, + {"T","flashversion"}, + {0,0} }; int args_callback_longoption(char*name,char*val) { - int t; - char*equal = strchr(name,'='); - if (equal) { - *equal = 0; - equal++; - } - for(t=0;t='0' && *digits<='9') - digits++; - if(digits == pos) { - fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange); - exit(1); - } - - tmp=*digits;*digits=0; - num = atoi(pos); - *digits=tmp; - pos = digits; +#if defined(WIN32) && defined(HAVE_STAT) && defined(HAVE_SYS_STAT_H) + FILE*test = fopen(concatPaths(FONTDIR,"\\d050000l.afm"), "rb"); + if(!test) { + fprintf(stderr, "Couldn't find file %s - pdf2swf not installed properly? OS says:\n", concatPaths(FONTDIR, "\\d050000l.afm")); + perror("open"); + exit(1); + } + fclose(test); +#endif - while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t') - pos++; +#ifdef HAVE_SRAND48 + srand48(time(0)); +#else +#ifdef HAVE_SRAND + srand(time(0)); +#endif +#endif + processargs(argn, argv); - if(range && last<=t && num>=t) - return 1; - if(range) { - range = 0; - if(*pos) - pos ++; - continue; - } + if(!filename) + { + fprintf(stderr, "Please specify an input file\n"); + exit(1); + } - if(*pos=='-') - { - if(range) { - fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange); - exit(1); - } - last = num; - range = 1; - if(*pos) - pos ++; - continue; + if(!outputname) + { + if(filename) { + outputname = stripFilename(filename, ".swf"); + msg(" Output filename not given. Writing to %s", outputname); } - else - { - /* if it isn't a '-', we assume it is a seperator like - ',', ';', ':', whatever. */ - if(t == num) - return 1; - if(*pos) - pos ++; - continue; - } } - if(range && last<=t) - return 1; - return 0; -} - -int main(int argn, char *argv[]) -{ - srand48(time(0)); - processargs(argn, argv); - initLog(0,-1,0,0,-1,loglevel); + + if(!outputname) + { + fprintf(stderr, "Please use -o to specify an output file\n"); + exit(1); + } // test if the page range is o.k. is_in_range(0x7fffffff, pagerange); @@ -241,37 +412,89 @@ int main(int argn, char *argv[]) exit(0); } - logf(" reading data files from %s\n", DATADIR); - //TODO: use tempnam here. Check if environment already contains a - //T1LIB_CONFIG. - putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp"); - FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb"); - fprintf(fi, "FONTDATABASE=%s/FontDataBase\n", DATADIR); - fprintf(fi, "ENCODING=%s:.\n", DATADIR); - fprintf(fi, "AFM=%s:.\n", DATADIR); - fprintf(fi, "TYPE1=%s:.\n", DATADIR); - fclose(fi); - /* initialize t1lib */ - T1_SetBitmapPad( 16); - if ((T1_InitLib(NO_LOGFILE)==NULL)){ - fprintf(stderr, "Initialization of t1lib failed\n"); - exit(1); + /* add fonts */ + pdfswf_addfontdir(FONTDIR); + for(t=0;t Couldn't open %s", filename); + exit(1); + } + swf_output_t* swf = swf_output_init(); - int pages = pdfswf_numpages(); - int t = 1; - for(t = 1; t <= pages; t++) + for(t = 1; t <= pdf->num_pages; t++) { - if(is_in_range(t, pagerange)) - pdfswf_convertpage(t); + if(is_in_range(t, pagerange)) { + /* for links: FIXME */ + pdfswf_preparepage(t); + } + if(is_in_range(t, pagerange)) { + pdf_page_t*page = pdf_getpage(pdf, t); + if(nup) { + pdf_page_info_t* info = pdf_page_getinfo(page); + if(nup_pos%nup == 0) { + pdf_page_render(page, swf); + x = info->xMax; + y = info->yMax; + } else if(nup_pos%nup == 1) { + pdf_page_rendersection(page, swf, x, 0, info->xMin+x, info->yMin, info->xMax+x, info->yMax); + } else if(nup_pos%nup == 2) { + pdf_page_rendersection(page, swf, 0, y, info->xMin, info->yMin+y, info->xMax, info->yMax+y); + } else if(nup_pos%nup == 3) { + pdf_page_rendersection(page, swf, x, y, info->xMin+x, info->yMin+y, info->xMax+x, info->yMax+y); + } + if(nup_pos % nup == nup-1) + swf_output_pagefeed(swf); + + pdf_page_info_destroy(info); + } else { + pdf_page_render(page, swf); + swf_output_pagefeed(swf); + } + + pdf_page_destroy(page); + } + nup_pos++; + } + if(!swf_output_save(swf, outputname)) + exit(1); + swf_output_destroy(swf); + + pdf_destroy(pdf); + + if(viewer || preloader) { +#ifndef SYSTEM_BACKTICKS + msg(" Not sure whether system() can handle command substitution"); + msg(" (According to config.h, it can't)"); +#endif + if(!system_quiet) + printf("\n"); + } + + if(viewer && !preloader) { + systemf("swfcombine `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"", + outputname, viewer, outputname, outputname); + if(!system_quiet) + printf("\n"); + } + if(preloader && !viewer) { + msg(" --preloader option without --viewer option doesn't make very much sense."); + ret = systemf("swfcombine `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"", + preloader, SWFDIR, preloader, outputname, outputname); + if(!system_quiet) + printf("\n"); + } + if(preloader && viewer) { + systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf", + viewer, outputname, outputname); + systemf("swfcombine `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"", + outputname, preloader, SWFDIR, preloader, outputname); + systemf("rm __tmp__.swf"); } - pdfswf_close(); return 0; } -