Skip to main content

retex_kpath/
format.rs

1//! File formats known to kpathsea and their search parameters.
2
3/// A kind of file to look up, mirroring kpathsea's `kpse_file_format_type`.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum FileFormat {
6    /// TeX input files (`.tex`, and anything `\input` can read).
7    Tex,
8    /// Lua modules (`require` from LuaTeX).
9    Lua,
10    /// Format files (`.fmt`).
11    Fmt,
12    /// TeX font metrics (`.tfm`).
13    Tfm,
14    /// OpenType fonts (`.otf`).
15    OpenType,
16    /// TrueType fonts and collections (`.ttf`, `.ttc`).
17    TrueType,
18    /// Type 1 fonts (`.pfb`, `.pfa`).
19    Type1,
20    /// Encoding vectors (`.enc`).
21    Enc,
22    /// Font map files (`.map`).
23    Map,
24    /// Virtual fonts (`.vf`).
25    Vf,
26    /// BibTeX databases (`.bib`).
27    Bib,
28    /// BibTeX styles (`.bst`).
29    Bst,
30    /// Graphics and other files read by the backend (`TEXPICTS`).
31    Pict,
32    /// Files opened with `kpse.find_file(name, "other text files")`.
33    Text,
34}
35
36/// Search parameters for a [`FileFormat`].
37#[derive(Debug, Clone, Copy)]
38pub struct FormatInfo {
39    /// Variables holding the search path, in order of precedence.
40    pub env_vars: &'static [&'static str],
41    /// The path to use when no variable is set.
42    pub default_path: &'static str,
43    /// Standard suffixes, tried before the bare name.
44    pub suffixes: &'static [&'static str],
45}
46
47impl FileFormat {
48    /// The search parameters for this format.
49    pub fn info(self) -> FormatInfo {
50        let (env_vars, suffixes): (&[&str], &[&str]) = match self {
51            FileFormat::Tex => (&["TEXINPUTS"], &[".tex"]),
52            FileFormat::Lua => (
53                &["LUAINPUTS"],
54                &[
55                    ".lua", ".luatex", ".luc", ".luctex", ".texlua", ".texluc", ".tlu",
56                ],
57            ),
58            FileFormat::Fmt => (&["TEXFORMATS"], &[".fmt"]),
59            FileFormat::Tfm => (&["TFMFONTS"], &[".tfm"]),
60            FileFormat::OpenType => (&["OPENTYPEFONTS"], &[".otf", ".OTF"]),
61            FileFormat::TrueType => (&["TTFONTS"], &[".ttf", ".ttc", ".TTF", ".TTC", ".dfont"]),
62            FileFormat::Type1 => (&["T1FONTS", "T1INPUTS"], &[".pfa", ".pfb"]),
63            FileFormat::Enc => (&["ENCFONTS"], &[".enc"]),
64            FileFormat::Map => (&["TEXFONTMAPS"], &[".map"]),
65            FileFormat::Vf => (&["VFFONTS"], &[".vf"]),
66            FileFormat::Bib => (&["BIBINPUTS"], &[".bib"]),
67            FileFormat::Bst => (&["BSTINPUTS"], &[".bst"]),
68            FileFormat::Pict => (&["TEXPICTS", "TEXINPUTS"], &[]),
69            FileFormat::Text => (&["TEXINPUTS"], &[]),
70        };
71        FormatInfo {
72            env_vars,
73            default_path: ".",
74            suffixes,
75        }
76    }
77}