Skip to content

VectorCraft: An open-source, vector art tool written by scratch in pure Rust

Fix #1529: Linux reads the font folders fontconfig is set up with

bflatastic3a88ad085577
diff --git a/crates/text/src/fontconfig.rs b/crates/text/src/fontconfig.rs
new file mode 100644
index 000000000..61a69f9e3
--- /dev/null
+++ b/crates/text/src/fontconfig.rs
@@ -0,0 +1,253 @@
+//! The font folders fontconfig is set up with (Linux and the other Unix desktops): the `<dir>`
+//! entries of its configuration, `/etc/fonts/fonts.conf` and what that includes (`conf.d`, the
+//! user's own files). Distributions and packages add folders outside the usual ones there: TeX
+//! fonts under `/usr/share/texmf/fonts`, say, which `fc-list` and other apps then show (#1529).
+//!
+//! The format is fontconfig's documented one
+//! (<https://www.freedesktop.org/software/fontconfig/fontconfig-user.html>, "Configuration file
+//! format"): `<dir prefix="…">` and `<include prefix="…">` are all that is read here. The files are
+//! read as they are: fontconfig itself (a library, or `fc-list`) isn't asked.
+
+use std::collections::HashSet;
+use std::path::{Path, PathBuf};
+
+/// The most configuration files read, and the largest one (they are a few kilobytes each).
+const MAX_FILES: usize = 512;
+const MAX_FILE_BYTES: u64 = 1 << 20;
+/// The most folders taken, and how deep files may include files.
+const MAX_DIRS: usize = 256;
+const MAX_DEPTH: usize = 8;
+
+/// A `<dir>` (a font folder) or an `<include>` (another configuration file, or a folder of them)
+/// with its `prefix` attribute and its text.
+#[derive(Debug, PartialEq)]
+struct Entry {
+ include: bool,
+ prefix: String,
+ path: String,
+}
+
+/// The `<dir>` and `<include>` elements of a configuration file, in order. Anything that isn't
+/// one of them, comments included, is skipped; a file that isn't well formed gives what was read
+/// before the damage.
+fn entries(xml: &str) -> Vec<Entry> {
+ let mut out = vec![];
+ let mut rest = xml;
+ while let Some(at) = rest.find('<') {
+ rest = rest.get(at + 1..).unwrap_or_default();
+ if let Some(comment) = rest.strip_prefix("!--") {
+ rest = comment.split_once("-->").map_or("", |(_, after)| after);
+ continue;
+ }
+ let Some((tag, after)) = rest.split_once('>') else { break };
+ let name = tag.split_whitespace().next().unwrap_or_default();
+ if !matches!(name, "dir" | "include") || tag.ends_with('/') {
+ continue;
+ }
+ let Some((text, after)) = after.split_once("</") else { break };
+ rest = after;
+ // No element is inside these: text with a tag in it isn't a path.
+ if text.contains('<') {
+ continue;
+ }
+ let path = text.trim().replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&apos;", "'");
+ if !path.is_empty() {
+ out.push(Entry { include: name == "include", prefix: attribute(tag, "prefix").unwrap_or_default().to_string(), path });
+ }
+ }
+ out
+}
+
+/// The value of attribute `name` in a start tag's text.
+fn attribute<'a>(tag: &'a str, name: &str) -> Option<&'a str> {
+ let mut rest = tag;
+ while let Some(at) = rest.find(name) {
+ let before_ok = rest.get(..at).is_some_and(|b| b.ends_with(char::is_whitespace));
+ rest = rest.get(at + name.len()..)?;
+ let Some(value) = rest.trim_start().strip_prefix('=') else { continue };
+ let value = value.trim_start();
+ let quote = value.chars().next().filter(|q| matches!(q, '"' | '\''))?;
+ let (value, _) = value.get(1..)?.split_once(quote)?;
+ if before_ok {
+ return Some(value);
+ }
+ }
+ None
+}
+
+/// Where the user's files are: the home folder and the XDG data and configuration folders.
+#[derive(Clone, Debug, Default)]
+pub(crate) struct Places {
+ pub home: Option<PathBuf>,
+ pub data_home: Option<PathBuf>,
+ pub config_home: Option<PathBuf>,
+}
+
+impl Places {
+ /// The path an entry of the file `from` names: `~` is the home folder, `prefix="xdg"` the XDG
+ /// data folder (a `<dir>`) or configuration folder (an `<include>`), `prefix="relative"` the
+ /// file's own folder, as is an `<include>` that isn't a full path. A `<dir>` relative to the
+ /// folder the app was started in is none.
+ fn resolve(&self, e: &Entry, from: &Path) -> Option<PathBuf> {
+ // `~` alone or before a `/`: another user's home (`~name`) isn't looked up.
+ if let Some(rest) = e.path.strip_prefix('~').filter(|rest| rest.is_empty() || rest.starts_with('/')) {
+ return Some(self.home.as_ref()?.join(rest.trim_start_matches('/')));
+ }
+ let path = Path::new(&e.path);
+ let base = match e.prefix.as_str() {
+ "xdg" if e.include => self.config_home.as_deref(),
+ "xdg" => self.data_home.as_deref(),
+ _ if path.is_absolute() => return Some(path.to_path_buf()),
+ "relative" => from.parent(),
+ _ if e.include => from.parent(),
+ _ => None,
+ };
+ Some(base?.join(path)).filter(|p| p.is_absolute())
+ }
+}
+
+/// The font folders the configuration file `root` and the files it includes list, in order, each
+/// once. Files that are missing or can't be read list none.
+pub(crate) fn font_dirs(root: &Path, places: &Places) -> Vec<PathBuf> {
+ let mut read = Reader { places, files: HashSet::new(), dirs: vec![] };
+ read.file(root, 0);
+ read.dirs
+}
+
+struct Reader<'a> {
+ places: &'a Places,
+ /// The files read so far (a file including itself is read once).
+ files: HashSet<PathBuf>,
+ dirs: Vec<PathBuf>,
+}
+
+impl Reader<'_> {
+ fn file(&mut self, path: &Path, depth: usize) {
+ if depth > MAX_DEPTH || self.files.len() >= MAX_FILES || !self.files.insert(path.to_path_buf()) {
+ return;
+ }
+ // A folder of configuration files: those named as fontconfig reads them (a digit first,
+ // `.conf` last), in name order.
+ if let Ok(list) = std::fs::read_dir(path) {
+ let mut files: Vec<PathBuf> = list
+ .flatten()
+ .take(MAX_FILES)
+ .filter(|e| {
+ let name = e.file_name();
+ let name = name.to_string_lossy();
+ name.starts_with(|c: char| c.is_ascii_digit()) && name.ends_with(".conf")
+ })
+ .map(|e| e.path())
+ .collect();
+ files.sort();
+ for f in files {
+ self.file(&f, depth + 1);
+ }
+ return;
+ }
+ // A file that is missing, too large or not text lists nothing.
+ if !std::fs::metadata(path).is_ok_and(|m| m.is_file() && m.len() <= MAX_FILE_BYTES) {
+ return;
+ }
+ let Ok(text) = std::fs::read_to_string(path) else { return };
+ for e in entries(&text) {
+ let Some(to) = self.places.resolve(&e, path) else { continue };
+ if e.include {
+ self.file(&to, depth + 1);
+ } else if self.dirs.len() < MAX_DIRS && !self.dirs.contains(&to) {
+ self.dirs.push(to);
+ }
+ }
+ }
+}
+
+/// The configuration file fontconfig starts from: `FONTCONFIG_FILE`, else `fonts.conf` in
+/// `FONTCONFIG_PATH`, else `/etc/fonts/fonts.conf`.
+pub(crate) fn root() -> PathBuf {
+ let var = |name| std::env::var_os(name).map(PathBuf::from).filter(|p| p.is_absolute());
+ var("FONTCONFIG_FILE").or_else(|| var("FONTCONFIG_PATH").map(|d| d.join("fonts.conf"))).unwrap_or_else(|| "/etc/fonts/fonts.conf".into())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn entry(include: bool, prefix: &str, path: &str) -> Entry {
+ Entry { include, prefix: prefix.into(), path: path.into() }
+ }
+
+ #[test]
+ fn dirs_and_includes_are_read_and_the_rest_is_skipped() {
+ let xml = r#"<?xml version="1.0"?>
+<!DOCTYPE fontconfig SYSTEM "urn:fontconfig:fonts.dtd">
+<fontconfig>
+ <description>Default configuration file</description>
+ <!-- <dir>/commented/out</dir> -->
+ <dir>/usr/share/fonts</dir>
+ <dir prefix="xdg">fonts</dir>
+ <dir prefix = 'relative' salt="x"> more &amp; more </dir>
+ <dir>~/.fonts</dir>
+ <dir></dir>
+ <cachedir>/var/cache/fontconfig</cachedir>
+ <match target="pattern"><test qual="any" name="family"><string>mono</string></test></match>
+ <include ignore_missing="yes">conf.d</include>
+ <include ignore_missing="yes" prefix="xdg">fontconfig/fonts.conf</include>
+ <dir>/cut/off"#;
+ assert_eq!(
+ entries(xml),
+ [
+ entry(false, "", "/usr/share/fonts"),
+ entry(false, "xdg", "fonts"),
+ entry(false, "relative", "more & more"),
+ entry(false, "", "~/.fonts"),
+ entry(true, "", "conf.d"),
+ entry(true, "xdg", "fontconfig/fonts.conf"),
+ ]
+ );
+ // Damaged files and files that aren't XML read as nothing, without a panic.
+ for junk in
+ ["", "<", "<dir", "<dir>", "<dir><dir></dir>", "</dir>", "<!--", "<dir prefix=>x</dir", "\u{0}<dir prefix=\"", "<dir prefix='é>ü</dir>"]
+ {
+ let _ = entries(junk);
+ }
+ assert_eq!(attribute("dir notprefix=\"a\" prefix=\"b\"", "prefix"), Some("b"));
+ }
+
+ /// A configuration as distributions lay it out: the main file lists the usual folders and
+ /// includes `conf.d`, where a package's file adds its own folder (TeX fonts, #1529) and
+ /// another includes the user's files.
+ #[test]
+ fn folders_of_included_files_are_listed_once_in_order() {
+ let tmp = std::env::temp_dir().join(format!("vc-fontconfig-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&tmp);
+ let (etc, home) = (tmp.join("etc/fonts"), tmp.join("home"));
+ let write = |path: PathBuf, body: &str| {
+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
+ std::fs::write(path, format!("<fontconfig>{body}</fontconfig>")).unwrap();
+ };
+ let texmf = tmp.join("usr/share/texmf/fonts/opentype");
+ write(
+ etc.join("fonts.conf"),
+ &format!(
+ "<dir>{}</dir><dir prefix=\"xdg\">fonts</dir><dir>~/.fonts</dir><dir>fonts-of-the-working-folder</dir>\
+ <include ignore_missing=\"yes\">conf.d</include><include>{}</include><include>fonts.conf</include>",
+ tmp.join("usr/share/fonts").display(),
+ etc.join("missing.conf").display()
+ ),
+ );
+ write(etc.join("conf.d/09-texlive.conf"), &format!("<dir>{}</dir>", texmf.display()));
+ write(etc.join("conf.d/50-user.conf"), "<include ignore_missing=\"yes\" prefix=\"xdg\">fontconfig/fonts.conf</include>");
+ write(etc.join("conf.d/README"), "<dir>/not/a/conf/file</dir>");
+ write(etc.join("conf.d/user.conf"), "<dir>/no/digit/first</dir>");
+ write(home.join(".config/fontconfig/fonts.conf"), &format!("<dir prefix=\"relative\">mine</dir><dir>{}</dir>", texmf.display()));
+ let places = Places { home: Some(home.clone()), data_home: Some(home.join(".local/share")), config_home: Some(home.join(".config")) };
+ assert_eq!(
+ font_dirs(&etc.join("fonts.conf"), &places),
+ [tmp.join("usr/share/fonts"), home.join(".local/share/fonts"), home.join(".fonts"), texmf, home.join(".config/fontconfig/mine")]
+ );
+ // Without a home folder, what needs one is left out; a missing file lists nothing.
+ assert_eq!(font_dirs(&etc.join("fonts.conf"), &Places::default()).len(), 2);
+ assert!(font_dirs(&tmp.join("none.conf"), &places).is_empty());
+ let _ = std::fs::remove_dir_all(&tmp);
+ }
+}
diff --git a/crates/text/src/fontdb.rs b/crates/text/src/fontdb.rs
index bd1702597..06599b360 100644
--- a/crates/text/src/fontdb.rs
+++ b/crates/text/src/fontdb.rs
@@ -1269,12 +1269,21 @@ pub fn system_font_dirs() -> Vec<PathBuf> {
// list their own data folders), relative paths ignored as the XDG spec says.
let xdg = |var| std::env::var_os(var).map(|v| std::env::split_paths(&v).filter(|p| p.is_absolute()).collect::<Vec<_>>()).unwrap_or_default();
let data_home = xdg("XDG_DATA_HOME").into_iter().next().or_else(|| home.as_ref().map(|h| h.join(".local/share")));
- dirs.extend(data_home.into_iter().chain(xdg("XDG_DATA_DIRS")).map(|d| d.join("fonts")));
+ dirs.extend(data_home.iter().cloned().chain(xdg("XDG_DATA_DIRS")).map(|d| d.join("fonts")));
if let Some(h) = &home {
dirs.push(h.join(".fonts"));
}
// In a Flatpak sandbox, the host's fonts (system, local and the user's).
dirs.extend(["/run/host/fonts", "/run/host/local-fonts", "/run/host/user-fonts"].map(Into::into));
+ // The folders fontconfig is set up with outside those: packages add their own (TeX fonts
+ // under `/usr/share/texmf/fonts`, #1529), and `fc-list` and other apps show their fonts.
+ let config_home = xdg("XDG_CONFIG_HOME").into_iter().next().or_else(|| home.as_ref().map(|h| h.join(".config")));
+ let places = crate::fontconfig::Places { home: home.clone(), data_home, config_home };
+ for dir in crate::fontconfig::font_dirs(&crate::fontconfig::root(), &places) {
+ if !dirs.iter().any(|known| dir.starts_with(known)) {
+ dirs.push(dir);
+ }
+ }
}
dirs.extend(app_font_dir().map(Path::to_path_buf));
#[cfg(not(target_arch = "wasm32"))]
diff --git a/crates/text/src/lib.rs b/crates/text/src/lib.rs
index 917aa7dd2..300bfb91e 100644
--- a/crates/text/src/lib.rs
+++ b/crates/text/src/lib.rs
@@ -14,6 +14,7 @@ pub mod craft_fonts;
pub mod edit;
pub mod embed;
mod features;
+mod fontconfig;
mod fontdb;
pub mod hyphen;
mod layout;
diff --git a/docs/mcp.md b/docs/mcp.md
index 96296b368..43414bd91 100644
--- a/docs/mcp.md
+++ b/docs/mcp.md
@@ -2977,8 +2977,9 @@ included) the fonts in DirectWrite's system font collection, such as those Adobe
`/Network/Library/Fonts`, `~/Library/Fonts` and downloaded system fonts, plus in the desktop app and `vectorcraft-cli`
the fonts CoreText's font manager lists outside them, such as those apps and font managers register from their own
folders; Linux and BSD: `/usr/share/fonts`,
-`/usr/local/share/fonts`, `~/.fonts` and the XDG data folders' `fonts`, `~/.local/share/fonts` among them, and in a
-Flatpak sandbox the host's fonts). Faces without outlines VectorCraft draws (no `glyf`, `CFF`, `CFF2` or `VARC` table, such
+`/usr/local/share/fonts`, `~/.fonts` and the XDG data folders' `fonts`, `~/.local/share/fonts` among them, in a
+Flatpak sandbox the host's fonts, and the folders fontconfig's configuration lists (`<dir>` in `/etc/fonts/fonts.conf`
+and the files it includes: where packages add theirs, such as TeX fonts under `/usr/share/texmf/fonts`)). Faces without outlines VectorCraft draws (no `glyf`, `CFF`, `CFF2` or `VARC` table, such
as bitmap-only fonts) are left out. The scan also reads the folder the `fontsFolder` preference names (Preferences › Type ›
Additional Fonts Folder) with its subfolders and, in the desktop app and `vectorcraft-cli`, VectorCraft's own `Fonts` folder
next to the preferences: `~/Library/Application Support/VectorCraft/Fonts` on macOS, `%APPDATA%\VectorCraft\Fonts` on