UNPKG

6.17 kBJavaScriptView Raw
1/* Sutton SignWriting Core Module v1.4.2 (https://github.com/sutton-signwriting/core), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */
2let o={symbol:"S[123][0-9a-f]{2}[0-5][0-9a-f]",coord:"[0-9]{3}x[0-9]{3}",sort:"A",box:"[BLMR]"};o.prefix=`(?:${o.sort}(?:${o.symbol})+)`,o.spatial=`${o.symbol}${o.coord}`,o.signbox=`${o.box}${o.coord}(?:${o.spatial})*`,o.sign=`${o.prefix}?${o.signbox}`,o.sortable=`${o.prefix}${o.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 e=o=>(new RegExp(`^${t.colorhex}$`).test(o)?"#":"")+o,s=o=>{const s=`^${t.full}`,i=("string"==typeof o?o.match(new RegExp(s)):[])||[];return n={colorize:i[1]?!!i[1]:void 0,padding:i[2]?parseInt(i[2].slice(1)):void 0,background:i[3]?e(i[3].slice(2,-1)):void 0,detail:i[4]?i[4].slice(2,-1).split(",").map(e):void 0,zoom:i[5]?"Zx"===i[5]?"x":parseFloat(i[5].slice(1)):void 0,detailsym:i[6]?i[6].match(new RegExp(t.detailsym,"g")).map((o=>{const t=o.split("_"),s=t[1].split(",").map(e);return{index:parseInt(t[0].slice(1)),detail:s}})):void 0,classes:i[7]?i[7]:void 0,id:i[8]?i[8]:void 0},Object.fromEntries(Object.entries(n).filter((([o,t])=>void 0!==t)));var n},i=o=>o.split("x").map((o=>parseInt(o))),n={symbol:e=>{const s=`^(${o.symbol})(${o.coord})?(${t.full})?`,n="string"==typeof e?e.match(new RegExp(s)):void 0;return{symbol:n?n[1]:void 0,coord:n&&n[2]?i(n[2]):void 0,style:n?n[3]:void 0}},sign:e=>{const s=`^(${o.prefix})?(${o.signbox})(${t.full})?`,n="string"==typeof e?e.match(new RegExp(s)):void 0;return n?{sequence:n[1]?n[1].slice(1).match(/.{6}/g):void 0,box:n[2][0],max:i(n[2].slice(1,8)),spatials:n[2].length<9?void 0:n[2].slice(8).match(/(.{13})/g).map((o=>({symbol:o.slice(0,6),coord:[parseInt(o.slice(6,9)),parseInt(o.slice(10,13))]}))),style:n[3]}:{}},text:e=>{if("string"!=typeof e)return[];const s=`(${o.sign}(${t.full})?|${o.spatial}(${t.full})?)`,i=e.match(new RegExp(s,"g"));return i?[...i]:[]}},a={symbol:e=>{if("string"==typeof e.symbol){const s=(e.symbol.match(o.symbol)||[""])[0];if(s){return s+((((e.coord&&e.coord[0]||"").toString()+"x"+(e.coord&&e.coord[1]||"").toString()).match(o.coord)||[""])[0]||"")+("string"==typeof e.style&&(e.style.match(t.full)||[""])[0]||"")}}},sign:e=>{let s="string"!=typeof e.box?"M":(e.box+"M").match(o.box);const i=(((e.max&&e.max[0]||"").toString()+"x"+(e.max&&e.max[1]||"").toString()).match(o.coord)||[""])[0]||"";if(!i)return;let n="";e.sequence&&Array.isArray(e.sequence)&&(n=e.sequence.map((t=>(t.match(o.symbol)||[""])[0])).join(""),n=n?"A"+n:"");let a="";e.spatials&&Array.isArray(e.spatials)&&(a=e.spatials.map((t=>{if("string"==typeof t.symbol){const e=(t.symbol.match(o.symbol)||[""])[0];if(e){const s=(((t.coord&&t.coord[0]||"").toString()+"x"+(t.coord&&t.coord[1]||"").toString()).match(o.coord)||[""])[0]||"";if(s)return e+s}}return""})).join(""));return n+s+i+a+("string"==typeof e.style&&(e.style.match(t.full)||[""])[0]||"")}},l=o=>{let t,e,i,a,l,r,c,p,m=n.sign(o);m.spatials?(a=Math.min(...m.spatials.map((o=>o.coord[0]))),l=m.max[0],t=l-a,r=Math.min(...m.spatials.map((o=>o.coord[1]))),c=m.max[1],e=c-r,i="sign",p=m.box):(m=n.symbol(o),p="M",m.coord?(a=m.coord[0],t=2*(500-a),r=m.coord[1],e=2*(500-r),i="symbol"):(a=490,t=20,r=490,e=20,i="none"));let d=s(m.style),g=d.zoom||1,u=d.padding||0;return{minX:a,minY:r,width:t,height:e,segment:i,lane:{B:0,L:-1,M:0,R:1}[p],padding:u,zoom:g}},r={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}},c=o=>("object"!=typeof o&&(o={}),{...r,...o,punctuation:{...r.punctuation,...o.punctuation},style:{...r.style,...o.style}}),p=(o,t)=>{if("string"!=typeof o)return{};const e=c(t);let s=n.text(o),i=0,a=[],r=[],p=0,m=parseInt(e.width/2),d=e.height-e.margin,g=!0,u=!1;for(let o of s){let t=l(o);i+=p,e.punctuation.spacing?i+="sign"==t.segment?e.pad:0:i+=e.pad,u=i+t.height>d,u&&"symbol"==t.segment&&e.punctuation.pull&&g&&(u=!1,g=!1),0==r.length&&(u=!1),u&&(i=e.pad,a.push(r),r=[],g=!0),r.push(Object.assign(t,{x:m+e.offset*t.lane-(500-t.minX)*t.zoom*e.style.zoom,y:i,text:o})),i+=t.height*t.zoom*e.style.zoom,p=e.punctuation.spacing?"sign"==t.segment?e.pad:e.punctuation.pad:e.pad}if(r.length&&a.push(r),e.punctuation.pull)for(let o of a){let t=o[o.length-1],s=t.y+t.height-(e.height-e.margin);if(s>0){let t=parseInt(s/o.length)+1;for(let e in o)o[e].y-=t*e+t}}let y=[];for(let o of a){let t=[m-e.offset-e.pad],s=[m+e.offset+e.pad];for(let i of o)t.push(i.x-e.pad),s.push(i.x+i.width+e.pad);t=Math.min(...t),s=Math.max(...s);let i=e.width,n=0;e.dynamic?(i=s-t,n=-t):n=m-parseInt((t+s)/2);for(let t of o)t.x+=n;y.push(i)}return{options:e,widths:y,columns:a}},m=[256,895,903],d=[256,517,759,767,877,895,903],g=[256,270,286,324,332,390,420,442,461,501,517,534,554,597,613,648,678,695,725,739,759,767,778,810,827,857,877,886,895,903],u={all:[256,907],writing:[256,894],hand:[256,516],movement:[517,758],dynamic:[759,766],head:[767,876],hcenter:[767,876],vcenter:[767,885],trunk:[877,885],limb:[886,894],location:[895,902],punctuation:[903,907]},y=(o,t)=>{const e=n.symbol(o);if(e.symbol){const o=parseInt(e.symbol.slice(1,4),16),s=u[t];if(s)return s[0]<=o&&s[1]>=o}return!1},f=["#0000CC","#CC0000","#FF0099","#006600","#000000","#884411","#FF9900"],h=o=>{const t=n.symbol(o);let e="#000000";if(t.symbol){const o=parseInt(t.symbol.slice(1,4),16),s=d.findIndex((t=>t>o));e=f[s<0?6:s-1]}return e};export{d as category,h as colorize,f as colors,r as columnDefaults,c as columnDefaultsMerge,p as columns,a as compose,g as group,l as info,y as isType,m as kind,n as parse,u as ranges,o as re};