Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lhjt opened this issue Jun 28, 2024 · 8 comments
Open

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

lhjt opened this issue Jun 28, 2024 · 8 comments

Comments

@lhjt
Copy link

lhjt commented Jun 28, 2024

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
Copy link
Owner

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
Copy link
Author

lhjt commented Jun 28, 2024

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
Copy link
Owner

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
Copy link
Author

lhjt commented Jun 28, 2024

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
Copy link
Owner

They are listed here:

/** @internal */
const _textFonts: string[] = [
'LinLibertine_R.ttf',
'LinLibertine_RB.ttf',
'LinLibertine_RBI.ttf',
'LinLibertine_RI.ttf',
'NewCMMath-Book.otf',
'NewCMMath-Regular.otf',
'NewCM10-Regular.otf',
'NewCM10-Bold.otf',
'NewCM10-Italic.otf',
'NewCM10-BoldItalic.otf',
'DejaVuSansMono.ttf',
'DejaVuSansMono-Bold.ttf',
'DejaVuSansMono-Oblique.ttf',
'DejaVuSansMono-BoldOblique.ttf',
];
/** @internal */
const _cjkFonts: string[] = [
'InriaSerif-Bold.ttf',
'InriaSerif-BoldItalic.ttf',
'InriaSerif-Italic.ttf',
'InriaSerif-Regular.ttf',
'Roboto-Regular.ttf',
'NotoSerifCJKsc-Regular.otf',
];

@lhjt
Copy link
Author

lhjt commented Jun 28, 2024

Fantastic, thank you 🙂

@Myriad-Dreamin
Copy link
Owner

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
Copy link
Author

lhjt commented Jun 30, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants