removed all: target
[swftools.git] / pdf2swf / pdf2swf.cc
index 39ad03f..5558e4d 100644 (file)
 #endif
 #include "../lib/args.h"
 #include "../lib/os.h"
+#include "../lib/rfxswf.h"
+#include "../lib/devices/swf.h"
+//#include "../lib/devices/render.h"
 #include "SWFOutputDev.h"
 #include "log.h"
 
-#define FONTDIR concatPaths(getInstallationPath(), "fonts")
 #define SWFDIR concatPaths(getInstallationPath(), "swfs")
 
 static char * outputname = 0;
@@ -54,6 +56,12 @@ static int ynup = 1;
 char* fontpaths[256];
 int fontpathpos = 0;
 
+int move_x=0;
+int move_y=0;
+int custom_move = 0;
+int clip_x1=0,clip_y1=0,clip_x2=0,clip_y2=0;
+int custom_clip = 0;
+
 static int system_quiet=0;
 
 int systemf(const char* format, ...)
@@ -134,6 +142,40 @@ int args_callback_option(char*name,char*val) {
        password = val;
        return 1;
     }
+    else if (!strcmp(name, "c"))
+    {
+       char*s = strdup(val);
+       char*x1 = strtok(s, ":");
+       char*y1 = strtok(0, ":");
+       char*x2 = strtok(0, ":");
+       char*y2 = strtok(0, ":");
+       if(!(x1 && y1 && x2 && y2)) {
+           fprintf(stderr, "-m option requires four arguments, <x1>:<y1>:<x2>:<y2>\n");
+           exit(1);
+       }
+       custom_clip = 1;
+       clip_x1 = atoi(x1);
+       clip_y1 = atoi(y1);
+       clip_x2 = atoi(x2);
+       clip_y2 = atoi(y2);
+       free(s);
+       return 1;
+    }
+    else if (!strcmp(name, "m"))
+    {
+       char*s = strdup(val);
+       char*c = strchr(s, ':');
+       if(!c) {
+           fprintf(stderr, "-m option requires two arguments, <x>:<y>\n");
+           exit(1);
+       }
+       *c = 0;
+       custom_move = 1;
+       move_x = atoi(val);
+       move_y = atoi(c+1);
+       free(s);
+       return 1;
+    }
     else if (!strcmp(name, "s"))
     {
        char*s = strdup(val);
@@ -360,15 +402,33 @@ void args_callback_usage(char*name)
     //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)\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);
 }
 
+float getRate(char*filename)
+{
+    int fi;
+    SWF swf;
+    fi = open(filename,O_RDONLY|O_BINARY);
+    if(fi<0) { 
+       char buffer[256];
+       sprintf(buffer, "Couldn't open %s", filename);
+        perror(buffer);
+        exit(1);
+    }
+    if(swf_ReadSWF(fi,&swf) < 0)
+    { 
+        fprintf(stderr, "%s is not a valid SWF file or contains errors.\n",filename);
+        close(fi);
+        exit(1);
+    }
+    swf_FreeTags(&swf);
+    return swf.frameRate / 256.0;
+}
+
 int main(int argn, char *argv[])
 {
     int ret;
@@ -378,17 +438,24 @@ int main(int argn, char *argv[])
     char t1searchpath[1024];
     int nup_pos = 0;
     int x,y;
+    char* installPath = getInstallationPath();
+    char* fontdir = 0;
     
     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);
+    if(installPath) {
+       fontdir = concatPaths(installPath, "fonts");
+       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);
     }
-    fclose(test);
+#else
+    fontdir = concatPaths(installPath, "fonts");
 #endif
 
 #ifdef HAVE_SRAND48
@@ -429,7 +496,9 @@ int main(int argn, char *argv[])
     }
 
     /* add fonts */
-    pdfswf_addfontdir(FONTDIR);
+    if(fontdir) {
+       pdfswf_addfontdir(fontdir);
+    }
     for(t=0;t<fontpathpos;t++) {
        pdfswf_addfontdir(fontpaths[t]);
     }
@@ -440,7 +509,10 @@ int main(int argn, char *argv[])
         exit(1);
     }
 
-    swf_output_t* swf = swf_output_init();
+    gfxdevice_t swfdev;
+    gfxdevice_swf_init(&swfdev);
+    //gfxdevice_render_init(&swfdev);
+    dev_output_t* swf = dev_output_init(&swfdev);
 
     struct mypage_t {
        int x;
@@ -455,7 +527,7 @@ int main(int argn, char *argv[])
     for(int pagenr = 1; pagenr <= pdf->num_pages; pagenr++) 
     {
        if(is_in_range(pagenr, pagerange)) {
-           swf_output_preparepage(swf, pagenr, frame);
+           dev_output_preparepage(swf, pagenr, frame);
            pagenum++;
        }
        if(pagenum == xnup*ynup || (pagenr == pdf->num_pages && pagenum>1)) {
@@ -504,7 +576,11 @@ int main(int argn, char *argv[])
                height += ymax[y];
                ymax[y] = height;
            }
-           swf_output_startframe(swf, width, height);
+           if(custom_clip) {
+               dev_output_startframe(swf, clip_x2 - clip_x1, clip_y2 - clip_y1);
+           } else {
+               dev_output_startframe(swf, width, height);
+           }
            for(t=0;t<pagenum;t++) {
                int x = t%xnup;
                int y = t/xnup;
@@ -518,14 +594,14 @@ int main(int argn, char *argv[])
                        info->yMin + ypos,
                        info->xMax + xpos,
                        info->yMax + ypos, xpos, ypos);
-               pdf_page_rendersection(pages[t].page, swf, xpos, 
-                                                          ypos,
-                                                          info->xMin + xpos, 
-                                                          info->yMin + ypos, 
-                                                          info->xMax + xpos, 
-                                                          info->yMax + ypos);
+               pdf_page_rendersection(pages[t].page, swf, custom_move? move_x : xpos, 
+                                                          custom_move? move_y : ypos,
+                                                          custom_clip? clip_x1 : info->xMin + xpos, 
+                                                          custom_clip? clip_y1 : info->yMin + ypos, 
+                                                          custom_clip? clip_x2 : info->xMax + xpos, 
+                                                          custom_clip? clip_y2 : info->yMax + ypos);
            }
-           swf_output_endframe(swf);
+           dev_output_endframe(swf);
            for(t=0;t<pagenum;t++)  {
                 pdf_page_info_destroy(pages[t].info);
                pdf_page_destroy(pages[t].page);
@@ -533,81 +609,62 @@ int main(int argn, char *argv[])
            pagenum = 0;
        }
     }
