olaf neophyte and de.po updates, valgrind tweaks, delete green lady, inkscape dpi=96
[goodguy/history.git] / cinelerra-5.1 / plugins / svg / svg.C
index c80bf199ab2c856a303d1a45d8c334dc237dfd8a..baa5efed7b362981092abe7780e849d9efbdd805 100644 (file)
@@ -21,6 +21,7 @@
 #include "clip.h"
 #include "filexml.h"
 #include "language.h"
+#include "mainerror.h"
 #include "svg.h"
 #include "svgwin.h"
 #include "overlayframe.inc"
@@ -32,6 +33,8 @@
 #include <string.h>
 #include <errno.h>
 #include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 
 REGISTER_PLUGIN(SvgMain)
@@ -40,7 +43,7 @@ SvgConfig::SvgConfig()
 {
        out_x = 0;   out_y = 0;
        out_w = 640; out_h = 480;
-       dpi = 90;
+       dpi = 96;
        strcpy(svg_file, "");
        ms_time = 0;
 }
@@ -53,6 +56,7 @@ int SvgConfig::equivalent(SvgConfig &that)
                EQUIV(out_w, that.out_w) &&
                EQUIV(out_h, that.out_h) &&
                !strcmp(svg_file, that.svg_file) &&
+               ms_time != 0 && that.ms_time != 0 &&
                ms_time == that.ms_time;
 }
 
@@ -97,7 +101,7 @@ SvgMain::~SvgMain()
        delete overlayer;
 }
 
-const char* SvgMain::plugin_title() { return _("SVG via Inkscape"); }
+const char* SvgMain::plugin_title() { return N_("SVG via Inkscape"); }
 int SvgMain::is_realtime() { return 1; }
 int SvgMain::is_synthesis() { return 1; }
 
@@ -109,7 +113,7 @@ void SvgMain::save_data(KeyFrame *keyframe)
        FileXML output;
 
 // cause data to be stored directly in text
-       output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
+       output.set_shared_output(keyframe->xbuf);
 
        output.tag.set_title("SVG");
        output.tag.set_property("OUT_X", config.out_x);
@@ -130,8 +134,7 @@ void SvgMain::read_data(KeyFrame *keyframe)
 {
        FileXML input;
 
-       const char *data = keyframe->get_data();
-       input.set_shared_input((char*)data, strlen(data));
+       input.set_shared_input(keyframe->xbuf);
        int result = 0;
 
        while( !(result = input.read_tag()) ) {
@@ -147,6 +150,24 @@ void SvgMain::read_data(KeyFrame *keyframe)
        }
 }
 
+static int exec_command(char* const*argv)
+{
+        pid_t pid = vfork();
+        if( pid < 0 ) return -1;
+        if( pid > 0 ) {
+                int stat = 0;
+               waitpid(pid, &stat, 0);
+                if( stat ) {
+                        char msg[BCTEXTLEN];
+                        sprintf(msg, "%s: error exit status %d", argv[0], stat);
+                        MainError::show_error(msg);
+                }
+                return 0;
+       }
+        execvp(argv[0], &argv[0]);
+        return -1;
+}
+
 
 int SvgMain::process_realtime(VFrame *input, VFrame *output)
 {
@@ -158,8 +179,8 @@ int SvgMain::process_realtime(VFrame *input, VFrame *output)
        char last_svg_file[BCTEXTLEN];
        strcpy(last_svg_file, config.svg_file);
        int64_t last_ms_time = config.ms_time;
-       load_configuration();
-       if( last_dpi != config.dpi )
+       need_reconfigure = load_configuration();
+       if( !ofrm || last_dpi != config.dpi )
                need_export = 1;
        if( strcmp(last_svg_file, config.svg_file) ||
            last_ms_time != config.ms_time )
@@ -177,42 +198,39 @@ int SvgMain::process_realtime(VFrame *input, VFrame *output)
                        st_png.st_mtim.tv_sec*1000 + st_png.st_mtim.tv_nsec/1000000;
                int fd = ms_time < config.ms_time ? -1 : open(filename_png, O_RDWR);
                if( fd < 0 ) { // file does not exist, export it
-                       char command[BCTEXTLEN];
-                       sprintf(command,
+                       char command[BCTEXTLEN], dpi[BCSTRLEN];
+                       snprintf(command, sizeof(command),
                                "inkscape --without-gui --export-background=0x000000 "
                                "--export-background-opacity=0 -d %f %s --export-png=%s",
                                config.dpi, config.svg_file, filename_png);
                        printf(_("Running command %s\n"), command);
-                       system(command);
+                       snprintf(dpi, sizeof(dpi), "%f", config.dpi);
+                       snprintf(command, sizeof(command), "--export-png=%s",filename_png);
+                       char *const argv[] = {
+                               (char*)"inkscape",
+                               (char*)"--without-gui",
+                               (char*)"--export-background=0x000000",
+                                (char*)"--export-background-opacity=0",
+                               (char*)"-d", dpi, config.svg_file, command,
+                               0, };
+                       exec_command(argv);
                        // in order for lockf to work it has to be open for writing
                        fd = open(filename_png, O_RDWR);
                        if( fd < 0 )
                                printf(_("Export of %s to %s failed\n"), config.svg_file, filename_png);
                }
                if( fd >= 0 ) {
-                       struct stat st_png;
-                       fstat(fd, &st_png);
-                       unsigned char *png_buffer = (unsigned char *)
-                               mmap (NULL, st_png.st_size, PROT_READ, MAP_SHARED, fd, 0);
-                       if( png_buffer != MAP_FAILED ) {
-                               if( png_buffer[0] == 0x89 && png_buffer[1] == 0x50 &&
-                                   png_buffer[2] == 0x4e && png_buffer[3] == 0x47 ) {
-                                       ofrm = new VFramePng(png_buffer, st_png.st_size, 1., 1.);
-                                       if( ofrm->get_color_model() != output->get_color_model() ) {
-                                               VFrame *vfrm = new VFrame(ofrm->get_w(), ofrm->get_h(),
-                                                       output->get_color_model());
-                                               vfrm->transfer_from(ofrm);
-                                               delete ofrm;  ofrm = vfrm;
-                                       }
-                               }
-                               else
-                                       printf (_("The file %s that was generated from %s is not in PNG format."
-                                                 " Try to delete all *.png files.\n"), filename_png, config.svg_file);
-                               munmap(png_buffer, st_png.st_size);
-                       }
-                       else
-                               printf(_("Access mmap to %s as %s failed.\n"), config.svg_file, filename_png);
+                       ofrm = VFramePng::vframe_png(fd);
                        close(fd);
+                       if( ofrm && ofrm->get_color_model() != output->get_color_model() ) {
+                               VFrame *vfrm = new VFrame(ofrm->get_w(), ofrm->get_h(),
+                                       output->get_color_model(), 0);
+                               vfrm->transfer_from(ofrm);
+                               delete ofrm;  ofrm = vfrm;
+                       }
+                       if( !ofrm )
+                               printf (_("The file %s that was generated from %s is not in PNG format."
+                                         " Try to delete all *.png files.\n"), filename_png, config.svg_file);
                }
        }
        if( ofrm ) {