3 lines
2.0 KiB
JavaScript
3 lines
2.0 KiB
JavaScript
var gdjs;(function(l){const i=new l.Logger("Font manager");class d{constructor(t,e){this._loadedFontFamily={};this._loadedFonts={};this._filenameToFontFamily={};this._resources=new Map,this.setResources(t),this._resourcesLoader=e}setResources(t){this._resources.clear();for(const e of t)e.kind==="font"&&this._resources.set(e.name,e)}getFontFamily(t){return this._loadedFontFamily[t]?this._loadedFontFamily[t]:"Arial"}getFontFile(t){return this._loadedFonts[t]?this._loadedFonts[t].file||"":t}_getFontFamilyFromFilename(t){if(this._filenameToFontFamily[t])return this._filenameToFontFamily[t];let e="gdjs_font_"+t.toLowerCase().replace(/[^\w]/gi,"-");const n=e;let r=2;for(;this._filenameToFontFamily[e];)e=e+"-"+r,r++;return this._filenameToFontFamily[t]=n}_loadFont(t,e){const n={},r="url("+encodeURI(e)+")";if(typeof FontFace!="undefined")return fetch(this._resourcesLoader.getFullUrl(e),{credentials:this._resourcesLoader.checkIfCredentialsRequired(e)?"include":"same-origin"}).then(o=>{if(!o.ok){const s="Unable to fetch "+e+" to be loaded as a font. HTTP status is: "+o.status+".";throw i.error(s),new Error(s)}return o.arrayBuffer()}).then(o=>{const s=new FontFace(t,o,n);document.fonts.add(s)});{const o=document.createElement("style");return o.appendChild(document.createTextNode("@font-face { font-family: '"+t+"'; src: "+r+"; }")),document.head.appendChild(o),new FontFaceObserver(t,n).load()}}async loadFonts(t){const e={};for(const o of this._resources.values())if(o.file){if(this._loadedFonts[o.name])continue;e[o.file]=e[o.file]?e[o.file].concat(o):[o]}const n=Object.keys(e).length;if(n===0)return 0;let r=0;return await Promise.all(Object.keys(e).map(async o=>{const s=this._getFontFamilyFromFilename(o),c=e[o];try{await this._loadFont(s,o)}catch(a){i.error('Error loading font resource "'+c[0].name+'" (file: '+o+"): "+(a.message||"Unknown error"))}c.forEach(a=>{this._loadedFontFamily[a.name]=s,this._loadedFonts[a.name]=a}),r++,t(r,n)})),n}}l.FontFaceObserverFontManager=d,l.FontManager=d})(gdjs||(gdjs={}));
|
|
//# sourceMappingURL=fontfaceobserver-font-manager.js.map
|