|
@@ -347,6 +347,41 @@ export const measureBaseline = (
|
|
|
return baseline;
|
|
|
};
|
|
|
|
|
|
+export const measureDOMHeight = (
|
|
|
+ text: string,
|
|
|
+ font: FontString,
|
|
|
+ lineHeight: ExcalidrawTextElement["lineHeight"],
|
|
|
+ wrapInContainer?: boolean,
|
|
|
+) => {
|
|
|
+ const container = document.createElement("div");
|
|
|
+ container.style.position = "absolute";
|
|
|
+ container.style.whiteSpace = "pre";
|
|
|
+ container.style.font = font;
|
|
|
+ container.style.minHeight = "1em";
|
|
|
+ if (wrapInContainer) {
|
|
|
+ container.style.overflow = "hidden";
|
|
|
+ container.style.wordBreak = "break-word";
|
|
|
+ container.style.whiteSpace = "pre-wrap";
|
|
|
+ }
|
|
|
+
|
|
|
+ container.style.lineHeight = String(lineHeight);
|
|
|
+
|
|
|
+ container.innerText = text;
|
|
|
+
|
|
|
+ // Baseline is important for positioning text on canvas
|
|
|
+ document.body.appendChild(container);
|
|
|
+
|
|
|
+ const span = document.createElement("span");
|
|
|
+ span.style.display = "inline-block";
|
|
|
+ span.style.overflow = "hidden";
|
|
|
+ span.style.width = "1px";
|
|
|
+ span.style.height = "1px";
|
|
|
+ container.appendChild(span);
|
|
|
+ const domHeight = container.offsetHeight;
|
|
|
+
|
|
|
+ document.body.removeChild(container);
|
|
|
+ return domHeight;
|
|
|
+};
|
|
|
/**
|
|
|
* To get unitless line-height (if unknown) we can calculate it by dividing
|
|
|
* height-per-line by fontSize.
|
|
@@ -366,7 +401,9 @@ export const getLineHeightInPx = (
|
|
|
fontSize: ExcalidrawTextElement["fontSize"],
|
|
|
lineHeight: ExcalidrawTextElement["lineHeight"],
|
|
|
) => {
|
|
|
- return fontSize * lineHeight;
|
|
|
+ const res = fontSize * lineHeight;
|
|
|
+
|
|
|
+ return res;
|
|
|
};
|
|
|
|
|
|
// FIXME rename to getApproxMinContainerHeight
|