UNPKG

@sutton-signwriting/core

Version:

a javascript package for node and browsers that supports general processing of the Sutton SignWriting script

3 lines (2 loc) 6.94 kB
/* Sutton SignWriting Core Module v2.0.0 (https://github.com/sutton-signwriting/core), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */ let o={null:"񀀀",symbol:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:\ud836[\udc0c-\uddff]){2}",sort:"𝠀",box:"\ud836[\udc01-\udc04]"};o.nullorsymbol=`(?:${o.null}|${o.symbol})`,o.prefix=`(?:${o.sort}(?:${o.nullorsymbol})+)`,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}`,n=("string"==typeof o?o.match(new RegExp(s)):[])||[];return l={colorize:n[1]?!!n[1]:void 0,padding:n[2]?parseInt(n[2].slice(1)):void 0,background:n[3]?e(n[3].slice(2,-1)):void 0,detail:n[4]?n[4].slice(2,-1).split(",").map(e):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 t=o.split("_"),s=t[1].split(",").map(e);return{index:parseInt(t[0].slice(1)),detail:s}})):void 0,classes:n[7]?n[7]:void 0,id:n[8]?n[8]:void 0},Object.fromEntries(Object.entries(l).filter((([o,t])=>void 0!==t)));var l},n=o=>parseInt(o.codePointAt(0))-120844+250,l=o=>[n(o.slice(0,2)),n(o.slice(2,4))],i=o=>o.map((o=>(o=>String.fromCodePoint(120844+parseInt(o)-250))(o))).join(""),a=o=>parseInt(o.codePointAt(0)),r={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]?l(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(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:e=>{if("string"!=typeof e)return[];const s=`(${o.sign}(${t.full})?|${o.spatial}(${t.full})?)`,n=e.match(new RegExp(s,"g"));return n?[...n]:[]}},c=o=>o.replace(/[\u007F-\uFFFF]/g,(function(o){return"\\u"+("0000"+o.charCodeAt(0).toString(16)).substr(-4).toUpperCase()})),d=o=>o.replace(/\\u([0-9A-F]{4})/g,(function(o,t){return String.fromCharCode(parseInt(t,16))})),p=o=>[o.charCodeAt(0).toString(16).toUpperCase(),o.charCodeAt(1).toString(16).toUpperCase()],u={symbol:e=>{if("object"==typeof e&&null!==e&&"string"==typeof e.symbol){const s=(e.symbol.match(o.symbol)||[""])[0];if(s){const o=e.coord&&e.coord[0]||"",n=e.coord&&e.coord[1]||"";return s+(o&&n?i([o,n]):"")+("string"==typeof e.style&&(e.style.match(t.full)||[""])[0]||"")}}},sign:e=>{if("object"!=typeof e||null===e)return;let s="string"!=typeof e.box?"𝠃":(e.box+"𝠃").match(o.box);const n=e.max&&e.max[0]||"",l=e.max&&e.max[1]||"",a=n&&l?i([n,l]):void 0;if(!a)return;let r="";e.sequence&&Array.isArray(e.sequence)&&(r=e.sequence.map((t=>(t.match(o.nullorsymbol)||[""])[0])).join(""),r=r?"𝠀"+r:"");let c="";e.spatials&&Array.isArray(e.spatials)&&(c=e.spatials.map((t=>{if("string"==typeof t.symbol){const e=(t.symbol.match(o.symbol)||[""])[0];if(e){const o=t.coord&&t.coord[0]||"",s=t.coord&&t.coord[1]||"",n=o&&s?i([o,s]):"";if(n)return e+n}}return""})).join(""));return r+s+a+c+("string"==typeof e.style&&(e.style.match(t.full)||[""])[0]||"")}},m=o=>{let t,e,n,l,i,a,c,d,p=r.sign(o);p.spatials?(l=Math.min(...p.spatials.map((o=>o.coord[0]))),i=p.max[0],t=i-l,a=Math.min(...p.spatials.map((o=>o.coord[1]))),c=p.max[1],e=c-a,n="sign",d=p.box):(p=r.symbol(o),d="𝠃",p.coord?(l=p.coord[0],t=2*(500-l),a=p.coord[1],e=2*(500-a),n="symbol"):(l=490,t=20,a=490,e=20,n="none"));let u=s(p.style),m=u.zoom||1,f=u.padding||0;return{minX:l,minY:a,width:t,height:e,segment:n,lane:{"𝠁":0,"𝠂":-1,"𝠃":0,"𝠄":1}[d],padding:f,zoom:m}},f={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}},g=o=>("object"!=typeof o&&(o={}),{...f,...o,punctuation:{...f.punctuation,...o.punctuation},style:{...f.style,...o.style}}),y=(o,t)=>{if("string"!=typeof o)return{};const e=g(t);let s=r.text(o),n=0,l=[],i=[],a=0,c=parseInt(e.width/2),d=e.height-e.margin,p=!0,u=!1;for(let o of s){let t=m(o);n+=a,e.punctuation.spacing?n+="sign"==t.segment?e.pad:0:n+=e.pad,u=n+t.height>d,u&&"symbol"==t.segment&&e.punctuation.pull&&p&&(u=!1,p=!1),0==i.length&&(u=!1),u&&(n=e.pad,l.push(i),i=[],p=!0),i.push(Object.assign(t,{x:c+e.offset*t.lane-(500-t.minX)*t.zoom*e.style.zoom,y:n,text:o})),n+=t.height*t.zoom*e.style.zoom,a=e.punctuation.spacing?"sign"==t.segment?e.pad:e.punctuation.pad:e.pad}if(i.length&&l.push(i),e.punctuation.pull)for(let o of l){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 f=[];for(let o of l){let t=[c-e.offset-e.pad],s=[c+e.offset+e.pad];for(let n of o)t.push(n.x-e.pad),s.push(n.x+n.width+e.pad);t=Math.min(...t),s=Math.max(...s);let n=e.width,l=0;e.dynamic?(n=s-t,l=-t):l=c-parseInt((t+s)/2);for(let t of o)t.x+=l;f.push(n)}return{options:e,widths:f,columns:l}},h=[262145,323489,324257],b=[262145,287201,310433,311201,321761,323489,324257],x=[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],$={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]},v=(o,t)=>{const e=r.symbol(o);if(e.symbol){const o=a(e.symbol),s=$[t];if(s)return s[0]<=o&&s[1]>=o}return!1},z=["#0000CC","#CC0000","#FF0099","#006600","#000000","#884411","#FF9900"],A=o=>{const t=r.symbol(o);let e="#000000";if(t.symbol){const o=a(t.symbol),s=b.findIndex((t=>t>o));e=z[s<0?6:s-1]}return e};export{b as category,A as colorize,z as colors,f as columnDefaults,g as columnDefaultsMerge,y as columns,u as compose,d as decode,c as encode,x as group,m as info,v as isType,h as kind,p as pair,r as parse,$ as ranges,o as re};