animation/numbers/index.js

91 lines
1.9 KiB
JavaScript

'use strict';
const { ArgumentParser } = require('argparse');
const { version, description } = require('./package.json');
const hershey = require('hersheytext');
const { writeFile } = require('fs-extra');
const { basename } = require('path');
const footer = '</svg>';
const W = 107;
const H = 99;
const charW = 3.25; //mm
const charH = 4.0; //mm
const header = `<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
height="${H}mm"
width="${W}mm"
viewBox="0 0 ${W} ${H}"
version="1.1">`;
let options = {
font : 'ems_nixish',
scale : 0.005,
weight : 1
};
async function loadFont (fontPath) {
let str;
let fileName;
let fontName;
try {
hershey.addSVGFont(fontPath);
} catch (err) {
console.error(err);
process.exit(1);
}
fileName = basename(fontPath);
fontName = fileName.split('.').slice(0, -1).join('.');
options = { font : fontName };
}
function textPos (str) {
const UNIT = 21000 / W;
const len = str.length;
const w = len * charW;
const h = charH;
const x = ((W - w) / 2.0) * UNIT;
const y = 1 * UNIT;
return { x, y };
}
async function main () {
const fontPath = args.font;
const num = (args.number + '').replace('\\n', '\n');
const size = args.size || '8.5x11';
const output = args.output || `number_${num}.svg`;
let data;
//console.log(num)
try {
//await loadFont(fontPath);
} catch (err) {
console.error(err);
}
//options.pos = { x: 50, y: 50 };
options.pos = textPos(num);
data = hershey.renderTextSVG(num, options);
try {
await writeFile(output, `${header}\n${data}\n${footer}`);
} catch (err) {
console.error(err);
}
}
const parser = new ArgumentParser({
description
});
parser.add_argument('-v', '--version', { action: 'version', version });
parser.add_argument('-f', '--font', { help: 'Path to font' });
parser.add_argument('-n', '--number', { help: 'Number to write' });
parser.add_argument('-o', '--output', { help: 'Output svg' });
const args = parser.parse_args();
main();