UNPKG

11.4 kBJavaScriptView Raw
1/* Sutton SignWriting TrueType Font Module v1.1.0 (https://github.com/sutton-signwriting/font-ttf), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */
2const t=t=>1+96*(parseInt(t.slice(1,4),16)-256)+16*parseInt(t.slice(4,5),16)+parseInt(t.slice(5,6),16);let o={};const e=document.createElement("canvas");e.width=152,e.height=152;const l=e.getContext("2d"),s=function(e){return function(t){if(t in o)return[...o[t]];l.clearRect(0,0,152,152),l.font="60px 'SuttonSignWritingLine'",l.fillText(String.fromCodePoint(t+983040),0,0);const e=l.getImageData(0,0,152,152).data;let s,i,a,n;t:for(s=151;s>=0;s--)for(i=0;i<152;i+=1)for(n=0;n<4;n+=1)if(e[a=4*s+4*i*152+n])break t;var r=s;t:for(i=151;i>=0;i--)for(s=0;s<r;s+=1)for(n=0;n<4;n+=1)if(e[a=4*s+4*i*152+n])break t;var d=i+1;if(r=Math.ceil(r/2),d=Math.ceil(d/2),14394==t&&(r=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(t)&&(r=20),31921==t&&(r=22),38460==t&&(r=23),[20164,20212].includes(t)&&(r=25),31894==t&&(r=28),46698==t&&(r=29),29606==t&&(r=30),44855==t&&(r=40),32667==t&&(r=50),[11088,11474,11490,11506].includes(t)&&(d=20),6285==t&&(d=21),40804==t&&(d=31),41475==t&&(d=36),0==r&&0==d){const o={9:[15,30],10:[21,30],11:[30,15],12:[30,21],13:[15,30],14:[21,30]};t in o&&(r=o[t][0],d=o[t][1])}return 0!=r||0!=d?(o[t]=[r,d],[r,d]):void 0}(t(e))},i=function(t){return String.fromCodePoint(t+983040)},a=function(t){return String.fromCodePoint(t+1048576)},n=function(o){return i(t(o))},r=function(o){return a(t(o))},d=function(o){return e=t(o),` <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;">${a(e)}</text>\n <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;">${i(e)}</text>`;var e};let c={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}"};c.colorbase=`(?:${c.colorhex}|${c.colorname})`,c.color=`_${c.colorbase}_`,c.colors=`_${c.colorbase}(?:,${c.colorbase})?_`,c.background=`G${c.color}`,c.detail=`D${c.colors}`,c.detailsym=`D[0-9]{2}${c.colors}`,c.classes=`${c.classbase}(?: ${c.classbase})*`,c.full=`-(${c.colorize})?(${c.padding})?(${c.background})?(${c.detail})?(${c.zoom})?(?:-((?:${c.detailsym})*)((?:${c.zoomsym})*))?(?:-(${c.classes})?!(?:(${c.id})!)?)?`;const m=t=>{return(new RegExp(`^${c.colorhex}$`).test(t)?"#":"")+t},p=t=>{const o=`^${c.full}`,e="string"==typeof t?t.match(new RegExp(o)):[];return{colorize:e[1]?!!e[1]:void 0,padding:e[2]?parseInt(e[2].slice(1)):void 0,background:e[3]?m(e[3].slice(2,-1)):void 0,detail:e[4]?e[4].slice(2,-1).split(",").map(m):void 0,zoom:e[5]?"Zx"===e[5]?"x":parseFloat(e[5].slice(1)):void 0,detailsym:e[6]?e[6].match(new RegExp(c.detailsym,"g")).map(t=>{const o=t.split("_"),e=o[1].split(",").map(m);return{index:parseInt(o[0].slice(1)),detail:e}}):void 0,zoomsym:e[7]?e[7].match(new RegExp(c.zoomsym,"g")).map(t=>{const o=t.split(",");return{index:parseInt(o[0].slice(1)),zoom:parseFloat(o[1]),offset:o[2]?o[2].split("x").map(t=>parseInt(t)-500):void 0}}):void 0,classes:e[8]?e[8]:void 0,id:e[9]?e[9]:void 0}};let f={symbol:"S[123][0-9a-f]{2}[0-5][0-9a-f]",coord:"[0-9]{3}x[0-9]{3}",sort:"A",box:"[BLMR]"};f.prefix=`(?:${f.sort}(?:${f.symbol})+)`,f.spatial=`${f.symbol}${f.coord}`,f.signbox=`${f.box}${f.coord}(?:${f.spatial})*`,f.sign=`${f.prefix}?${f.signbox}`,f.term=`${f.prefix}${f.signbox}`;let g={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}"};g.colorbase=`(?:${g.colorhex}|${g.colorname})`,g.color=`_${g.colorbase}_`,g.colors=`_${g.colorbase}(?:,${g.colorbase})?_`,g.background=`G${g.color}`,g.detail=`D${g.colors}`,g.detailsym=`D[0-9]{2}${g.colors}`,g.classes=`${g.classbase}(?: ${g.classbase})*`,g.full=`-(${g.colorize})?(${g.padding})?(${g.background})?(${g.detail})?(${g.zoom})?(?:-((?:${g.detailsym})*)((?:${g.zoomsym})*))?(?:-(${g.classes})?!(?:(${g.id})!)?)?`;const x=t=>t.split("x").map(t=>parseInt(t)),$=t=>{const o=`^(${f.symbol})(${f.coord})?(${g.full})?`,e="string"==typeof t?t.match(new RegExp(o)):void 0;return{symbol:e?e[1]:void 0,coord:e&&e[2]?x(e[2]):void 0,style:e?e[3]:void 0}},y=t=>{const o=`^(${f.prefix})?(${f.signbox})(${g.full})?`,e="string"==typeof t?t.match(new RegExp(o)):void 0;return e?{sequence:e[1]?e[1].slice(1).match(/.{6}/g):void 0,box:e[2][0],max:x(e[2].slice(1,8)),spatials:e[2].length<9?void 0:e[2].slice(8).match(/(.{13})/g).map(t=>({symbol:t.slice(0,6),coord:[parseInt(t.slice(6,9)),parseInt(t.slice(10,13))]})),style:e[3]}:{}},b=[256,517,759,767,877,895,903],h=[767,876],u=[767,885],z=["#0000CC","#CC0000","#FF0099","#006600","#000000","#884411","#FF9900"],v=t=>{const o=$(t);let e="#000000";if(o.symbol){const t=parseInt(o.symbol.slice(1,4),16),l=b.findIndex(o=>o>t);e=z[l<0?6:l-1]}return e},w=t=>{const o=$(t);if(o.symbol){let e=s(o.symbol);if(e){let l,s=p(o.style),i=d(o.symbol);i=` <g transform="translate(500,500)">\n${i}\n </g>`,s.colorize?l=v(o.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,r=500,c="";s.padding&&(n-=s.padding,r-=s.padding,e[0]+=2*s.padding,e[1]+=2*s.padding),s.background&&(c=`\n <rect x="${n}" y="${r}" 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 f="";s.classes&&(f=` class="${s.classes}"`);let g="";return s.id&&(g=` id="${s.id}"`),`<svg${f}${g} version="1.1" xmlns="http://www.w3.org/2000/svg"${m} viewBox="${n} ${r} ${e[0]} ${e[1]}">\n <text font-size="0">${t}</text>${c}\n${i}\n</svg>`}}return'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>'},S=t=>{const o=function(t){const o=$(t);if(o.symbol){let t=s(o.symbol);if(t){const e=document.createElement("canvas"),l=e.getContext("2d");let s=p(o.style),i="black";s.colorize?i=v(o.symbol):s.detail&&(i=s.detail[0]);let a=s.detail&&s.detail[1]||"white",d=500,c=d+t[0],m=500,f=m+t[1];s.padding&&(d-=s.padding,m-=s.padding,c+=s.padding,f+=s.padding);let g=1;"x"!=s.zoom&&(g=s.zoom);let x=(c-d)*g,$=(f-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(o.symbol),(500-d)*g,(500-m)*g),l.font=30*g+"px 'SuttonSignWritingLine'",l.fillStyle=i,l.fillText(n(o.symbol),(500-d)*g,(500-m)*g),e}}}(t),e=o.toDataURL("image/png");return o.remove(),e},I=t=>{const o=$(t);if(!o.symbol)return null;{let t=s(o.symbol);if(t)return`${o.symbol}${500-parseInt(t[0]/2)}x${500-parseInt(t[1]/2)}${o.style||""}`}},k=t=>{let o=y(t);if(o.spatials){let e=p(o.style);e.detailsym&&e.detailsym.forEach(t=>{o.spatials[t.index-1]&&(o.spatials[t.index-1].detail=t.detail)});let l=Math.min(...o.spatials.map(t=>t.coord[0])),i=Math.min(...o.spatials.map(t=>t.coord[1])),a=o.max[0],n=o.max[1];e.zoomsym&&e.zoomsym.forEach(t=>{if(o.spatials[t.index-1]){o.spatials[t.index-1].zoom=t.zoom,t.offset&&(o.spatials[t.index-1].coord[0]+=t.offset[0],o.spatials[t.index-1].coord[1]+=t.offset[1]);let e=s(o.spatials[t.index-1].symbol);a=Math.max(a,o.spatials[t.index-1].coord[0]+e[0]*t.zoom),n=Math.max(n,o.spatials[t.index-1].coord[1]+e[1]*t.zoom)}});let r="";e.classes&&(r=` class="${e.classes}"`);let c="";e.id&&(c=` id="${e.id}"`);let m="";e.padding&&(l-=e.padding,i-=e.padding,a+=e.padding,n+=e.padding),e.background&&(m=`\n <rect x="${l}" y="${i}" width="${a-l}" height="${n-i}" style="fill:${e.background};" />`);let f="";"x"!=e.zoom&&(f=` width="${(a-l)*(e.zoom?e.zoom:1)}" height="${(n-i)*(e.zoom?e.zoom:1)}"`);let g=`<svg${r}${c} version="1.1" xmlns="http://www.w3.org/2000/svg"${f} viewBox="${l} ${i} ${a-l} ${n-i}">\n <text font-size="0">${t}</text>${m}`;const x=e.detail&&e.detail[0],$=e.detail&&e.detail[1];return g+="\n"+o.spatials.map(t=>{let o=d(t.symbol),l=x;t.detail?l=t.detail[0]:e.colorize&&(l=v(t.symbol)),l&&(o=o.replace(/class="sym-line" fill="black"/,`class="sym-line" fill="${l}"`));let s=$;return t.detail&&t.detail[1]&&(s=t.detail[1]),s&&(o=o.replace(/class="sym-fill" fill="white"/,`class="sym-fill" fill="${s}"`)),t.zoom&&(o=`<g transform="scale(${t.zoom})">${o}</g>`),` <g transform="translate(${t.coord[0]},${t.coord[1]})">\n${o}\n </g>`}).join("\n"),g+="\n</svg>"}return'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>'},M=t=>{const o=function(t){const o=y(t);if(o.spatials){const t=document.createElement("canvas"),e=t.getContext("2d");let l=p(o.style);l.detailsym&&l.detailsym.forEach(t=>{o.spatials[t.index-1]&&(o.spatials[t.index-1].detail=t.detail)});let i=Math.min(...o.spatials.map(t=>t.coord[0])),a=Math.min(...o.spatials.map(t=>t.coord[1])),d=o.max[0],c=o.max[1];l.zoomsym&&l.zoomsym.forEach(t=>{if(o.spatials[t.index-1]){o.spatials[t.index-1].zoom=t.zoom,t.offset&&(o.spatials[t.index-1].coord[0]+=t.offset[0],o.spatials[t.index-1].coord[1]+=t.offset[1]);let e=s(o.spatials[t.index-1].symbol);d=Math.max(d,o.spatials[t.index-1].coord[0]+e[0]*t.zoom),c=Math.max(c,o.spatials[t.index-1].coord[1]+e[1]*t.zoom)}}),l.padding&&(i-=l.padding,a-=l.padding,d+=l.padding,c+=l.padding);let m=1;"x"!=l.zoom&&(m=l.zoom);let f=(d-i)*m,g=(c-a)*m;t.width=f||1,t.height=g||1,l.background&&(e.rect(0,0,f,g),e.fillStyle=l.background,e.fill());const x=l.detail&&l.detail[0]||"black",$=l.detail&&l.detail[1]||"white";return o.spatials.forEach(t=>{let o=x;t.detail?o=t.detail[0]:l.colorize&&(o=v(t.symbol));let s=$;t.detail&&t.detail[1]&&(s=t.detail[1]);let d=t.zoom||1;e.font=30*m*d+"px 'SuttonSignWritingFill'",e.fillStyle=s,e.fillText(r(t.symbol),(t.coord[0]-i)*m,(t.coord[1]-a)*m),e.font=30*m*d+"px 'SuttonSignWritingLine'",e.fillStyle=o,e.fillText(n(t.symbol),(t.coord[0]-i)*m,(t.coord[1]-a)*m)}),t}}(t),e=o.toDataURL("image/png");return o.remove(),e},Z=t=>{const o=y(t);if(o.spatials){const t=o.spatials.reduce((t,o)=>{const e=s(o.symbol);return t[o.symbol]={width:e[0],height:e[1]},t},{}),e=o=>{return{x1:Math.min(...o.map(t=>t.coord[0])),y1:Math.min(...o.map(t=>t.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)))}},l=h,i=o.spatials.filter(t=>{const o=parseInt(t.symbol.slice(1,4),16);return l[0]<=o&&l[1]>=o}),a=u,n=o.spatials.filter(t=>{const o=parseInt(t.symbol.slice(1,4),16);return a[0]<=o&&a[1]>=o});let r=e(o.spatials),d=[r.x2,r.y2];if(i.length){const t=e(i);r.x1=t.x1,r.x2=t.x2}if(n.length){const t=e(n);r.y1=t.y1,r.y2=t.y2}const c=[parseInt((r.x2+r.x1)/2)-500,parseInt((r.y2+r.y1)/2)-500];return(o.sequence?"A"+o.sequence.join(""):"")+o.box+(d[0]-c[0])+"x"+(d[1]-c[1])+o.spatials.map(t=>t.symbol+(t.coord[0]-c[0])+"x"+(t.coord[1]-c[1])).join("")+(o.style||"")}};export{Z as signNormalize,M as signPng,k as signSvg,r as symbolFill,n as symbolLine,I as symbolNormalize,S as symbolPng,s as symbolSize,w as symbolSvg,d as symbolText};