UNPKG

11.5 kBJavaScriptView Raw
1/**
2* Sutton SignWriting Core Module v1.1.0
3* https://github.com/Slevinski/SignWriting
4* Copyright (c) 2007-2019, Steve Slevinski
5* convert.js is released under the MIT License.
6*/
7
8(function (global, factory) {
9 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
10 typeof define === 'function' && define.amd ? define(['exports'], factory) :
11 (global = global || self, factory((global.ssw = global.ssw || {}, global.ssw.convert = {})));
12}(this, function (exports) { 'use strict';
13
14 /**
15 * Object of regular expressions for FSW strings
16 *
17 * { symbol, coord, sort, box, prefix, spatial, signbox, sign, term }
18 * @alias fsw.re
19 * @type {object}
20 */
21 let re = {
22 'symbol': 'S[123][0-9a-f]{2}[0-5][0-9a-f]',
23 'coord': '[0-9]{3}x[0-9]{3}',
24 'sort': 'A',
25 'box': '[BLMR]'
26 };
27 re.prefix = `(?:${re.sort}(?:${re.symbol})+)`;
28 re.spatial = `${re.symbol}${re.coord}`;
29 re.signbox = `${re.box}${re.coord}(?:${re.spatial})*`;
30 re.sign = `${re.prefix}?${re.signbox}`;
31 re.term = `${re.prefix}${re.signbox}`;
32
33 /**
34 * Object of regular expressions for FSW strings
35 *
36 * { symbol, coord, sort, box, prefix, spatial, signbox, sign, term }
37 * @alias swu.re
38 * @type {object}
39 */
40 let re$1 = {
41 'symbol': '(?:(?:\uD8C0[\uDC01-\uDFFF])|(?:[\uD8C1-\uD8FC][\uDC00-\uDFFF])|(?:\uD8FD[\uDC00-\uDC80]))',
42 'coord': '(?:\uD836[\uDC0C-\uDDFF]){2}',
43 'sort': '\uD836\uDC00',
44 'box': '\uD836[\uDC01-\uDC04]'
45 };
46 re$1.prefix = `(?:${re$1.sort}(?:${re$1.symbol})+)`;
47 re$1.spatial = `${re$1.symbol}${re$1.coord}`;
48 re$1.signbox = `${re$1.box}${re$1.coord}(?:${re$1.spatial})*`;
49 re$1.sign = `${re$1.prefix}?${re$1.signbox}`;
50 re$1.term = `${re$1.prefix}${re$1.signbox}`;
51
52 /** The convert module contains functions to convert between Formal SignWriitng in ASCII (FSW) and SignWriting in Unicode (SWU) characters, along with other types of data.
53 * [Characters set definitions](https://tools.ietf.org/id/draft-slevinski-formal-signwriting-07.html#rfc.section.2.2)
54 * @module convert
55 */
56 /**
57 * Function to convert an SWU structural marker to FSW equivalent
58 * @function convert.swu2mark
59 * @param {string} swuMark - character for SWU structural marker
60 * @returns {string} FSW structural marker
61 * @example
62 * convert.swu2mark('𝠀')
63 *
64 * return 'A'
65 */
66
67 const swu2mark = swuMark => {
68 return {
69 '𝠀': 'A',
70 '𝠁': 'B',
71 '𝠂': 'L',
72 '𝠃': 'M',
73 '𝠄': 'R'
74 }[swuMark];
75 };
76 /**
77 * Function to convert an FSW structural marker to SWU equivalent
78 * @function convert.mark2swu
79 * @param {string} fswMark - character for FSW structural marker
80 * @returns {string} SWU structural marker
81 * @example
82 * convert.mark2swu('A')
83 *
84 * return '𝠀'
85 */
86
87
88 const mark2swu = fswMark => {
89 return {
90 'A': '𝠀',
91 'B': '𝠁',
92 'L': '𝠂',
93 'M': '𝠃',
94 'R': '𝠄'
95 }[fswMark];
96 };
97 /**
98 * Function to convert an SWU number character to an integer
99 * @function convert.swu2num
100 * @param {string} swuNum - SWU number character
101 * @returns {number} Integer value for number
102 * @example
103 * convert.swu2num('ðĪ†')
104 *
105 * return 500
106 */
107
108
109 const swu2num = swuNum => parseInt(swuNum.codePointAt(0)) - 0x1D80C + 250;
110 /**
111 * Function to convert a number to an SWU number character
112 * @function convert.num2swu
113 * @param {number} num - Integer value for number
114 * @returns {string} SWU number character
115 * @example
116 * convert.num2swu(500)
117 *
118 * return 'ðĪ†'
119 */
120
121
122 const num2swu = num => String.fromCodePoint(0x1D80C + parseInt(num) - 250);
123 /**
124 * Function to convert two SWU number characters to an array of x,y integers
125 * @function convert.swu2coord
126 * @param {string} swuCoord - Two SWU number character
127 * @returns {number[]} Array of x,y integers
128 * @example
129 * convert.swu2coord('ðĪ†ðĪ†')
130 *
131 * return [500, 500]
132 */
133
134
135 const swu2coord = swuCoord => [swu2num(swuCoord.slice(0, 2)), swu2num(swuCoord.slice(2, 4))];
136 /**
137 * Function to convert an array of x,y integers to two SWU number characters
138 * @function convert.coord2swu
139 * @param {number[]} coord - Array of x,y integers
140 * @returns {string} Two SWU number character
141 * @example
142 * convert.coord2swu([500, 500])
143 *
144 * return 'ðĪ†ðĪ†'
145 */
146
147
148 const coord2swu = coord => coord.map(num => num2swu(num)).join('');
149 /**
150 * Function to convert an FSW coordinate string to an array of x,y integers
151 * @function convert.fsw2coord
152 * @param {string} fswCoord - An FSW coordinate string
153 * @returns {number[]} Array of x,y integers
154 * @example
155 * convert.fsw2coord('500x500')
156 *
157 * return [500, 500]
158 */
159
160
161 const fsw2coord = fswCoord => fswCoord.split('x').map(num => parseInt(num));
162 /**
163 * Function to convert an array of x,y integers to an FSW coordinate string
164 * @function convert.coord2fsw
165 * @param {number[]} coord - Array of x,y integers
166 * @returns {string} An FSW coordinate string
167 * @example
168 * convert.coord2fsw([500, 500])
169 *
170 * return '500x500'
171 */
172
173
174 const coord2fsw = coord => coord.join('x');
175 /**
176 * Function to convert an SWU symbol character to a code point on plane 4
177 * @function convert.swu2code
178 * @param {string} swuSym - SWU symbol character
179 * @returns {number} Code point on plane 4
180 * @example
181 * convert.swu2code('ņ€€')
182 *
183 * return 0x40001
184 */
185
186
187 const swu2code = swuSym => parseInt(swuSym.codePointAt(0));
188 /**
189 * Function to convert a code point on plane 4 to an SWU symbol character
190 * @function convert.code2swu
191 * @param {number} code - Code point on plane 4
192 * @returns {string} SWU symbol character
193 * @example
194 * convert.code2swu(0x40001)
195 *
196 * return 'ņ€€'
197 */
198
199
200 const code2swu = code => String.fromCodePoint(code);
201 /**
202 * Function to convert an SWU symbol character to a 16-bit ID
203 * @function convert.swu2id
204 * @param {string} swuSym - SWU symbol character
205 * @returns {number} 16-bit ID
206 * @example
207 * convert.swu2id('ņ€€')
208 *
209 * return 1
210 */
211
212
213 const swu2id = swuSym => swu2code(swuSym) - 0x40000;
214 /**
215 * Function to convert a 16-bit ID to an SWU symbol character
216 * @function convert.id2swu
217 * @param {number} id - 16-bit ID
218 * @returns {string} SWU symbol character
219 * @example
220 * convert.id2swu(1)
221 *
222 * return 'ņ€€'
223 */
224
225
226 const id2swu = id => code2swu(id + 0x40000);
227 /**
228 * Function to convert an FSW symbol key to a 16-bit ID
229 * @function convert.key2id
230 * @param {string} key - FSW symbol key
231 * @returns {number} 16-bit ID
232 * @example
233 * convert.swu2id('S10000')
234 *
235 * return 1
236 */
237
238
239 const key2id = key => 1 + (parseInt(key.slice(1, 4), 16) - 256) * 96 + parseInt(key.slice(4, 5), 16) * 16 + parseInt(key.slice(5, 6), 16);
240 /**
241 * Function to convert a 16-bit ID to an FSW symbol key
242 * @function convert.id2key
243 * @param {number} id - 16-bit ID
244 * @returns {string} FSW symbol key
245 * @example
246 * convert.id2fsw(1)
247 *
248 * return 'S10000'
249 */
250
251
252 const id2key = id => {
253 const symcode = id - 1;
254 const base = parseInt(symcode / 96);
255 const fill = parseInt((symcode - base * 96) / 16);
256 const rotation = parseInt(symcode - base * 96 - fill * 16);
257 return 'S' + (base + 0x100).toString(16) + fill.toString(16) + rotation.toString(16);
258 };
259 /**
260 * Function to convert an SWU symbol character to an FSW symbol key
261 * @function convert.swu2key
262 * @param {string} swuSym - SWU symbol character
263 * @returns {string} FSW symbol key
264 * @example
265 * convert.swu2key('ņ€€')
266 *
267 * return 'S10000'
268 */
269
270
271 const swu2key = swuSym => {
272 const symcode = swu2code(swuSym) - 0x40001;
273 const base = parseInt(symcode / 96);
274 const fill = parseInt((symcode - base * 96) / 16);
275 const rotation = parseInt(symcode - base * 96 - fill * 16);
276 return 'S' + (base + 0x100).toString(16) + fill.toString(16) + rotation.toString(16);
277 };
278 /**
279 * Function to convert an FSW symbol key to an SWU symbol character
280 * @function convert.key2swu
281 * @param {string} key - FSW symbol key
282 * @returns {string} SWU symbol character
283 * @example
284 * convert.key2swu('S10000')
285 *
286 * return 'ņ€€'
287 */
288
289
290 const key2swu = key => code2swu(0x40001 + (parseInt(key.slice(1, 4), 16) - 256) * 96 + parseInt(key.slice(4, 5), 16) * 16 + parseInt(key.slice(5, 6), 16));
291 /**
292 * Function to convert SWU text to FSW text
293 * @function convert.swu2fsw
294 * @param {string} swuText - SWU text
295 * @returns {string} FSW text
296 * @example
297 * convert.swu2fsw('𝠀ņ€€’ņ€€šņ‹šĨņ‹›Đ𝠃ðĪŸðĪĐņ‹›ĐðĢĩðĪņ€€’ðĪ‡ðĢĪņ‹šĨðĪðĪ†ņ€€šðĢŪðĢ­')
298 *
299 * return 'AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475'
300 */
301
302
303 const swu2fsw = swuText => {
304 if (!swuText) return '';
305 let fsw = swuText.replace(/𝠀/g, "A").replace(/𝠁/g, "B").replace(/𝠂/g, "L").replace(/𝠃/g, "M").replace(/𝠄/g, "R");
306 const syms = fsw.match(new RegExp(re$1.symbol, 'g'));
307
308 if (syms) {
309 syms.forEach(function (sym) {
310 fsw = fsw.replace(sym, swu2key(sym));
311 });
312 }
313
314 const coords = fsw.match(new RegExp(re$1.coord, 'g'));
315
316 if (coords) {
317 coords.forEach(function (coord) {
318 fsw = fsw.replace(coord, swu2coord(coord).join('x'));
319 });
320 }
321
322 return fsw;
323 };
324 /**
325 * Function to convert FSW text to SWU text
326 * @function convert.fsw2swu
327 * @param {string} fswText - FSW text
328 * @returns {string} SWU text
329 * @example
330 * convert.fsw2swu('AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475')
331 *
332 * return '𝠀ņ€€’ņ€€šņ‹šĨņ‹›Đ𝠃ðĪŸðĪĐņ‹›ĐðĢĩðĪņ€€’ðĪ‡ðĢĪņ‹šĨðĪðĪ†ņ€€šðĢŪðĢ­'
333 */
334
335
336 const fsw2swu = fswText => {
337 if (!fswText) return '';
338 const prefixes = fswText.match(new RegExp(re.prefix, 'g'));
339
340 if (prefixes) {
341 prefixes.forEach(function (prefix) {
342 fswText = fswText.replace(prefix, '𝠀' + prefix.slice(1).match(/.{6}/g).map(key => key2swu(key)).join(''));
343 });
344 }
345
346 const boxes = fswText.match(new RegExp(re.box + re.coord, 'g'));
347
348 if (boxes) {
349 boxes.forEach(function (boxes) {
350 fswText = fswText.replace(boxes, mark2swu(boxes.slice(0, 1)) + coord2swu(fsw2coord(boxes.slice(1, 8))));
351 });
352 }
353
354 const spatials = fswText.match(new RegExp(re.spatial, 'g'));
355
356 if (spatials) {
357 spatials.forEach(function (spatial) {
358 fswText = fswText.replace(spatial, key2swu(spatial.slice(0, 6)) + coord2swu(fsw2coord(spatial.slice(6, 13))));
359 });
360 }
361
362 return fswText;
363 };
364
365 exports.code2swu = code2swu;
366 exports.coord2fsw = coord2fsw;
367 exports.coord2swu = coord2swu;
368 exports.fsw2coord = fsw2coord;
369 exports.fsw2swu = fsw2swu;
370 exports.id2key = id2key;
371 exports.id2swu = id2swu;
372 exports.key2id = key2id;
373 exports.key2swu = key2swu;
374 exports.mark2swu = mark2swu;
375 exports.num2swu = num2swu;
376 exports.swu2code = swu2code;
377 exports.swu2coord = swu2coord;
378 exports.swu2fsw = swu2fsw;
379 exports.swu2id = swu2id;
380 exports.swu2key = swu2key;
381 exports.swu2mark = swu2mark;
382 exports.swu2num = swu2num;
383
384 Object.defineProperty(exports, '__esModule', { value: true });
385
386}));
387
388/* help fund development on https://patreon.com/signwriting */