1 | // font-kit/src/sources/fs.rs |
2 | // |
3 | // Copyright © 2018 The Pathfinder Project Developers. |
4 | // |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
8 | // option. This file may not be copied, modified, or distributed |
9 | // except according to those terms. |
10 | |
11 | //! A source that loads fonts from a directory or directories on disk. |
12 | //! |
13 | //! This source uses the WalkDir abstraction from the `walkdir` crate to locate fonts. |
14 | //! |
15 | //! This is the native source on Android. |
16 | |
17 | use std::any::Any; |
18 | use std::fs::File; |
19 | use std::path::PathBuf; |
20 | use walkdir::WalkDir; |
21 | |
22 | #[cfg (not(any(target_os = "android" , target_family = "windows" )))] |
23 | use dirs_next; |
24 | #[cfg (target_family = "windows" )] |
25 | use std::ffi::OsString; |
26 | #[cfg (target_family = "windows" )] |
27 | use std::os::windows::ffi::OsStringExt; |
28 | #[cfg (target_family = "windows" )] |
29 | use winapi::shared::minwindef::{MAX_PATH, UINT}; |
30 | #[cfg (target_family = "windows" )] |
31 | use winapi::um::sysinfoapi; |
32 | |
33 | use crate::error::SelectionError; |
34 | use crate::family_handle::FamilyHandle; |
35 | use crate::family_name::FamilyName; |
36 | use crate::file_type::FileType; |
37 | use crate::font::Font; |
38 | use crate::handle::Handle; |
39 | use crate::properties::Properties; |
40 | use crate::source::Source; |
41 | use crate::sources::mem::MemSource; |
42 | |
43 | /// A source that loads fonts from a directory or directories on disk. |
44 | /// |
45 | /// This source uses the WalkDir abstraction from the `walkdir` crate to locate fonts. |
46 | /// |
47 | /// This is the native source on Android. |
48 | #[allow (missing_debug_implementations)] |
49 | pub struct FsSource { |
50 | mem_source: MemSource, |
51 | } |
52 | |
53 | impl FsSource { |
54 | /// Opens the default set of directories on this platform and indexes the fonts found within. |
55 | /// |
56 | /// Do not rely on this function for systems other than Android. It makes a best effort to |
57 | /// locate fonts in the typical platform directories, but it is too simple to pick up fonts |
58 | /// that are stored in unusual locations but nevertheless properly installed. |
59 | pub fn new() -> FsSource { |
60 | let mut fonts = vec![]; |
61 | for font_directory in default_font_directories() { |
62 | for directory_entry in WalkDir::new(font_directory).into_iter() { |
63 | let directory_entry = match directory_entry { |
64 | Ok(directory_entry) => directory_entry, |
65 | Err(_) => continue, |
66 | }; |
67 | let path = directory_entry.path(); |
68 | let mut file = match File::open(path) { |
69 | Err(_) => continue, |
70 | Ok(file) => file, |
71 | }; |
72 | match Font::analyze_file(&mut file) { |
73 | Err(_) => continue, |
74 | Ok(FileType::Single) => fonts.push(Handle::from_path(path.to_owned(), 0)), |
75 | Ok(FileType::Collection(font_count)) => { |
76 | for font_index in 0..font_count { |
77 | fonts.push(Handle::from_path(path.to_owned(), font_index)) |
78 | } |
79 | } |
80 | } |
81 | } |
82 | } |
83 | |
84 | FsSource { |
85 | mem_source: MemSource::from_fonts(fonts.into_iter()).unwrap(), |
86 | } |
87 | } |
88 | |
89 | /// Returns paths of all fonts installed on the system. |
90 | pub fn all_fonts(&self) -> Result<Vec<Handle>, SelectionError> { |
91 | self.mem_source.all_fonts() |
92 | } |
93 | |
94 | /// Returns the names of all families installed on the system. |
95 | pub fn all_families(&self) -> Result<Vec<String>, SelectionError> { |
96 | self.mem_source.all_families() |
97 | } |
98 | |
99 | /// Looks up a font family by name and returns the handles of all the fonts in that family. |
100 | pub fn select_family_by_name(&self, family_name: &str) -> Result<FamilyHandle, SelectionError> { |
101 | self.mem_source.select_family_by_name(family_name) |
102 | } |
103 | |
104 | /// Selects a font by PostScript name, which should be a unique identifier. |
105 | /// |
106 | /// This implementation does a brute-force search of installed fonts to find the one that |
107 | /// matches. |
108 | pub fn select_by_postscript_name( |
109 | &self, |
110 | postscript_name: &str, |
111 | ) -> Result<Handle, SelectionError> { |
112 | self.mem_source.select_by_postscript_name(postscript_name) |
113 | } |
114 | |
115 | /// Performs font matching according to the CSS Fonts Level 3 specification and returns the |
116 | /// handle. |
117 | #[inline ] |
118 | pub fn select_best_match( |
119 | &self, |
120 | family_names: &[FamilyName], |
121 | properties: &Properties, |
122 | ) -> Result<Handle, SelectionError> { |
123 | <Self as Source>::select_best_match(self, family_names, properties) |
124 | } |
125 | } |
126 | |
127 | impl Source for FsSource { |
128 | #[inline ] |
129 | fn all_fonts(&self) -> Result<Vec<Handle>, SelectionError> { |
130 | self.all_fonts() |
131 | } |
132 | |
133 | #[inline ] |
134 | fn all_families(&self) -> Result<Vec<String>, SelectionError> { |
135 | self.all_families() |
136 | } |
137 | |
138 | fn select_family_by_name(&self, family_name: &str) -> Result<FamilyHandle, SelectionError> { |
139 | self.select_family_by_name(family_name) |
140 | } |
141 | |
142 | fn select_by_postscript_name(&self, postscript_name: &str) -> Result<Handle, SelectionError> { |
143 | self.select_by_postscript_name(postscript_name) |
144 | } |
145 | |
146 | #[inline ] |
147 | fn as_any(&self) -> &dyn Any { |
148 | self |
149 | } |
150 | |
151 | #[inline ] |
152 | fn as_mut_any(&mut self) -> &mut dyn Any { |
153 | self |
154 | } |
155 | } |
156 | |
157 | #[cfg (target_os = "android" )] |
158 | fn default_font_directories() -> Vec<PathBuf> { |
159 | vec![PathBuf::from("/system/fonts" )] |
160 | } |
161 | |
162 | #[cfg (target_family = "windows" )] |
163 | fn default_font_directories() -> Vec<PathBuf> { |
164 | unsafe { |
165 | let mut buffer = vec![0; MAX_PATH]; |
166 | let len = sysinfoapi::GetWindowsDirectoryW(buffer.as_mut_ptr(), buffer.len() as UINT); |
167 | assert!(len != 0); |
168 | buffer.truncate(len as usize); |
169 | |
170 | let mut path = PathBuf::from(OsString::from_wide(&buffer)); |
171 | path.push("Fonts" ); |
172 | vec![path] |
173 | } |
174 | } |
175 | |
176 | #[cfg (target_os = "macos" )] |
177 | fn default_font_directories() -> Vec<PathBuf> { |
178 | let mut directories = vec![ |
179 | PathBuf::from("/System/Library/Fonts" ), |
180 | PathBuf::from("/Library/Fonts" ), |
181 | PathBuf::from("/Network/Library/Fonts" ), |
182 | ]; |
183 | if let Some(mut path) = dirs_next::home_dir() { |
184 | path.push("Library" ); |
185 | path.push("Fonts" ); |
186 | directories.push(path); |
187 | } |
188 | directories |
189 | } |
190 | |
191 | #[cfg (not(any(target_os = "android" , target_family = "windows" , target_os = "macos" )))] |
192 | fn default_font_directories() -> Vec<PathBuf> { |
193 | let mut directories: Vec = vec![ |
194 | PathBuf::from("/usr/share/fonts" ), |
195 | PathBuf::from("/usr/local/share/fonts" ), |
196 | PathBuf::from("/var/run/host/usr/share/fonts" ), // Flatpak specific |
197 | PathBuf::from("/var/run/host/usr/local/share/fonts" ), |
198 | ]; |
199 | if let Some(path: PathBuf) = dirs_next::home_dir() { |
200 | directories.push(path.join(path:".fonts" )); // ~/.fonts is deprecated |
201 | directories.push(path.join("local" ).join("share" ).join(path:"fonts" )); // Flatpak specific |
202 | } |
203 | if let Some(mut path: PathBuf) = dirs_next::data_dir() { |
204 | path.push(path:"fonts" ); |
205 | directories.push(path); |
206 | } |
207 | directories |
208 | } |
209 | |