kittyhtml 0.1.0 → 0.2.0

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/README.md CHANGED
@@ -14,7 +14,7 @@ npm install
14
14
  npm link # exposes the `kittyhtml` binary on your PATH
15
15
  ```
16
16
 
17
- Requires Node 20+. Pulls in [`canvas`](https://www.npmjs.com/package/canvas) (native build) and `dropflow`.
17
+ Requires Node 20+. Pulls in [`@napi-rs/canvas`](https://www.npmjs.com/package/@napi-rs/canvas) (prebuilt native binary, no compile step) and `dropflow`.
18
18
 
19
19
  ## Use
20
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kittyhtml",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Render HTML to an image and display it inline in Kitty/iTerm2-capable terminals. No browser — CSS layout via DropFlow.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -26,7 +26,7 @@
26
26
  "demo": "node src/cli.js --demo"
27
27
  },
28
28
  "dependencies": {
29
- "canvas": "^2.11.2",
29
+ "@napi-rs/canvas": "^1.0.0",
30
30
  "dropflow": "^0.6.1"
31
31
  },
32
32
  "keywords": [
package/src/render.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as flow from 'dropflow';
2
2
  import parse from 'dropflow/parse.js';
3
- import { createCanvas } from 'canvas';
3
+ import { createCanvas, GlobalFonts, loadImage } from '@napi-rs/canvas';
4
4
 
5
5
  const FONTS_DIR = new URL('../assets/fonts/', import.meta.url);
6
6
  const BUNDLED_FONTS = [
@@ -12,13 +12,26 @@ const BUNDLED_FONTS = [
12
12
  'NotoSansMono-Bold.ttf',
13
13
  ];
14
14
 
15
- let fontsReady = false;
16
- function ensureFonts() {
17
- if (fontsReady) return;
15
+ let envReady = false;
16
+ function ensureEnv() {
17
+ if (envReady) return;
18
+
19
+ // Tell DropFlow how to register a font and decode an image into the
20
+ // @napi-rs/canvas backend (default wiring in environment-node.js targets the
21
+ // legacy `canvas` package).
22
+ flow.environment.registerFont = face => {
23
+ const key = GlobalFonts.register(face.getBuffer(), face.uniqueFamily);
24
+ if (key) return () => GlobalFonts.remove(key);
25
+ };
26
+ flow.environment.createDecodedImage = async image => {
27
+ return await loadImage(Buffer.from(image.buffer));
28
+ };
29
+
18
30
  for (const file of BUNDLED_FONTS) {
19
31
  flow.fonts.add(flow.createFaceFromTablesSync(new URL(file, FONTS_DIR)));
20
32
  }
21
- fontsReady = true;
33
+
34
+ envReady = true;
22
35
  }
23
36
 
24
37
  /**
@@ -34,7 +47,7 @@ function ensureFonts() {
34
47
  */
35
48
  export async function renderHtml(html, opts = {}) {
36
49
  const { width = 800, height = null, scale = 1, background = null } = opts;
37
- ensureFonts();
50
+ ensureEnv();
38
51
 
39
52
  const root = parse(html);
40
53
  await flow.load(root);
@@ -60,5 +73,5 @@ export async function renderHtml(html, opts = {}) {
60
73
  ctx.fillRect(0, 0, pxWidth, pxHeight);
61
74
  }
62
75
  flow.paintToCanvas(layout, ctx);
63
- return canvas.toBuffer('image/png');
76
+ return await canvas.encode('png');
64
77
  }