circuit-json-to-gltf 0.0.33 → 0.0.35

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.
@@ -0,0 +1,71 @@
1
+ import {
2
+ tscircuit_font_default
3
+ } from "./chunk-W5ZY3YD5.js";
4
+ import "./chunk-QGM4M3NI.js";
5
+
6
+ // lib/utils/svg-to-png.ts
7
+ import { Resvg } from "@resvg/resvg-js";
8
+ var isNode = typeof process !== "undefined" && process.versions && process.versions.node;
9
+ async function svgToPng(svgString, options = {}) {
10
+ const fontBuffer = Buffer.from(tscircuit_font_default, "base64");
11
+ let tempFontPath;
12
+ let cleanupFn;
13
+ if (isNode) {
14
+ try {
15
+ const [fs, os, path] = await Promise.all([
16
+ import("fs"),
17
+ import("os"),
18
+ import("path")
19
+ ]);
20
+ const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "resvg-font-"));
21
+ tempFontPath = path.join(tempDir, "tscircuit-font.ttf");
22
+ fs.writeFileSync(tempFontPath, fontBuffer);
23
+ cleanupFn = () => {
24
+ try {
25
+ fs.unlinkSync(tempFontPath);
26
+ } catch {
27
+ }
28
+ };
29
+ } catch (err) {
30
+ console.warn(
31
+ "Failed to create temporary font file, falling back to browser mode:",
32
+ err
33
+ );
34
+ }
35
+ }
36
+ try {
37
+ const opts = {
38
+ background: options.background,
39
+ fitTo: options.width ? {
40
+ mode: "width",
41
+ value: options.width
42
+ } : options.height ? {
43
+ mode: "height",
44
+ value: options.height
45
+ } : void 0,
46
+ font: {
47
+ fontFiles: tempFontPath ? [tempFontPath, ...options.fonts || []] : options.fonts || [],
48
+ loadSystemFonts: false,
49
+ defaultFontFamily: "TscircuitAlphabet",
50
+ monospaceFamily: "TscircuitAlphabet",
51
+ sansSerifFamily: "TscircuitAlphabet"
52
+ }
53
+ };
54
+ const resvg = new Resvg(svgString, opts);
55
+ const pngData = resvg.render();
56
+ const pngBuffer = pngData.asPng();
57
+ return Buffer.from(pngBuffer);
58
+ } finally {
59
+ if (cleanupFn) {
60
+ cleanupFn();
61
+ }
62
+ }
63
+ }
64
+ async function svgToPngDataUrl(svgString, options = {}) {
65
+ const pngBuffer = await svgToPng(svgString, options);
66
+ return `data:image/png;base64,${pngBuffer.toString("base64")}`;
67
+ }
68
+ export {
69
+ svgToPng,
70
+ svgToPngDataUrl
71
+ };
@@ -0,0 +1,99 @@
1
+ import {
2
+ tscircuit_font_default
3
+ } from "./chunk-W5ZY3YD5.js";
4
+ import {
5
+ __require
6
+ } from "./chunk-QGM4M3NI.js";
7
+
8
+ // lib/utils/svg-to-png-browser.ts
9
+ import { Resvg, initWasm } from "@resvg/resvg-wasm";
10
+ var wasmInitialized = false;
11
+ async function ensureWasmInitialized() {
12
+ if (!wasmInitialized) {
13
+ try {
14
+ if (typeof process !== "undefined" && process.versions?.node) {
15
+ const { readFileSync } = await import("fs");
16
+ const { dirname, join } = await import("path");
17
+ try {
18
+ const packagePath = __require.resolve("@resvg/resvg-wasm/package.json");
19
+ const wasmPath = join(dirname(packagePath), "index_bg.wasm");
20
+ const wasmBuffer = readFileSync(wasmPath);
21
+ await initWasm(wasmBuffer);
22
+ } catch (pathError) {
23
+ try {
24
+ const modulePath = __require.resolve("@resvg/resvg-wasm");
25
+ const wasmPath = join(dirname(modulePath), "index_bg.wasm");
26
+ const wasmBuffer = readFileSync(wasmPath);
27
+ await initWasm(wasmBuffer);
28
+ } catch (fallbackError) {
29
+ throw new Error(
30
+ `Failed to locate WASM file: ${pathError.message}, ${fallbackError.message}`
31
+ );
32
+ }
33
+ }
34
+ } else {
35
+ try {
36
+ const wasmUrl = await import("@resvg/resvg-wasm/index_bg.wasm?url");
37
+ await initWasm(fetch(wasmUrl.default));
38
+ } catch {
39
+ await initWasm(
40
+ fetch("https://unpkg.com/@resvg/resvg-wasm@2.6.2/index_bg.wasm")
41
+ );
42
+ }
43
+ }
44
+ wasmInitialized = true;
45
+ } catch (error) {
46
+ console.error("Failed to initialize WASM:", error);
47
+ throw error;
48
+ }
49
+ }
50
+ }
51
+ async function svgToPng(svgString, options = {}) {
52
+ await ensureWasmInitialized();
53
+ const base64ToUint8Array = (base64) => {
54
+ const binaryString = atob(base64);
55
+ const len = binaryString.length;
56
+ const bytes = new Uint8Array(len);
57
+ for (let i = 0; i < len; i++) {
58
+ bytes[i] = binaryString.charCodeAt(i);
59
+ }
60
+ return bytes;
61
+ };
62
+ const fontBuffer = base64ToUint8Array(tscircuit_font_default);
63
+ const opts = {
64
+ background: options.background,
65
+ font: {
66
+ loadSystemFonts: false,
67
+ fontBuffers: [fontBuffer],
68
+ defaultFontFamily: "TscircuitAlphabet",
69
+ monospaceFamily: "TscircuitAlphabet",
70
+ sansSerifFamily: "TscircuitAlphabet"
71
+ },
72
+ fitTo: options.width ? {
73
+ mode: "width",
74
+ value: options.width
75
+ } : options.height ? {
76
+ mode: "height",
77
+ value: options.height
78
+ } : void 0
79
+ };
80
+ const resvg = new Resvg(svgString, opts);
81
+ const pngData = resvg.render();
82
+ const pngBuffer = pngData.asPng();
83
+ return pngBuffer;
84
+ }
85
+ async function svgToPngDataUrl(svgString, options = {}) {
86
+ const pngBuffer = await svgToPng(svgString, options);
87
+ let binary = "";
88
+ const bytes = new Uint8Array(pngBuffer);
89
+ const len = bytes.byteLength;
90
+ for (let i = 0; i < len; i++) {
91
+ binary += String.fromCharCode(bytes[i]);
92
+ }
93
+ const base64 = btoa(binary);
94
+ return `data:image/png;base64,${base64}`;
95
+ }
96
+ export {
97
+ svgToPng,
98
+ svgToPngDataUrl
99
+ };
package/package.json CHANGED
@@ -2,12 +2,13 @@
2
2
  "name": "circuit-json-to-gltf",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.33",
