利用Perl中Inkscape中壳 [英] Using the Inkscape shell from perl

查看:122
本文介绍了利用Perl中Inkscape中壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Inkscape中援引这样的shell模式

Inkscape has a shell mode invoked like this

inkscape --shell

在这里你可以执行如下命令:

where you can execute commands like this:

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

这将产生一个PNG文件,或者是这样的:

which will generate a PNG file, or like this:

/home/simone/some_text.svg -S

它给你的所有元素的边框文件中像这样

which gives you the bounding box of all elements in the file in the return message like this

 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的每一次。

The benefit of this is that you can perform operations on SVG files without having to restart Inkscape every time.

我愿做这样的事情:

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

物联网工作确定,如果我使用open2和分叉是这样的:

Things work OK if I use open2 and forking like this:

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的每一次。

but I can't find out how to input only one line, and capture only that output without having to restart Inkscape every time.

感谢

西蒙娜

推荐答案

您不必到餐桌, open2 处理,通过本身。你需要做的是找到检测时 Inkscape中等待输入的方式。

You don't need to fork, open2 handles that by itself. What you need to do is find a way of detecting when inkscape is waiting for input.

下面是你如何能达到一个非常基本的例子:

Here's a very basic example of how you could achieve that:

#! /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_prompt Inkscape中的输出读取直到它找到一个&GT; 字符,并假定当它看到的, Inkscape中就绪。

The read_until_prompt reads from inkscapes output until it finds a > character, and assumes that when it sees one, inkscape is ready.

注:这是太简单了,你可能会需要更多的逻辑在那里,​​使其更可靠地工作,如果一个&GT; 在输出的提示外就会出现你'重新期待。也没有错误在上面的脚本,这是坏的检查。

Note: This is too simple, you will probably need more logic in there to make it work more reliably if a > can appear outside the prompt in the output you're expecting. There is also no error checking at all in the above script, which is bad.

这篇关于利用Perl中Inkscape中壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