three-bmfont-text-es
Version:
ES6 Module that enables BMFont files in ThreeJS with word-wrapping. Originally created by Matt DesLauriers (https://github.com/mattdesl/)
1 lines • 13.7 kB
JavaScript
import*as t from"three";var e={d:(t,r)=>{for(var a in r)e.o(r,a)&&!e.o(t,a)&&Object.defineProperty(t,a,{enumerable:!0,get:r[a]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},r={};e.d(r,{eZ:()=>j,mg:()=>M,j8:()=>G,MY:()=>F,O_:()=>D,ST:()=>N,qD:()=>T,L1:()=>C,m7:()=>O,$B:()=>B});var a=/\n/,i=/\s/;function n(t,e){if(0===(e=e||{}).width&&"nowrap"!==e.mode)return[];t=t||"";var r="number"==typeof e.width?e.width:Number.MAX_VALUE,i=Math.max(0,e.start||0),n="number"==typeof e.end?e.end:t.length,c=e.mode,l=e.measure||u;return"pre"===c?function(t,e,r,i,n){for(var o=[],s=r,u=r;u<i&&u<e.length;u++){var c=e.charAt(u),l=a.test(c);if(l||u===i-1){var h=t(e,s,l?u:u+1,n);o.push(h),s=u+1}}return o}(l,t,i,n,r):function(t,e,r,a,i,n){var u=[],c=i;"nowrap"===n&&(c=Number.MAX_VALUE);for(;r<a&&r<e.length;){for(var l=o(e,"\n",r,a);r<l&&s(e.charAt(r));)r++;var h=t(e,r,l,c),p=r+(h.end-h.start),f=p+"\n".length;if(p<l){for(;p>r&&!s(e.charAt(p));)p--;if(p===r)f>r+"\n".length&&f--,p=f;else for(f=p;p>r&&s(e.charAt(p-"\n".length));)p--}if(p>=r){var v=t(e,r,p,c);u.push(v)}r=f}return u}(l,t,i,n,r,c)}function o(t,e,r,a){var i=t.indexOf(e,r);return-1===i||i>a?a:i}function s(t){return i.test(t)}function u(t,e,r,a){return{start:e,end:e+Math.min(a,r-e)}}var c=["x","e","a","o","n","s","r","c","u","m","v","w","z"],l=["m","w"],h=["H","I","N","E","F","K","L","T","U","V","W","X","Y","Z"],p="\t".charCodeAt(0),f=" ".charCodeAt(0);function v(t,e){return"number"==typeof t?t:"number"==typeof e?e:0}class m{constructor(t){["width","height","descender","ascender","xHeight","baseline","capHeight","lineHeight"].forEach((t=>{Object.defineProperty(m,t,{get:d(t),configurable:!0})})),this.glyphs=[],this._measure=this.computeMetrics.bind(this),this.update(t)}update(t){if(t=Object.assign({measure:this._measure},t),this._opt=t,this._opt.tabSize=v(this._opt.tabSize,4),!t.font)throw new Error("must provide a valid bitmap font");var e=this.glyphs,r=t.text||"",a=t.font;this._setupSpaceGlyphs(a);var i=n(r,t),o=t.width||0;e.length=0;var s=i.reduce((function(t,e){return Math.max(t,e.width,o)}),0),u=0,l=0,p=v(t.lineHeight,a.common.lineHeight),f=a.common.base,m=p-f,d=t.letterSpacing||0,g=p*i.length-m,b=function(t){if("center"===t)return 1;if("right"===t)return 2;return 0}(this._opt.align);l-=g,this._width=s,this._height=g,this._descender=p-f,this._baseline=f,this._xHeight=function(t){for(var e=0;e<c.length;e++){var r=c[e].charCodeAt(0),a=x(t.chars,r);if(a>=0)return t.chars[a].height}return 0}(a),this._capHeight=function(t){for(var e=0;e<h.length;e++){var r=h[e].charCodeAt(0),a=x(t.chars,r);if(a>=0)return t.chars[a].height}return 0}(a),this._lineHeight=p,this._ascender=p-m-this._xHeight,i.forEach(((t,i)=>{for(var n,o=t.start,c=t.end,h=t.width,f=o;f<c;f++){var v=r.charCodeAt(f),m=this.getGlyph(a,v);if(m){n&&(u+=y(a,n.id,m.id));var g=u;1===b?g+=(s-h)/2:2===b&&(g+=s-h),e.push({position:[g,l],data:m,index:f,line:i}),u+=m.xadvance+d,n=m}}l+=p,u=0})),this._linesTotal=i.length}_setupSpaceGlyphs(t){if(this._fallbackSpaceGlyph=null,this._fallbackTabGlyph=null,t.chars&&0!==t.chars.length){var e=g(t,f)||function(t){for(var e=0;e<l.length;e++){var r=l[e].charCodeAt(0),a=x(t.chars,r);if(a>=0)return t.chars[a]}return 0}(t)||t.chars[0],r=(e=JSON.parse(JSON.stringify(e)),this._opt.tabSize*e.xadvance);this._fallbackSpaceGlyph=e,this._fallbackTabGlyph=Object.assign(e,{x:0,y:0,xadvance:r,id:p,xoffset:0,yoffset:0,width:0,height:0})}}getGlyph(t,e){var r=g(t,e);return r||(e===p?this._fallbackTabGlyph:e===f?this._fallbackSpaceGlyph:null)}computeMetrics(t,e,r,a){var i,n=this._opt.letterSpacing||0,o=this._opt.font,s=0,u=0,c=0;if(!o.chars||0===o.chars.length)return{start:e,end:e,width:0};r=Math.min(t.length,r);for(var l=e;l<r;l++){var h,p=t.charCodeAt(l);if(h=this.getGlyph(o,p)){h.xoffset;var f=(s+=i?y(o,i.id,h.id):0)+h.xadvance+n,v=s+h.width;if(v>=a||f>=a)break;s=f,u=v,i=h}c++}return i&&(u+=i.xoffset),{start:e,end:e+c,width:u}}}function d(t){return new Function(["return function "+t+"() {"," return this._"+t,"}"].join("\n"))()}function g(t,e){if(!t.chars||0===t.chars.length)return null;var r=x(t.chars,e);return r>=0?t.chars[r]:null}function y(t,e,r){if(!t.kernings||0===t.kernings.length)return 0;for(var a=t.kernings,i=0;i<a.length;i++){var n=a[i];if(n.first===e&&n.second===r)return n.amount}return 0}function x(t,e,r){for(var a=r=r||0;a<t.length;a++)if(t[a].id===e)return a;return-1}var b=[0,2,3],_=[2,1,3];function w(t,e){var r,a,i;t&&(a=t,i=Object.prototype.toString,a.BYTES_PER_ELEMENT&&"[object ArrayBuffer]"===i.call(a.buffer)||Array.isArray(a)||null!=(r=t)&&null!=r.constructor&&"function"==typeof r.constructor.isBuffer&&r.constructor.isBuffer(r))||(e=t||{},t=null);for(var n="string"==typeof(e="number"==typeof e?{count:e}:e||{}).type?e.type:"uint16",o="number"==typeof e.count?e.count:1,s=e.start||0,u=!1!==e.clockwise?b:_,c=u[0],l=u[1],h=u[2],p=6*o,f=t||new(function(t){switch(t){case"int8":return Int8Array;case"int16":return Int16Array;case"int32":return Int32Array;case"uint8":return Uint8Array;case"uint16":return Uint16Array;case"uint32":return Uint32Array;case"float32":return Float32Array;case"float64":return Float64Array;case"array":return Array;case"uint8_clamped":return Uint8ClampedArray}}(n))(p),v=0,m=0;v<p;v+=6,m+=4){var d=v+s;f[d+0]=m+0,f[d+1]=m+1,f[d+2]=m+2,f[d+3]=m+c,f[d+4]=m+l,f[d+5]=m+h}return f}var S={min:[0,0,0],max:[0,0,0]};function A(t){var e=t.length/3;S.min[0]=t[0],S.min[1]=t[1],S.min[2]=t[2],S.max[0]=t[0],S.max[1]=t[1],S.max[2]=t[2];for(var r=0;r<e;r++){var a=t[3*r+0],i=t[3*r+1],n=t[3*r+2];S.min[0]=Math.min(a,S.min[0]),S.min[1]=Math.min(i,S.min[1]),S.min[2]=Math.min(n,S.min[2]),S.max[0]=Math.max(a,S.max[0]),S.max[1]=Math.max(i,S.max[1]),S.max[2]=Math.max(n,S.max[2])}}function j(t,e){A(t),e.min.set(S.min[0],S.min[1],S.min[2]),e.max.set(S.max[0],S.max[1],S.max[2])}function M(t,e){A(t);var r=S.min[0],a=S.min[1],i=S.min[2],n=S.max[0]-r,o=S.max[1]-a,s=S.max[2]-i,u=Math.sqrt(n*n+o*o);e.center.set(r+n/2,a+o/2,i+s/2),e.radius=u/2}function C(t){var e=new Float32Array(4*t.length*1),r=0;return t.forEach((function(t){var a=t.data.page||0;e[r++]=a,e[r++]=a,e[r++]=a,e[r++]=a})),e}function B(t,e,r,a){var i=new Float32Array(4*t.length*2),n=0;return t.forEach((function(t){var o=t.data,s=o.x+o.width,u=o.y+o.height,c=o.x/e,l=o.y/r,h=s/e,p=u/r;a&&(l=(r-o.y)/r,p=(r-u)/r),i[n++]=c,i[n++]=l,i[n++]=c,i[n++]=p,i[n++]=h,i[n++]=p,i[n++]=h,i[n++]=l})),i}function O(t){var e=new Float32Array(4*t.length*3),r=0;return t.forEach((function(t){var a=t.data,i=t.position[0]+a.xoffset,n=t.position[1]+a.yoffset,o=a.width,s=a.height;e[r++]=i,e[r++]=n,e[r++]=0,e[r++]=i,e[r++]=n+s,e[r++]=0,e[r++]=i+o,e[r++]=n+s,e[r++]=0,e[r++]=i+o,e[r++]=n,e[r++]=0})),e}const E=(t=>{var r={};return e.d(r,t),r})({Box3:()=>t.Box3,BufferAttribute:()=>t.BufferAttribute,BufferGeometry:()=>t.BufferGeometry,Color:()=>t.Color,REVISION:()=>t.REVISION,Sphere:()=>t.Sphere,Texture:()=>t.Texture});function T(t){return new U(t)}class U extends E.BufferGeometry{constructor(t){super(),"string"==typeof t&&(t={text:t}),this._opt=Object.assign({},t),t&&this.update(t)}update(t){if("string"==typeof t&&(t={text:t}),!(t=Object.assign({},this._opt,t)).font)throw new TypeError("must specify a { font } in options");this.layout=function(t){return new m(t)}(t);var e=!1!==t.flipY,r=t.font,a=r.common.scaleW,i=r.common.scaleH,n=this.layout.glyphs.filter((function(t){var e=t.data;return e.width*e.height>0}));this.visibleGlyphs=n;var o=O(n),s=B(n,a,i,e),u=w([],{clockwise:!0,type:"uint16",count:n.length});if(this.setIndex(u),this.setAttribute("position",new E.BufferAttribute(o,3)),this.setAttribute("uv",new E.BufferAttribute(s,2)),!t.multipage&&"page"in this.attributes)this.removeAttribute("page");else if(t.multipage){var c=C(n);this.setAttribute("page",new E.BufferAttribute(c,1))}}computeBoundingSphere(){null===this.boundingSphere&&(this.boundingSphere=new E.Sphere);var t=this.attributes.position.array,e=this.attributes.position.itemSize;if(!t||!e||t.length<3)return this.boundingSphere.radius=0,void this.boundingSphere.center.set(0,0,0);M(t,this.boundingSphere),isNaN(this.boundingSphere.radius)&&console.error('BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.')}computeBoundingBox(){null===this.boundingBox&&(this.boundingBox=new E.Box3);var t=this.boundingBox,e=this.attributes.position.array,r=this.attributes.position.itemSize;!e||!r||e.length<3?t.makeEmpty():j(e,t)}}function G(t){var e="number"==typeof(t=t||{}).opacity?t.opacity:1,r="number"==typeof t.alphaTest?t.alphaTest:1e-4,a=t.precision||"highp",i=t.color,n=t.map;return delete t.map,delete t.color,delete t.precision,delete t.opacity,Object.assign({uniforms:{opacity:{type:"f",value:e},map:{type:"t",value:n||new E.Texture},color:{type:"c",value:new E.Color(i)}},vertexShader:["attribute vec2 uv;","attribute vec4 position;","uniform mat4 projectionMatrix;","uniform mat4 modelViewMatrix;","varying vec2 vUv;","void main() {","vUv = uv;","gl_Position = projectionMatrix * modelViewMatrix * position;","}"].join("\n"),fragmentShader:["precision "+a+" float;","uniform float opacity;","uniform vec3 color;","uniform sampler2D map;","varying vec2 vUv;","void main() {"," gl_FragColor = texture2D(map, vUv) * vec4(color, opacity);",0===r?"":" if (gl_FragColor.a < "+r+") discard;","}"].join("\n")},t)}function F(t){var e="number"==typeof(t=t||{}).opacity?t.opacity:1,r="number"==typeof t.alphaTest?t.alphaTest:1e-4,a=t.precision||"highp",i=t.color,n=t.map,o="boolean"!=typeof t.negate||t.negate;return delete t.map,delete t.color,delete t.precision,delete t.opacity,delete t.negate,Object.assign({uniforms:{opacity:{type:"f",value:e},map:{type:"t",value:n||new E.Texture},color:{type:"c",value:new E.Color(i)}},vertexShader:["attribute vec2 uv;","attribute vec4 position;","uniform mat4 projectionMatrix;","uniform mat4 modelViewMatrix;","varying vec2 vUv;","void main() {","vUv = uv;","gl_Position = projectionMatrix * modelViewMatrix * position;","}"].join("\n"),fragmentShader:["#ifdef GL_OES_standard_derivatives","#extension GL_OES_standard_derivatives : enable","#endif","precision "+a+" float;","uniform float opacity;","uniform vec3 color;","uniform sampler2D map;","varying vec2 vUv;","float median(float r, float g, float b) {"," return max(min(r, g), min(max(r, g), b));","}","void main() {"," vec3 sample = "+(o?"1.0 - ":"")+"texture2D(map, vUv).rgb;"," float sigDist = median(sample.r, sample.g, sample.b) - 0.5;"," float alpha = clamp(sigDist/fwidth(sigDist) + 0.5, 0.0, 1.0);"," gl_FragColor = vec4(color.xyz, alpha * opacity);",0===r?"":" if (gl_FragColor.a < "+r+") discard;","}"].join("\n")},t)}function D(t){var e="number"==typeof(t=t||{}).opacity?t.opacity:1,r=t.precision||"highp",a="number"==typeof t.alphaTest?t.alphaTest:1e-4,i=t.textures||[];i=Array.isArray(i)?i:[i];var n={};i.forEach((function(t,e){n["texture"+e]={type:"t",value:t}}));var o=i.map((function(t,e){return"uniform sampler2D texture"+e+";"})).join("\n"),s=i.map((function(t,e){return[(0===e?"if":"else if")+" (vPage == "+e+".0) {","sampleColor = texture2D(texture"+e+", vUv);","}"].join("\n")})).join("\n"),u=t.color;delete t.textures,delete t.color,delete t.precision,delete t.opacity;var c={attributes:{page:{type:"f",value:0}}};return(0|(parseInt(E.REVISION,10)||0))>=72&&(c=void 0),Object.assign({uniforms:Object.assign({},n,{opacity:{type:"f",value:e},color:{type:"c",value:new E.Color(u)}}),vertexShader:["attribute vec4 position;","attribute vec2 uv;","attribute float page;","uniform mat4 projectionMatrix;","uniform mat4 modelViewMatrix;","varying vec2 vUv;","varying float vPage;","void main() {","vUv = uv;","vPage = page;","gl_Position = projectionMatrix * modelViewMatrix * position;","}"].join("\n"),fragmentShader:["precision "+r+" float;","uniform float opacity;","uniform vec3 color;",o,"varying float vPage;","varying vec2 vUv;","void main() {","vec4 sampleColor = vec4(0.0);",s,"gl_FragColor = sampleColor * vec4(color, opacity);",0===a?"":" if (gl_FragColor.a < "+a+") discard;","}"].join("\n")},c,t)}function N(t){var e="number"==typeof(t=t||{}).opacity?t.opacity:1,r="number"==typeof t.alphaTest?t.alphaTest:1e-4,a=t.precision||"highp",i=t.color,n=t.map;return delete t.map,delete t.color,delete t.precision,delete t.opacity,Object.assign({uniforms:{opacity:{type:"f",value:e},map:{type:"t",value:n||new E.Texture},color:{type:"c",value:new E.Color(i)}},vertexShader:["attribute vec2 uv;","attribute vec4 position;","uniform mat4 projectionMatrix;","uniform mat4 modelViewMatrix;","varying vec2 vUv;","void main() {","vUv = uv;","gl_Position = projectionMatrix * modelViewMatrix * position;","}"].join("\n"),fragmentShader:["#ifdef GL_OES_standard_derivatives","#extension GL_OES_standard_derivatives : enable","#endif","precision "+a+" float;","uniform float opacity;","uniform vec3 color;","uniform sampler2D map;","varying vec2 vUv;","float aastep(float value) {"," #ifdef GL_OES_standard_derivatives"," float afwidth = length(vec2(dFdx(value), dFdy(value))) * 0.70710678118654757;"," #else"," float afwidth = (1.0 / 32.0) * (1.4142135623730951 / (2.0 * gl_FragCoord.w));"," #endif"," return smoothstep(0.5 - afwidth, 0.5 + afwidth, value);","}","void main() {"," vec4 texColor = texture2D(map, vUv);"," float alpha = aastep(texColor.a);"," gl_FragColor = vec4(color, opacity * alpha);",0===r?"":" if (gl_FragColor.a < "+r+") discard;","}"].join("\n")},t)}var k=r.eZ,V=r.mg,I=r.j8,P=r.MY,H=r.O_,L=r.ST,z=r.qD,Y=r.L1,R=r.m7,q=r.$B;export{k as computeBox,V as computeSphere,I as createBasicShader,P as createMSDFShader,H as createMultipageShader,L as createSDFShader,z as createTextGeometry,Y as pages,R as positions,q as uvs};