1 |
|
2 | const { db } = require('../db/db');
|
3 | const fsw = require('@sutton-signwriting/core/fsw');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const signNormalize = async fswSign => {
|
20 | const blank = '';
|
21 | const parsed = fsw.parse.sign(fswSign);
|
22 | if (!parsed.spatials) return blank;
|
23 |
|
24 | const symbols = parsed.spatials.map((spatial) => spatial.symbol);
|
25 | const syms = await db.query(`select symkey, width, height from symbol where symkey in ("${symbols.join('","')}")`);
|
26 | if (!syms.length) return blank;
|
27 | const symbolsizes = syms.reduce((obj, row) => {
|
28 | obj[row.symkey] = row
|
29 | return obj;
|
30 | }, {})
|
31 | const bbox = (symbols) => {
|
32 | const x1 = Math.min(...symbols.map(spatial => spatial.coord[0]));
|
33 | const y1 = Math.min(...symbols.map(spatial => spatial.coord[1]));
|
34 | const x2 = Math.max(...symbols.map(spatial => spatial.coord[0] + parseInt(symbolsizes[spatial.symbol].width)));
|
35 | const y2 = Math.max(...symbols.map(spatial => spatial.coord[1] + parseInt(symbolsizes[spatial.symbol].height)));
|
36 | return {
|
37 | x1: x1,
|
38 | y1: y1,
|
39 | x2: x2,
|
40 | y2: y2
|
41 | };
|
42 | }
|
43 |
|
44 | const hrange = fsw.ranges['hcenter'];
|
45 | const hsyms = parsed.spatials.filter((spatial) => {
|
46 | const dec = parseInt(spatial.symbol.slice(1, 4), 16);
|
47 | return (hrange[0] <= dec && hrange[1] >= dec);
|
48 | })
|
49 |
|
50 | const vrange = fsw.ranges['vcenter'];
|
51 | const vsyms = parsed.spatials.filter((spatial) => {
|
52 | const dec = parseInt(spatial.symbol.slice(1, 4), 16);
|
53 | return (vrange[0] <= dec && vrange[1] >= dec);
|
54 | })
|
55 |
|
56 | let abox = bbox(parsed.spatials);
|
57 | let max = [abox.x2, abox.y2];
|
58 | if (hsyms.length) {
|
59 | const hbox = bbox(hsyms);
|
60 | abox.x1 = hbox.x1;
|
61 | abox.x2 = hbox.x2;
|
62 | }
|
63 | if (vsyms.length) {
|
64 | const vbox = bbox(vsyms);
|
65 | abox.y1 = vbox.y1;
|
66 | abox.y2 = vbox.y2;
|
67 | }
|
68 |
|
69 | const offset = [parseInt((abox.x2 + abox.x1) / 2) - 500, parseInt((abox.y2 + abox.y1) / 2) - 500]
|
70 |
|
71 | return (parsed.sequence ? 'A' + parsed.sequence.join('') : '') +
|
72 | parsed.box + (max[0] - offset[0]) + 'x' + (max[1] - offset[1]) +
|
73 | parsed.spatials.map(spatial => spatial.symbol + (spatial.coord[0] - offset[0]) + 'x' + (spatial.coord[1] - offset[1])).join('') +
|
74 | (parsed.style || '');
|
75 | }
|
76 |
|
77 | if (require.main === module) {
|
78 | signNormalize(process.argv[2]).then( res => {
|
79 | console.log(res);
|
80 | })
|
81 | } else {
|
82 | module.exports = { signNormalize }
|
83 | }
|