UNPKG

9.38 kBJavaScriptView Raw
1/**
2* Sutton SignWriting Unicode 8 Module v1.1.2 (https://github.com/sutton-signwriting/unicode8)
3* Author: Steve Slevinski (https://SteveSlevinski.me)
4* unicode8.mjs is released under the MIT License.
5*/
6
7/**
8 * Function that appends font-face CSS for the Noto Sans SignWriting font for use with SignWriting in Unicode 8 (uni8) characters.
9 *
10 * This font-face declaration will use a locally installed font if available.
11 * If not found, the font will be loaded from a content delivery network.
12 *
13 * The list of local names is currently a work in progress.
14 * The font-face currently works for Windows and iOS.
15 * This CSS is duplicated in the src/font/index.css file.
16 *
17 * @function font.cssAppend
18 * @param {string} dir - an optional relative directory for font location
19 * @example
20 * font.cssAppend('./font/')
21 */
22const cssAppend = function (dir = '') {
23 const id = "SgnwUnicode8FontCss";
24
25 if (!document.getElementById(id)) {
26 const style = document.createElement('style');
27 style.setAttribute("id", "SgnwUnicode8FontCss");
28 style.appendChild(document.createTextNode(`
29 @font-face {
30 font-family: "NotoSansSignWriting";
31 src:
32 local('NotoSansSignWriting'),
33 local('Noto Sans SignWriting'),
34 local('Noto_Sans_SignWriting'),
35 local('Noto Sans SignWriting Regular'),
36 local('Noto_Sans_SignWriting_Regular'),
37 ${dir ? `url('${dir}NotoSansSignWriting-Regular.otf') format('truetype'),` : ""}
38 url('https://cdn.jsdelivr.net/gh/googlefonts/noto-fonts/unhinted/otf/NotoSansSignWriting/NotoSansSignWriting-Regular.otf') format('opentype');
39 }
40 `));
41 document.head.appendChild(style);
42 }
43};
44
45/** The font module contains functions for handing the font.
46 * @module font
47 */
48
49var index = /*#__PURE__*/Object.freeze({
50 __proto__: null,
51 cssAppend: cssAppend
52});
53
54/**
55 * Object of regular expressions for symbol strings
56 *
57 * { base, fill, rotation, full }
58 * @alias symbol.re
59 * @type {object}
60 */
61let re = {
62 'base': '(?:\uD836[\uDC00-\uDE8B])',
63 'fill': '(?:\uD836[\uDE9B-\uDE9F])',
64 'rotation': '(?:\uD836[\uDEA1-\uDEAF])'
65};
66re.full = `(${re.base})(${re.fill})?(${re.rotation})?`;
67
68/**
69 * Function to parse symbol string to object
70 * @function symbol.parse
71 * @param {string} symbolString - a symbol string
72 * @returns {object} elements of symbol string
73 * @example
74 * symbol.parse('𝠀')
75 *
76 * return {
77 * 'base': '𝠀',
78 * 'fill': undefined,
79 * 'rotation': undefined
80 * }
81 */
82
83const parse = symbolString => {
84 const regex = `^${re.full}`;
85 const m = (typeof symbolString === 'string' ? symbolString.match(new RegExp(regex)) : []) || [];
86 return {
87 'base': !m[1] ? undefined : m[1],
88 'fill': !m[2] ? undefined : m[2],
89 'rotation': !m[3] ? undefined : m[3]
90 };
91};
92
93/**
94 * Function to compose symbol string from object
95 * @function symbol.compose
96 * @param {object} symbolObject - an object of symbol parts
97 * @param {string} symbolObject.base - base character for symbol
98 * @param {string} symbolObject.fill - fill character for symbol
99 * @param {string} symbolObject.rotation - rotation character for symbol
100 * @returns {string} symbol string
101 * @example
102 * symbol.compose({
103 * 'base': '𝠀'
104 * })
105 *
106 * return '𝠀'
107 */
108
109const compose = symbolObject => {
110 if (typeof symbolObject !== 'object' || symbolObject === null) return undefined;
111 const sym = (symbolObject.base ? symbolObject.base : '') + (symbolObject.fill ? symbolObject.fill : '') + (symbolObject.rotation ? symbolObject.rotation : '');
112 return sym ? sym : undefined;
113};
114
115/** The symbol module contains regular expressions and functions for parsing and composing SignWriting in Unicode 8 (uni8) characters.
116 * @module symbol
117 */
118
119var index$1 = /*#__PURE__*/Object.freeze({
120 __proto__: null,
121 re: re,
122 parse: parse,
123 compose: compose
124});
125
126/**
127 * Object of regular expressions for string of symbols
128 *
129 * { full }
130 * @alias string.re
131 * @type {object}
132 */
133let re$1 = {
134 'full': `(?:${re.full})+`
135};
136
137/**
138 * Function to parse string of uni8 symbols to array
139 * @function string.parse
140 * @param {string} uni8String - a string of uni8 symbols
141 * @returns {array} array of uni8 symbols
142 * @example
143 * string.parse('𝠀𝠁')
144 *
145 * return ['𝠀','𝠁']
146 */
147
148const parse$1 = uni8String => {
149 const regex = `^(${re$1.full})`;
150 const m = (typeof uni8String === 'string' ? uni8String.match(new RegExp(regex)) : []) || [];
151 return !m[1] ? [] : [...m[1].matchAll(new RegExp(re.full, 'g'))].map(mm => mm[0]);
152};
153
154/**
155 * Function to compose uni8 string from array
156 * @function string.compose
157 * @param {array} stringArray - an array of uni8 symbols
158 * @returns {string} string of uni8 symbols
159 * @example
160 * string.compose(['𝠀','𝠁'])
161 *
162 * return '𝠀𝠁'
163 */
164
165const compose$1 = stringArray => {
166 if (!Array.isArray(stringArray)) return undefined;
167 return stringArray.join('');
168};
169
170/** The string module contains regular expressions and functions for parsing and composing SignWriting in Unicode 8 (uni8) symbol strings.
171 * @module string
172 */
173
174var index$2 = /*#__PURE__*/Object.freeze({
175 __proto__: null,
176 re: re$1,
177 parse: parse$1,
178 compose: compose$1
179});
180
181/** The convert module contains functions to help process the various SignWriting Character sets.
182 * @module convert
183 */
184/**
185* Function to convert a SignWriting in Unicode 8 (uni8) code point to a character
186* @function convert.code2uni
187* @param {integer} code - unicode code point
188* @returns {string} SignWriting in Unicode 8 character
189* @example
190* convert.code2uni(0x1D800)
191*
192* return '𝠀'
193*/
194
195const code2uni = code => String.fromCharCode(0xD800 + (code - 0x10000 >> 10), 0xDC00 + (code - 0x10000 & 0x3FF));
196/**
197* Function to convert a SignWriting in Unicode 8 (uni8) character to a code point
198* @function convert.uni2code
199* @param {string} uni8 - SignWriting in Unicode 8 character
200* @returns {integer} unicode code point
201* @example
202* convert.uni2code('𝠀')
203*
204* return 0x1D800
205*/
206
207
208const uni2code = uni8 => uni8.codePointAt(0);
209/**
210* Function to convert a SignWriting in Unicode 8 (uni8) character to hex values
211* @function convert.uni2hex
212* @param {string} uni8 - SignWriting in Unicode 8 character
213* @returns {string} hex value of unicode character
214* @example
215* convert.uni2hex('𝠀')
216*
217* return "1D800"
218*/
219
220
221const uni2hex = uni8 => uni8.codePointAt(0).toString(16).toUpperCase();
222/**
223* Function to convert a SignWriting in Unicode 8 (uni8) symbol to Formal SignWriting in ASCII (FSW)
224* @function convert.uni2fsw
225* @param {string} uni8 - SignWriting in Unicode 8 character(s)
226* @returns {string} an FSW symbol key
227* @example
228* convert.uni2fsw('𝠀')
229*
230* return 'S10000'
231*/
232
233
234const uni2fsw = uni8 => {
235 let sym = parse(uni8);
236
237 if (sym.base) {
238 sym.base = sym.base.codePointAt(0) - 0x1D700;
239 sym.fill = sym.fill ? sym.fill.codePointAt(0) - 0x1DA9A : 0;
240 sym.rotation = sym.rotation ? sym.rotation.codePointAt(0) - 0x1DAA0 : 0;
241 return "S" + sym.base.toString(16) + sym.fill.toString(16) + sym.rotation.toString(16);
242 } else {
243 return undefined;
244 }
245};
246/**
247* Function to convert a SignWriting in Unicode 8 (uni8) symbol to SignWriting in Unicode (SWU)
248* @function convert.uni2swu
249* @param {string} uni8 - SignWriting in Unicode 8 character(s)
250* @returns {string} an SWU symbol
251* @example
252* convert.uni2swu('𝠀')
253*
254* return '񀀁'
255*/
256
257
258const uni2swu = uni8 => {
259 let sym = parse(uni8);
260
261 if (sym.base) {
262 sym.base = sym.base.codePointAt(0) - 0x1D800;
263 sym.fill = sym.fill ? sym.fill.codePointAt(0) - 0x1DA9A : 0;
264 sym.rotation = sym.rotation ? sym.rotation.codePointAt(0) - 0x1DAA0 : 0;
265 return code2uni(0x40001 + sym.base * 96 + sym.fill * 16 + sym.rotation);
266 } else {
267 return undefined;
268 }
269};
270/**
271* Function to convert a Formal SignWriting in ASCII (FSW) to SignWriting in Unicode 8 (uni8)
272* @function convert.fsw2uni
273* @param {string} fswSym - an FSW symbol key
274* @returns {string} SignWriting in Unicode 8 character(s)
275* @example
276* convert.fsw2uni('S10000')
277*
278* return '𝠀'
279*/
280
281
282const fsw2uni = fswSym => {
283 let base = parseInt(fswSym.slice(1, 4), 16);
284 let fill = parseInt(fswSym.slice(4, 5), 16);
285 let rotation = parseInt(fswSym.slice(5, 6), 16);
286 return code2uni(base + 0x1D700) + (fill ? code2uni(fill + 0x1DA9A) : '') + (rotation ? code2uni(rotation + 0x1DAA0) : '');
287};
288/**
289* Function to convert a SignWriting in Unicode (SWU) to SignWriting in Unicode 8 (uni8)
290* @function convert.swu2uni
291* @param {string} swuSym - an SWU symbol
292* @returns {string} SignWriting in Unicode 8 character(s)
293* @example
294* convert.swu2uni('񀀁')
295*
296* return '𝠀'
297*/
298
299
300const swu2uni = swuSym => {
301 const symcode = swuSym.codePointAt(0) - 0x40001;
302 const base = parseInt(symcode / 96);
303 const fill = parseInt((symcode - base * 96) / 16);
304 const rotation = parseInt(symcode - base * 96 - fill * 16);
305 return code2uni(base + 0x1D800) + (fill ? code2uni(fill + 0x1DA9A) : '') + (rotation ? code2uni(rotation + 0x1DAA0) : '');
306};
307
308var index$3 = /*#__PURE__*/Object.freeze({
309 __proto__: null,
310 code2uni: code2uni,
311 uni2code: uni2code,
312 uni2hex: uni2hex,
313 uni2fsw: uni2fsw,
314 uni2swu: uni2swu,
315 fsw2uni: fsw2uni,
316 swu2uni: swu2uni
317});
318
319export { index$3 as convert, index as font, index$2 as string, index$1 as symbol };
320
321/* support ongoing development on https://patreon.com/signwriting */