UNPKG

@altano/satori-fit-text

Version:
41 lines (39 loc) 1.33 kB
import log_default from "../log.js"; import { TextMeasurer } from "./TextMeasurer.js"; //#region src/TextMeasurer/BrowserTextMeasurer.ts var BrowserTextMeasurer = class extends TextMeasurer { #createSvgElement(svgXML) { const wrapper = document.createElement("div"); wrapper.innerHTML = svgXML; const svg = wrapper.firstElementChild; if (svg == null || !(svg instanceof SVGSVGElement)) throw new Error(`Child should be svg element`); return svg; } async getDimensions(fontSize) { const svgXML = await this.createSvgXmlString(fontSize); const svg = this.#createSvgElement(svgXML); try { document.body.appendChild(svg); const dimensions = svg.getBBox(); const width = dimensions.width + Math.min(dimensions.x, 0); const height = dimensions.height + Math.min(dimensions.y, 0); log_default({ fontSize, width: `${width}px`, height: `${height}px`, widthFits: `${width}px ${width <= this.maxWidth ? "DOES" : "does NOT"} fit in ${this.maxWidth}`, heightFits: `${height}px ${height <= this.maxHeight ? "DOES" : "does NOT"} fit in ${this.maxHeight}` }); return { width, height }; } finally { svg.parentElement?.removeChild?.(svg); } } }; /* v8 ignore stop */ //#endregion export { BrowserTextMeasurer as default }; //# sourceMappingURL=BrowserTextMeasurer.js.map