UNPKG

1.73 kBJavaScriptView Raw
1import { parse } from './swu-parse';
2import { parse as parseStyle } from '../style/style-parse';
3
4/**
5 * Function to gather sizing information about an swu sign or symbol
6 * @function swu.info
7 * @param {string} swu - an swu sign or symbol
8 * @returns {object} information about the swu string
9 * @example
10 * swu.info('𝠀񁲡񈩧𝠂𝤘𝤣񁲡𝣳𝣩񈩧𝤉𝣻-P10Z2')
11 *
12 * return {
13 * minX: 481,
14 * minY: 471,
15 * width: 37,
16 * height: 58,
17 * segment: 'sign',
18 * lane: -1
19 * padding: 10,
20 * zoom: 2
21 * }
22 */
23 const info = (swu) => {
24 let lanes = {
25 '𝠁': 0,
26 '𝠂': -1,
27 '𝠃': 0,
28 '𝠄': 1
29 }
30
31 let parsed = parse.sign(swu);
32 let width, height, segment, x1, x2, y1, y2, lane;
33 if (parsed.spatials) {
34 x1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[0]));
35 x2 = parsed.max[0];
36 width = x2 - x1;
37 y1 = Math.min(...parsed.spatials.map(spatial => spatial.coord[1]));
38 y2 = parsed.max[1];
39 height = y2 - y1;
40 segment = 'sign';
41 lane = parsed.box;
42 } else {
43 parsed = parse.symbol(swu);
44 lane = "𝠃";
45 if (parsed.coord){
46 x1 = parsed.coord[0];
47 width = (500 - x1) * 2;
48 y1 = parsed.coord[1];
49 height = (500 - y1) * 2;
50 segment = 'symbol';
51 } else {
52 x1 = 490;
53 width = 20;
54 y1 = 490;
55 height = 20;
56 segment = 'none';
57 }
58 }
59 let style = parseStyle(parsed.style);
60 let zoom = style.zoom || 1;
61 let padding = style.padding || 0;
62 return {
63 minX: x1,
64 minY: y1,
65 width: width,
66 height: height,
67 segment: segment,
68 lane: lanes[lane],
69 padding: padding,
70 zoom: zoom
71 };
72}
73
74export { info }
\No newline at end of file