Skip to content

Commit

Permalink
Merge pull request #7588 from quarto-dev/typst/custom-fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv authored Nov 15, 2023
2 parents 6fa1a24 + 445d4c3 commit 0837bb6
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 16 deletions.
9 changes: 8 additions & 1 deletion src/command/render/output-typst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { dirname, join, normalize, relative } from "path/mod.ts";
import { ensureDirSync } from "fs/mod.ts";

import {
kFontPaths,
kKeepTyp,
kOutputExt,
kOutputFile,
Expand All @@ -23,6 +24,7 @@ import {
typstCompile,
validateRequiredTypstVersion,
} from "../../core/typst.ts";
import { asArray } from "../../core/array.ts";

export function useTypstPdfOutputRecipe(
format: Format,
Expand Down Expand Up @@ -58,7 +60,12 @@ export function typstPdfOutputRecipe(
// run typst
await validateRequiredTypstVersion();
const pdfOutput = join(inputDir, inputStem + ".pdf");
const result = await typstCompile(input, pdfOutput, options.flags?.quiet);
const result = await typstCompile(
input,
pdfOutput,
options.flags?.quiet,
asArray(format.metadata?.[kFontPaths]) as string[],
);
if (!result.success) {
throw new Error();
}
Expand Down
2 changes: 2 additions & 0 deletions src/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export const kQuartoRequired = "quarto-required";
export const kPreviewMode = "preview-mode";
export const kPreviewModeRaw = "raw";

export const kFontPaths = "font-paths";

export const kHtmlTableProcessing = "html-table-processing";

export const kIdentifierDefaultsKeys = [
Expand Down
21 changes: 18 additions & 3 deletions src/core/typst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,36 @@ export function typstBinaryPath() {
architectureToolsPath("typst");
}

function fontPathsArgs(fontPaths?: string[]) {
// orders matter and fontPathsQuarto should be first for our template to work
const fontPathsQuarto = ["--font-path", resourcePath("formats/typst/fonts")];
const fontPathsEnv = Deno.env.get("TYPST_FONT_PATHS");
let fontExtrasArgs: string[] = [];
if (fontPaths && fontPaths.length > 0) {
fontExtrasArgs = fontPaths.map((p) => ["--font-path", p]).flat();
} else if (fontPathsEnv) {
// Env var is used only if not specified in config by user
// to respect Typst behavior where `--font-path` has precedence over env var
return fontExtrasArgs = ["--font-path", fontPathsEnv];
}

return fontPathsQuarto.concat(fontExtrasArgs);
}

export async function typstCompile(
input: string,
output: string,
quiet = false,
fontPaths?: string[],
) {
if (!quiet) {
typstProgress(input, output);
}

const cmd = [
typstBinaryPath(),
"compile",
input,
"--font-path",
resourcePath("formats/typst/fonts"),
...fontPathsArgs(fontPaths),
output,
];
const result = await execProcess({ cmd });
Expand Down
31 changes: 27 additions & 4 deletions src/resources/editor/tools/vs-code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13440,6 +13440,21 @@ var require_yaml_intelligence_resources = __commonJS({
long: "The math font options for use with `xelatex` or `lualatex` allowing\nany options available through [`fontspec`](https://ctan.org/pkg/fontspec).\n"
}
},
{
name: "font-paths",
schema: {
maybeArrayOf: "string"
},
tags: {
formats: [
"typst"
]
},
description: {
short: "Adds additional directories to search for fonts when compiling with Typst.",
long: "Locally, Typst uses installed system fonts. In addition, some custom path \ncan be specified to add directories that should be scanned for fonts.\nSetting this configuration will take precedence over any path set in TYPST_FONT_PATHS environment variable.\n"
}
},
{
name: "CJKoptions",
schema: {
Expand Down Expand Up @@ -21275,6 +21290,10 @@ var require_yaml_intelligence_resources = __commonJS({
short: "The mode to use when previewing this document.",
long: "The mode to use when previewing this document. To disable any special\npreviewing features, pass <code>raw</code> as the preview-mode."
},
{
short: "Adds additional directories to search for fonts when compiling with\nTypst.",
long: "Locally, Typst uses installed system fonts. In addition, some custom\npath can be specified to add directories that should be scanned for\nfonts. Setting this configuration will take precedence over any path set\nin TYPST_FONT_PATHS environment variable."
},
{
short: "Adds the necessary setup to the document preamble to generate PDF/A\nof the type specified.",
long: 'Adds the necessary setup to the document preamble to generate PDF/A\nof the type specified.\nIf the value is set to <code>true</code>, <code>1b:2005</code> will\nbe used as default.\nTo successfully generate PDF/A the required ICC color profiles have\nto be available and the content and all included files (such as images)\nhave to be standard conforming. The ICC profiles and output intent may\nbe specified using the variables <code>pdfaiccprofile</code> and\n<code>pdfaintent</code>. See also <a href="https://wiki.contextgarden.net/PDF/A">ConTeXt PDFA</a> for more\ndetails.'
Expand Down Expand Up @@ -22226,7 +22245,11 @@ var require_yaml_intelligence_resources = __commonJS({
},
"Disambiguating year suffix in author-date styles (e.g.&nbsp;\u201Ca\u201D in \u201CDoe,\n1999a\u201D).",
"Manuscript configuration",
"internal-schema-hack"
"internal-schema-hack",
{
short: "Adds additional directories to search for fonts when compiling with\nTypst.",
long: "Locally, Typst uses installed system fonts. In addition, some custom\npath can be specified to add directories that should be scanned for\nfonts. Setting this configuration will take precedence over any path set\nin TYPST_FONT_PATHS environment variable."
}
],
"schema/external-schemas.yml": [
{
Expand Down Expand Up @@ -22450,12 +22473,12 @@ var require_yaml_intelligence_resources = __commonJS({
mermaid: "%%"
},
"handlers/mermaid/schema.yml": {
_internalId: 178122,
_internalId: 178550,
type: "object",
description: "be an object",
properties: {
"mermaid-format": {
_internalId: 178114,
_internalId: 178542,
type: "enum",
enum: [
"png",
Expand All @@ -22471,7 +22494,7 @@ var require_yaml_intelligence_resources = __commonJS({
exhaustiveCompletions: true
},
theme: {
_internalId: 178121,
_internalId: 178549,
type: "anyOf",
anyOf: [
{
Expand Down
31 changes: 27 additions & 4 deletions src/resources/editor/tools/yaml/web-worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 27 additions & 4 deletions src/resources/editor/tools/yaml/yaml-intelligence-resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -6412,6 +6412,21 @@
"long": "The math font options for use with `xelatex` or `lualatex` allowing\nany options available through [`fontspec`](https://ctan.org/pkg/fontspec).\n"
}
},
{
"name": "font-paths",
"schema": {
"maybeArrayOf": "string"
},
"tags": {
"formats": [
"typst"
]
},
"description": {
"short": "Adds additional directories to search for fonts when compiling with Typst.",
"long": "Locally, Typst uses installed system fonts. In addition, some custom path \ncan be specified to add directories that should be scanned for fonts.\nSetting this configuration will take precedence over any path set in TYPST_FONT_PATHS environment variable.\n"
}
},
{
"name": "CJKoptions",
"schema": {
Expand Down Expand Up @@ -14247,6 +14262,10 @@
"short": "The mode to use when previewing this document.",
"long": "The mode to use when previewing this document. To disable any special\npreviewing features, pass <code>raw</code> as the preview-mode."
},
{
"short": "Adds additional directories to search for fonts when compiling with\nTypst.",
"long": "Locally, Typst uses installed system fonts. In addition, some custom\npath can be specified to add directories that should be scanned for\nfonts. Setting this configuration will take precedence over any path set\nin TYPST_FONT_PATHS environment variable."
},
{
"short": "Adds the necessary setup to the document preamble to generate PDF/A\nof the type specified.",
"long": "Adds the necessary setup to the document preamble to generate PDF/A\nof the type specified.\nIf the value is set to <code>true</code>, <code>1b:2005</code> will\nbe used as default.\nTo successfully generate PDF/A the required ICC color profiles have\nto be available and the content and all included files (such as images)\nhave to be standard conforming. The ICC profiles and output intent may\nbe specified using the variables <code>pdfaiccprofile</code> and\n<code>pdfaintent</code>. See also <a href=\"https://wiki.contextgarden.net/PDF/A\">ConTeXt PDFA</a> for more\ndetails."
Expand Down Expand Up @@ -15198,7 +15217,11 @@
},
"Disambiguating year suffix in author-date styles (e.g.&nbsp;“a” in “Doe,\n1999a”).",
"Manuscript configuration",
"internal-schema-hack"
"internal-schema-hack",
{
"short": "Adds additional directories to search for fonts when compiling with\nTypst.",
"long": "Locally, Typst uses installed system fonts. In addition, some custom\npath can be specified to add directories that should be scanned for\nfonts. Setting this configuration will take precedence over any path set\nin TYPST_FONT_PATHS environment variable."
}
],
"schema/external-schemas.yml": [
{
Expand Down Expand Up @@ -15422,12 +15445,12 @@
"mermaid": "%%"
},
"handlers/mermaid/schema.yml": {
"_internalId": 178122,
"_internalId": 178550,
"type": "object",
"description": "be an object",
"properties": {
"mermaid-format": {
"_internalId": 178114,
"_internalId": 178542,
"type": "enum",
"enum": [
"png",
Expand All @@ -15443,7 +15466,7 @@
"exhaustiveCompletions": true
},
"theme": {
"_internalId": 178121,
"_internalId": 178549,
"type": "anyOf",
"anyOf": [
{
Expand Down
12 changes: 12 additions & 0 deletions src/resources/schema/document-fonts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@
The math font options for use with `xelatex` or `lualatex` allowing
any options available through [`fontspec`](https://ctan.org/pkg/fontspec).

- name: font-paths
schema:
maybeArrayOf: string
tags:
formats: [typst]
description:
short: "Adds additional directories to search for fonts when compiling with Typst."
long: |
Locally, Typst uses installed system fonts. In addition, some custom path
can be specified to add directories that should be scanned for fonts.
Setting this configuration will take precedence over any path set in TYPST_FONT_PATHS environment variable.

- name: CJKoptions
schema:
maybeArrayOf: string
Expand Down
16 changes: 16 additions & 0 deletions tests/docs/smoke-all/typst/custom-fonts.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
title: font typst
format:
typst:
mainfont: "Agbalumo"
font-paths: myfonts
#TODO: How to test the PDF for correct font usage ?
---

# Content

This is a sentence

::: {.callout-note}
Where the icon should be ok
:::
Binary file not shown.

0 comments on commit 0837bb6

Please sign in to comment.