UNPKG

@sutton-signwriting/font-ttf

Version:

a javascript package for the web components and browser that generates SVG and PNG images for individual symbols and complete signs

3 lines (2 loc) 11.9 kB
/* Sutton SignWriting TrueType Font Module v1.2.0 (https://github.com/sutton-signwriting/font-ttf), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */ !function(o,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((o=o||self).ssw=o.ssw||{},o.ssw.ttf=o.ssw.ttf||{},o.ssw.ttf.swu={}))}(this,(function(o){"use strict";const t=o=>o.map(o=>(o=>String.fromCodePoint(120844+parseInt(o)-250))(o)).join(""),e=o=>(o=>parseInt(o.codePointAt(0)))(o)-262144;let l={};const s=document.createElement("canvas");s.width=152,s.height=152;const i=s.getContext("2d"),a=function(o){return function(o){if(o in l)return[...l[o]];i.clearRect(0,0,152,152),i.font="60px 'SuttonSignWritingLine'",i.fillText(String.fromCodePoint(o+983040),0,0);const t=i.getImageData(0,0,152,152).data;let e,s,a,n;o:for(e=151;e>=0;e--)for(s=0;s<152;s+=1)for(n=0;n<4;n+=1)if(a=4*e+4*s*152+n,t[a])break o;var d=e;o:for(s=151;s>=0;s--)for(e=0;e<d;e+=1)for(n=0;n<4;n+=1)if(a=4*e+4*s*152+n,t[a])break o;var r=s+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?(l[o]=[d,r],[d,r]):void 0}(e(o))},n=function(o){return String.fromCodePoint(o+983040)},d=function(o){return String.fromCodePoint(o+1048576)},r=function(o){return n(e(o))},c=function(o){return d(e(o))},m=function(o){return t=e(o),` <text class="sym-fill" fill="white" style="pointer-events:none;font-family:'SuttonSignWritingFill';font-size:30px;">${d(t)}</text>\n <text class="sym-line" fill="black" style="pointer-events:none;font-family:'SuttonSignWritingLine';font-size:30px;">${n(t)}</text>`;var t};let f={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}"};f.colorbase=`(?:${f.colorhex}|${f.colorname})`,f.color=`_${f.colorbase}_`,f.colors=`_${f.colorbase}(?:,${f.colorbase})?_`,f.background="G"+f.color,f.detail="D"+f.colors,f.detailsym="D[0-9]{2}"+f.colors,f.classes=`${f.classbase}(?: ${f.classbase})*`,f.full=`-(${f.colorize})?(${f.padding})?(${f.background})?(${f.detail})?(${f.zoom})?(?:-((?:${f.detailsym})*)((?:${f.zoomsym})*))?(?:-(${f.classes})?!(?:(${f.id})!)?)?`;const p=o=>(new RegExp(`^${f.colorhex}$`).test(o)?"#":"")+o,g=o=>{const t="^"+f.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(f.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(f.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 x={symbol:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:\ud836[\udc0c-\uddff]){2}",sort:"𝠀",box:"\ud836[\udc01-\udc04]"};x.prefix=`(?:${x.sort}(?:${x.symbol})+)`,x.spatial=`${x.symbol}${x.coord}`,x.signbox=`${x.box}${x.coord}(?:${x.spatial})*`,x.sign=`${x.prefix}?${x.signbox}`,x.sortable=`${x.prefix}${x.signbox}`;let y={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}"};y.colorbase=`(?:${y.colorhex}|${y.colorname})`,y.color=`_${y.colorbase}_`,y.colors=`_${y.colorbase}(?:,${y.colorbase})?_`,y.background="G"+y.color,y.detail="D"+y.colors,y.detailsym="D[0-9]{2}"+y.colors,y.classes=`${y.classbase}(?: ${y.classbase})*`,y.full=`-(${y.colorize})?(${y.padding})?(${y.background})?(${y.detail})?(${y.zoom})?(?:-((?:${y.detailsym})*)((?:${y.zoomsym})*))?(?:-(${y.classes})?!(?:(${y.id})!)?)?`;const $=o=>parseInt(o.codePointAt(0))-120844+250,u=o=>[$(o.slice(0,2)),$(o.slice(2,4))],b=o=>{const t=`^(${x.symbol})(${x.coord})?(${y.full})?`,e="string"==typeof o?o.match(new RegExp(t)):void 0;return{symbol:e?e[1]:void 0,coord:e&&e[2]?u(e[2]):void 0,style:e?e[3]:void 0}},h=o=>{const t=`^(${x.prefix})?(${x.signbox})(${y.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:u(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:u(o.slice(2))})),style:e[3]}:{}},z=[262145,287201,310433,311201,321761,323489,324257],v=[311201,321760],w=[311201,322624],S=["#0000CC","#CC0000","#FF0099","#006600","#000000","#884411","#FF9900"],k=o=>{const t=b(o);let e="#000000";if(t.symbol){const o=(o=>parseInt(o.codePointAt(0)))(t.symbol),l=z.findIndex(t=>t>o);e=S[l<0?6:l-1]}return e};o.signNormalize=o=>{const e=h(o);if(e.spatials){const o=e.spatials.reduce((o,t)=>{const e=a(t.symbol);return o[t.symbol]={width:e[0],height:e[1]},o},{}),l=t=>({x1:Math.min(...t.map(o=>o.coord[0])),y1:Math.min(...t.map(o=>o.coord[1])),x2:Math.max(...t.map(t=>t.coord[0]+parseInt(o[t.symbol].width))),y2:Math.max(...t.map(t=>t.coord[1]+parseInt(o[t.symbol].height)))}),s=v,i=e.spatials.filter(o=>{const t=parseInt(o.symbol.slice(1,4),16);return s[0]<=t&&s[1]>=t}),n=w,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(i.length){const o=l(i);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+t([c[0]-m[0],c[1]-m[1]])+e.spatials.map(o=>o.symbol+t([o.coord[0]-m[0],o.coord[1]-m[1]])).join("")+(e.style||"")}},o.signPng=o=>{const t=function(o){const t=h(o);if(t.spatials){const o=document.createElement("canvas"),e=o.getContext("2d");let l=g(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])),i=Math.min(...t.spatials.map(o=>o.coord[1])),n=t.max[0],d=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=a(t.spatials[o.index-1].symbol);n=Math.max(n,t.spatials[o.index-1].coord[0]+e[0]*o.zoom),d=Math.max(d,t.spatials[o.index-1].coord[1]+e[1]*o.zoom)}}),l.padding&&(s-=l.padding,i-=l.padding,n+=l.padding,d+=l.padding);let m=1;"x"!=l.zoom&&(m=l.zoom);let f=(n-s)*m,p=(d-i)*m;o.width=f||1,o.height=p||1,l.background&&(e.rect(0,0,f,p),e.fillStyle=l.background,e.fill());const x=l.detail&&l.detail[0]||"black",y=l.detail&&l.detail[1]||"white";return t.spatials.forEach(o=>{let t=x;o.detail?t=o.detail[0]:l.colorize&&(t=k(o.symbol));let a=y;o.detail&&o.detail[1]&&(a=o.detail[1]);let n=o.zoom||1;e.font=30*m*n+"px 'SuttonSignWritingFill'",e.fillStyle=a,e.fillText(c(o.symbol),(o.coord[0]-s)*m,(o.coord[1]-i)*m),e.font=30*m*n+"px 'SuttonSignWritingLine'",e.fillStyle=t,e.fillText(r(o.symbol),(o.coord[0]-s)*m,(o.coord[1]-i)*m)}),o}}(o),e=t.toDataURL("image/png");return t.remove(),e},o.signSvg=o=>{let t=h(o);if(t.spatials){let e=g(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])),i=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=a(t.spatials[o.index-1].symbol);i=Math.max(i,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 c="";e.padding&&(l-=e.padding,s-=e.padding,i+=e.padding,n+=e.padding),e.background&&(c=`\n <rect x="${l}" y="${s}" width="${i-l}" height="${n-s}" style="fill:${e.background};" />`);let f="";"x"!=e.zoom&&(f=` width="${(i-l)*(e.zoom?e.zoom:1)}" height="${(n-s)*(e.zoom?e.zoom:1)}"`);let p=`<svg${d}${r} version="1.1" xmlns="http://www.w3.org/2000/svg"${f} viewBox="${l} ${s} ${i-l} ${n-s}">\n <text font-size="0">${o}</text>${c}`;const x=e.detail&&e.detail[0],y=e.detail&&e.detail[1];return p+="\n"+t.spatials.map(o=>{let t=m(o.symbol,o.coord),l=x;o.detail?l=o.detail[0]:e.colorize&&(l=k(o.symbol)),l&&(t=t.replace(/class="sym-line" fill="black"/,`class="sym-line" fill="${l}"`));let s=y;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"),p+="\n</svg>",p}return'<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="1" height="1"></svg>'},o.symbolFill=c,o.symbolLine=r,o.symbolNormalize=o=>{const e=b(o);if(!e.symbol)return null;{let o=a(e.symbol);if(o)return`${e.symbol}${t([500-parseInt(o[0]/2),500-parseInt(o[1]/2)])}${e.style||""}`}},o.symbolPng=o=>{const t=function(o){const t=b(o);if(t.symbol){let o=a(t.symbol);if(o){const e=document.createElement("canvas"),l=e.getContext("2d");let s=g(t.style),i="black";s.colorize?i=k(t.symbol):s.detail&&(i=s.detail[0]);let a=s.detail&&s.detail[1]||"white",n=500,d=n+o[0],m=500,f=m+o[1];s.padding&&(n-=s.padding,m-=s.padding,d+=s.padding,f+=s.padding);let p=1;"x"!=s.zoom&&(p=s.zoom);let x=(d-n)*p,y=(f-m)*p;return e.width=x||1,e.height=y||1,s.background&&(l.rect(0,0,x,y),l.fillStyle=s.background,l.fill()),l.font=30*p+"px 'SuttonSignWritingFill'",l.fillStyle=a,l.fillText(c(t.symbol),(500-n)*p,(500-m)*p),l.font=30*p+"px 'SuttonSignWritingLine'",l.fillStyle=i,l.fillText(r(t.symbol),(500-n)*p,(500-m)*p),e}}}(o),e=t.toDataURL("image/png");return t.remove(),e},o.symbolSize=a,o.symbolSvg=o=>{const t=b(o);if(t.symbol){let e=a(t.symbol);if(e){let l,s=g(t.style),i=m(t.symbol);i=` <g transform="translate(500,500)">\n${i}\n </g>`,s.colorize?l=k(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 c="";"x"!=s.zoom&&(c=` 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 p="";return s.id&&(p=` id="${s.id}"`),`<svg${f}${p} version="1.1" xmlns="http://www.w3.org/2000/svg"${c} 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>'},o.symbolText=m,Object.defineProperty(o,"__esModule",{value:!0})}));