UNPKG

3.5 kBJavaScriptView Raw
1/* Sutton SignWriting Core Module v1.3.1 (https://github.com/sutton-signwriting/core), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */
2!function(o,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(((o="undefined"!=typeof globalThis?globalThis:o||self).ssw=o.ssw||{},o.ssw.style={}))}(this,(function(o){"use strict";let e={colorize:"C",colorhex:"(?:[0-9a-fA-F]{3}){1,2}",colorname:"[a-zA-Z]+",padding:"P[0-9]{2}",zoom:"Z(?:[0-9]+(?:\\.[0-9]+)?|x)",zoomsym:"Z[0-9]{2},[0-9]+(?:\\.[0-9]+)?(?:,[0-9]{3}x[0-9]{3})?",classbase:"-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}",id:"[a-zA-Z][_a-zA-Z0-9-]{0,100}"};e.colorbase=`(?:${e.colorhex}|${e.colorname})`,e.color=`_${e.colorbase}_`,e.colors=`_${e.colorbase}(?:,${e.colorbase})?_`,e.background=`G${e.color}`,e.detail=`D${e.colors}`,e.detailsym=`D[0-9]{2}${e.colors}`,e.classes=`${e.classbase}(?: ${e.classbase})*`,e.full=`-(${e.colorize})?(${e.padding})?(${e.background})?(${e.detail})?(${e.zoom})?(?:-((?:${e.detailsym})*)((?:${e.zoomsym})*))?(?:-(${e.classes})?!(?:(${e.id})!)?)?`;const s=o=>(new RegExp(`^${e.colorhex}$`).test(o)?"#":"")+o;o.compose=o=>{if("object"!=typeof o||null===o)return;let s="-";s+=o.colorize?"C":"";const t=parseInt(o.padding);s+=!t||t<=0||t>99?"":"P"+(t>9?t:"0"+t);const a=o.background&&"string"==typeof o.background?o.background.match(e.colorbase)[0]:void 0;s+=a?"G_"+a+"_":"";const i=o.detail&&o.detail[0]&&"string"==typeof o.detail[0]?o.detail[0].match(e.colorbase)[0]:void 0,l=o.detail&&o.detail[1]&&"string"==typeof o.detail[1]?o.detail[1].match(e.colorbase)[0]:void 0;i&&(s+="D_"+i,l&&(s+=","+l),s+="_");const r="x"===o.zoom?"x":parseFloat(o.zoom);s+=!r||r<=0?"":"Z"+r;let c="";const n=o.detailsym&&Array.isArray(o.detailsym)?o.detailsym.map((o=>{const s=parseInt(o.index);if(!s||s<=0||s>99)return"";let t="D"+(s>9?s:"0"+s);const a=o.detail&&o.detail[0]?o.detail[0].match(e.colorbase)[0]:void 0,i=o.detail&&o.detail[1]?o.detail[1].match(e.colorbase)[0]:void 0;return a&&(t+="_"+a,i&&(t+=","+i),t+="_"),t})):[];c+=n.join("");const d=o.zoomsym&&Array.isArray(o.zoomsym)?o.zoomsym.map((o=>{const e=parseInt(o.index);if(!e||e<=0||e>99)return"";let s="Z"+(e>9?e:"0"+e);const t=parseFloat(o.zoom);if(s+=!t||t<=0?"":","+t,o.offset&&0 in o.offset&&1 in o.offset){const e=parseInt(o.offset[0])+500,t=parseInt(o.offset[1])+500;e>=250&&e<750&&t>=250&&t<750&&(s+=","+e+"x"+t)}return s})):[];c+=d.join("");let m="";const p=o.classes&&"string"==typeof o.classes?o.classes.match(e.classes)[0]:void 0;m+=p||"";const f=o.id&&"string"==typeof o.id?o.id.match(e.id)[0]:void 0;return m+=p||f?"!":"",m+=f?f+"!":"",s+(c||m?"-"+c:"")+(m?"-"+m:"")},o.parse=o=>{const t=`^${e.full}`,a=("string"==typeof o?o.match(new RegExp(t)):[])||[];return{colorize:a[1]?!!a[1]:void 0,padding:a[2]?parseInt(a[2].slice(1)):void 0,background:a[3]?s(a[3].slice(2,-1)):void 0,detail:a[4]?a[4].slice(2,-1).split(",").map(s):void 0,zoom:a[5]?"Zx"===a[5]?"x":parseFloat(a[5].slice(1)):void 0,detailsym:a[6]?a[6].match(new RegExp(e.detailsym,"g")).map((o=>{const e=o.split("_"),t=e[1].split(",").map(s);return{index:parseInt(e[0].slice(1)),detail:t}})):void 0,zoomsym:a[7]?a[7].match(new RegExp(e.zoomsym,"g")).map((o=>{const e=o.split(",");return{index:parseInt(e[0].slice(1)),zoom:parseFloat(e[1]),offset:e[2]?e[2].split("x").map((o=>parseInt(o)-500)):void 0}})):void 0,classes:a[8]?a[8]:void 0,id:a[9]?a[9]:void 0}},o.re=e,Object.defineProperty(o,"__esModule",{value:!0})}));