1 |
|
2 | /** The convert module contains functions to help process the various SignWriting Character sets.
|
3 | * @module convert
|
4 | */
|
5 |
|
6 | import { parse as parseSymbol } from '../symbol/symbol-parse';
|
7 |
|
8 | /**
|
9 | * Function to convert a SignWriting in Unicode 8 (uni8) code point to a character
|
10 | * @function convert.code2uni
|
11 | * @param {integer} code - unicode code point
|
12 | * @returns {string} SignWriting in Unicode 8 character
|
13 | * @example
|
14 | * convert.code2uni(0x1D800)
|
15 | *
|
16 | * return '𝠀'
|
17 | */
|
18 | const code2uni = (code) => String.fromCharCode(0xD800 + ((code - 0x10000) >> 10), 0xDC00 + ((code - 0x10000) & 0x3FF));
|
19 |
|
20 | /**
|
21 | * Function to convert a SignWriting in Unicode 8 (uni8) character to a code point
|
22 | * @function convert.uni2code
|
23 | * @param {string} uni8 - SignWriting in Unicode 8 character
|
24 | * @returns {integer} unicode code point
|
25 | * @example
|
26 | * convert.uni2code('𝠀')
|
27 | *
|
28 | * return 0x1D800
|
29 | */
|
30 | const uni2code = (uni8) => uni8.codePointAt(0);
|
31 |
|
32 | /**
|
33 | * Function to convert a SignWriting in Unicode 8 (uni8) character to hex values
|
34 | * @function convert.uni2hex
|
35 | * @param {string} uni8 - SignWriting in Unicode 8 character
|
36 | * @returns {string} hex value of unicode character
|
37 | * @example
|
38 | * convert.uni2hex('𝠀')
|
39 | *
|
40 | * return "1D800"
|
41 | */
|
42 | const uni2hex = (uni8) => uni8.codePointAt(0).toString(16).toUpperCase();
|
43 |
|
44 | /**
|
45 | * Function to convert a SignWriting in Unicode 8 (uni8) symbol to Formal SignWriting in ASCII (FSW)
|
46 | * @function convert.uni2fsw
|
47 | * @param {string} uni8 - SignWriting in Unicode 8 character(s)
|
48 | * @returns {string} an FSW symbol key
|
49 | * @example
|
50 | * convert.uni2fsw('𝠀')
|
51 | *
|
52 | * return 'S10000'
|
53 | */
|
54 | const uni2fsw = (uni8) => {
|
55 | let sym = parseSymbol(uni8);
|
56 | if (sym.base){
|
57 | sym.base = sym.base.codePointAt(0) - 0x1D700;
|
58 | sym.fill = sym.fill?(sym.fill.codePointAt(0) - 0x1DA9A):0;
|
59 | sym.rotation = sym.rotation?(sym.rotation.codePointAt(0) - 0x1DAA0):0;
|
60 | return "S" + sym.base.toString(16) + sym.fill.toString(16) + sym.rotation.toString(16)
|
61 | } else {
|
62 | return undefined;
|
63 | }
|
64 | }
|
65 |
|
66 | /**
|
67 | * Function to convert a SignWriting in Unicode 8 (uni8) symbol to SignWriting in Unicode (SWU)
|
68 | * @function convert.uni2swu
|
69 | * @param {string} uni8 - SignWriting in Unicode 8 character(s)
|
70 | * @returns {string} an SWU symbol
|
71 | * @example
|
72 | * convert.uni2swu('𝠀')
|
73 | *
|
74 | * return ''
|
75 | */
|
76 | const uni2swu = (uni8) => {
|
77 | let sym = parseSymbol(uni8);
|
78 | if (sym.base){
|
79 | sym.base = sym.base.codePointAt(0) - 0x1D800;
|
80 | sym.fill = sym.fill?(sym.fill.codePointAt(0) - 0x1DA9A):0;
|
81 | sym.rotation = sym.rotation?(sym.rotation.codePointAt(0) - 0x1DAA0):0;
|
82 | return code2uni(0x40001 + (sym.base*96) + (sym.fill * 16) + sym.rotation);
|
83 | } else {
|
84 | return undefined;
|
85 | }
|
86 | }
|
87 |
|
88 | /**
|
89 | * Function to convert a Formal SignWriting in ASCII (FSW) to SignWriting in Unicode 8 (uni8)
|
90 | * @function convert.fsw2uni
|
91 | * @param {string} fswSym - an FSW symbol key
|
92 | * @returns {string} SignWriting in Unicode 8 character(s)
|
93 | * @example
|
94 | * convert.fsw2uni('S10000')
|
95 | *
|
96 | * return '𝠀'
|
97 | */
|
98 | const fsw2uni = (fswSym) => {
|
99 | let base = parseInt(fswSym.slice(1, 4), 16);
|
100 | let fill = parseInt(fswSym.slice(4, 5), 16);
|
101 | let rotation = parseInt(fswSym.slice(5, 6), 16);
|
102 | return code2uni(base + 0x1D700) + (fill?code2uni(fill + 0x1DA9A):'') + (rotation?code2uni(rotation + 0x1DAA0):'');
|
103 | }
|
104 |
|
105 | /**
|
106 | * Function to convert a SignWriting in Unicode (SWU) to SignWriting in Unicode 8 (uni8)
|
107 | * @function convert.swu2uni
|
108 | * @param {string} swuSym - an SWU symbol
|
109 | * @returns {string} SignWriting in Unicode 8 character(s)
|
110 | * @example
|
111 | * convert.swu2uni('')
|
112 | *
|
113 | * return '𝠀'
|
114 | */
|
115 | const swu2uni = (swuSym) => {
|
116 | const symcode = swuSym.codePointAt(0) - 0x40001;
|
117 | const base = parseInt(symcode / 96);
|
118 | const fill = parseInt((symcode - (base * 96)) / 16);
|
119 | const rotation = parseInt(symcode - (base * 96) - (fill * 16));
|
120 | return code2uni(base + 0x1D800) + (fill?code2uni(fill + 0x1DA9A):'') + (rotation?code2uni(rotation + 0x1DAA0):'')
|
121 | }
|
122 |
|
123 | export { code2uni, uni2code, uni2hex, uni2fsw, uni2swu, fsw2uni, swu2uni }
|