@altano/satori-fit-text 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{BrowserTextMeasurer-IY3VY5L4.js → BrowserTextMeasurer-TCSGQHDB.js} +1 -1
- package/dist/{HeadlessTextMeasurer-SN5RPZSG.js → HeadlessTextMeasurer-46CHWL7H.js} +1 -1
- package/dist/{chunk-MLBSPHAO.js → chunk-JIONCYY3.js} +41 -49
- package/dist/index.js +2 -2
- package/package.json +3 -3
- package/dist/getSatoriFriendlyVNode.d.ts +0 -4
|
@@ -5,40 +5,6 @@ var log_default = log;
|
|
|
5
5
|
|
|
6
6
|
// src/TextMeasurer/TextMeasurer.ts
|
|
7
7
|
import satori from "satori";
|
|
8
|
-
|
|
9
|
-
// src/getSatoriFriendlyVNode.ts
|
|
10
|
-
import { html as htmlToVNode } from "satori-html";
|
|
11
|
-
import he from "he";
|
|
12
|
-
function walkJsxTree(vnode, callback) {
|
|
13
|
-
callback(vnode);
|
|
14
|
-
const { children } = vnode.props;
|
|
15
|
-
if (children == null || typeof children === "string") {
|
|
16
|
-
return;
|
|
17
|
-
} else if (Array.isArray(children)) {
|
|
18
|
-
children.forEach((child) => walkJsxTree(child, callback));
|
|
19
|
-
} else {
|
|
20
|
-
walkJsxTree(children, callback);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function patchJsxTreeForSatori(vnode) {
|
|
24
|
-
walkJsxTree(vnode, (vnode2) => {
|
|
25
|
-
const { style } = vnode2.props;
|
|
26
|
-
if (style != null && typeof style["lineHeight"] === "string" && style["lineHeight"] != "") {
|
|
27
|
-
const num = Number(style["lineHeight"]);
|
|
28
|
-
if (!isNaN(num)) {
|
|
29
|
-
style["lineHeight"] = num;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
function getSatoriFriendlyVNode(html) {
|
|
35
|
-
const htmlWithDecodedHtmlEntities = he.decode(html);
|
|
36
|
-
const vnode = htmlToVNode(htmlWithDecodedHtmlEntities);
|
|
37
|
-
patchJsxTreeForSatori(vnode);
|
|
38
|
-
return vnode;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/TextMeasurer/TextMeasurer.ts
|
|
42
8
|
var TextMeasurer = class {
|
|
43
9
|
constructor(text, font, maxWidth, maxHeight, lineHeight) {
|
|
44
10
|
this.text = text;
|
|
@@ -54,21 +20,47 @@ var TextMeasurer = class {
|
|
|
54
20
|
return width <= this.maxWidth && height <= this.maxHeight;
|
|
55
21
|
}
|
|
56
22
|
async generateSvg(fontSize) {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
23
|
+
const node = {
|
|
24
|
+
type: "div",
|
|
25
|
+
props: {
|
|
26
|
+
style: {
|
|
27
|
+
all: "initial",
|
|
28
|
+
display: "flex",
|
|
29
|
+
flexDirection: "column",
|
|
30
|
+
width: "100%",
|
|
31
|
+
height: "100%",
|
|
32
|
+
margin: 0,
|
|
33
|
+
padding: 0
|
|
34
|
+
},
|
|
35
|
+
children: [
|
|
36
|
+
{
|
|
37
|
+
type: "div",
|
|
38
|
+
props: {
|
|
39
|
+
style: {
|
|
40
|
+
// Static styles
|
|
41
|
+
margin: 0,
|
|
42
|
+
padding: 0,
|
|
43
|
+
boxSizing: "border-box",
|
|
44
|
+
// We render a border so that we measure the true dimensions of the text.
|
|
45
|
+
// Without one, the bounding box might not include significant whitespace.
|
|
46
|
+
// For example, if the last line of text has no descenders, the bounding box
|
|
47
|
+
// would only extend to the baseline of the last line. With a border, the
|
|
48
|
+
// bounding box will include all the whitespace between the baseline and the
|
|
49
|
+
// descent of the text.
|
|
50
|
+
border: "solid red 1px",
|
|
51
|
+
// Dynamic styles based on input
|
|
52
|
+
fontSize,
|
|
53
|
+
fontFamily: this.font.name,
|
|
54
|
+
maxWidth: this.maxWidth,
|
|
55
|
+
lineHeight: this.lineHeight
|
|
56
|
+
},
|
|
57
|
+
children: this.text
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return satori(node, {
|
|
72
64
|
fonts: this.#fonts,
|
|
73
65
|
width: this.maxWidth * 2,
|
|
74
66
|
height: this.maxHeight * 2
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// src/TextMeasurer/getTextMeasurer.ts
|
|
2
2
|
async function getTextMeasurer(...args) {
|
|
3
3
|
if (typeof window !== "undefined") {
|
|
4
|
-
const Measurer = await import("./BrowserTextMeasurer-
|
|
4
|
+
const Measurer = await import("./BrowserTextMeasurer-TCSGQHDB.js");
|
|
5
5
|
return new Measurer.default(...args);
|
|
6
6
|
} else {
|
|
7
|
-
const Measurer = await import("./HeadlessTextMeasurer-
|
|
7
|
+
const Measurer = await import("./HeadlessTextMeasurer-46CHWL7H.js");
|
|
8
8
|
return new Measurer.default(...args);
|
|
9
9
|
}
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altano/satori-fit-text",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Fit text to a bounding box in Node or the browser",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -58,9 +58,9 @@
|
|
|
58
58
|
"typescript": "^5.1.6",
|
|
59
59
|
"vite": "^4.4.9",
|
|
60
60
|
"vitest": "^0.34.3",
|
|
61
|
-
"@altano/tiny-async-pool": "1.0.2",
|
|
62
61
|
"@altano/assets": "0.0.0",
|
|
63
|
-
"@altano/tsconfig": "0.0.0"
|
|
62
|
+
"@altano/tsconfig": "0.0.0",
|
|
63
|
+
"@altano/tiny-async-pool": "1.0.2"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
66
|
"build": "tsup",
|