UNPKG

3.59 kBJavaScriptView Raw
1
2const { columns } = require('@sutton-signwriting/core/fsw');
3const { columnSvg } = require('./fsw-column-svg');
4const fs = require('fs');
5const path = require('path');
6
7/**
8 * Function that creates an array of SVG column images for an FSW text
9 * @function fsw.columnsSvg
10 * @param {string} fswText - a text of FSW signs and punctuation
11 * @param {ColumnOptions} options - an object of column options
12 * @returns {string[]} array of svg columns
13 * @example
14 * const fswText = "AS14c20S27106M518x529S14c20481x471S27106503x489 AS18701S1870aS2e734S20500M518x533S1870a489x515S18701482x490S20500508x496S2e734500x468 S38800464x496";
15 * const columnOptions = {"height": 250, "width": 150};
16 * @example
17 * // using promise.then
18 * fsw.columnsSvg(fswText, columnOptions).then( svgs => {
19 * console.log(svgs)
20 * })
21 * @example
22 * // using async/await
23 * const svgs = await fsw.columnsSvg(fswText, columnOptions)
24 */
25const columnsSvg = async (fswText, options) => {
26 const cols = columns(fswText, options);
27 let svgs = [];
28 for (let i=0; i<cols.columns.length; i++){
29 svgs.push(await columnSvg(cols.columns[i],{...cols.options,...{width:cols.widths[i]}}));
30 }
31 return svgs;
32}
33
34const parseArg = (val) => {
35 try {
36 return JSON.parse(val)
37 } catch {
38 return undefined
39 }
40}
41
42const helpArgs = () => {
43 console.log("FSW Columns SVG\n");
44 console.log("Usage: node fsw/fsw-columns-svg.js FswText [OutputFile] [Arguments]\n");
45 console.log("ColumnOptions");
46 console.log(" https://www.sutton-signwriting.io/core/#columnoptions\n");
47 console.log("StyleObject");
48 console.log(" https://www.sutton-signwriting.io/core/#styleobject\n");
49 console.log("Arguments for ColumnOptions");
50 console.log("--height the height of the column");
51 console.log("--width the width of the column");
52 console.log("--offset the lane offset for left and right lanes");
53 console.log("--pad amount of padding before and after signs as well as at top, left, and right of column");
54 console.log("--margin amount of space at bottom of column that is not available");
55 console.log("--dynamic enables variable width column");
56 console.log("--background background color for column");
57 console.log("--style an object of style options for signs");
58 console.log("--puncuation an object of punctuation options");
59 process.exit(1);
60}
61
62if (require.main === module) {
63 const args = require('minimist')(process.argv.slice(2));
64 const fswText = args._[0];
65 let file = args._[1]
66 if (!fswText || args.help) helpArgs();
67
68 args.style = parseArg(args["style"])
69 args.punctuation = parseArg(args["punctuation"])
70 args.detail = parseArg(args["detail"])
71
72 args.options = parseArg(args["options"])
73 args.options = (typeof args.options == 'object')?args.options:{};
74 Object.keys(args).map((key)=>{
75 switch (key) {
76 case '_':
77 case 'options':
78 break;
79 default:
80 args.options[key] = args[key]
81 }
82 })
83 columnsSvg(fswText,args.options).then( svgs => {
84 if (file) {
85 let ext = path.extname(file);
86 if (!ext) {
87 ext = ".svg";
88 file += ext;
89 }
90 svgs.map( (svg,i) => {
91 const out = file.replace(ext,"-" + (1+i) + ext);
92 fs.writeFileSync(out, svg);
93 })
94 } else {
95 svgs.map( (svg,i) => {
96 console.log("========== SVG Column " + (1+i) + " ==========")
97 console.log(svg);
98 })
99 }
100 })
101
102} else {
103 module.exports = { columnsSvg }
104}