|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Custom svg-term renderer with configurable lineHeight. |
| 4 | + * |
| 5 | + * This script uses svg-term as a library to have full control over the theme, |
| 6 | + * specifically setting lineHeight to fix box-drawing character rendering. |
| 7 | + * |
| 8 | + * Usage: |
| 9 | + * node svg-term-render.js <input.json> <output.svg> [options] |
| 10 | + * |
| 11 | + * Options: |
| 12 | + * --at <ms> Timestamp of frame to render |
| 13 | + * --line-height <n> Line height multiplier (default: 1.0) |
| 14 | + * --font-family <s> Font family (default: Consolas, monospace) |
| 15 | + */ |
| 16 | + |
| 17 | +const fs = require('fs'); |
| 18 | +const {render} = require('svg-term'); |
| 19 | + |
| 20 | +// Parse command line arguments. |
| 21 | +const args = process.argv.slice(2); |
| 22 | + |
| 23 | +if (args.length < 2 || args.includes('--help')) { |
| 24 | + console.log('Usage: node svg-term-render.js <input.json> <output.svg> [options]'); |
| 25 | + console.log(''); |
| 26 | + console.log('Options:'); |
| 27 | + console.log(' --at <ms> Timestamp of frame to render'); |
| 28 | + console.log(' --line-height <n> Line height multiplier (default: 1.0)'); |
| 29 | + console.log(' --font-family <s> Font family (default: Consolas, monospace)'); |
| 30 | + process.exit(args.includes('--help') ? 0 : 1); |
| 31 | +} |
| 32 | + |
| 33 | +const inputFile = args[0]; |
| 34 | +const outputFile = args[1]; |
| 35 | + |
| 36 | +// Parse options. |
| 37 | +let at = null; |
| 38 | +let lineHeight = 1.0; |
| 39 | +let fontFamily = 'Consolas, "Courier New", Courier, "Liberation Mono", monospace'; |
| 40 | + |
| 41 | +for (let i = 2; i < args.length; i++) { |
| 42 | + if (args[i] === '--at' && i + 1 < args.length) { |
| 43 | + at = parseInt(args[i + 1], 10); |
| 44 | + i++; |
| 45 | + } else if (args[i] === '--line-height' && i + 1 < args.length) { |
| 46 | + lineHeight = parseFloat(args[i + 1]); |
| 47 | + i++; |
| 48 | + } else if (args[i] === '--font-family' && i + 1 < args.length) { |
| 49 | + fontFamily = args[i + 1]; |
| 50 | + i++; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Read input cast file. |
| 55 | +const input = fs.readFileSync(inputFile, 'utf8'); |
| 56 | + |
| 57 | +// Define custom theme with lineHeight set to 1.0. |
| 58 | +// Based on Atom One Dark theme colors. |
| 59 | +// Note: svg-term 1.3.1 expects RGB arrays, not hex strings. |
| 60 | +const theme = { |
| 61 | + background: [40, 44, 52], // #282c34 |
| 62 | + text: [171, 178, 191], // #abb2bf |
| 63 | + cursor: [82, 139, 255], // #528bff |
| 64 | + black: [40, 44, 52], // #282c34 |
| 65 | + red: [224, 108, 117], // #e06c75 |
| 66 | + green: [152, 195, 121], // #98c379 |
| 67 | + yellow: [209, 154, 102], // #d19a66 |
| 68 | + blue: [97, 175, 239], // #61afef |
| 69 | + magenta: [198, 120, 221], // #c678dd |
| 70 | + cyan: [86, 182, 194], // #56b6c2 |
| 71 | + white: [171, 178, 191], // #abb2bf |
| 72 | + brightBlack: [92, 99, 112], // #5c6370 |
| 73 | + brightRed: [224, 108, 117], // #e06c75 |
| 74 | + brightGreen: [152, 195, 121], // #98c379 |
| 75 | + brightYellow: [209, 154, 102], // #d19a66 |
| 76 | + brightBlue: [97, 175, 239], // #61afef |
| 77 | + brightMagenta: [198, 120, 221], // #c678dd |
| 78 | + brightCyan: [86, 182, 194], // #56b6c2 |
| 79 | + brightWhite: [255, 255, 255], // #ffffff |
| 80 | + bold: [171, 178, 191], // #abb2bf |
| 81 | + fontSize: 1.67, |
| 82 | + lineHeight: lineHeight, |
| 83 | + fontFamily: fontFamily, |
| 84 | +}; |
| 85 | + |
| 86 | +// Render options. |
| 87 | +const options = { |
| 88 | + theme: theme, |
| 89 | +}; |
| 90 | + |
| 91 | +if (at !== null) { |
| 92 | + options.at = at; |
| 93 | +} |
| 94 | + |
| 95 | +// Render SVG. |
| 96 | +try { |
| 97 | + const svg = render(input, options); |
| 98 | + fs.writeFileSync(outputFile, svg, 'utf8'); |
| 99 | + console.log(`SVG rendered successfully: ${outputFile}`); |
| 100 | + console.log(` lineHeight: ${lineHeight}`); |
| 101 | + console.log(` fontFamily: ${fontFamily}`); |
| 102 | + if (at !== null) { |
| 103 | + console.log(` at: ${at}ms`); |
| 104 | + } |
| 105 | +} catch (error) { |
| 106 | + console.error('Error rendering SVG:', error.message); |
| 107 | + process.exit(1); |
| 108 | +} |
0 commit comments