Skip to content

Commit 266fae4

Browse files
committed
og_image: Support custom font paths via TYPST_FONT_PATH environment variable
1 parent 63cfa7e commit 266fae4

File tree

1 file changed

+46
-4
lines changed
  • crates/crates_io_og_image/src

1 file changed

+46
-4
lines changed

crates/crates_io_og_image/src/lib.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ impl<'a> OgImageAuthorData<'a> {
7474
/// generating PNG images from a Typst template.
7575
pub struct OgImageGenerator {
7676
typst_binary_path: PathBuf,
77+
typst_font_path: Option<PathBuf>,
7778
}
7879

7980
impl OgImageGenerator {
@@ -88,7 +89,10 @@ impl OgImageGenerator {
8889
/// let generator = OgImageGenerator::new(PathBuf::from("/usr/local/bin/typst"));
8990
/// ```
9091
pub fn new(typst_binary_path: PathBuf) -> Self {
91-
Self { typst_binary_path }
92+
Self {
93+
typst_binary_path,
94+
typst_font_path: None,
95+
}
9296
}
9397

9498
/// Creates a new `OgImageGenerator` using the `TYPST_PATH` environment variable.
@@ -105,11 +109,43 @@ impl OgImageGenerator {
105109
/// # Ok::<(), crates_io_og_image::OgImageError>(())
106110
/// ```
107111
pub fn from_environment() -> Result<Self, OgImageError> {
108-
if let Some(path) = var("TYPST_PATH").map_err(OgImageError::EnvVarError)? {
109-
Ok(Self::new(PathBuf::from(path)))
112+
let typst_path = var("TYPST_PATH").map_err(OgImageError::EnvVarError)?;
113+
let font_path = var("TYPST_FONT_PATH").map_err(OgImageError::EnvVarError)?;
114+
115+
let mut generator = if let Some(path) = typst_path {
116+
Self::new(PathBuf::from(path))
110117
} else {
111-
Ok(Self::default())
118+
Self::default()
119+
};
120+
121+
if let Some(font_path) = font_path {
122+
let current_dir = std::env::current_dir()?;
123+
let font_path = current_dir.join(font_path).canonicalize()?;
124+
generator = generator.with_font_path(font_path);
112125
}
126+
127+
Ok(generator)
128+
}
129+
130+
/// Sets the font path for the Typst compiler.
131+
///
132+
/// This allows specifying a custom directory where Typst will look for fonts
133+
/// during compilation. Setting a custom font directory implies using the
134+
/// `--ignore-system-fonts` flag of the Typst CLI. If not set, Typst will
135+
/// use its default font discovery.
136+
///
137+
/// # Examples
138+
///
139+
/// ```
140+
/// use std::path::PathBuf;
141+
/// use crates_io_og_image::OgImageGenerator;
142+
///
143+
/// let generator = OgImageGenerator::default()
144+
/// .with_font_path(PathBuf::from("/usr/share/fonts"));
145+
/// ```
146+
pub fn with_font_path(mut self, font_path: PathBuf) -> Self {
147+
self.typst_font_path = Some(font_path);
148+
self
113149
}
114150

115151
/// Processes avatars by downloading URLs and copying assets to the assets directory.
@@ -248,6 +284,12 @@ impl OgImageGenerator {
248284
let input = format!("avatar_map={json_avatar_map}");
249285
command.arg("--input").arg(input);
250286

287+
// Pass in the font path if specified
288+
if let Some(font_path) = &self.typst_font_path {
289+
command.arg("--font-path").arg(font_path);
290+
command.arg("--ignore-system-fonts");
291+
}
292+
251293
// Pass input and output file paths
252294
command.arg(&typ_file_path).arg(output_file.path());
253295

0 commit comments

Comments
 (0)