UNPKG

7.13 kBJavaScriptView Raw
1/* Sutton SignWriting Core Module v1.4.2 (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.swu={}))}(this,(function(o){"use strict";let e={symbol:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:\ud836[\udc0c-\uddff]){2}",sort:"𝠀",box:"\ud836[\udc01-\udc04]"};e.prefix=`(?:${e.sort}(?:${e.symbol})+)`,e.spatial=`${e.symbol}${e.coord}`,e.signbox=`${e.box}${e.coord}(?:${e.spatial})*`,e.sign=`${e.prefix}?${e.signbox}`,e.sortable=`${e.prefix}${e.signbox}`;let t={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)",classbase:"-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}",id:"[a-zA-Z][_a-zA-Z0-9-]{0,100}"};t.colorbase=`(?:${t.colorhex}|${t.colorname})`,t.color=`_${t.colorbase}_`,t.colors=`_${t.colorbase}(?:,${t.colorbase})?_`,t.background=`G${t.color}`,t.detail=`D${t.colors}`,t.detailsym=`D[0-9]{2}${t.colors}`,t.classes=`${t.classbase}(?: ${t.classbase})*`,t.full=`-(${t.colorize})?(${t.padding})?(${t.background})?(${t.detail})?(${t.zoom})?(?:-((?:${t.detailsym})*))?(?:-(${t.classes})?!(?:(${t.id})!)?)?`;const s=o=>(new RegExp(`^${t.colorhex}$`).test(o)?"#":"")+o,n=o=>{const e=`^${t.full}`,n=("string"==typeof o?o.match(new RegExp(e)):[])||[];return i={colorize:n[1]?!!n[1]:void 0,padding:n[2]?parseInt(n[2].slice(1)):void 0,background:n[3]?s(n[3].slice(2,-1)):void 0,detail:n[4]?n[4].slice(2,-1).split(",").map(s):void 0,zoom:n[5]?"Zx"===n[5]?"x":parseFloat(n[5].slice(1)):void 0,detailsym:n[6]?n[6].match(new RegExp(t.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,classes:n[7]?n[7]:void 0,id:n[8]?n[8]:void 0},Object.fromEntries(Object.entries(i).filter((([o,e])=>void 0!==e)));var i},i=o=>parseInt(o.codePointAt(0))-120844+250,l=o=>[i(o.slice(0,2)),i(o.slice(2,4))],a=o=>o.map((o=>(o=>String.fromCodePoint(120844+parseInt(o)-250))(o))).join(""),r=o=>parseInt(o.codePointAt(0)),c={symbol:o=>{const s=`^(${e.symbol})(${e.coord})?(${t.full})?`,n="string"==typeof o?o.match(new RegExp(s)):void 0;return{symbol:n?n[1]:void 0,coord:n&&n[2]?l(n[2]):void 0,style:n?n[3]:void 0}},sign:o=>{const s=`^(${e.prefix})?(${e.signbox})(${t.full})?`,n="string"==typeof o?o.match(new RegExp(s)):void 0;return n?{sequence:n[1]?n[1].slice(2).match(/.{2}/g):void 0,box:n[2].slice(0,2),max:l(n[2].slice(2,6)),spatials:n[2].length<7?void 0:n[2].slice(6).match(/(.{6})/g).map((o=>({symbol:o.slice(0,2),coord:l(o.slice(2))}))),style:n[3]}:{}},text:o=>{if("string"!=typeof o)return[];const s=`(${e.sign}(${t.full})?|${e.spatial}(${t.full})?)`,n=o.match(new RegExp(s,"g"));return n?[...n]:[]}},d={symbol:o=>{if("object"==typeof o&&null!==o&&"string"==typeof o.symbol){const s=(o.symbol.match(e.symbol)||[""])[0];if(s){const e=o.coord&&o.coord[0]||"",n=o.coord&&o.coord[1]||"";return s+(e&&n?a([e,n]):"")+("string"==typeof o.style&&(o.style.match(t.full)||[""])[0]||"")}}},sign:o=>{if("object"!=typeof o||null===o)return;let s="string"!=typeof o.box?"𝠃":(o.box+"𝠃").match(e.box);const n=o.max&&o.max[0]||"",i=o.max&&o.max[1]||"",l=n&&i?a([n,i]):void 0;if(!l)return;let r="";o.sequence&&Array.isArray(o.sequence)&&(r=o.sequence.map((o=>(o.match(e.symbol)||[""])[0])).join(""),r=r?"𝠀"+r:"");let c="";o.spatials&&Array.isArray(o.spatials)&&(c=o.spatials.map((o=>{if("string"==typeof o.symbol){const t=(o.symbol.match(e.symbol)||[""])[0];if(t){const e=o.coord&&o.coord[0]||"",s=o.coord&&o.coord[1]||"",n=e&&s?a([e,s]):"";if(n)return t+n}}return""})).join(""));return r+s+l+c+("string"==typeof o.style&&(o.style.match(t.full)||[""])[0]||"")}},p=o=>{let e,t,s,i,l,a,r,d,p=c.sign(o);p.spatials?(i=Math.min(...p.spatials.map((o=>o.coord[0]))),l=p.max[0],e=l-i,a=Math.min(...p.spatials.map((o=>o.coord[1]))),r=p.max[1],t=r-a,s="sign",d=p.box):(p=c.symbol(o),d="𝠃",p.coord?(i=p.coord[0],e=2*(500-i),a=p.coord[1],t=2*(500-a),s="symbol"):(i=490,e=20,a=490,t=20,s="none"));let u=n(p.style),m=u.zoom||1,f=u.padding||0;return{minX:i,minY:a,width:e,height:t,segment:s,lane:{"𝠁":0,"𝠂":-1,"𝠃":0,"𝠄":1}[d],padding:f,zoom:m}},u={height:500,width:150,offset:50,pad:20,margin:5,dynamic:!1,background:void 0,punctuation:{spacing:!0,pad:30,pull:!0},style:{detail:["black","white"],zoom:1}},m=o=>("object"!=typeof o&&(o={}),{...u,...o,punctuation:{...u.punctuation,...o.punctuation},style:{...u.style,...o.style}}),f=[262145,287201,310433,311201,321761,323489,324257],g={all:[262145,324736],writing:[262145,323488],hand:[262145,287200],movement:[287201,310432],dynamic:[310433,311200],head:[311201,321760],hcenter:[311201,321760],vcenter:[311201,322624],trunk:[321761,322624],limb:[322625,323488],location:[323489,324256],punctuation:[324257,324736]},y=["#0000CC","#CC0000","#FF0099","#006600","#000000","#884411","#FF9900"];o.category=f,o.colorize=o=>{const e=c.symbol(o);let t="#000000";if(e.symbol){const o=r(e.symbol),s=f.findIndex((e=>e>o));t=y[s<0?6:s-1]}return t},o.colors=y,o.columnDefaults=u,o.columnDefaultsMerge=m,o.columns=(o,e)=>{if("string"!=typeof o)return{};const t=m(e);let s=c.text(o),n=0,i=[],l=[],a=0,r=parseInt(t.width/2),d=t.height-t.margin,u=!0,f=!1;for(let o of s){let e=p(o);n+=a,t.punctuation.spacing?n+="sign"==e.segment?t.pad:0:n+=t.pad,f=n+e.height>d,f&&"symbol"==e.segment&&t.punctuation.pull&&u&&(f=!1,u=!1),0==l.length&&(f=!1),f&&(n=t.pad,i.push(l),l=[],u=!0),l.push(Object.assign(e,{x:r+t.offset*e.lane-(500-e.minX)*e.zoom*t.style.zoom,y:n,text:o})),n+=e.height*e.zoom*t.style.zoom,a=t.punctuation.spacing?"sign"==e.segment?t.pad:t.punctuation.pad:t.pad}if(l.length&&i.push(l),t.punctuation.pull)for(let o of i){let e=o[o.length-1],s=e.y+e.height-(t.height-t.margin);if(s>0){let e=parseInt(s/o.length)+1;for(let t in o)o[t].y-=e*t+e}}let g=[];for(let o of i){let e=[r-t.offset-t.pad],s=[r+t.offset+t.pad];for(let n of o)e.push(n.x-t.pad),s.push(n.x+n.width+t.pad);e=Math.min(...e),s=Math.max(...s);let n=t.width,i=0;t.dynamic?(n=s-e,i=-e):i=r-parseInt((e+s)/2);for(let e of o)e.x+=i;g.push(n)}return{options:t,widths:g,columns:i}},o.compose=d,o.decode=o=>o.replace(/\\u([0-9A-F]{4})/g,(function(o,e){return String.fromCharCode(parseInt(e,16))})),o.encode=o=>o.replace(/[\u007F-\uFFFF]/g,(function(o){return"\\u"+("0000"+o.charCodeAt(0).toString(16)).substr(-4).toUpperCase()})),o.group=[262145,263489,265025,268673,269441,275009,277889,280001,281825,285665,287201,288833,290753,294881,296417,299777,302657,304289,307169,308513,310433,311201,312257,315329,316961,319841,321761,322625,323489,324257],o.info=p,o.isType=(o,e)=>{const t=c.symbol(o);if(t.symbol){const o=r(t.symbol),s=g[e];if(s)return s[0]<=o&&s[1]>=o}return!1},o.kind=[262145,323489,324257],o.pair=o=>[o.charCodeAt(0).toString(16).toUpperCase(),o.charCodeAt(1).toString(16).toUpperCase()],o.parse=c,o.ranges=g,o.re=e,Object.defineProperty(o,"__esModule",{value:!0})}));