#!/usr/bin/env node // This is a command to test the text rendering compliance of OpenType.js. // It is designed to operate with https://github.com/unicode-org/text-rendering-tests. // // Call it like this: // // ./bin/test-render --font=fonts/FiraSansOT-Medium.otf --testcase=TEST-1 --render=BALL // // The output will look like this: // // // // // // // // // // // // // When viewing the SVG, it will be upside-down (since glyphs are designed Y-up). var opentype = require('../dist/opentype.js'); const SVG_FOOTER = ``; function printUsage() { console.log('Usage: test-render --font=filename.otf --testcase=TEST_NAME --render=TEXT_TO_RENDER'); console.log('This commands output the text to render as an SVG file.'); console.log(); } let filename; let testcase; let textToRender; for (let i = 0; i < process.argv.length; i++) { const arg = process.argv[i]; if (arg.startsWith('--font=')) { filename = arg.substring('--font='.length); } else if (arg.startsWith('--testcase=')) { testcase = arg.substring('--testcase='.length); } else if (arg.startsWith('--render=')) { textToRender = arg.substring('--render='.length); } } if (filename === undefined || testcase === undefined || textToRender === undefined) { printUsage(); process.exit(1); } function renderSVG() { var font = opentype.loadSync(filename); let svgSymbols = []; let svgBody = []; var glyphSet = new Set(); let x = 0; const glyphs = font.stringToGlyphs(textToRender); for (let i = 0; i < glyphs.length; i++) { const glyph = glyphs[i]; const symbolId = testcase + '.' + glyph.name; if (!glyphSet.has(glyph)) { glyphSet.add(glyph); const svgPath = glyph.path.toSVG(); svgSymbols.push(` ${svgPath}`); } svgBody.push(` `); x += glyph.advanceWidth; } let minX = 0; let minY = Math.round(font.descender); let width = Math.round(x); let height = Math.round(font.ascender - font.descender); let svgHeader = ` `; return svgHeader + svgSymbols.join('\n') + svgBody.join('\n') + SVG_FOOTER; } try { var svg = renderSVG(); console.log(svg); } catch(e) { console.error(e.stack); process.exit(1); }