UNPKG

3.5 kBJavaScriptView Raw
1
2const { db } = require('../db/db');
3const fsw = require('@sutton-signwriting/core/fsw');
4
5const blank = null;
6
7/**
8 * Function that normalizes an FSW sign for a center of 500,500
9 * @function fsw.signNormalize
10 * @param {string} fswSign - an FSW sign with optional style string
11 * @param {function} callback - a callback function with error and result parameters
12 * @example
13 * const callback = (error, result) => {
14 * if (error) {
15 * console.log(error)
16 * } else {
17 * console.log(result + " is 'M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475'")
18 * }
19 * }
20 *
21 * fsw.signNormalize('M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475', callback )
22 */
23const 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
91if (require.main === module) {
92 signNormalize(process.argv[2], (err, res) => {
93 console.log(err || res);
94 })
95} else {
96 module.exports = { signNormalize }
97}