* moved os-specific functions to ../lib/os.c
[swftools.git] / pdf2swf / pdf2swf.cc
index 7f064cc..96f125b 100644 (file)
@@ -3,9 +3,21 @@
 
    Part of the swftools package.
    
-   Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
-
-   This file is distributed under the GPL, see file COPYING for details */
+   Copyright (c) 2001,2002,2003 Matthias Kramm <kramm@quiss.org> 
+   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 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 <stdlib.h>
 #include <stdio.h>
 #ifdef HAVE_DIRENT_H
 #include <dirent.h>
 #endif
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
 #include "../lib/args.h"
+#include "../lib/os.h"
 #include "SWFOutputDev.h"
-#include "t1lib.h"
-extern "C" {
 #include "log.h"
-}
+
+#define FONTDIR concatPaths(getInstallationPath(), "fonts")
+#define SWFDIR concatPaths(getInstallationPath(), "swfs")
 
 static char * outputname = 0;
 static int loglevel = 3;
@@ -31,6 +47,7 @@ static char * password = 0;
 
 static char * preloader = 0;
 static char * viewer = 0;
+static int nup = 0;
 
 char* fontpaths[256];
 int fontpathpos = 0;
@@ -67,11 +84,23 @@ 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;
     }
@@ -110,40 +139,41 @@ int args_callback_option(char*name,char*val) {
     }
     else if (!strcmp(name, "S"))
     {
-       pdfswf_drawonlyshapes();
+       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_enablezlib();
+       pdfswf_setparameter("enablezlib", "1");
        return 0;
     }
     else if (!strcmp(name, "n"))
     {
-       pdfswf_linksopennewwindow();
+       pdfswf_setparameter("opennewwindow", "1");
        return 0;
     }
     else if (!strcmp(name, "t"))
     {
-       pdfswf_insertstop();
+       pdfswf_setparameter("insertstop", "1");
        return 0;
     }
     else if (!strcmp(name, "T"))
     {
-       int i = atoi(val);
        if(!strcasecmp(val, "mx"))
-           i = 6;
-       pdfswf_setversion(i);
+           pdfswf_setparameter("flashversion", "6");
+       else
+           pdfswf_setparameter("flashversion", val);
+
        return 1;
     }
     else if (!strcmp(name, "f"))
     {
-       pdfswf_storeallcharacters();
+       pdfswf_setparameter("storeallcharacters", "1");
        return 0;
     }
     else if (!strcmp(name, "F"))
