STDIN上的readFileSync与字符编码中的readFile



我正在尝试从STDIN读取一个文件,用paper.js处理它并返回结果。

为了达到目的,我首先尝试从fs中读取一个文件,如下所示:

const Project = require('paper').Project
var fs = require('fs');
let p = new Project() 
fs.readFile( process.argv[2], function (err, data) {
if (err) { throw err; }

p.importSVG(data.toString(), { onError: function(e){ console.log(e) } })
console.log(p.exportSVG({ asString: true }));
});

效果很好。

然后,我尝试从STDIN(cat foo_02.svg | node swiss.js-是的,毫无意义地使用cat,但仅用于示例(获取数据,如下所示:

const Project = require('paper').Project
var fs = require('fs');
let p = new Project() 
const data = fs.readFileSync(process.stdin.fd).toString();
p.importSVG(data, { onError: function(e){ console.log(e) } })
console.log(p.exportSVG({ asString: true }));

这似乎破坏了数据。

添加的console.log(data)显示了以下内容(截断(:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0"
viewBox="0,0,0,0">
<defs>
<clipPath id="clip-1">
<rect x="0" y="0" width="2074" height="748" fill="none">
</rect>
</clipPath>
</defs>
<g fill="none" fill-rule="nonzero" stroke="none" stroke-width="1"
stroke-linecap="butt" stroke-linejoin="miter"
stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0"
font-family="none" font-weight="none" font-size="none"
text-anchor="none" style="mix-blend-mode: normal">
<g clip-path="url(#clip-1)">
<g fill="#f98673" font-family="sans-serif"
font-weight="normal" font-size="12" text-anchor="start"></g>
<path d="M1042.7,104.3c-1.6,1.3 -3.3,9.5 -2.8,14.1c0.3,3.6 4.7,10.3 7.2,11.1c1,0.3 3.6,2.7 5.8,5.3c9,10.7 12.1,14.7 12.1,15.9c0,0.7 0.6,1.6 1.3,2c0.8,0.4 2,2 2.8,3.4c2.3,4.2 7,11.3 8.3,12.4c0.7,0.5 1.8,1.9 2.6,3.1c2.1,2.9 10,12.3 12.8,14.9c1.2,1.2 2.2,2.7 2.2,3.2c0,0.5 3.6,4.4 8,8.8c4.4,4.3 9.9,10.5 12.1,13.8c4.5,6.6 14,18.7 17.9,22.8c2.8,2.9 3.9,4.2 12.3,14.7c3.1,4 8.2,10.3 11.2,14.1c3,3.7 5.5,7.3 5.5,7.8c0,0.5 0.8,1.8 1.8,2.9c0.9,1 3.1,3.8 4.9,6.2c3.1,4.4 11.4,13.8 18.8,21.7c6.4,6.7 9.3,10.4 14.4,17.9c4.2,6.3 9.1,15.6 9.1,17.2c0,0.3 1.4,2.5 3,4.9c3.7,5.4 3.7,6.7 0.3,11.3c-3.8,5 -21.8,27.1 -24.7,30.2c-1.3,1.4 -3.3,4.6 -4.6,7.2c-1.3,2.7 -2.7,4.8 -3,4.8c-0.4,0 -2,1.9 -3.5,4.3c-3.7,5.6 -8.1,11.1 -13,16.3c-7.8,8.2 -9.3,10.1 -21.8,25.9c-16,20.2 -15.9,20.1 -19.5,26c-1.6,2.8 -5.2,8.7 -8.1,13.3c-2.8,4.6 -5.1,8.7 -5.1,9c0,0.4 -1.8,2.9 -4,5.7c-2.2,2.7

而最终的CCD_ 3导致(也被截断(:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0"
viewBox="0,0,0,0">
<defs>
<clipPath id="clip-1">
<rect x="0" y="0" transform="scale(0,0)" width="0" height="0"
fill="none"></rect>
</clipPath>
<clipPath id="clip-2">
<rect x="-1037" y="-374" transform="scale(0,0)" width="2074"
height="748" id="clip-1" fill="none"></rect>
</clipPath>
</defs>
<g fill="none" fill-rule="nonzero" stroke="none" stroke-width="1"
stroke-linecap="butt" stroke-linejoin="miter"
stroke-miterlimit="10" stroke-dasharray="" stroke-dashoffset="0"
font-family="none" font-weight="none" font-size="none"
text-anchor="none" style="mix-blend-mode: normal">
<g clip-path="url(#clip-1)">
<g>
<g clip-path="url(#clip-2)">
<g fill="#f98673" font-family="sans-serif"
font-weight="normal" font-size="12" text-anchor="start">
</g>
<path d="M0,0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0v0zM0,0v0v0v0v0v0v0v0v0v0v0v0v

这里有一些隐含的编码吗?在linux/bash/node v10.19.0上工作。

我无法用另一个svg文件来复制它,比如https://upload.wikimedia.org/wikipedia/commons/2/21/Speaker_Icon.svg.

你能链接你的svg文件吗?

我建议您尝试使用以下代码,因为stdin上的readFile无法正常工作(https://github.com/nodejs/node-v0.x-archive/issues/7412)

const Project = require('paper').Project
var fs = require('fs');
let p = new Project() 
var chunks = [];
process.stdin
.on("data", function(chunk) { chunks.push(chunk); })
.on("end", function() {
console.log(chunks.join("").toString());
p.importSVG(chunks.join("").toString(), { onError: function(e){ console.log(e) } })
console.log(p.exportSVG({ asString: true }));
})
.setEncoding("utf8");

最新更新