5
+ "version": "0.0.35",
6
6
  "scripts": {
7
7
  "test": "bun test tests/",
8
8
  "format": "biome format --write .",
9
9
  "format:check": "biome format .",
10
10
  "build:site": "cosmos-export",
11
+ "build:font": "bun scripts/encode-font.ts",
11
12
  "start": "cosmos",
12
13
  "build": "tsup-node"
13
14
  },
@@ -24,6 +25,7 @@
24
25
  "@google/model-viewer": "^4.1.0",
25
26
  "@resvg/resvg-js": "^2.6.2",
26
27
  "@resvg/resvg-wasm": "^2.6.2",
28
+ "@tscircuit/alphabet": "^0.0.8",
27
29
  "@tscircuit/circuit-json-util": "^0.0.72",
28
30
  "@types/bun": "latest",
29
31
  "@types/react": "^19.1.9",
@@ -1,31 +0,0 @@
1
- import "./chunk-JSBRDJBE.js";
2
-
3
- // lib/utils/svg-to-png.ts
4
- import { Resvg } from "@resvg/resvg-js";
5
- async function svgToPng(svgString, options = {}) {
6
- const opts = {
7
- background: options.background,
8
- fitTo: options.width ? {
9
- mode: "width",
10
- value: options.width
11
- } : options.height ? {
12
- mode: "height",
13
- value: options.height
14
- } : void 0,
15
- font: {
16
- fontFiles: options.fonts || []
17
- }
18
- };
19
- const resvg = new Resvg(svgString, opts);
20
- const pngData = resvg.render();
21
- const pngBuffer = pngData.asPng();
22
- return Buffer.from(pngBuffer);
23
- }
24
- async function svgToPngDataUrl(svgString, options = {}) {
25
- const pngBuffer = await svgToPng(svgString, options);
26
- return `data:image/png;base64,${pngBuffer.toString("base64")}`;
27
- }
28
- export {
29
- svgToPng,
30
- svgToPngDataUrl
31
- };