@@ -74,6 +74,7 @@ impl<'a> OgImageAuthorData<'a> {
7474/// generating PNG images from a Typst template.
7575pub struct OgImageGenerator {
7676 typst_binary_path : PathBuf ,
77+ typst_font_path : Option < PathBuf > ,
7778}
7879
7980impl 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,41 @@ 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+ generator = generator. with_font_path ( PathBuf :: from ( font_path) ) ;
112123 }
124+
125+ Ok ( generator)
126+ }
127+
128+ /// Sets the font path for the Typst compiler.
129+ ///
130+ /// This allows specifying a custom directory where Typst will look for fonts
131+ /// during compilation. Setting a custom font directory implies using the
132+ /// `--ignore-system-fonts` flag of the Typst CLI. If not set, Typst will
133+ /// use its default font discovery.
134+ ///
135+ /// # Examples
136+ ///
137+ /// ```
138+ /// use std::path::PathBuf;
139+ /// use crates_io_og_image::OgImageGenerator;
140+ ///
141+ /// let generator = OgImageGenerator::default()
142+ /// .with_font_path(PathBuf::from("/usr/share/fonts"));
143+ /// ```
144+ pub fn with_font_path ( mut self , font_path : PathBuf ) -> Self {
145+ self . typst_font_path = Some ( font_path) ;
146+ self
113147 }
114148
115149 /// Processes avatars by downloading URLs and copying assets to the assets directory.
@@ -239,20 +273,30 @@ impl OgImageGenerator {
239273 let json_avatar_map = json_avatar_map. map_err ( OgImageError :: JsonSerializationError ) ?;
240274
241275 // Run typst compile command with input data
242- let output = Command :: new ( & self . typst_binary_path )
243- . arg ( "compile" )
244- . arg ( "--format" )
245- . arg ( "png" )
246- . arg ( "--input" )
247- . arg ( format ! ( "data={}" , json_data) )
248- . arg ( "--input" )
249- . arg ( format ! ( "avatar_map={}" , json_avatar_map) )
250- . arg ( & typ_file_path)
251- . arg ( output_file. path ( ) )
252- . env_clear ( )
253- . output ( )
254- . await
255- . map_err ( OgImageError :: TypstNotFound ) ?;
276+ let mut command = Command :: new ( & self . typst_binary_path ) ;
277+
278+ command. arg ( "compile" ) . arg ( "--format" ) . arg ( "png" ) ;
279+
280+ // Pass in the data and avatar map as JSON inputs
281+ let input = format ! ( "data={}" , json_data) ;
282+ command. arg ( "--input" ) . arg ( input) ;
283+ let input = format ! ( "avatar_map={}" , json_avatar_map) ;
284+ command. arg ( "--input" ) . arg ( input) ;
285+
286+ // Pass in the font path if specified
287+ if let Some ( font_path) = & self . typst_font_path {
288+ command. arg ( "--font-path" ) . arg ( font_path) ;
289+ command. arg ( "--ignore-system-fonts" ) ;
290+ }
291+
292+ // Pass input and output file paths
293+ command. arg ( & typ_file_path) . arg ( output_file. path ( ) ) ;
294+
295+ // Clear environment variables to avoid leaking sensitive data
296+ command. env_clear ( ) ;
297+
298+ let output = command. output ( ) . await ;
299+ let output = output. map_err ( OgImageError :: TypstNotFound ) ?;
256300
257301 if !output. status . success ( ) {
258302 let stderr = String :: from_utf8_lossy ( & output. stderr ) . to_string ( ) ;
0 commit comments