Myriad-Dreamin / typst.ts

Run Typst in JavaScriptWorld.
https://myriad-dreamin.github.io/typst.ts
Apache License 2.0
368 stars 22 forks source link

How to load CJK fonts into server-side (non-cli) #548

Closed lhjt closed 2 months ago

lhjt commented 3 months ago

Is your feature request related to a problem? Please describe. Hello - I have been using this library for a few months and have been very happy with it; thanks for your contribution!

However, I have run into a recent issue where trying to render CJK fonts with

    "@myriaddreamin/typst-ts-renderer": "0.5.0-rc3",
    "@myriaddreamin/typst-ts-web-compiler": "0.5.0-rc3",
    "@myriaddreamin/typst.ts": "0.5.0-rc3",

leads to squares in my PDF document:

20240628T161214-Arc-T's Stories

Describe the solution you'd like I noticed in another issue that the CLI has a "feature" called embedded_cjk_fonts - is it possible to enable this feature via the $typst export from import { $typst } from "@myriaddreamin/typst.ts/dist/esm/contrib/snippet.mjs";?

Additional context Calling typst like so:

$typst.setCompilerInitOptions({
  getModule: async () =>
    await fs.readFile("./assets/typst_ts_web_compiler_bg.wasm"),
});

const doc = `...`

await $typst.resetShadow();
const pdf = await $typst.pdf({ mainContent: doc });

Thanks!

Myriad-Dreamin commented 3 months ago

The typst_ts_web_compiler_bg.wasm is designed for web environment. We can load system fonts by overriding the fetch api as node-fetch, but it is cumbersome.

You can wait for the typst_ts_node_compiler

lhjt commented 3 months ago

I authored a previous issue (#514) and you expressed a technique here - so the solution is that I have to load custom CJK-compatible fonts into this preload list?

Myriad-Dreamin commented 3 months ago

Ah, I didn't notice you had opened a same issue.

so the solution is that I have to load custom CJK-compatible fonts into this preload list?

Exactly, you will have to load all fonts for running the web compiler, but not for running the node compiler.

lhjt commented 3 months ago

Exactly, you will have to load all fonts for running the web compiler, but not for running the node compiler.

Alright, thanks! Do you use specific fonts for the embedded_cjk_fonts feature? I am looking forward to the typst_ts_node_compiler but will try and get this to work for the time being as I am unsure of when it will be available.

Myriad-Dreamin commented 3 months ago

They are listed here: https://github.com/Myriad-Dreamin/typst.ts/blob/4f8b1453c000eed5d7c3cf3eb8199447400b0cc2/packages/typst.ts/src/options.init.mts#L62-L87

lhjt commented 3 months ago

Fantastic, thank you 🙂

Myriad-Dreamin commented 3 months ago

I have published @myriaddreamin/typst-ts-node-compiler 0.5.0-rc5. While there is still a dependency issue in published version, you can try the compiler-node template a bit.

yarn add @myriaddreamin/typst-ts-node-compiler

Since there is a dependency bug, you must install the native packages manually, which will be fixed in the next version.


// for darwin arm64 
yarn add @myriaddreamin/typst-ts-node-compiler-darwin-arm64
// for linux arm64 
yarn add @myriaddreamin/typst-ts-node-compiler-linux-arm64-gnu
// for linux arm64 
yarn add @myriaddreamin/typst-ts-node-compiler-linux-arm64-musl
// for win32 arm64 
yarn add @myriaddreamin/typst-ts-node-compiler-win32-arm64-msvc
// for darwin x64 
yarn add @myriaddreamin/typst-ts-node-compiler-darwin-x64
// for win32 x64 
yarn add @myriaddreamin/typst-ts-node-compiler-win32-x64-msvc
// for linux x64 
yarn add @myriaddreamin/typst-ts-node-compiler-linux-x64-gnu
// for linux x64 
yarn add @myriaddreamin/typst-ts-node-compiler-linux-x64-musl
// for freebsd x64 
yarn add @myriaddreamin/typst-ts-node-compiler-freebsd-x64
// for win32 ia32 
yarn add @myriaddreamin/typst-ts-node-compiler-win32-ia32-msvc
// for linux arm 
yarn add @myriaddreamin/typst-ts-node-compiler-linux-arm-gnueabihf
// for linux riscv64 
yarn add @myriaddreamin/typst-ts-node-compiler-linux-riscv64-gnu

image

But I'll review all of public APIs before publishing 0.5.0-rc6 or the stable version 0.5.0.

lhjt commented 3 months ago

Thanks for releasing this - I will have a look at your documentation to see if I can get it to work. Do you plan on pushing a multiarch package that will automatically download the current architecture for the machine under one dependency?

Myriad-Dreamin commented 2 months ago

closing in flavor of having node compiler. Please see templates/compiler-node for use.