-    if(swf_output_save(swf, outputname) < 0) {
+    dev_output_finish(swf);
+
+    gfxresult_t*result = swfdev.finish(&swfdev);
+    if(result->save(result, outputname) < 0) {
         exit(1);
     }
+
+    int width = (int)result->get(result, "width");
+    int height = (int)result->get(result, "height");
     msg("<notice> SWF written");
-    swf_output_destroy(swf);
+    
+    result->destroy(result);
 
-    pdf_destroy(pdf);
+    dev_output_destroy(swf);
 
-    if(viewer || preloader) {
-#ifndef SYSTEM_BACKTICKS
-       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");
-    }
+    pdf_destroy(pdf);
 
     char*zip = "";
     if(zlib)
        zip = "-z";
-
-#ifdef _WIN32
-    char*batchname = "_pdf2swf.bat";
-#endif
-
+#undef SYSTEM_BACKTICKS
     if(viewer && !preloader) {
-#ifdef _WIN32
-       system_quiet=1;
-       systemf("echo swfcombine %s \"%s\" viewport=\"%s\" -o \"%s\" ^^>%s",zip,
-               viewer, outputname, outputname, batchname);
-       systemf("swfdump -XY \"%s\">>%s", outputname, batchname);
-       systemf("call %s", batchname);
-       systemf("del %s", batchname);
-#else
+#ifdef SYSTEM_BACKTICKS
        systemf("swfcombine %s `swfdump -XY \"%s\"` \"%s\" viewport=\"%s\" -o \"%s\"",zip,
-               outputname, viewer, outputname, outputname);
+               viewer, outputname, outputname);
+#else
+       systemf("swfcombine %s -X %d -Y %d \"%s\" viewport=\"%s\" -o \"%s\"",zip,width,height,
+               viewer, outputname, outputname);
 #endif
        if(!system_quiet)
            printf("\n");
     }
     if(preloader && !viewer) {
        msg("<warning> --preloader option without --viewer option doesn't make very much sense.");
-#ifdef _WIN32
-       system_quiet=1;
-       systemf("echo @SET RATE=^^>%s", batchname);
-       systemf("swfdump -r \"%s\">>%s", preloader, batchname);
-       systemf("echo swfcombine %s %s\\PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\" %%RATE%%>>%s",zip,
-               SWFDIR, preloader, outputname, outputname, batchname);
-       systemf("call %s", batchname);
-       systemf("del %s", batchname);
-#else
-       systemf("swfcombine %s `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,
+#ifdef SYSTEM_BACKTICKS
+       ret = systemf("swfcombine %s `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,
                preloader, SWFDIR, preloader, outputname, outputname);
+#else
+       ret = systemf("swfcombine %s -Y %d -X %d %s/PreLoaderTemplate.swf loader=\"%s\" movie=\"%s\" -o \"%s\"",zip,width,height,
+               SWFDIR, preloader, outputname, outputname);
 #endif
        if(!system_quiet)
            printf("\n");
     }
     if(preloader && viewer) {
        systemf("swfcombine \"%s\" viewport=%s -o __tmp__.swf",
-               viewer, outputname);
-#ifdef _WIN32
-       system_quiet=1;
-       systemf("echo @SET X_AND_Y=^^>%s", batchname);
-       systemf("swfdump -XY \"%s\">>%s", outputname, batchname);
-       systemf("echo @SET RATE=^^>>%s", batchname);
-       systemf("swfdump -r \"%s\">>%s", preloader, batchname);
-       systemf("echo swfcombine %s %%X_AND_Y%% %%RATE%% %s\\PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\">>%s",zip,
-               SWFDIR, preloader, outputname, batchname);
-       systemf("call %s", batchname);
-       systemf("del __tmp__.swf %s", batchname);
-#else
+               viewer, outputname, outputname);
+#ifdef SYSTEM_BACKTICKS
        systemf("swfcombine %s `swfdump -XY \"%s\"` `swfdump -r \"%s\"` %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,
                outputname, preloader, SWFDIR, preloader, outputname);
-       systemf("rm __tmp__.swf");
+#else
+       /* TODO: read out rate */
+       systemf("swfcombine %s -X %d -Y %d -r %f %s/PreLoaderTemplate.swf loader=%s movie=__tmp__.swf -o \"%s\"",zip,width,height,
+               getRate(preloader), preloader, SWFDIR, preloader, outputname);
 #endif
+       systemf("rm __tmp__.swf");
     }
 
     return 0;