import*as i18n from"../i18n/i18n.js";import*as Platform from"../platform/platform.js";import*as TextUtils from"../text_utils/text_utils.js";import*as UI from"../ui/ui.js";const UIStrings={font:"Font",previewOfFontFromS:"Preview of font from {PH1}"},str_=i18n.i18n.registerUIStrings("source_frame/FontView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);export class FontView extends UI.View.SimpleView{constructor(e,t){super(i18nString(UIStrings.font)),this.registerRequiredCSS("source_frame/fontView.css",{enableLegacyPatching:!1}),this.element.classList.add("font-view"),this._url=t.contentURL(),UI.ARIAUtils.setAccessibleName(this.element,i18nString(UIStrings.previewOfFontFromS,{PH1:this._url})),this._mimeType=e,this._contentProvider=t,this._mimeTypeLabel=new UI.Toolbar.ToolbarText(e)}async toolbarItems(){return[this._mimeTypeLabel]}_onFontContentLoaded(e,t){const{content:i}=t,n=i?TextUtils.ContentProvider.contentAsDataURL(i,this._mimeType,!0):this._url;this.fontStyleElement&&(this.fontStyleElement.textContent=Platform.StringUtilities.sprintf('@font-face { font-family: "%s"; src: url(%s); }',e,n),this.updateFontPreviewSize())}_createContentIfNeeded(){if(this.fontPreviewElement)return;const e="WebInspectorFontPreview"+ ++_fontId;this.fontStyleElement=document.createElement("style"),this._contentProvider.requestContent().then((t=>{this._onFontContentLoaded(e,t)})),this.element.appendChild(this.fontStyleElement);const t=document.createElement("div");for(let i=0;i<_fontPreviewLines.length;++i)i>0&&t.createChild("br"),UI.UIUtils.createTextChild(t,_fontPreviewLines[i]);this.fontPreviewElement=t.cloneNode(!0),this.fontPreviewElement&&(UI.ARIAUtils.markAsHidden(this.fontPreviewElement),this.fontPreviewElement.style.overflow="hidden",this.fontPreviewElement.style.setProperty("font-family",e),this.fontPreviewElement.style.setProperty("visibility","hidden"),this._dummyElement=t,this._dummyElement.style.visibility="hidden",this._dummyElement.style.zIndex="-1",this._dummyElement.style.display="inline",this._dummyElement.style.position="absolute",this._dummyElement.style.setProperty("font-family",e),this._dummyElement.style.setProperty("font-size",_measureFontSize+"px"),this.element.appendChild(this.fontPreviewElement))}wasShown(){this._createContentIfNeeded(),this.updateFontPreviewSize()}onResize(){if(!this._inResize){this._inResize=!0;try{this.updateFontPreviewSize()}finally{this._inResize=null}}}_measureElement(){if(!this._dummyElement)throw new Error("No font preview loaded");this.element.appendChild(this._dummyElement);const e={width:this._dummyElement.offsetWidth,height:this._dummyElement.offsetHeight};return this.element.removeChild(this._dummyElement),e}updateFontPreviewSize(){if(!this.fontPreviewElement||!this.isShowing())return;this.fontPreviewElement.style.removeProperty("visibility");const e=this._measureElement(),t=e.height,i=e.width,n=this.element.offsetWidth-50,s=this.element.offsetHeight-30;if(!t||!i||!n||!s)return void this.fontPreviewElement.style.removeProperty("font-size");const o=n/i,r=s/t,l=Math.floor(_measureFontSize*Math.min(o,r))-2;this.fontPreviewElement.style.setProperty("font-size",l+"px",void 0)}}let _fontId=0;const _fontPreviewLines=["ABCDEFGHIJKLM","NOPQRSTUVWXYZ","abcdefghijklm","nopqrstuvwxyz","1234567890"],_measureFontSize=50;