【问题标题】:Using the Inkscape shell from perl在 perl 中使用 Inkscape shell
【发布时间】:2011-12-03 11:36:17
【问题描述】:

Inkscape 有一个像这样调用的 shell 模式

inkscape --shell

你可以在哪里执行这样的命令:

some_svg_file.svg -e some_png_output.png -y 1.0 -b #ffffff -D -d 150 

会生成一个PNG文件,或者像这样:

/home/simone/some_text.svg -S

在返回消息中为您提供文件中所有元素的边界框,如下所示

 svg2,0.72,-12.834,122.67281,12.942
 layer1,0.72,-12.834,122.67281,12.942
 text2985,0.72,-12.834,122.67281,12.942
 tspan2987,0.72,-12.834,122.67281,12.942

这样做的好处是您可以对 SVG 文件执行操作,而无需每次都重新启动 Inkscape。

我想做这样的事情:

sub do_inkscape {
     my ($file, $commands) = @_;
     # capture output
     return $output
}

如果我像这样使用 open2 并分叉,一切都会正常:

use IPC::Open2;

$pid = open2(\*CHLD_OUT, \*CHLD_IN, 'inkscape --shell');
$\ = "\n"; $/ = ">";

my $out; open my $fh, '>', \$out;

if (!defined($kidpid = fork())) {
    die "cannot fork: $!";
} elsif ($kidpid == 0) {
    while (<>) { print CHLD_IN $_; }
} else { 
    while (<CHLD_OUT>) { chop; s/\s*$//gmi; print "\"$_\"";  }
    waitpid($kidpid, 0); 
}

但我不知道如何只输入一行,然后只捕获该输出,而不必每次都重新启动 Inkscape。

谢谢

西蒙娜

【问题讨论】:

    标签: perl svg automation command-line-interface inkscape


    【解决方案1】:

    您不需要分叉,open2 会自行处理。您需要做的是找到一种方法来检测inkscape 何时等待输入。

    这是一个非常基本的示例,说明如何实现这一目标:

    #! /usr/bin/perl
    use strict;
    use warnings;
    
    use IPC::Open2;
    
    sub read_until_prompt($) {
        my ($fh) = (@_);
        my $done = 0;
        while (!$done) {
            my $in;
            read($fh, $in, 1);
            if ($in eq '>') {
                $done = 1;
            } else {
                print $in;
            }
        }
    }
    
    my ($is_in, $is_out);
    my $pid = open2($is_out, $is_in, 'inkscape --shell');
    
    read_until_prompt($is_out);
    print "ready\n";
    
    print $is_in "test.svg -S\n";
    read_until_prompt($is_out);
    
    print $is_in "quit\n";
    waitpid $pid, 0;
    
    print "done!\n";
    

    read_until_promptinkscapes 输出中读取,直到找到&gt; 字符,并假定当它看到一个字符时,inkscape 已准备就绪。

    注意:这太简单了,如果&gt; 可以出现在您期望的输出中的提示之外,您可能需要更多的逻辑来使其更可靠地工作。上面的脚本也完全没有错误检查,这很糟糕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 2017-09-07
      • 2017-10-24
      相关资源
      最近更新 更多