| 1 | //! Hide a system Noto Color Emoji face before GPUI loads fonts. |
| 2 | //! |
| 3 | //! Cosmic Text on Linux falls back to the family "Noto Color Emoji", and |
| 4 | //! fontdb returns the first face of that name. A COLRv1-only face draws |
| 5 | //! blank. The app bundles a CBDT build of the same name and loads it with |
| 6 | //! `add_fonts`. fontdb only honors `<dir>` entries from `FONTCONFIG_FILE`, |
| 7 | //! so this module rewrites those directories and leaves the system color |
| 8 | //! emoji file out. An existing `FONTCONFIG_FILE` is left alone. |
| 9 | //! |
| 10 | //! This `FONTCONFIG_FILE` is for the app process only. Chrome must not |
| 11 | //! inherit the app-generated `fontconfig-emoji` conf (`browser::launch` |
| 12 | //! strips that path on spawn) — the dirs-only file breaks Segoe/Arial |
| 13 | //! fallback on the Microsoft login page. A user-provided |
| 14 | //! `FONTCONFIG_FILE` is left alone for both the app and Chrome. |
| 15 | |
| 16 | use std::collections::HashSet; |
| 17 | use std::fs; |
| 18 | use std::path::{Path, PathBuf}; |
| 19 | |
| 20 | const FONT_EXTENSIONS: &[&str] = &["ttf", "ttc", "otf", "otc"]; |
| 21 | |
| 22 | /// A system color-emoji file that would win the "Noto Color Emoji" lookup. |
| 23 | pub fn is_system_color_emoji_file(name: &str) -> bool { |
| 24 | let lower = name.to_ascii_lowercase(); |
| 25 | let stem = lower |
| 26 | .rsplit_once('.') |
| 27 | .map(|(stem, _)| stem) |
| 28 | .unwrap_or(lower.as_str()); |
| 29 | stem == "notocoloremoji" |
| 30 | || stem.starts_with("notocoloremoji-") |
| 31 | || (stem.contains("noto") && stem.contains("colrv1")) |
| 32 | } |
| 33 | |
| 34 | /// Point `FONTCONFIG_FILE` at a font list that omits the system color emoji face. |
| 35 | pub fn hide_system_color_emoji() { |
| 36 | if std::env::var_os("FONTCONFIG_FILE").is_some() { |
| 37 | log::info!("FONTCONFIG_FILE is already set, leaving the system emoji font alone"); |
| 38 | return; |
| 39 | } |
| 40 | let Some(cache) = directories::ProjectDirs::from("", "", store::DATA_DIR_NAME) else { |
| 41 | return; |
| 42 | }; |
| 43 | let staging = cache.cache_dir().join("fontconfig-emoji"); |
| 44 | let _ = fs::remove_dir_all(&staging); |
| 45 | if fs::create_dir_all(&staging).is_err() { |
| 46 | return; |
| 47 | } |
| 48 | let dirs: Vec<PathBuf> = font_roots() |
| 49 | .into_iter() |
| 50 | .enumerate() |
| 51 | .filter_map(|(index, root)| publish_root(&root, &staging, index)) |
| 52 | .collect(); |
| 53 | if dirs.is_empty() { |
| 54 | return; |
| 55 | } |
| 56 | let config = staging.join("fonts.conf"); |
| 57 | if write_config(&config, &dirs).is_err() { |
| 58 | return; |
| 59 | } |
| 60 | unsafe { std::env::set_var("FONTCONFIG_FILE", &config) }; |
| 61 | log::info!("emoji fontconfig {}", config.display()); |
| 62 | } |
| 63 | |
| 64 | fn font_roots() -> Vec<PathBuf> { |
| 65 | let mut roots = Vec::new(); |
| 66 | let data_dirs = |
| 67 | std::env::var("XDG_DATA_DIRS").unwrap_or_else(|_| "/usr/local/share:/usr/share".to_owned()); |
| 68 | for dir in data_dirs.split(':').filter(|dir| !dir.is_empty()) { |
| 69 | roots.push(PathBuf::from(dir).join("fonts")); |
| 70 | } |
| 71 | if let Ok(xdg) = std::env::var("XDG_DATA_HOME") { |
| 72 | roots.push(PathBuf::from(xdg).join("fonts")); |
| 73 | } else if let Ok(home) = std::env::var("HOME") { |
| 74 | roots.push(PathBuf::from(home).join(".local/share/fonts")); |
| 75 | } |
| 76 | if let Ok(home) = std::env::var("HOME") { |
| 77 | roots.push(PathBuf::from(home).join(".fonts")); |
| 78 | } |
| 79 | roots.retain(|path| path.is_dir()); |
| 80 | roots.sort(); |
| 81 | roots.dedup(); |
| 82 | roots |
| 83 | } |
| 84 | |
| 85 | /// Use `root` as-is when it has no color emoji file. Otherwise mirror it and skip that file. |
| 86 | pub(crate) fn publish_root(root: &Path, staging: &Path, index: usize) -> Option<PathBuf> { |
| 87 | if !contains_emoji(root) { |
| 88 | return Some(root.to_path_buf()); |
| 89 | } |
| 90 | let dest = staging.join(format!("root-{index}")); |
| 91 | let mut seen = HashSet::new(); |
| 92 | mirror(root, &dest, &mut seen).then_some(dest) |
| 93 | } |
| 94 | |
| 95 | fn contains_emoji(dir: &Path) -> bool { |
| 96 | fn walk(dir: &Path, seen: &mut HashSet<PathBuf>) -> bool { |
| 97 | let canon = fs::canonicalize(dir).unwrap_or_else(|_| dir.to_path_buf()); |
| 98 | if !seen.insert(canon) { |
| 99 | return false; |
| 100 | } |
| 101 | let Ok(entries) = fs::read_dir(dir) else { |
| 102 | return false; |
| 103 | }; |
| 104 | for entry in entries.flatten() { |
| 105 | let path = entry.path(); |
| 106 | let Ok(meta) = fs::metadata(&path) else { |
| 107 | continue; |
| 108 | }; |
| 109 | if meta.is_dir() { |
| 110 | if walk(&path, seen) { |
| 111 | return true; |
| 112 | } |
| 113 | } else if is_system_color_emoji_file(&entry.file_name().to_string_lossy()) { |
| 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | false |
| 118 | } |
| 119 | walk(dir, &mut HashSet::new()) |
| 120 | } |
| 121 | |
| 122 | fn mirror(src: &Path, dest: &Path, seen: &mut HashSet<PathBuf>) -> bool { |
| 123 | let canon = fs::canonicalize(src).unwrap_or_else(|_| src.to_path_buf()); |
| 124 | if !seen.insert(canon) { |
| 125 | return false; |
| 126 | } |
| 127 | let Ok(entries) = fs::read_dir(src) else { |
| 128 | return false; |
| 129 | }; |
| 130 | let mut wrote = false; |
| 131 | for entry in entries.flatten() { |
| 132 | let path = entry.path(); |
| 133 | let name = entry.file_name(); |
| 134 | let Ok(meta) = fs::metadata(&path) else { |
| 135 | continue; |
| 136 | }; |
| 137 | if meta.is_dir() { |
| 138 | if contains_emoji(&path) { |
| 139 | let child = dest.join(&name); |
| 140 | let _ = fs::create_dir_all(&child); |
| 141 | wrote |= mirror(&path, &child, seen); |
| 142 | } else { |
| 143 | let _ = fs::create_dir_all(dest); |
| 144 | wrote |= symlink_to(&path, &dest.join(&name)).is_ok(); |
| 145 | } |
| 146 | } else if is_font_file(&name.to_string_lossy()) |
| 147 | && !is_system_color_emoji_file(&name.to_string_lossy()) |
| 148 | { |
| 149 | let _ = fs::create_dir_all(dest); |
| 150 | wrote |= symlink_to(&path, &dest.join(&name)).is_ok(); |
| 151 | } |
| 152 | } |
| 153 | wrote |
| 154 | } |
| 155 | |
| 156 | fn is_font_file(name: &str) -> bool { |
| 157 | name.rsplit_once('.').is_some_and(|(_, extension)| { |
| 158 | FONT_EXTENSIONS |
| 159 | .iter() |
| 160 | .any(|known| extension.eq_ignore_ascii_case(known)) |
| 161 | }) |
| 162 | } |
| 163 | |
| 164 | fn symlink_to(target: &Path, link: &Path) -> std::io::Result<()> { |
| 165 | let target = fs::canonicalize(target).unwrap_or_else(|_| target.to_path_buf()); |
| 166 | std::os::unix::fs::symlink(target, link) |
| 167 | } |
| 168 | |
| 169 | fn write_config(path: &Path, dirs: &[PathBuf]) -> std::io::Result<()> { |
| 170 | let mut body = String::from("<?xml version=\"1.0\"?>\n<fontconfig>\n"); |
| 171 | for dir in dirs { |
| 172 | body.push_str(" <dir>"); |
| 173 | body.push_str(&xml_escape(&dir.to_string_lossy())); |
| 174 | body.push_str("</dir>\n"); |
| 175 | } |
| 176 | body.push_str("</fontconfig>\n"); |
| 177 | fs::write(path, body) |
| 178 | } |
| 179 | |
| 180 | fn xml_escape(text: &str) -> String { |
| 181 | text.replace('&', "&") |
| 182 | .replace('<', "<") |
| 183 | .replace('>', ">") |
| 184 | } |
| 185 | |
| 186 | #[cfg(test)] |
| 187 | mod tests { |
| 188 | use super::*; |
| 189 | |
| 190 | #[test] |
| 191 | fn color_emoji_file_names() { |
| 192 | assert!(is_system_color_emoji_file("NotoColorEmoji.ttf")); |
| 193 | assert!(is_system_color_emoji_file("Noto-COLRv1.ttf")); |
| 194 | assert!(is_system_color_emoji_file("NotoColorEmoji-emojicompat.ttf")); |
| 195 | assert!(!is_system_color_emoji_file("NotoSans-Regular.ttf")); |
| 196 | assert!(!is_system_color_emoji_file("NotoEmoji-Regular.ttf")); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn publish_root_drops_the_color_emoji_file_and_keeps_the_rest() { |
| 201 | let temp = tempfile::tempdir().unwrap(); |
| 202 | let root = temp.path().join("fonts"); |
| 203 | let noto = root.join("noto"); |
| 204 | let other = root.join("liberation"); |
| 205 | fs::create_dir_all(¬o).unwrap(); |
| 206 | fs::create_dir_all(&other).unwrap(); |
| 207 | fs::write(noto.join("NotoColorEmoji.ttf"), b"emoji").unwrap(); |
| 208 | fs::write(noto.join("NotoSans-Regular.ttf"), b"sans").unwrap(); |
| 209 | fs::write(other.join("LiberationSans.ttf"), b"serif").unwrap(); |
| 210 | let staging = temp.path().join("staging"); |
| 211 | fs::create_dir_all(&staging).unwrap(); |
| 212 | let published = publish_root(&root, &staging, 0).unwrap(); |
| 213 | assert!(published.starts_with(&staging)); |
| 214 | assert!(!published.join("noto/NotoColorEmoji.ttf").exists()); |
| 215 | assert_eq!( |
| 216 | fs::read(published.join("noto/NotoSans-Regular.ttf")).unwrap(), |
| 217 | b"sans" |
| 218 | ); |
| 219 | assert_eq!( |
| 220 | fs::read(published.join("liberation/LiberationSans.ttf")).unwrap(), |
| 221 | b"serif" |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn publish_root_keeps_a_tree_without_a_color_emoji_file() { |
| 227 | let temp = tempfile::tempdir().unwrap(); |
| 228 | let root = temp.path().join("fonts"); |
| 229 | fs::create_dir_all(&root).unwrap(); |
| 230 | fs::write(root.join("DejaVuSans.ttf"), b"dejavu").unwrap(); |
| 231 | let published = publish_root(&root, &temp.path().join("staging"), 0).unwrap(); |
| 232 | assert_eq!(published, root); |
| 233 | } |
| 234 | } |