@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.
@@ -2,7 +2,7 @@ import {
2
2
  TextMeasurer,
3
3
  getWidthAdjustedForInlineBleed,
4
4
  log_default
5
- } from "./chunk-MLBSPHAO.js";
5
+ } from "./chunk-JIONCYY3.js";
6
6
 
7
7
  // src/TextMeasurer/BrowserTextMeasurer.ts
8
8
  var BrowserTextMeasurer = class extends TextMeasurer {
@@ -2,7 +2,7 @@ import {
2
2
  TextMeasurer,
3
3
  getWidthAdjustedForInlineBleed,
4
4
  log_default
5
- } from "./chunk-MLBSPHAO.js";
5
+ } from "./chunk-JIONCYY3.js";
6
6
 
7
7
  // src/TextMeasurer/HeadlessTextMeasurer.ts
8
8
  import { SVG, registerWindow } from "@svgdotjs/svg.js";
@@ -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 html = `
58
- <div style="
59
- margin: 0;
60
- padding: 0;
61
- font-size: ${fontSize}px;
62
- max-width: ${this.maxWidth}px;
63
- line-height: ${this.lineHeight};
64
- box-sizing: border-box;
65
- border: solid red 1px;
66
- ">
67
- ${this.text}
68
- </div>
69
- `.trim();
70
- const vnode = getSatoriFriendlyVNode(html);
71
- return satori(vnode, {
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-IY3VY5L4.js");
4
+ const Measurer = await import("./BrowserTextMeasurer-TCSGQHDB.js");
5
5
  return new Measurer.default(...args);
6
6
  } else {
7
- const Measurer = await import("./HeadlessTextMeasurer-SN5RPZSG.js");
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.0",
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",
@@ -1,4 +0,0 @@
1
- import { html as htmlToVNode } from "satori-html";
2
- export type VNode = ReturnType<typeof htmlToVNode>;
3
- export default function getSatoriFriendlyVNode(html: string): VNode;
4
- //# sourceMappingURL=getSatoriFriendlyVNode.d.ts.map