1 |
|
2 | const { svg2png } = require('../common/svg2png');
|
3 | const { columnsSvg } = require('./swu-columns-svg');
|
4 | const fs = require('fs');
|
5 | const path = require('path');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | const 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 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
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 |
|
58 | const parseArg = (val) => {
|
59 | try {
|
60 | return JSON.parse(val)
|
61 | } catch {
|
62 | return undefined
|
63 | }
|
64 | }
|
65 |
|
66 | const 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 |
|
86 | if (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 | }
|