UNPKG

@sutton-signwriting/unicode8

Version:

a javascript package for processing SignWriting in Unicode 8 (uni8) characters

312 lines (287 loc) 9.42 kB
/** * Sutton SignWriting Unicode 8 Module v1.2.0 (https://github.com/sutton-signwriting/unicode8) * Author: Steve Slevinski (https://SteveSlevinski.me) * Sponsor: https://patreon.com/signwriting * Donate: https://donate.sutton-signwriting.io * * unicode8.mjs is released under the MIT License. */ /** * Function that appends font-face CSS for the Noto Sans SignWriting font for use with SignWriting in Unicode 8 (uni8) characters. * * This font-face declaration will use a locally installed font if available. * If not found, the font will be loaded from a content delivery network. * * The list of local names is currently a work in progress. * The font-face currently works for Windows and iOS. * This CSS is duplicated in the src/font/index.css file. * * @function font.cssAppend * @param {string} dir - an optional relative directory for font location * @example * font.cssAppend('./font/') */ const cssAppend = function (dir = '') { const id = "SgnwUnicode8FontCss"; if (!document.getElementById(id)) { const style = document.createElement('style'); style.setAttribute("id", "SgnwUnicode8FontCss"); style.appendChild(document.createTextNode(` @font-face { font-family: "NotoSansSignWriting"; src: local('NotoSansSignWriting'), local('Noto Sans SignWriting'), local('Noto_Sans_SignWriting'), local('Noto Sans SignWriting Regular'), local('Noto_Sans_SignWriting_Regular'), ${dir ? `url('${dir}NotoSansSignWriting-Regular.ttf') format('truetype'),` : ""} url('https://notofonts.github.io/sign-writing/fonts/NotoSansSignWriting/full/ttf/NotoSansSignWriting-Regular.ttf') format('opentype'); } `)); document.head.appendChild(style); } }; /** The font module contains functions for handing the font. * @module font */ var index$3 = /*#__PURE__*/Object.freeze({ __proto__: null, cssAppend: cssAppend }); /** * Object of regular expressions for symbol strings * * { base, fill, rotation, full } * @alias symbol.re * @type {object} */ let re$1 = { 'base': '(?:\uD836[\uDC00-\uDE8B])', 'fill': '(?:\uD836[\uDE9B-\uDE9F])', 'rotation': '(?:\uD836[\uDEA1-\uDEAF])' }; re$1.full = `(${re$1.base})(${re$1.fill})?(${re$1.rotation})?`; /** * Function to parse symbol string to object * @function symbol.parse * @param {string} symbolString - a symbol string * @returns {object} elements of symbol string * @example * symbol.parse('𝠀') * * return { * 'base': '𝠀', * 'fill': undefined, * 'rotation': undefined * } */ const parse$1 = symbolString => { const regex = `^${re$1.full}`; const m = (typeof symbolString === 'string' ? symbolString.match(new RegExp(regex)) : []) || []; return { 'base': !m[1] ? undefined : m[1], 'fill': !m[2] ? undefined : m[2], 'rotation': !m[3] ? undefined : m[3] }; }; /** * Function to compose symbol string from object * @function symbol.compose * @param {object} symbolObject - an object of symbol parts * @param {string} symbolObject.base - base character for symbol * @param {string} symbolObject.fill - fill character for symbol * @param {string} symbolObject.rotation - rotation character for symbol * @returns {string} symbol string * @example * symbol.compose({ * 'base': '𝠀' * }) * * return '𝠀' */ const compose$1 = symbolObject => { if (typeof symbolObject !== 'object' || symbolObject === null) return undefined; const sym = (symbolObject.base ? symbolObject.base : '') + (symbolObject.fill ? symbolObject.fill : '') + (symbolObject.rotation ? symbolObject.rotation : ''); return sym ? sym : undefined; }; /** The symbol module contains regular expressions and functions for parsing and composing SignWriting in Unicode 8 (uni8) characters. * @module symbol */ var index$2 = /*#__PURE__*/Object.freeze({ __proto__: null, re: re$1, parse: parse$1, compose: compose$1 }); /** * Object of regular expressions for string of symbols * * { full } * @alias string.re * @type {object} */ let re = { 'full': `(?:${re$1.full})+` }; /** * Function to parse string of uni8 symbols to array * @function string.parse * @param {string} uni8String - a string of uni8 symbols * @returns {array} array of uni8 symbols * @example * string.parse('𝠀𝠁') * * return ['𝠀','𝠁'] */ const parse = uni8String => { const regex = `^(${re.full})`; const m = (typeof uni8String === 'string' ? uni8String.match(new RegExp(regex)) : []) || []; return !m[1] ? [] : [...m[1].matchAll(new RegExp(re$1.full, 'g'))].map(mm => mm[0]); }; /** * Function to compose uni8 string from array * @function string.compose * @param {array} stringArray - an array of uni8 symbols * @returns {string} string of uni8 symbols * @example * string.compose(['𝠀','𝠁']) * * return '𝠀𝠁' */ const compose = stringArray => { if (!Array.isArray(stringArray)) return undefined; return stringArray.join(''); }; /** The string module contains regular expressions and functions for parsing and composing SignWriting in Unicode 8 (uni8) symbol strings. * @module string */ var index$1 = /*#__PURE__*/Object.freeze({ __proto__: null, re: re, parse: parse, compose: compose }); /** The convert module contains functions to help process the various SignWriting Character sets. * @module convert */ /** * Function to convert a SignWriting in Unicode 8 (uni8) code point to a character * @function convert.code2uni * @param {integer} code - unicode code point * @returns {string} SignWriting in Unicode 8 character * @example * convert.code2uni(0x1D800) * * return '𝠀' */ const code2uni = code => String.fromCharCode(0xD800 + (code - 0x10000 >> 10), 0xDC00 + (code - 0x10000 & 0x3FF)); /** * Function to convert a SignWriting in Unicode 8 (uni8) character to a code point * @function convert.uni2code * @param {string} uni8 - SignWriting in Unicode 8 character * @returns {integer} unicode code point * @example * convert.uni2code('𝠀') * * return 0x1D800 */ const uni2code = uni8 => uni8.codePointAt(0); /** * Function to convert a SignWriting in Unicode 8 (uni8) character to hex values * @function convert.uni2hex * @param {string} uni8 - SignWriting in Unicode 8 character * @returns {string} hex value of unicode character * @example * convert.uni2hex('𝠀') * * return "1D800" */ const uni2hex = uni8 => uni8.codePointAt(0).toString(16).toUpperCase(); /** * Function to convert a SignWriting in Unicode 8 (uni8) symbol to Formal SignWriting in ASCII (FSW) * @function convert.uni2fsw * @param {string} uni8 - SignWriting in Unicode 8 character(s) * @returns {string} an FSW symbol key * @example * convert.uni2fsw('𝠀') * * return 'S10000' */ const uni2fsw = uni8 => { let sym = parse$1(uni8); if (sym.base) { sym.base = sym.base.codePointAt(0) - 0x1D700; sym.fill = sym.fill ? sym.fill.codePointAt(0) - 0x1DA9A : 0; sym.rotation = sym.rotation ? sym.rotation.codePointAt(0) - 0x1DAA0 : 0; return "S" + sym.base.toString(16) + sym.fill.toString(16) + sym.rotation.toString(16); } else { return undefined; } }; /** * Function to convert a SignWriting in Unicode 8 (uni8) symbol to SignWriting in Unicode (SWU) * @function convert.uni2swu * @param {string} uni8 - SignWriting in Unicode 8 character(s) * @returns {string} an SWU symbol * @example * convert.uni2swu('𝠀') * * return '񀀁' */ const uni2swu = uni8 => { let sym = parse$1(uni8); if (sym.base) { sym.base = sym.base.codePointAt(0) - 0x1D800; sym.fill = sym.fill ? sym.fill.codePointAt(0) - 0x1DA9A : 0; sym.rotation = sym.rotation ? sym.rotation.codePointAt(0) - 0x1DAA0 : 0; return code2uni(0x40001 + sym.base * 96 + sym.fill * 16 + sym.rotation); } else { return undefined; } }; /** * Function to convert a Formal SignWriting in ASCII (FSW) to SignWriting in Unicode 8 (uni8) * @function convert.fsw2uni * @param {string} fswSym - an FSW symbol key * @returns {string} SignWriting in Unicode 8 character(s) * @example * convert.fsw2uni('S10000') * * return '𝠀' */ const fsw2uni = fswSym => { let base = parseInt(fswSym.slice(1, 4), 16); let fill = parseInt(fswSym.slice(4, 5), 16); let rotation = parseInt(fswSym.slice(5, 6), 16); return code2uni(base + 0x1D700) + (fill ? code2uni(fill + 0x1DA9A) : '') + (rotation ? code2uni(rotation + 0x1DAA0) : ''); }; /** * Function to convert a SignWriting in Unicode (SWU) to SignWriting in Unicode 8 (uni8) * @function convert.swu2uni * @param {string} swuSym - an SWU symbol * @returns {string} SignWriting in Unicode 8 character(s) * @example * convert.swu2uni('񀀁') * * return '𝠀' */ const swu2uni = swuSym => { const symcode = swuSym.codePointAt(0) - 0x40001; const base = parseInt(symcode / 96); const fill = parseInt((symcode - base * 96) / 16); const rotation = parseInt(symcode - base * 96 - fill * 16); return code2uni(base + 0x1D800) + (fill ? code2uni(fill + 0x1DA9A) : '') + (rotation ? code2uni(rotation + 0x1DAA0) : ''); }; var index = /*#__PURE__*/Object.freeze({ __proto__: null, code2uni: code2uni, uni2code: uni2code, uni2hex: uni2hex, uni2fsw: uni2fsw, uni2swu: uni2swu, fsw2uni: fsw2uni, swu2uni: swu2uni }); export { index as convert, index$3 as font, index$1 as string, index$2 as symbol }; /* the end */