UNPKG

11.7 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 */
2!function(t,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o(((t=t||self).ssw=t.ssw||{},t.ssw.ttf=t.ssw.ttf||{},t.ssw.ttf.fsw={}))}(this,(function(t){"use strict";const o=t=>1+96*(parseInt(t.slice(1,4),16)-256)+16*parseInt(t.slice(4,5),16)+parseInt(t.slice(5,6),16);let e={};const s=document.createElement("canvas");s.width=152,s.height=152;const l=s.getContext("2d"),i=function(t){return function(t){if(t in e)return[...e[t]];l.clearRect(0,0,152,152),l.font="60px 'SuttonSignWritingLine'",l.fillText(String.fromCodePoint(t+983040),0,0);const o=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(a=4*s+4*i*152+n,o[a])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(a=4*s+4*i*152+n,o[a])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?(e[t]=[r,d],[r,d]):void 0}(o(t))},a=function(t){return String.fromCodePoint(t+983040)},n=function(t){return String.fromCodePoint(t+1048576)},r=function(t){return a(o(t))},d=function(t){return n(o(t))},c=function(t){return e=o(t),` <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=t=>(new RegExp(`^${m.colorhex}$`).test(t)?"#":"")+t,f=t=>{const o="^"+m.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]?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(t=>{const o=t.split("_"),e=o[1].split(",").map(p);return{index:parseInt(o[0].slice(1)),detail:e}}):void 0,zoomsym:e[7]?e[7].match(new RegExp(m.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 g={symbol:"S[123][0-9a-f]{2}[0-5][0-9a-f]",coord:"[0-9]{3}x[0-9]{3}",sort:"A",box:"[BLMR]"};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 y=t=>t.split("x").map(t=>parseInt(t)),$=t=>{const o=`^(${g.symbol})(${g.coord})?(${x.full})?`,e="string"==typeof t?t.match(new RegExp(o)):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=t=>{const o=`^(${g.prefix})?(${g.signbox})(${x.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:y(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]}:{}},u=[256,517,759,767,877,895,903],h=[767,876],z=[767,885],v=["#0000CC","#CC0000","#FF0099","#006600","#000000","#884411","#FF9900"],w=t=>{const o=$(t);let e="#000000";if(o.symbol){const t=parseInt(o.symbol.slice(1,4),16),s=u.findIndex(o=>o>t);e=v[s<0?6:s-1]}return e};t.signNormalize=t=>{const o=b(t);if(o.spatials){const t=o.spatials.reduce((t,o)=>{const e=i(o.symbol);return t[o.symbol]={width:e[0],height:e[1]},t},{}),e=o=>({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)))}),s=h,l=o.spatials.filter(t=>{const o=parseInt(t.symbol.slice(1,4),16);return s[0]<=o&&s[1]>=o}),a=z,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(l.length){const t=e(l);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||"")}},t.signPng=t=>{const o=function(t){const o=b(t);if(o.spatials){const t=document.createElement("canvas"),e=t.getContext("2d");let s=f(o.style);s.detailsym&&s.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])),a=Math.min(...o.spatials.map(t=>t.coord[1])),n=o.max[0],c=o.max[1];s.zoomsym&&s.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=i(o.spatials[t.index-1].symbol);n=Math.max(n,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)}}),s.padding&&(l-=s.padding,a-=s.padding,n+=s.padding,c+=s.padding);let m=1;"x"!=s.zoom&&(m=s.zoom);let p=(n-l)*m,g=(c-a)*m;t.width=p||1,t.height=g||1,s.background&&(e.rect(0,0,p,g),e.fillStyle=s.background,e.fill());const x=s.detail&&s.detail[0]||"black",y=s.detail&&s.detail[1]||"white";return o.spatials.forEach(t=>{let o=x;t.detail?o=t.detail[0]:s.colorize&&(o=w(t.symbol));let i=y;t.detail&&t.detail[1]&&(i=t.detail[1]);let n=t.zoom||1;e.font=30*m*n+"px 'SuttonSignWritingFill'",e.fillStyle=i,e.fillText(d(t.symbol),(t.coord[0]-l)*m,(t.coord[1]-a)*m),e.font=30*m*n+"px 'SuttonSignWritingLine'",e.fillStyle=o,e.fillText(r(t.symbol),(t.coord[0]-l)*m,(t.coord[1]-a)*m)}),t}}(t),e=o.toDataURL("image/png");return o.remove(),e},t.signSvg=t=>{let o=b(t);if(o.spatials){let e=f(o.style);e.detailsym&&e.detailsym.forEach(t=>{o.spatials[t.index-1]&&(o.spatials[t.index-1].detail=t.detail)});let s=Math.min(...o.spatials.map(t=>t.coord[0])),l=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=i(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 d="";e.id&&(d=` id="${e.id}"`);let m="";e.padding&&(s-=e.padding,l-=e.padding,a+=e.padding,n+=e.padding),e.background&&(m=`\n <rect x="${s}" y="${l}" width="${a-s}" height="${n-l}" style="fill:${e.background};" />`);let p="";"x"!=e.zoom&&(p=` width="${(a-s)*(e.zoom?e.zoom:1)}" height="${(n-l)*(e.zoom?e.zoom:1)}"`);let g=`<svg${r}${d} version="1.1" xmlns="http://www.w3.org/2000/svg"${p} viewBox="${s} ${l} ${a-s} ${n-l}">\n <text font-size="0">${t}</text>${m}`;const x=e.detail&&e.detail[0],y=e.detail&&e.detail[1];return g+="\n"+o.spatials.map(t=>{let o=c(t.symbol),s=x;t.detail?s=t.detail[0]:e.colorize&&(s=w(t.symbol)),s&&(o=o.replace(/class="sym-line" fill="black"/,`class="sym-line" fill="${s}"`));let l=y;return t.detail&&t.detail[1]&&(l=t.detail[1]),l&&(o=o.replace(/class="sym-fill" fill="white"/,`class="sym-fill" fill="${l}"`)),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>",g}return'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>'},t.symbolFill=d,t.symbolLine=r,t.symbolNormalize=t=>{const o=$(t);if(!o.symbol)return null;{let t=i(o.symbol);if(t)return`${o.symbol}${500-parseInt(t[0]/2)}x${500-parseInt(t[1]/2)}${o.style||""}`}},t.symbolPng=t=>{const o=function(t){const o=$(t);if(o.symbol){let t=i(o.symbol);if(t){const e=document.createElement("canvas"),s=e.getContext("2d");let l=f(o.style),i="black";l.colorize?i=w(o.symbol):l.detail&&(i=l.detail[0]);let a=l.detail&&l.detail[1]||"white",n=500,c=n+t[0],m=500,p=m+t[1];l.padding&&(n-=l.padding,m-=l.padding,c+=l.padding,p+=l.padding);let g=1;"x"!=l.zoom&&(g=l.zoom);let x=(c-n)*g,y=(p-m)*g;return e.width=x||1,e.height=y||1,l.background&&(s.rect(0,0,x,y),s.fillStyle=l.background,s.fill()),s.font=30*g+"px 'SuttonSignWritingFill'",s.fillStyle=a,s.fillText(d(o.symbol),(500-n)*g,(500-m)*g),s.font=30*g+"px 'SuttonSignWritingLine'",s.fillStyle=i,s.fillText(r(o.symbol),(500-n)*g,(500-m)*g),e}}}(t),e=o.toDataURL("image/png");return o.remove(),e},t.symbolSize=i,t.symbolSvg=t=>{const o=$(t);if(o.symbol){let e=i(o.symbol);if(e){let s,l=f(o.style),i=c(o.symbol);i=` <g transform="translate(500,500)">\n${i}\n </g>`,l.colorize?s=w(o.symbol):l.detail&&(s=l.detail[0]),s&&(i=i.replace(/class="sym-line" fill="black"/,`class="sym-line" fill="${s}"`));let a=l.detail&&l.detail[1];a&&(i=i.replace(/class="sym-fill" fill="white"/,`class="sym-fill" fill="${a}"`));let n=500,r=500,d="";l.padding&&(n-=l.padding,r-=l.padding,e[0]+=2*l.padding,e[1]+=2*l.padding),l.background&&(d=`\n <rect x="${n}" y="${r}" width="${e[0]}" height="${e[1]}" style="fill:${l.background};" />`);let m="";"x"!=l.zoom&&(m=` width="${e[0]*(l.zoom?l.zoom:1)}" height="${e[1]*(l.zoom?l.zoom:1)}"`);let p="";l.classes&&(p=` class="${l.classes}"`);let g="";return l.id&&(g=` id="${l.id}"`),`<svg${p}${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>${d}\n${i}\n</svg>`}}return'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>'},t.symbolText=c,Object.defineProperty(t,"__esModule",{value:!0})}));