38 lines
890 B
JavaScript
38 lines
890 B
JavaScript
#!/usr/bin/env node
|
|
|
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
import { SVG, registerWindow } from "@svgdotjs/svg.js";
|
|
import { createSVGWindow } from "svgdom";
|
|
|
|
const input = process.argv[2];
|
|
const output = process.argv[3];
|
|
const margin = Number.parseFloat(process.argv[4] ?? "0");
|
|
const bg = process.argv[5] ?? null;
|
|
|
|
if (!input || !output) {
|
|
process.exit(1);
|
|
}
|
|
|
|
const window = createSVGWindow();
|
|
const document = window.document;
|
|
registerWindow(window, document);
|
|
|
|
const raw = readFileSync(input, "utf8");
|
|
const canvas = SVG(document.documentElement);
|
|
canvas.svg(raw);
|
|
|
|
const box = canvas.bbox(); // REAL geometric bbox
|
|
|
|
const x = box.x - margin;
|
|
const y = box.y - margin;
|
|
const w = box.width + margin * 2;
|
|
const h = box.height + margin * 2;
|
|
|
|
canvas.viewbox(x, y, w, h);
|
|
|
|
if (bg) {
|
|
canvas.attr("style", `background:${bg}`);
|
|
}
|
|
|
|
writeFileSync(output, canvas.svg());
|