UNPKG

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