UNPKG

11.6 kBJavaScriptView Raw
1/* Sutton SignWriting TrueType Font Module v1.2.0 (https://github.com/sutton-signwriting/font-ttf), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */
2const o=o=>o.map(o=>(o=>String.fromCodePoint(120844+parseInt(o)-250))(o)).join(""),t=o=>(o=>parseInt(o.codePointAt(0)))(o)-262144;let e={};const l=document.createElement("canvas");l.width=152,l.height=152;const s=l.getContext("2d"),i=function(o){return function(o){if(o in e)return[...e[o]];s.clearRect(0,0,152,152),s.font="60px 'SuttonSignWritingLine'",s.fillText(String.fromCodePoint(o+983040),0,0);const t=s.getImageData(0,0,152,152).data;let l,i,a,n;o:for(l=151;l>=0;l--)for(i=0;i<152;i+=1)for(n=0;n<4;n+=1)if(a=4*l+4*i*152+n,t[a])break o;var d=l;o:for(i=151;i>=0;i--)for(l=0;l<d;l+=1)for(n=0;n<4;n+=1)if(a=4*l+4*i*152+n,t[a])break o;var r=i+1;if(d=Math.ceil(d/2),r=Math.ceil(r/2),14394==o&&(d=19),[10468,10480,10496,10512,10500,10532,10548,10862,10878,10894,11058,11074,11476,11488,11492,11504,11508,11520,10516,10910,10926,11042,11082,10942].includes(o)&&(d=20),31921==o&&(d=22),38460==o&&(d=23),[20164,20212].includes(o)&&(d=25),31894==o&&(d=28),46698==o&&(d=29),29606==o&&(d=30),44855==o&&(d=40),32667==o&&(d=50),[11088,11474,11490,11506].includes(o)&&(r=20),6285==o&&(r=21),40804==o&&(r=31),41475==o&&(r=36),0==d&&0==r){const t={9:[15,30],10:[21,30],11:[30,15],12:[30,21],13:[15,30],14:[21,30]};o in t&&(d=t[o][0],r=t[o][1])}return 0!=d||0!=r?(e[o]=[d,r],[d,r]):void 0}(t(o))},a=function(o){return String.fromCodePoint(o+983040)},n=function(o){return String.fromCodePoint(o+1048576)},d=function(o){return a(t(o))},r=function(o){return n(t(o))},c=function(o){return e=t(o),` <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;">${n(e)}</text>\n <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;">${a(e)}</text>`;var e};let m={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}"};m.colorbase=`(?:${m.colorhex}|${m.colorname})`,m.color=`_${m.colorbase}_`,m.colors=`_${m.colorbase}(?:,${m.colorbase})?_`,m.background="G"+m.color,m.detail="D"+m.colors,m.detailsym="D[0-9]{2}"+m.colors,m.classes=`${m.classbase}(?: ${m.classbase})*`,m.full=`-(${m.colorize})?(${m.padding})?(${m.background})?(${m.detail})?(${m.zoom})?(?:-((?:${m.detailsym})*)((?:${m.zoomsym})*))?(?:-(${m.classes})?!(?:(${m.id})!)?)?`;const p=o=>(new RegExp(`^${m.colorhex}$`).test(o)?"#":"")+o,f=o=>{const t="^"+m.full,e=("string"==typeof o?o.match(new RegExp(t)):[])||[];return{colorize:e[1]?!!e[1]:void 0,padding:e[2]?parseInt(e[2].slice(1)):void 0,background:e[3]?p(e[3].slice(2,-1)):void 0,detail:e[4]?e[4].slice(2,-1).split(",").map(p):void 0,zoom:e[5]?"Zx"===e[5]?"x":parseFloat(e[5].slice(1)):void 0,detailsym:e[6]?e[6].match(new RegExp(m.detailsym,"g")).map(o=>{const t=o.split("_"),e=t[1].split(",").map(p);return{index:parseInt(t[0].slice(1)),detail:e}}):void 0,zoomsym:e[7]?e[7].match(new RegExp(m.zoomsym,"g")).map(o=>{const t=o.split(",");return{index:parseInt(t[0].slice(1)),zoom:parseFloat(t[1]),offset:t[2]?t[2].split("x").map(o=>parseInt(o)-500):void 0}}):void 0,classes:e[8]?e[8]:void 0,id:e[9]?e[9]:void 0}};let g={symbol:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:\ud836[\udc0c-\uddff]){2}",sort:"𝠀",box:"\ud836[\udc01-\udc04]"};g.prefix=`(?:${g.sort}(?:${g.symbol})+)`,g.spatial=`${g.symbol}${g.coord}`,g.signbox=`${g.box}${g.coord}(?:${g.spatial})*`,g.sign=`${g.prefix}?${g.signbox}`,g.sortable=`${g.prefix}${g.signbox}`;let x={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}"};x.colorbase=`(?:${x.colorhex}|${x.colorname})`,x.color=`_${x.colorbase}_`,x.colors=`_${x.colorbase}(?:,${x.colorbase})?_`,x.background="G"+x.color,x.detail="D"+x.colors,x.detailsym="D[0-9]{2}"+x.colors,x.classes=`${x.classbase}(?: ${x.classbase})*`,x.full=`-(${x.colorize})?(${x.padding})?(${x.background})?(${x.detail})?(${x.zoom})?(?:-((?:${x.detailsym})*)((?:${x.zoomsym})*))?(?:-(${x.classes})?!(?:(${x.id})!)?)?`;const $=o=>parseInt(o.codePointAt(0))-120844+250,y=o=>[$(o.slice(0,2)),$(o.slice(2,4))],u=o=>{const t=`^(${g.symbol})(${g.coord})?(${x.full})?`,e="string"==typeof o?o.match(new RegExp(t)):void 0;return{symbol:e?e[1]:void 0,coord:e&&e[2]?y(e[2]):void 0,style:e?e[3]:void 0}},b=o=>{const t=`^(${g.prefix})?(${g.signbox})(${x.full})?`,e="string"==typeof o?o.match(new RegExp(t)):void 0;return e?{sequence:e[1]?e[1].slice(2).match(/.{2}/g):void 0,box:e[2].slice(0,2),max:y(e[2].slice(2,6)),spatials:e[2].length<7?void 0:e[2].slice(6).match(/(.{6})/g).map(o=>({symbol:o.slice(0,2),coord:y(o.slice(2))})),style:e[3]}:{}},h=[262145,287201,310433,311201,321761,323489,324257],z=[311201,321760],v=[311201,322624],w=["#0000CC","#CC0000","#FF0099","#006600","#000000","#884411","#FF9900"],S=o=>{const t=u(o);let e="#000000";if(t.symbol){const o=(o=>parseInt(o.codePointAt(0)))(t.symbol),l=h.findIndex(t=>t>o);e=w[l<0?6:l-1]}return e},k=o=>{const t=u(o);if(t.symbol){let e=i(t.symbol);if(e){let l,s=f(t.style),i=c(t.symbol);i=` <g transform="translate(500,500)">\n${i}\n </g>`,s.colorize?l=S(t.symbol):s.detail&&(l=s.detail[0]),l&&(i=i.replace(/class="sym-line" fill="black"/,`class="sym-line" fill="${l}"`));let a=s.detail&&s.detail[1];a&&(i=i.replace(/class="sym-fill" fill="white"/,`class="sym-fill" fill="${a}"`));let n=500,d=500,r="";s.padding&&(n-=s.padding,d-=s.padding,e[0]+=2*s.padding,e[1]+=2*s.padding),s.background&&(r=`\n <rect x="${n}" y="${d}" width="${e[0]}" height="${e[1]}" style="fill:${s.background};" />`);let m="";"x"!=s.zoom&&(m=` width="${e[0]*(s.zoom?s.zoom:1)}" height="${e[1]*(s.zoom?s.zoom:1)}"`);let p="";s.classes&&(p=` class="${s.classes}"`);let g="";return s.id&&(g=` id="${s.id}"`),`<svg${p}${g} version="1.1" xmlns="http://www.w3.org/2000/svg"${m} viewBox="${n} ${d} ${e[0]} ${e[1]}">\n <text font-size="0">${o}</text>${r}\n${i}\n</svg>`}}return'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>'},I=o=>{const t=function(o){const t=u(o);if(t.symbol){let o=i(t.symbol);if(o){const e=document.createElement("canvas"),l=e.getContext("2d");let s=f(t.style),i="black";s.colorize?i=S(t.symbol):s.detail&&(i=s.detail[0]);let a=s.detail&&s.detail[1]||"white",n=500,c=n+o[0],m=500,p=m+o[1];s.padding&&(n-=s.padding,m-=s.padding,c+=s.padding,p+=s.padding);let g=1;"x"!=s.zoom&&(g=s.zoom);let x=(c-n)*g,$=(p-m)*g;return e.width=x||1,e.height=$||1,s.background&&(l.rect(0,0,x,$),l.fillStyle=s.background,l.fill()),l.font=30*g+"px 'SuttonSignWritingFill'",l.fillStyle=a,l.fillText(r(t.symbol),(500-n)*g,(500-m)*g),l.font=30*g+"px 'SuttonSignWritingLine'",l.fillStyle=i,l.fillText(d(t.symbol),(500-n)*g,(500-m)*g),e}}}(o),e=t.toDataURL("image/png");return t.remove(),e},A=t=>{const e=u(t);if(!e.symbol)return null;{let t=i(e.symbol);if(t)return`${e.symbol}${o([500-parseInt(t[0]/2),500-parseInt(t[1]/2)])}${e.style||""}`}},Z=o=>{let t=b(o);if(t.spatials){let e=f(t.style);e.detailsym&&e.detailsym.forEach(o=>{t.spatials[o.index-1]&&(t.spatials[o.index-1].detail=o.detail)});let l=Math.min(...t.spatials.map(o=>o.coord[0])),s=Math.min(...t.spatials.map(o=>o.coord[1])),a=t.max[0],n=t.max[1];e.zoomsym&&e.zoomsym.forEach(o=>{if(t.spatials[o.index-1]){t.spatials[o.index-1].zoom=o.zoom,o.offset&&(t.spatials[o.index-1].coord[0]+=o.offset[0],t.spatials[o.index-1].coord[1]+=o.offset[1]);let e=i(t.spatials[o.index-1].symbol);a=Math.max(a,t.spatials[o.index-1].coord[0]+e[0]*o.zoom),n=Math.max(n,t.spatials[o.index-1].coord[1]+e[1]*o.zoom)}});let d="";e.classes&&(d=` class="${e.classes}"`);let r="";e.id&&(r=` id="${e.id}"`);let m="";e.padding&&(l-=e.padding,s-=e.padding,a+=e.padding,n+=e.padding),e.background&&(m=`\n <rect x="${l}" y="${s}" width="${a-l}" height="${n-s}" style="fill:${e.background};" />`);let p="";"x"!=e.zoom&&(p=` width="${(a-l)*(e.zoom?e.zoom:1)}" height="${(n-s)*(e.zoom?e.zoom:1)}"`);let g=`<svg${d}${r} version="1.1" xmlns="http://www.w3.org/2000/svg"${p} viewBox="${l} ${s} ${a-l} ${n-s}">\n <text font-size="0">${o}</text>${m}`;const x=e.detail&&e.detail[0],$=e.detail&&e.detail[1];return g+="\n"+t.spatials.map(o=>{let t=c(o.symbol,o.coord),l=x;o.detail?l=o.detail[0]:e.colorize&&(l=S(o.symbol)),l&&(t=t.replace(/class="sym-line" fill="black"/,`class="sym-line" fill="${l}"`));let s=$;return o.detail&&o.detail[1]&&(s=o.detail[1]),s&&(t=t.replace(/class="sym-fill" fill="white"/,`class="sym-fill" fill="${s}"`)),o.zoom&&(t=`<g transform="scale(${o.zoom})">${t}</g>`),` <g transform="translate(${o.coord[0]},${o.coord[1]})">\n${t}\n </g>`}).join("\n"),g+="\n</svg>",g}return'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>'},_=o=>{const t=function(o){const t=b(o);if(t.spatials){const o=document.createElement("canvas"),e=o.getContext("2d");let l=f(t.style);l.detailsym&&l.detailsym.forEach(o=>{t.spatials[o.index-1]&&(t.spatials[o.index-1].detail=o.detail)});let s=Math.min(...t.spatials.map(o=>o.coord[0])),a=Math.min(...t.spatials.map(o=>o.coord[1])),n=t.max[0],c=t.max[1];l.zoomsym&&l.zoomsym.forEach(o=>{if(t.spatials[o.index-1]){t.spatials[o.index-1].zoom=o.zoom,o.offset&&(t.spatials[o.index-1].coord[0]+=o.offset[0],t.spatials[o.index-1].coord[1]+=o.offset[1]);let e=i(t.spatials[o.index-1].symbol);n=Math.max(n,t.spatials[o.index-1].coord[0]+e[0]*o.zoom),c=Math.max(c,t.spatials[o.index-1].coord[1]+e[1]*o.zoom)}}),l.padding&&(s-=l.padding,a-=l.padding,n+=l.padding,c+=l.padding);let m=1;"x"!=l.zoom&&(m=l.zoom);let p=(n-s)*m,g=(c-a)*m;o.width=p||1,o.height=g||1,l.background&&(e.rect(0,0,p,g),e.fillStyle=l.background,e.fill());const x=l.detail&&l.detail[0]||"black",$=l.detail&&l.detail[1]||"white";return t.spatials.forEach(o=>{let t=x;o.detail?t=o.detail[0]:l.colorize&&(t=S(o.symbol));let i=$;o.detail&&o.detail[1]&&(i=o.detail[1]);let n=o.zoom||1;e.font=30*m*n+"px 'SuttonSignWritingFill'",e.fillStyle=i,e.fillText(r(o.symbol),(o.coord[0]-s)*m,(o.coord[1]-a)*m),e.font=30*m*n+"px 'SuttonSignWritingLine'",e.fillStyle=t,e.fillText(d(o.symbol),(o.coord[0]-s)*m,(o.coord[1]-a)*m)}),o}}(o),e=t.toDataURL("image/png");return t.remove(),e},E=t=>{const e=b(t);if(e.spatials){const t=e.spatials.reduce((o,t)=>{const e=i(t.symbol);return o[t.symbol]={width:e[0],height:e[1]},o},{}),l=o=>({x1:Math.min(...o.map(o=>o.coord[0])),y1:Math.min(...o.map(o=>o.coord[1])),x2:Math.max(...o.map(o=>o.coord[0]+parseInt(t[o.symbol].width))),y2:Math.max(...o.map(o=>o.coord[1]+parseInt(t[o.symbol].height)))}),s=z,a=e.spatials.filter(o=>{const t=parseInt(o.symbol.slice(1,4),16);return s[0]<=t&&s[1]>=t}),n=v,d=e.spatials.filter(o=>{const t=parseInt(o.symbol.slice(1,4),16);return n[0]<=t&&n[1]>=t});let r=l(e.spatials),c=[r.x2,r.y2];if(a.length){const o=l(a);r.x1=o.x1,r.x2=o.x2}if(d.length){const o=l(d);r.y1=o.y1,r.y2=o.y2}const m=[parseInt((r.x2+r.x1)/2)-500,parseInt((r.y2+r.y1)/2)-500];return(e.sequence?"𝠀"+e.sequence.join(""):"")+e.box+o([c[0]-m[0],c[1]-m[1]])+e.spatials.map(t=>t.symbol+o([t.coord[0]-m[0],t.coord[1]-m[1]])).join("")+(e.style||"")}};export{E as signNormalize,_ as signPng,Z as signSvg,r as symbolFill,d as symbolLine,A as symbolNormalize,I as symbolPng,i as symbolSize,k as symbolSvg,c as symbolText};