@altano/satori-fit-text
Version:
Fit text to a bounding box in Node or the browser
50 lines (48 loc) • 1.16 kB
JavaScript
import satori from "satori";
//#region src/TextMeasurer/TextMeasurer.ts
const epsilon = 1e-4;
var TextMeasurer = class {
#fonts;
constructor(text, font, maxWidth, maxHeight, lineHeight) {
this.text = text;
this.font = font;
this.maxWidth = maxWidth;
this.maxHeight = maxHeight;
this.lineHeight = lineHeight;
this.#fonts = [this.font];
}
async doesSizeFit(fontSize) {
const { width, height } = await this.getDimensions(fontSize);
return width - epsilon <= this.maxWidth && height - epsilon <= this.maxHeight;
}
async createSvgXmlString(fontSize) {
const node = {
type: "div",
key: "satori-fit-text-measure-node",
props: {
style: {
background: "white",
color: "black",
display: "flex",
flex: 1,
boxSizing: "border-box",
margin: 0,
padding: 0,
fontSize,
fontFamily: this.font.name,
maxWidth: this.maxWidth,
lineHeight: this.lineHeight
},
children: this.text
}
};
return satori(node, {
fonts: this.#fonts,
width: this.maxWidth * 2,
height: this.maxHeight * 2
});
}
};
//#endregion
export { TextMeasurer };
//# sourceMappingURL=TextMeasurer.js.map