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