UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 1.58 kB
import{path as t}from"../../core/path.js";import{string as e}from"../../core/string.js";import{http as a}from"../../platform/net/http.js";import{Font as o}from"../font/font.js";import{ResourceHandler as r}from"./handler.js";function n(t){return t.version<3&&(t.version<2&&(t.info.maps=t.info.maps||[{width:t.info.width,height:t.info.height}]),t.chars=Object.keys(t.chars||{}).reduce(((a,o)=>{const r=t.chars[o],n=void 0!==r.letter?r.letter:e.fromCodePoint(o);return t.version<2&&(r.map=r.map||0),a[n]=r,a}),{}),t.version=3),t}class s extends r{constructor(t){super(t,"font"),this._loader=t.loader,this.maxRetries=0}load(e,o,r){"string"==typeof e&&(e={load:e,original:e});const s=this;".json"===t.getExtension(e.original)?a.get(e.load,{retry:this.maxRetries>0,maxRetries:this.maxRetries},((t,a)=>{if(t)o(`Error loading font resource: ${e.original} [${t}]`);else{const t=n(a);s._loadTextures(e.load.replace(".json",".png"),t,((e,a)=>{e?o(e):o(null,{data:t,textures:a})}))}})):(r&&r.data&&(r.data=n(r.data)),this._loadTextures(e.load,r&&r.data,o))}_loadTextures(t,e,a){const o=e.info.maps.length;let r=0,n=null;const s=new Array(o),i=this._loader,d=function(e){const d=function(t,i){if(!n){if(t)return n=t,void a(t);i.upload(),s[e]=i,r++,r===o&&a(null,s)}};0===e?i.load(t,"texture",d):i.load(t.replace(".png",`${e}.png`),"texture",d)};for(let t=0;t<o;t++)d(t)}open(t,e,a){let r;return r=e.textures?new o(e.textures,e.data):new o(e,null),r}patch(t,e){const a=t.resource;!a.data&&t.data?a.data=t.data:!t.data&&a.data&&(t.data=a.data),t.data&&(t.data=n(t.data))}}export{s as FontHandler};