export-kerning
Version:
Exports the kerning of an OpenType font
49 lines (41 loc) • 1.61 kB
JavaScript
import fs from 'fs';
import opentype from 'opentype.js';
import { Command } from 'commander';
import { exportKerning } from './core.js';
const program = new Command();
program
.name('export-kerning')
.description('Export kerning information from OpenType fonts')
.argument('<font-file>', 'Path to the font file')
.option('-o, --output <file>', 'Output file path', 'output/kerning.json')
.option('-t, --text <text>', 'Text to analyze for kerning pairs', null)
.option('-r, --ranges <ranges>', 'Unicode ranges to analyze', null)
.action(async (fontPath, options) => {
try {
const font = await opentype.load(fontPath);
// Use the exportKerning function
const result = exportKerning(font, {
ranges: options.ranges || null,
text: options.text || null
});
// Save main kerning file
const mainData = {
unitsPerEm: result.unitsPerEm,
kerningPairs: result.kerningPairs
};
fs.writeFileSync(options.output, JSON.stringify(mainData));
console.log(`✔️ Optimized kerning pairs saved to ${options.output}`);
// If subset was generated, save it
if (result.subset) {
const subsetOutput = options.output.replace(/\.[^.]+$/, '-subset$&');
fs.writeFileSync(subsetOutput, JSON.stringify(result.subset));
console.log(`✔️ Optimized used kerning subset saved to ${subsetOutput}`);
}
} catch (err) {
console.error('Error:', err.message || err);
if (err.stack) console.error(err.stack);
process.exit(1);
}
});
program.parse();