Skip to content

Instantly share code, notes, and snippets.

@robbi5
Created July 20, 2018 09:08
Show Gist options
  • Save robbi5/353e80eff5de0379c81666c52b683c0f to your computer and use it in GitHub Desktop.
Save robbi5/353e80eff5de0379c81666c52b683c0f to your computer and use it in GitHub Desktop.
Chunk long PD (pen down) lines in hpgl into small blocks. This should help with plotters w/ little memory
process.stdin.setEncoding('utf8');
let commands = [];
let current = '';
const flatten = (arr) => {
return Array.prototype.concat(...arr);
}
const collectChunk = (chunk) => {
current = current + chunk;
if (current.indexOf(';') == -1) {
return;
}
let x = current.split(';');
current = x.pop();
commands.push(x.map( (e) => e.trim() ));
commands = flatten(commands);
}
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
collectChunk(chunk);
}
});
process.stdin.on('end', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
collectChunk(chunk);
}
run();
});
function run() {
commands.forEach((cmd) => {
// suppress Select Pen command
if (cmd.indexOf('SP') > -1) return;
if (cmd.length > 128) {
let parts = cmd.split(',');
if (parts[0][0] == 'P' && parts[0][1] == 'D') {
parts[0] = parts[0].replace(/^PD/i, '');
var chunk = 8;
for (var i = 0, j = parts.length; i < j; i += chunk) {
process.stdout.write('PD' + parts.slice(i, i+chunk).join(',') + ';');
}
}
} else {
process.stdout.write(cmd + ';');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment