added font2swf
[swftools.git] / pdf2swf / pdf2swf.cc
index 1122126..285e2a7 100644 (file)
@@ -3,9 +3,21 @@
 
    Part of the swftools package.
    
-   Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
+   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 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 <stdlib.h>
 #include <stdio.h>
@@ -97,6 +109,19 @@ int args_callback_option(char*name,char*val) {
     }
     else if (!strcmp(name, "s"))
     {
+       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_drawonlyshapes();
        return 0;
     }
@@ -115,6 +140,19 @@ int args_callback_option(char*name,char*val) {
        pdfswf_linksopennewwindow();
        return 0;
     }
+    else if (!strcmp(name, "t"))
+    {
+       pdfswf_insertstop();
+       return 0;
+    }
+    else if (!strcmp(name, "T"))
+    {
+       int i = atoi(val);
+       if(!strcasecmp(val, "mx"))
+           i = 6;
+       pdfswf_setversion(i);
+       return 1;
+    }
     else if (!strcmp(name, "f"))
     {
        pdfswf_storeallcharacters();
@@ -134,14 +172,14 @@ int args_callback_option(char*name,char*val) {
     else if (!strcmp(name, "l"))
     {
        char buf[256];
-       sprintf(buf, "%s/swfs/default_loader.swf", DATADIR);
+       sprintf(buf, "%s/swfs/default_loader.swf", SWFTOOLS_DATADIR);
        preloader = strdup(buf);
        return 0;
     }
     else if (!strcmp(name, "b"))
     {
        char buf[256];
-       sprintf(buf, "%s/swfs/default_viewer.swf", DATADIR);
+       sprintf(buf, "%s/swfs/default_viewer.swf", SWFTOOLS_DATADIR);
        viewer = strdup(buf);
        return 0;
     }
@@ -153,7 +191,7 @@ int args_callback_option(char*name,char*val) {
        }
        else
        {
-           systemf("ls %s/swfs/*_loader.swf", DATADIR);
+           systemf("ls %s/swfs/*_loader.swf", SWFTOOLS_DATADIR);
            if(!system_quiet)
                printf("\n");
            exit(1);
@@ -168,7 +206,7 @@ int args_callback_option(char*name,char*val) {
        }
        else
        {
-           systemf("ls %s/swfs/*_viewer.swf", DATADIR);
+           systemf("ls %s/swfs/*_viewer.swf", SWFTOOLS_DATADIR);
            if(!system_quiet)
                printf("\n");
            exit(1);
@@ -221,16 +259,19 @@ struct options_t options[] =
  {"V","version"},
  {"i","ignore"},
  {"z","zlib"},
- {"s","shapes"},
+ {"s","set"},
+ {"S","shapes"},
  {"j","jpegquality"},
  {"p","pages"},
  {"w","samewindow"},
  {"f","fonts"},
- {"F","fontpath"},
+ {"F","fontdir"},
  {"B","viewer"},
  {"L","preloader"},
  {"b","defaultviewer"},
  {"l","defaultpreloader"},
+ {"t","stop"},
+ {"T","flashversion"},
  {0,0}
 };
 
@@ -255,28 +296,35 @@ int args_callback_command(char*name, char*val) {
 
 void args_callback_usage(char*name)
 {
-    printf("Usage: %s [-si] [-j quality] [-p range] [-P password] input.pdf -o output.swf\n", name);
-    printf("\n");
+    printf("Usage: %s [Options] input.pdf [-o output.swf]\n", name);
+    printf("\nBasic options:\n");
     printf("-p  --pages=range          Convert only pages in range\n");
     printf("-P  --password=password    Use password for deciphering the pdf\n");
-    printf("-s  --shapes               Don't use SWF Fonts, but store everything as shape\n");
-    printf("-i  --ignore               Ignore draw order (makes the SWF file smaller, but may produce\n");
-    printf("                           graphic errors)\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("-v  --verbose              Be verbose. Use more than one -v for greater effect\n");
     printf("-q  --quiet                Suppress normal messages. Use -qq to suppress warnings, also.\n");
-    printf("-w  --samewindow           Don't open a new Browser Window for Links in the SWF\n");
-#ifdef HAVE_DIRENT
+#ifdef HAVE_DIRENT_H
     printf("-F  --fontdir directory    Add directory to font search path\n");
 #endif
-    printf("-f  --fonts                Store full fonts in SWF. (Don't reduce to used characters)\n");
     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 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"
+          "                           graphic errors)\n");
+    printf("Postprocessing options:\n");
 #ifndef SYSTEM_BACKTICKS
-    printf("The following might not work because your system call doesn't support command substitution:\n");
+    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", DATADIR);
-    printf("-l  --defaultpreloader     Link default preloader the pdf (%s/swfs/default_loader.swf)\n", DATADIR);
+    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  --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);
 }
@@ -289,11 +337,11 @@ void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath)
            strcat(searchpath, ":");
        strcat(searchpath, dirname);
     }
-    logf("<verbose> Adding %s to search path\n", dirname);
+    msg("<verbose> Adding %s to search path\n", dirname);
 
     DIR*dir = opendir(dirname);
     if(!dir) {
-       logf("<warning> Couldn't open directory %s\n", dirname);
+       msg("<warning> Couldn't open directory %s\n", dirname);
        return;
     }
     dirent*ent;
@@ -320,10 +368,11 @@ void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath)
                sprintf(buf, "%s/%s", dirname,name);
                fi = fopen(buf, "rb");
                if(!fi || !fread(&a,1,1,fi)) {
-                   logf("<warning> Couldn't read from %s", buf);
+                   msg("<warning> Couldn't read from %s", buf);
                }
                fprintf(database, "%s\n", buf);
