Skip to content

Read font path from environment variables - #649

Merged
laurmaedje merged 3 commits into
typst:mainfrom
michidk:font-path-via-env
Apr 12, 2023
Merged

laurmaedje merged 3 commits into
typst:mainfrom
michidk:font-path-via-env

Conversation

@michidk

@michidk michidk commented Apr 7, 2023 •

Copy link
Copy Markdown
Contributor

Hallo 😃,

It would be great if we could read font paths from the environment variables (env vars).

Advantages:

This change enables the "env" feature of clap, which handles the env parsing for us. Command line arguments will take precedence over env vars.

@laurmaedje

Copy link
Copy Markdown
Member

Before proceeding with this, I would like to take a step back and discuss which kinds of use cases people have and whether they are best handled with env var, CLI flag, or something in the Typst file itself.

This is of course a simple change, but once people start relying on it, it's not easy to take it away again. Therefore, I want to be sure we are making the right choice here. The env var solves certain problems with the CLI flag (which I might have merged to quickly), but maybe listing the font dirs in the Typst file itself would be even better for certain cases?

The use case you describe where you set a global font path seems to me as if the font should just be installed on the system. If Typst doesn't find certain system fonts currently that's something we have to fix.

@michidk

michidk commented Apr 11, 2023 •

Copy link
Copy Markdown
Contributor Author

I see your point. In my use case, having a typst instruction that points to the relative file path (probably ./fonts) would actually do the job.

  1. However, I might have a font collection in some directory, with fonts I dont want to install on my system, since then they would appear in every application.

  2. Also imagine mono repos with multiple font templates but only one fonts folder.

  3. With Docker containers I might want to mount my fonts to some arbitrary location without knowing what OS the actual base image is based on and which font dir is used (e.g. the root folder, /fonts).

@michidk

michidk commented Apr 12, 2023 •

Copy link
Copy Markdown
Contributor Author

Okay, I feel like I haven't elaborated on the Docker part well enough, even though that's probably the most important use case.

Usecase 3.1

Imagine some Docker container that either runs typst directly or has some application executing typst (like some invoicing app, generating invoices with typst). In that app, everything can be customized using the web interface, but of course, you cannot install system fonts - since you can only change configuration via the web interface.

In this case, we can assume the Docker container is a black box since we don't want to create a custom Dockerfile and build it (all sorts of stuff could go wrong there).

So what can we do? Simply mount your fonts to /fonts and tell any typst instance to use that directory by passing the environment variable to the Docker container. Both can be done with command line arguments of Docker, without modifying the Dockerfile:

docker run -it --rm -v /home/michi/fonts:/fonts -e TYPST_FONT_DIR=/fonts some-typst-app

This is especially useful in Kubernetes or cloud environments like AWS Fargate, since I can deploy a given Docker container with custom fonts.

Usecase 3.2

A second use case (one I personally have) is devcontainers. I use them in combination with vs code to develop stuff without installing different versions of SDKs or other software. I use some arbitrary base OS like Ubuntu and then use the typst layer to setup typst:

devcontainer config
{
  "name": "Ubuntu",
  "image": "mcr.microsoft.com/devcontainers/base:jammy",
  "features": {
    "ghcr.io/guiyomh/features/just:0": {},
    "ghcr.io/michidk/devcontainers-features/typst:0": {}
  },
  "customizations": {
    "vscode": {
      "extensions": [
        "nvarner.typst-lsp",
        "tomoki1207.pdf",
        "zengfanfan.zeng-color-picker"
      ]
    }
  }
}
The only thing missing now are my system fonts that I have installed on Windows. I want them to be available in my different typst projects without manually installing them in the Docker container everytime I rebuilt it or creating a custom Dockerfile.

With environment variables this gets very easy. The devcontainer config:

{
  "name": "Ubuntu",
  "image": "mcr.microsoft.com/devcontainers/base:jammy",
  "features": {
    "ghcr.io/guiyomh/features/just:0": {},
    "ghcr.io/michidk/devcontainers-features/typst:0": {}
  },
  "mounts": [
      "source=/home/michi/fonts,target=/fonts,type=bind"
  ],
  "containerEnv": {
      "TYPST_FONT_DIR": "/fonts"
  },
  "customizations": {
  ...
  }
}

Comment thread cli/src/args.rs Resolved
@laurmaedje
laurmaedje merged commit 8300f75 into typst:main Apr 12, 2023
@michidk
michidk deleted the font-path-via-env branch April 12, 2023 18:23
@luochen1990

Copy link
Copy Markdown

Hoping for a more detailed description about this ENV variable, e.g. WHAT is the expected seperator when there is more than one path; Which directory level should this PATH point to, 'xxx/share/fonts/opentype/SourceCodePro-Black.otf' or 'xxx/share/fonts/opentype' or 'xxx/share/fonts' ?

Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
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

Successfully merging this pull request may close these issues.

3 participants