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