UNPKG

4.67 kBJavaScriptView Raw
1
2const { svg2png } = require('../common/svg2png');
3const { columnsSvg } = require('./swu-columns-svg');
4const fs = require('fs');
5const path = require('path');
6
7/**
8 * Function that creates an array of PNG column images for an SWU text
9 * @function swu.columnsPng
10 * @param {string} swuText - a text of SWU signs and punctuation
11 * @param {ColumnOptions} options - an object of column options
12 * @returns {ArrayBuffer[]} array of PNG data urls
13 * @example
14 * const swuText = "𝠀ρ²‘ρˆ©§π ƒπ€˜π€£ρ²‘𝣳𝣩ρˆ©§π€‰π£» 𝠀ρƒŠ’ρƒŠ«ρ‹›•ρ†‡‘π ƒπ€˜π€§ρƒŠ«π£»π€•ρƒŠ’𝣴𝣼ρ†‡‘π€Žπ€‚ρ‹›•π€†π£¦ ρŒπ£’𝀂";
15 * const columnOptions = {"height": 250, "width": 150};
16 * @example
17 * // using promise.then
18 * swu.columnsPng(swuText, columnOptions).then( pngs => {
19 * console.log(pngs)
20 * })
21 * @example
22 * // using async/await
23 * const pngs = await swu.columnsPng(swuText, columnOptions)
24 */
25const columnsPng = async (swuText, options) => {
26 const svgs = await columnsSvg(swuText,options);
27 let pngs = [];
28 for (let i=0; i<svgs.length; i++){
29 let svg = svgs[i].replace(/<text.*text>/g, "");
30 pngs.push(await svg2png(svg));
31 }
32 return pngs;
33}
34
35/**
36 * Function that creates an array of PNG column images for an SWU text
37 * @function swu.columnsPngDataUrl
38 * @param {string} swuText - a text of SWU signs and punctuation
39 * @param {ColumnOptions} options - an object of column options
40 * @returns {string[]} array of PNG data urls
41 * @example
42 * const swuText = "𝠀ρ²‘ρˆ©§π ƒπ€˜π€£ρ²‘𝣳𝣩ρˆ©§π€‰π£» 𝠀ρƒŠ’ρƒŠ«ρ‹›•ρ†‡‘π ƒπ€˜π€§ρƒŠ«π£»π€•ρƒŠ’𝣴𝣼ρ†‡‘π€Žπ€‚ρ‹›•π€†π£¦ ρŒπ£’𝀂";
43 * const columnOptions = {"height": 250, "width": 150};
44 * @example
45 * // using promise.then
46 * swu.columnsPng(swuText, columnOptions).then( pngs => {
47 * console.log(pngs)
48 * })
49 * @example
50 * // using async/await
51 * const pngs = await swu.columnsPng(swuText, columnOptions)
52 */
53 const columnsPngDataUrl = async (swuText, options) => {
54 const pngs = await columnsPng(swuText, options);
55 return pngs.map ( png => 'data:image/png;base64,' + png.toString('base64'));
56}
57
58const parseArg = (val) => {
59 try {
60 return JSON.parse(val)
61 } catch {
62 return undefined
63 }
64}
65
66const helpArgs = () => {
67 console.log("SWU Columns PNG\n")
68 console.log("Usage: node swu/swu-columns-png.js FswText [OutputFile] [Arguments]\n");
69 console.log("ColumnOptions");
70 console.log(" https://www.sutton-signwriting.io/core/#columnoptions\n");
71 console.log("StyleObject");
72 console.log(" https://www.sutton-signwriting.io/core/#styleobject\n");
73 console.log("Arguments for ColumnOptions used to create ColumnData");
74 console.log("--height the height of the column");
75 console.log("--width the width of the column");
76 console.log("--offset the lane offset for left and right lanes");
77 console.log("--pad amount of padding before and after signs as well as at top, left, and right of column");
78 console.log("--margin amount of space at bottom of column that is not available");
79 console.log("--dynamic enables variable width column");
80 console.log("--background background color for column");
81 console.log("--style an object of style options for signs");
82 console.log("--puncuation an object of punctuation options");
83 process.exit(1);
84}
85
86if (require.main === module) {
87 const args = require('minimist')(process.argv.slice(2));
88 const swuText = args._[0];
89 let file = args._[1];
90 let ext;
91 if (file) {
92 ext = path.extname(file);
93 if (!ext) {
94 ext = ".png";
95 file += ext;
96 }
97 }
98
99 if (!swuText || args.help) helpArgs();
100
101 args.style = parseArg(args["style"]);
102 args.punctuation = parseArg(args["punctuation"]);
103 args.detail = parseArg(args["detail"]);
104
105 args.options = parseArg(args["options"]);
106 args.options = (typeof args.options == 'object')?args.options:{};
107 Object.keys(args).map((key)=>{
108 switch (key) {
109 case '_':
110 case 'options':
111 break;
112 default:
113 args.options[key] = args[key]
114 }
115 })
116 if (file) {
117 columnsPng(swuText,args.options).then( pngs => {
118 pngs.map( (png,i) => {
119 const out = file.replace(ext,"-" + (1+i) + ext);
120 fs.writeFileSync(out, png);
121 })
122 })
123 } else {
124 columnsPngDataUrl(swuText,args.options).then( pngs => {
125 pngs.map( (png,i) => {
126 console.log("========== PNG Column " + (1+i) + " ==========")
127 console.log(png);
128 })
129 })
130 }
131} else {
132 module.exports = { columnsPng, columnsPngDataUrl }
133}