summaryrefslogtreecommitdiff
path: root/src/loading/fs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/loading/fs.rs')
-rw-r--r--src/loading/fs.rs96
1 files changed, 57 insertions, 39 deletions
diff --git a/src/loading/fs.rs b/src/loading/fs.rs
index 8785499e..407e2d94 100644
--- a/src/loading/fs.rs
+++ b/src/loading/fs.rs
@@ -3,6 +3,7 @@ use std::collections::HashMap;
use std::fs::{self, File};
use std::io;
use std::path::{Path, PathBuf};
+use std::rc::Rc;
use memmap2::Mmap;
use same_file::Handle;
@@ -28,8 +29,57 @@ impl FsLoader {
Self { faces: vec![], paths: RefCell::default() }
}
+ /// Builder-style variant of `search_system`.
+ pub fn with_system(mut self) -> Self {
+ self.search_system();
+ self
+ }
+
+ /// Builder-style variant of `search_path`.
+ pub fn with_path(mut self, dir: impl AsRef<Path>) -> Self {
+ self.search_path(dir);
+ self
+ }
+
+ /// Builder-style method to wrap the loader in an [`Rc`] to make it usable
+ /// with the [`Context`](crate::Context).
+ pub fn wrap(self) -> Rc<Self> {
+ Rc::new(self)
+ }
+
+ /// Search for fonts in the operating system's font directories.
+ pub fn search_system(&mut self) {
+ self.search_system_impl();
+ }
+
+ /// Search for all fonts at a path.
+ ///
+ /// If the path is a directory, all contained fonts will be searched for
+ /// recursively.
+ pub fn search_path(&mut self, dir: impl AsRef<Path>) {
+ let walk = WalkDir::new(dir)
+ .follow_links(true)
+ .sort_by(|a, b| a.file_name().cmp(b.file_name()))
+ .into_iter()
+ .filter_map(|e| e.ok());
+
+ for entry in walk {
+ let path = entry.path();
+ if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
+ match ext {
+ #[rustfmt::skip]
+ "ttf" | "otf" | "TTF" | "OTF" |
+ "ttc" | "otc" | "TTC" | "OTC" => {
+ self.search_file(path).ok();
+ }
+ _ => {}
+ }
+ }
+ }
+ }
+
/// Resolve a file id for a path.
- pub fn resolve_path(&self, path: &Path) -> io::Result<FileId> {
+ pub fn resolve(&self, path: &Path) -> io::Result<FileId> {
let file = File::open(path)?;
let meta = file.metadata()?;
if meta.is_file() {
@@ -42,9 +92,8 @@ impl FsLoader {
}
}
- /// Search for fonts in the operating system's font directories.
#[cfg(all(unix, not(target_os = "macos")))]
- pub fn search_system(&mut self) {
+ fn search_system_impl(&mut self) {
self.search_path("/usr/share/fonts");
self.search_path("/usr/local/share/fonts");
@@ -53,9 +102,8 @@ impl FsLoader {
}
}
- /// Search for fonts in the operating system's font directories.
#[cfg(target_os = "macos")]
- pub fn search_system(&mut self) {
+ fn search_system_impl(&mut self) {
self.search_path("/Library/Fonts");
self.search_path("/Network/Library/Fonts");
self.search_path("/System/Library/Fonts");
@@ -65,9 +113,8 @@ impl FsLoader {
}
}
- /// Search for fonts in the operating system's font directories.
#[cfg(windows)]
- pub fn search_system(&mut self) {
+ fn search_system_impl(&mut self) {
let windir =
std::env::var("WINDIR").unwrap_or_else(|_| "C:\\Windows".to_string());
@@ -82,32 +129,6 @@ impl FsLoader {
}
}
- /// Search for all fonts at a path.
- ///
- /// If the path is a directory, all contained fonts will be searched for
- /// recursively.
- pub fn search_path(&mut self, dir: impl AsRef<Path>) {
- let walk = WalkDir::new(dir)
- .follow_links(true)
- .sort_by(|a, b| a.file_name().cmp(b.file_name()))
- .into_iter()
- .filter_map(|e| e.ok());
-
- for entry in walk {
- let path = entry.path();
- if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
- match ext {
- #[rustfmt::skip]
- "ttf" | "otf" | "TTF" | "OTF" |
- "ttc" | "otc" | "TTC" | "OTC" => {
- self.search_file(path).ok();
- }
- _ => {}
- }
- }
- }
- }
-
/// Index the font faces in the file at the given path.
///
/// The file may form a font collection and contain multiple font faces,
@@ -154,7 +175,7 @@ impl FsLoader {
stretch: FontStretch::from_number(face.width().to_number()),
};
- let file = self.resolve_path(path)?;
+ let file = self.resolve(path)?;
self.faces.push(FaceInfo { file, index, family, variant });
Ok(())
@@ -182,11 +203,8 @@ mod tests {
#[test]
fn test_index_font_dir() {
- let mut loader = FsLoader::new();
- loader.search_path("fonts");
-
- let map = loader.paths.borrow();
- let mut paths: Vec<_> = map.values().collect();
+ let map = FsLoader::new().with_path("fonts").paths.into_inner();
+ let mut paths: Vec<_> = map.into_iter().map(|p| p.1).collect();
paths.sort();
assert_eq!(paths, [