UNPKG

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