@@ -74,6 +74,7 @@ impl<'a> OgImageAuthorData<'a> {
74
74
/// generating PNG images from a Typst template.
75
75
pub struct OgImageGenerator {
76
76
typst_binary_path : PathBuf ,
77
+ typst_font_path : Option < PathBuf > ,
77
78
}
78
79
79
80
impl OgImageGenerator {
@@ -88,7 +89,10 @@ impl OgImageGenerator {
88
89
/// let generator = OgImageGenerator::new(PathBuf::from("/usr/local/bin/typst"));
89
90
/// ```
90
91
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
+ }
92
96
}
93
97
94
98
/// Creates a new `OgImageGenerator` using the `TYPST_PATH` environment variable.
@@ -105,11 +109,43 @@ impl OgImageGenerator {
105
109
/// # Ok::<(), crates_io_og_image::OgImageError>(())
106
110
/// ```
107
111
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) )
110
117
} 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) ;
112
125
}
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
113
149
}
114
150
115
151
/// Processes avatars by downloading URLs and copying assets to the assets directory.
@@ -248,6 +284,12 @@ impl OgImageGenerator {
248
284
let input = format ! ( "avatar_map={json_avatar_map}" ) ;
249
285
command. arg ( "--input" ) . arg ( input) ;
250
286
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
+
251
293
// Pass input and output file paths
252
294
command. arg ( & typ_file_path) . arg ( output_file. path ( ) ) ;
253
295
0 commit comments