@@ -160,14 +190,14 @@ int args_callback_option(char*name,char*val) {
     else if (!strcmp(name, "l"))
     {
        char buf[256];
-       sprintf(buf, "%s/swfs/default_loader.swf", SWFTOOLS_DATADIR);
+       sprintf(buf, "%s/default_loader.swf", SWFDIR);
        preloader = strdup(buf);
        return 0;
     }
     else if (!strcmp(name, "b"))
     {
        char buf[256];
-       sprintf(buf, "%s/swfs/default_viewer.swf", SWFTOOLS_DATADIR);
+       sprintf(buf, "%s/default_viewer.swf", SWFDIR);
        viewer = strdup(buf);
        return 0;
     }
@@ -179,7 +209,7 @@ int args_callback_option(char*name,char*val) {
        }
        else
        {
-           systemf("ls %s/swfs/*_loader.swf", SWFTOOLS_DATADIR);
+           systemf("ls %s/*_loader.swf", SWFDIR);
            if(!system_quiet)
                printf("\n");
            exit(1);
@@ -194,7 +224,7 @@ int args_callback_option(char*name,char*val) {
        }
        else
        {
-           systemf("ls %s/swfs/*_viewer.swf", SWFTOOLS_DATADIR);
+           systemf("ls %s/*_viewer.swf", SWFDIR);
            if(!system_quiet)
                printf("\n");
            exit(1);
@@ -204,10 +234,10 @@ int args_callback_option(char*name,char*val) {
     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;
        }
     }
@@ -295,100 +325,34 @@ void args_callback_usage(char*name)
 #endif
     printf("-V  --version              Print program version\n");
     printf("\nEnhanced conversion options:\n");
-    printf("-t  --stop                 Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
     printf("-S  --shapes               Don't use SWF Fonts, but store everything as shape\n");
     printf("-z  --zlib                 Use Flash 6 (MX) zlib compression (Needs at least Flash 6 Plugin to play)\n");
-    printf("-j  --jpegquality=quality  Set quality of embedded jpeg pictures (default:85)\n");
     printf("-w  --samewindow           Don't open a new Browser Window for Links in the SWF\n");
     printf("-f  --fonts                Store full fonts in SWF. (Don't reduce to used characters)\n");
     printf("-T  --flashversion=num     Set the flash version in the header to num (default: 4)\n");
+    printf("-s insertstop              Insert a \"Stop\" Tag in every frame (don't turn pages automatically)\n");
+    printf("-s zoom=factor             Scale result, default: 72\n");
+    printf("-s jpegquality=quality     Set quality of embedded jpeg pictures (default:85)\n");
     printf("-s caplinewidth=value      Set the minimum line width to trigger cap style handling to value. (3)\n");
     printf("-s splinequality=value     Set the quality of spline convertion to value (0-100, default: 100).\n");
     printf("-s fontquality=value       Set the quality of font convertion to value (0-100, default: 100).\n");
-    printf("-s ignoredraworder         Ignore draw order (makes the SWF file smaller, but may produce\n"
+    printf("-s ignoredraworder         Ignore draw order (makes the SWF file smaller and faster, but may produce\n"
           "                           graphic errors)\n");
+    printf("-s filloverlap             Make intersecting shapes overlap, instead of canceling each\n"
+          "                           other out. (Needed for some Powerpoint PDFs)\n");
+    //deliberately undocumented (for now)
+    //printf("-2                         Put 2 pages into each frame.\n");
+    //printf("-4                         Put 4 pages into each frame.\n");
     printf("Postprocessing options:\n");
 #ifndef SYSTEM_BACKTICKS
     printf("(They might not work because your system call doesn't support command substitution)\n");
 #endif
-    printf("-b  --defaultviewer        Link default viewer to the pdf (%s/swfs/default_viewer.swf)\n", SWFTOOLS_DATADIR);
-    printf("-l  --defaultpreloader     Link default preloader the pdf (%s/swfs/default_loader.swf)\n", SWFTOOLS_DATADIR);
+    printf("-b  --defaultviewer        Link default viewer to the pdf (%s)\n", concatPaths(SWFDIR, "default_viewer.swf"));
+    printf("-l  --defaultpreloader     Link default preloader the pdf (%s)\n", concatPaths(SWFDIR, "default_loader.swf"));
     printf("-B  --viewer=filename      Link viewer \"name\" to the pdf (\"%s -B\" for list)\n", name);
     printf("-L  --preloader=filename   Link preloader \"name\" to the pdf (\"%s -L\" for list)\n",name);
 }
 
-#ifdef HAVE_DIRENT_H
-void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath) 
-{
-    if(searchpath) {
-       if(searchpath[0])
-           strcat(searchpath, ":");
-       strcat(searchpath, dirname);
-    }
-    msg("<verbose> Adding %s to search path\n", dirname);
-
-    DIR*dir = opendir(dirname);
-    if(!dir) {
-       msg("<warning> Couldn't open directory %s\n", dirname);
-       return;
-    }
-    dirent*ent;
-    while(1) {
-       ent = readdir (dir);
-       if (!ent) 
-           break;
-       int l;
-       char*name = ent->d_name;
-       char type = 0;
-       if(!name) continue;
-       l=strlen(name);
-       if(l<4)
-           continue;
-       if(!strncasecmp(&name[l-4], ".afm", 4)) 
-           type=1;
-       if(!strncasecmp(&name[l-4], ".ttf", 4)) 
-           type=2;
-       if(type)
-       {
-           if(database && type==1) {
-               char buf[256],a;
-               FILE*fi;
-               sprintf(buf, "%s/%s", dirname,name);
-               fi = fopen(buf, "rb");
-               if(!fi || !fread(&a,1,1,fi)) {
-                   msg("<warning> Couldn't read from %s", buf);
-               }
-               fprintf(database, "%s\n", buf);
-               msg("<verbose> Found font %s\n", buf);
-               fclose(fi);
-           } 
-           if(numfonts)
-               (*numfonts)++;
-       }
-    }
-    closedir(dir);
-}
-#endif
-
-char* stripfilename(char*filename, char*newext)
-{
-    char*last1 = strrchr(filename, '/');
-    char*last2 = strrchr(filename, '\\');
-    char*pos = filename;
-    char*name;
-    char*dot;
-    if(last1>pos) pos = last1 + 1;
-    if(last2>pos) pos = last2 + 1;
-    name = (char*)malloc(strlen(pos)+5);
-    strcpy(name, pos);
-    dot = strrchr(name, '.');
-    if(dot) {
-       *dot = 0;
-    }
-    strcat(name, newext);
-    return name;
-}
-
 int main(int argn, char *argv[])
 {
     int ret;
@@ -396,6 +360,21 @@ int main(int argn, char *argv[])
     int numfonts = 0;
     int t;
     char t1searchpath[1024];
+    int nup_pos = 0;
+    int x,y;
+    
+    initLog(0,-1,0,0,-1,loglevel);
+
+#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
+
 #ifdef HAVE_SRAND48
     srand48(time(0));
 #else
@@ -404,7 +383,6 @@ int main(int argn, char *argv[])
 #endif
 #endif
     processargs(argn, argv);
-    initLog(0,-1,0,0,-1,loglevel);
 
     if(!filename)
     {
@@ -415,7 +393,7 @@ int main(int argn, char *argv[])
     if(!outputname)
     {
        if(filename) {
-           outputname = stripfilename(filename, ".swf");
+           outputname = stripFilename(filename, ".swf");
            msg("<notice> Output filename not given. Writing to %s", outputname);
        } 
     }
@@ -434,79 +412,58 @@ int main(int argn, char *argv[])
        exit(0);
     }
 
-    msg("<verbose> reading font files from %s/fonts\n", SWFTOOLS_DATADIR);
-    //TODO: use tempnam here. Check if environment already contains a
-    //T1LIB_CONFIG.
-    putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
-    FILE*db = fopen("/tmp/FontDataBase", "wb");
-    FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
-    if(!db || !fi) {
-       fprintf(stderr, "Couldn't create temporary file in /tmp/\n");
-       exit(1);
-    }
-    t1searchpath[0] = 0;
-#ifdef HAVE_DIRENT_H
-    sprintf(buf, "%s/fonts",SWFTOOLS_DATADIR);
-    // pass 1
-    addfontdir(0, buf, &numfonts, 0);
-    for(t=0;t<fontpathpos;t++) {
-       addfontdir(0, fontpaths[t], &numfonts,0);
-    }
-    fprintf(db, "%d\n", numfonts);
-    // pass 2
-    addfontdir(db, buf, 0, t1searchpath);
+    /* add fonts */
+    pdfswf_addfontdir(FONTDIR);
     for(t=0;t<fontpathpos;t++) {
-       addfontdir(db, fontpaths[t], 0, t1searchpath);
+       pdfswf_addfontdir(fontpaths[t]);
     }
-#else
-/* This is a workaround. The correct way would be to
-   get directory listings working on all systems.
-*/
-    strcpy(t1searchpath, SWFTOOLS_DATADIR);
-    strcat(t1searchpath, "/fonts");
-    fprintf(db, "14\n");
-    fprintf(db, "n021003l.afm\n");
-    fprintf(db, "n021023l.afm\n");
-    fprintf(db, "n021004l.afm\n");
-    fprintf(db, "n021024l.afm\n");
-    fprintf(db, "n019003l.afm\n");
-    fprintf(db, "n019023l.afm\n");
-    fprintf(db, "n019004l.afm\n");
-    fprintf(db, "n019024l.afm\n");
-    fprintf(db, "n022003l.afm\n");
-    fprintf(db, "n022023l.afm\n");
-    fprintf(db, "n022004l.afm\n");
-    fprintf(db, "n022024l.afm\n");
-    fprintf(db, "s050000l.afm\n");
-    fprintf(db, "d050000l.afm\n");
-#endif
 
-    fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
-    fprintf(fi, "ENCODING=%s:.\n", t1searchpath);
-    fprintf(fi, "AFM=%s:.\n", t1searchpath);
-    fprintf(fi, "TYPE1=%s:.\n", t1searchpath);
-    fclose(fi);
-    fclose(db);
-    /* initialize t1lib */
-    T1_SetBitmapPad( 16);
-    if ((T1_InitLib(NO_LOGFILE)==NULL)){
-       fprintf(stderr, "Initialization of t1lib failed\n");
-       exit(1);
+    pdf_doc_t* pdf = pdf_init(filename, password);
+    if(!pdf) {
+        msg("<error> Couldn't open %s", filename);
+        exit(1);
     }
-    unlink("/tmp/t1lib.config.tmp");
-
-    pdfswf_init(filename, password);
-    pdfswf_setoutputfilename(outputname);
+    swf_output_t* swf = swf_output_init();
 
-    int pages = pdfswf_numpages();
-    for(t = 1; t <= pages; t++) 
+    for(t = 1; t <= pdf->num_pages; t++) 
     {
-       if(is_in_range(t, pagerange))
-       pdfswf_convertpage(t);
-    }
-    pdfswf_performconversion();
-
-    pdfswf_close();
+       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
@@ -518,28 +475,26 @@ int main(int argn, char *argv[])
     }
 
     if(viewer && !preloader) {
-       systemf("swfcombine `swfdump -XY %s` %s viewport=%s -o %s",
+       systemf("swfcombine `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",
                outputname, viewer, outputname, outputname);
        if(!system_quiet)
            printf("\n");
     }
     if(preloader && !viewer) {
        msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
-       ret = systemf("swfcombine `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=%s -o %s",
-               preloader, SWFTOOLS_DATADIR, preloader, outputname, outputname);
+       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",
+       systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
                viewer, outputname, outputname);
-       systemf("swfcombine `swfdump -XY %s` `swfdump -r %s` %s/swfs/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o %s",
-               outputname, preloader, SWFTOOLS_DATADIR, preloader, 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");
     }
 
-    unlink("/tmp/FontDataBase");
     return 0;
 }
 
-