-               logf("<verbose> Found font %s\n", buf);
+               msg("<verbose> Found font %s\n", buf);
+               fclose(fi);
            } 
            if(numfonts)
                (*numfonts)++;
@@ -333,6 +382,25 @@ void addfontdir(FILE*database, char* dirname, int*numfonts, char*searchpath)
 }
 #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;
@@ -349,6 +417,21 @@ int main(int argn, char *argv[])
 #endif
     processargs(argn, argv);
     initLog(0,-1,0,0,-1,loglevel);
+
+    if(!filename)
+    {
+       fprintf(stderr, "Please specify an input file\n");
+       exit(1);
+    }
+
+    if(!outputname)
+    {
+       if(filename) {
+           outputname = stripfilename(filename, ".swf");
+           msg("<notice> Output filename not given. Writing to %s", outputname);
+       } 
+    }
+       
     if(!outputname)
     {
        fprintf(stderr, "Please use -o to specify an output file\n");
@@ -363,7 +446,7 @@ int main(int argn, char *argv[])
        exit(0);
     }
 
-    logf("<verbose> reading font files from %s/fonts\n", DATADIR);
+    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");
@@ -375,7 +458,7 @@ int main(int argn, char *argv[])
     }
     t1searchpath[0] = 0;
 #ifdef HAVE_DIRENT_H
-    sprintf(buf, "%s/fonts",DATADIR);
+    sprintf(buf, "%s/fonts",SWFTOOLS_DATADIR);
     // pass 1
     addfontdir(0, buf, &numfonts, 0);
     for(t=0;t<fontpathpos;t++) {
@@ -391,7 +474,7 @@ int main(int argn, char *argv[])
 /* This is a workaround. The correct way would be to
    get directory listings working on all systems.
 */
-    strcpy(t1searchpath, DATADIR);
+    strcpy(t1searchpath, SWFTOOLS_DATADIR);
     strcat(t1searchpath, "/fonts");
     fprintf(db, "14\n");
     fprintf(db, "n021003l.afm\n");
@@ -439,8 +522,8 @@ int main(int argn, char *argv[])
 
     if(viewer || preloader) {
 #ifndef SYSTEM_BACKTICKS
-       logf("<warning> Not sure whether system() can handle command substitution");
-       logf("<warning> (According to config.h, it can't)");
+       msg("<warning> Not sure whether system() can handle command substitution");
+       msg("<warning> (According to config.h, it can't)");
 #endif
        if(!system_quiet)
            printf("\n");
@@ -453,9 +536,9 @@ int main(int argn, char *argv[])
            printf("\n");
     }
     if(preloader && !viewer) {
-       logf("<warning> --preloader option without --viewer option doesn't make very much sense.");
+       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, DATADIR, preloader, outputname, outputname);
+               preloader, SWFTOOLS_DATADIR, preloader, outputname, outputname);
        if(!system_quiet)
            printf("\n");
     }
@@ -463,7 +546,7 @@ int main(int argn, char *argv[])
        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, DATADIR, preloader, outputname);
+               outputname, preloader, SWFTOOLS_DATADIR, preloader, outputname);
        systemf("rm __tmp__.swf");
     }