Skip to content

Commit e34ab7e

Browse files
committed
feat: add ghostscript wasm
1 parent 2beb76e commit e34ab7e

File tree

13 files changed

+2994
-0
lines changed

13 files changed

+2994
-0
lines changed

packages/ghostscript/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out/

packages/ghostscript/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# gs-wasm
2+
3+
## Special Thank ❤️
4+
5+
[@jsscheller]((https://github.com/jsscheller/ghostscript-wasm)) - for build script.

packages/ghostscript/build.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
OUT_DIR="$PWD/out"
5+
ROOT="$PWD"
6+
EMCC_FLAGS_DEBUG="-Os -g3"
7+
EMCC_FLAGS_RELEASE="-Oz -flto"
8+
9+
export CPPFLAGS="-I$OUT_DIR/include"
10+
export LDFLAGS="-L$OUT_DIR/lib"
11+
export PKG_CONFIG_PATH="$OUT_DIR/pkgconfig"
12+
export EM_PKG_CONFIG_PATH="$PKG_CONFIG_PATH"
13+
export CFLAGS="$EMCC_FLAGS_RELEASE"
14+
export CXXFLAGS="$CFLAGS"
15+
16+
mkdir -p "$OUT_DIR"
17+
18+
cd "$ROOT/ghostpdl"
19+
emconfigure ./autogen.sh \
20+
CCAUX=gcc CFLAGSAUX= CPPFLAGSAUX= \
21+
--host="wasm32-unknown-linux" \
22+
--prefix="$OUT_DIR" \
23+
--disable-threading \
24+
--disable-cups \
25+
--disable-dbus \
26+
--disable-gtk \
27+
--with-drivers=FILES \
28+
--with-arch_h="$ROOT/build/arch_wasm.h"
29+
30+
export GS_LDFLAGS="\
31+
-lnodefs.js -lworkerfs.js \
32+
--pre-js "$ROOT/build/pre.js" \
33+
--post-js "$ROOT/build/post.js" \
34+
--closure 1 \
35+
-s STACK_SIZE=131072 \
36+
-s USE_ZLIB=1 \
37+
-s EXPORT_ES6=1 \
38+
-s BINARYEN_EXTRA_PASSES=\"--pass-arg=max-func-params@39\" \
39+
-s WASM_BIGINT=1 \
40+
-s INITIAL_MEMORY=67108864 \
41+
-s ALLOW_MEMORY_GROWTH=1 \
42+
-s EXPORTED_RUNTIME_METHODS='[\"callMain\",\"FS\",\"NODEFS\",\"WORKERFS\",\"ENV\"]' \
43+
-s INCOMING_MODULE_JS_API='[\"noInitialRun\",\"noFSInit\",\"locateFile\",\"preRun\",\"instantiateWasm\"]' \
44+
-s NO_DISABLE_EXCEPTION_CATCHING=1 \
45+
-s MODULARIZE=1 \
46+
"
47+
48+
emmake make \
49+
XE=".js" \
50+
LDFLAGS="$LDFLAGS $GS_LDFLAGS" \
51+
-j install
52+
53+
# Copy to dist
54+
mkdir -p "$ROOT/dist"
55+
cd "$ROOT/dist"
56+
cp $ROOT/ghostpdl/bin/gs.* .
57+
cp $ROOT/build/gs.cjs .
58+
59+
# Apply version & create d.ts
60+
node "$ROOT/build/post-build.js"
61+
62+
# Cleanup
63+
cd "$ROOT/ghostpdl"
64+
git clean -xdf
65+
git checkout .
66+
67+
echo "Done!"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright (C) 2001-2018 Artifex Software, Inc.
2+
All Rights Reserved.
3+
This software is provided AS-IS with no warranty, either express or
4+
implied.
5+
This software is distributed under license and may not be copied,
6+
modified or distributed except as expressly authorized under the terms
7+
of the license contained in the file LICENSE in this distribution.
8+
Refer to licensing information at http://www.artifex.com or contact
9+
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
10+
CA 94945, U.S.A., +1(415)492-9861, for further information.
11+
*/
12+
/* Parameters derived from machine and compiler architecture. */
13+
/* This file was generated mechanically by genarch.c, for a 64bit */
14+
/* Microsoft Windows machine, compiling with MSVC. */
15+
16+
/* ---------------- Scalar alignments ---------------- */
17+
18+
#define ARCH_ALIGN_SHORT_MOD 2
19+
#define ARCH_ALIGN_INT_MOD 4
20+
#define ARCH_ALIGN_LONG_MOD 4
21+
#define ARCH_ALIGN_PTR_MOD 4
22+
#define ARCH_ALIGN_FLOAT_MOD 4
23+
#define ARCH_ALIGN_DOUBLE_MOD 8
24+
25+
/* ---------------- Scalar sizes ---------------- */
26+
27+
#define ARCH_LOG2_SIZEOF_CHAR 0
28+
#define ARCH_LOG2_SIZEOF_SHORT 1
29+
#define ARCH_LOG2_SIZEOF_INT 2
30+
#define ARCH_LOG2_SIZEOF_LONG 2
31+
32+
#ifndef ARCH_SIZEOF_GX_COLOR_INDEX
33+
#define ARCH_SIZEOF_GX_COLOR_INDEX 8
34+
#endif
35+
36+
#define ARCH_SIZEOF_PTR 4
37+
#define ARCH_SIZEOF_FLOAT 4
38+
#define ARCH_SIZEOF_DOUBLE 8
39+
#define ARCH_FLOAT_MANTISSA_BITS 24
40+
#define ARCH_DOUBLE_MANTISSA_BITS 53
41+
42+
/* ---------------- Unsigned max values ---------------- */
43+
44+
#define ARCH_MAX_UCHAR ((unsigned char)0xff + (unsigned char)0)
45+
#define ARCH_MAX_USHORT ((unsigned short)0xffff + (unsigned short)0)
46+
#define ARCH_MAX_UINT ((unsigned int)~0 + (unsigned int)0)
47+
#define ARCH_MAX_ULONG ((unsigned long)~0L + (unsigned long)0)
48+
49+
/* ---------------- Miscellaneous ---------------- */
50+
51+
#define ARCH_IS_BIG_ENDIAN 0
52+
#define ARCH_PTRS_ARE_SIGNED 0
53+
#define ARCH_FLOATS_ARE_IEEE 1
54+
#define ARCH_ARITH_RSHIFT 2
55+
#define ARCH_DIV_NEG_POS_TRUNCATES 1
56+
57+
#define ARCH_SIZEOF_SIZE_T 4
58+
#define ARCH_MAX_SIZE_T ((unsigned int)~0 + (unsigned int)0)

packages/ghostscript/build/gs.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = function (...args) {
2+
return import('./gs.js')
3+
.then(m => m.default.call(this, ...args))
4+
}

packages/ghostscript/build/gs.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface GSModule extends EmscriptenModule {
2+
version: string,
3+
callMain: (args: string[]) => Promise<number>
4+
FS: typeof FS,
5+
NODEFS: Emscripten.FileSystemType,
6+
WORKERFS: Emscripten.FileSystemType,
7+
}
8+
9+
declare const initGS: EmscriptenModuleFactory<GSModule>
10+
11+
export default initGS
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { writeFile, readFile } from 'node:fs/promises'
2+
import { fileURLToPath } from 'url';
3+
import { dirname, resolve } from 'path'
4+
import { EOL } from 'node:os'
5+
6+
const __filename = fileURLToPath(import.meta.url)
7+
const __dirname = dirname(__filename)
8+
9+
async function applyVersion () {
10+
const pkgJSON = JSON.parse(await readFile(resolve(__dirname, '../package.json')))
11+
const fileJS = resolve(__dirname, '../dist/gs.js')
12+
const origin = await readFile(fileJS)
13+
14+
await writeFile(fileJS, origin.toString().replace('__VERSION__', pkgJSON.version))
15+
}
16+
17+
async function createDTS () {
18+
const emscriptenDTS = await readFile(resolve(__dirname, '../../../node_modules/@types/emscripten/index.d.ts'))
19+
const gsDTS = await readFile(resolve(__dirname, './gs.d.ts'))
20+
21+
await writeFile(resolve(__dirname, '../dist/gs.d.ts'), `${emscriptenDTS}${EOL}${gsDTS}`)
22+
}
23+
24+
async function main () {
25+
await applyVersion()
26+
await createDTS()
27+
}
28+
29+
main()

packages/ghostscript/build/post.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Object.assign(FS, {
2+
["init"]: FS.init,
3+
["mkdir"]: FS.mkdir,
4+
["mount"]: FS.mount,
5+
["chdir"]: FS.chdir,
6+
["writeFile"]: FS.writeFile,
7+
["readFile"]: FS.readFile,
8+
["createLazyFile"]: FS.createLazyFile,
9+
["setIgnorePermissions"]: function(val) {
10+
FS.ignorePermissions = val
11+
},
12+
})
13+
14+
Module['version'] = '__VERSION__'

packages/ghostscript/build/pre.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Module["noInitialRun"] = true;

packages/ghostscript/ghostpdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 7145885041bb52cc23964f0aa2aec1b1c82b5908

0 commit comments

Comments
 (0)