UNPKG

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