@open-pencil/cli 0.9.0 → 0.10.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/package.json +2 -2
- package/src/commands/export.ts +1 -2
- package/src/headless.ts +6 -46
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-pencil/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"provenance": true
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@open-pencil/core": "^0.
|
|
22
|
+
"@open-pencil/core": "^0.10.0",
|
|
23
23
|
"agentfmt": "^0.1.3",
|
|
24
24
|
"canvaskit-wasm": "^0.40.0",
|
|
25
25
|
"citty": "^0.1.6"
|
package/src/commands/export.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { basename, extname, resolve } from 'node:path'
|
|
|
3
3
|
|
|
4
4
|
import { renderNodesToSVG, sceneNodeToJSX, selectionToJSX } from '@open-pencil/core'
|
|
5
5
|
|
|
6
|
-
import { loadDocument,
|
|
6
|
+
import { loadDocument, exportNodes, exportThumbnail } from '../headless'
|
|
7
7
|
import { isAppMode, requireFile, rpc } from '../app-client'
|
|
8
8
|
import { ok, printError } from '../format'
|
|
9
9
|
import type { ExportFormat, JSXFormat } from '@open-pencil/core'
|
|
@@ -60,7 +60,6 @@ async function exportViaApp(format: string, args: ExportArgs) {
|
|
|
60
60
|
async function exportFromFile(format: string, args: ExportArgs) {
|
|
61
61
|
const file = requireFile(args.file)
|
|
62
62
|
const graph = await loadDocument(file)
|
|
63
|
-
await loadFonts(graph)
|
|
64
63
|
|
|
65
64
|
const pages = graph.getPages()
|
|
66
65
|
const page = args.page ? pages.find((p) => p.name === args.page) : pages[0]
|
package/src/headless.ts
CHANGED
|
@@ -1,27 +1,14 @@
|
|
|
1
|
-
import CanvasKitInit from 'canvaskit-wasm/full'
|
|
2
|
-
import type { CanvasKit } from 'canvaskit-wasm'
|
|
3
1
|
import {
|
|
4
2
|
parseFigFile,
|
|
3
|
+
initCanvasKit,
|
|
5
4
|
type SceneGraph,
|
|
6
5
|
type ExportFormat,
|
|
7
|
-
SkiaRenderer,
|
|
8
6
|
computeAllLayouts,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
renderThumbnail
|
|
7
|
+
headlessRenderNodes,
|
|
8
|
+
headlessRenderThumbnail
|
|
12
9
|
} from '@open-pencil/core'
|
|
13
10
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
export async function initCanvasKit(): Promise<CanvasKit> {
|
|
17
|
-
if (ck) return ck
|
|
18
|
-
const ckPath = import.meta.resolve('canvaskit-wasm/full')
|
|
19
|
-
const binDir = new URL('.', ckPath).pathname
|
|
20
|
-
ck = await CanvasKitInit({
|
|
21
|
-
locateFile: (file) => binDir + file
|
|
22
|
-
})
|
|
23
|
-
return ck
|
|
24
|
-
}
|
|
11
|
+
export { initCanvasKit }
|
|
25
12
|
|
|
26
13
|
export async function loadDocument(filePath: string): Promise<SceneGraph> {
|
|
27
14
|
const data = await Bun.file(filePath).arrayBuffer()
|
|
@@ -30,38 +17,13 @@ export async function loadDocument(filePath: string): Promise<SceneGraph> {
|
|
|
30
17
|
return graph
|
|
31
18
|
}
|
|
32
19
|
|
|
33
|
-
export async function loadFonts(graph: SceneGraph): Promise<void> {
|
|
34
|
-
const families = new Set<string>()
|
|
35
|
-
for (const node of graph.getAllNodes()) {
|
|
36
|
-
if (node.fontFamily) families.add(node.fontFamily)
|
|
37
|
-
}
|
|
38
|
-
for (const family of families) {
|
|
39
|
-
await loadFont(family)
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function createRenderer(ckInstance: CanvasKit, width: number, height: number): SkiaRenderer {
|
|
44
|
-
const surface = ckInstance.MakeSurface(width, height)!
|
|
45
|
-
const renderer = new SkiaRenderer(ckInstance, surface)
|
|
46
|
-
renderer.viewportWidth = width
|
|
47
|
-
renderer.viewportHeight = height
|
|
48
|
-
renderer.dpr = 1
|
|
49
|
-
return renderer
|
|
50
|
-
}
|
|
51
|
-
|
|
52
20
|
export async function exportNodes(
|
|
53
21
|
graph: SceneGraph,
|
|
54
22
|
pageId: string,
|
|
55
23
|
nodeIds: string[],
|
|
56
24
|
options: { scale?: number; format?: ExportFormat; quality?: number }
|
|
57
25
|
): Promise<Uint8Array | null> {
|
|
58
|
-
|
|
59
|
-
const renderer = createRenderer(ckInstance, 1, 1)
|
|
60
|
-
return renderNodesToImage(ckInstance, renderer, graph, pageId, nodeIds, {
|
|
61
|
-
scale: options.scale ?? 1,
|
|
62
|
-
format: options.format ?? 'PNG',
|
|
63
|
-
quality: options.quality
|
|
64
|
-
})
|
|
26
|
+
return headlessRenderNodes(graph, pageId, nodeIds, options)
|
|
65
27
|
}
|
|
66
28
|
|
|
67
29
|
export async function exportThumbnail(
|
|
@@ -70,7 +32,5 @@ export async function exportThumbnail(
|
|
|
70
32
|
width: number,
|
|
71
33
|
height: number
|
|
72
34
|
): Promise<Uint8Array | null> {
|
|
73
|
-
|
|
74
|
-
const renderer = createRenderer(ckInstance, width, height)
|
|
75
|
-
return renderThumbnail(ckInstance, renderer, graph, pageId, width, height)
|
|
35
|
+
return headlessRenderThumbnail(graph, pageId, width, height)
|
|
76
36
|
}
|