|
| 1 | +use egui::FontData; |
| 2 | +use egui::FontFamily::{Monospace, Proportional}; |
| 3 | +use egui::epaint::text::FontPriority::Lowest; |
| 4 | +use egui::epaint::text::{FontInsert, InsertFontFamily}; |
| 5 | +use serde::Deserialize; |
| 6 | +use std::collections::HashMap; |
| 7 | +use log::warn; |
| 8 | + |
| 9 | +const FONT_LIST: &str = include_str!("../../res/font/fonts.json"); |
| 10 | + |
| 11 | +#[cfg(target_os = "windows")] |
| 12 | +const WINDOWS_FONT_PATH: &str = r"C:\Windows\Fonts\"; |
| 13 | +#[cfg(target_os = "macos")] |
| 14 | +const MACOS_FONT_PATH: &str = "/System/Library/Fonts/"; |
| 15 | +#[cfg(target_os = "macos")] |
| 16 | +const MACOS_FONT_PATH_SHARED: &str = "/Library/Fonts/"; |
| 17 | + |
| 18 | +#[derive(Deserialize)] |
| 19 | +struct SystemFontList { |
| 20 | + #[cfg(target_os = "windows")] |
| 21 | + windows: PlatformFonts, |
| 22 | + #[cfg(target_os = "linux")] |
| 23 | + linux: PlatformFonts, |
| 24 | + #[cfg(target_os = "macos")] |
| 25 | + macos: PlatformFonts, |
| 26 | +} |
| 27 | + |
| 28 | +type PlatformFonts = HashMap<String, Vec<String>>; |
| 29 | + |
| 30 | +pub fn load_system_font_to_egui(ctx: &egui::Context) { |
| 31 | + let system_font = find_system_font(); |
| 32 | + |
| 33 | + if system_font.is_empty() { |
| 34 | + warn!("No system font found, some languages may not display properly."); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + for font in system_font { |
| 39 | + let font_insert = FontInsert::new( |
| 40 | + &font.0, |
| 41 | + font.1, |
| 42 | + vec![ |
| 43 | + InsertFontFamily { |
| 44 | + family: Proportional, |
| 45 | + priority: Lowest, // low priority to not override existing fonts |
| 46 | + }, |
| 47 | + InsertFontFamily { |
| 48 | + family: Monospace, |
| 49 | + priority: Lowest, |
| 50 | + }, |
| 51 | + ], |
| 52 | + ); |
| 53 | + |
| 54 | + ctx.add_font(font_insert); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn find_system_font() -> HashMap<String, FontData> { |
| 59 | + let sys_font_list: SystemFontList = |
| 60 | + serde_json::from_str(FONT_LIST).expect("failed to parse font list"); |
| 61 | + |
| 62 | + let mut result: HashMap<String, FontData> = HashMap::new(); |
| 63 | + |
| 64 | + #[cfg(target_os = "windows")] |
| 65 | + { |
| 66 | + load_fonts_from_paths(&sys_font_list.windows, &[WINDOWS_FONT_PATH], &mut result); |
| 67 | + } |
| 68 | + |
| 69 | + #[cfg(target_os = "macos")] |
| 70 | + { |
| 71 | + load_fonts_from_paths( |
| 72 | + &sys_font_list.macos, |
| 73 | + &[MACOS_FONT_PATH, MACOS_FONT_PATH_SHARED], |
| 74 | + &mut result, |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + #[cfg(target_os = "linux")] |
| 79 | + { |
| 80 | + // use fontconfig for linux fo find fonts |
| 81 | + load_fonts_from_fontconfig(&sys_font_list.linux, &mut result); |
| 82 | + } |
| 83 | + |
| 84 | + result |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(any(target_os = "windows", target_os = "macos"))] |
| 88 | +fn load_fonts_from_paths( |
| 89 | + platform_fonts: &PlatformFonts, |
| 90 | + search_paths: &[&str], |
| 91 | + result: &mut HashMap<String, FontData>, |
| 92 | +) { |
| 93 | + for (language, font_files) in platform_fonts { |
| 94 | + let mut loaded = false; |
| 95 | + for font_file in font_files { |
| 96 | + for search_path in search_paths { |
| 97 | + let font_path = format!("{}{}", search_path, font_file); |
| 98 | + if let Ok(font_data) = std::fs::read(&font_path) { |
| 99 | + result.insert(language.to_string(), FontData::from_owned(font_data)); |
| 100 | + loaded = true; |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + if loaded { |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(all(target_os = "linux", feature = "gui"))] |
| 112 | +fn load_fonts_from_fontconfig( |
| 113 | + platform_fonts: &PlatformFonts, |
| 114 | + result: &mut HashMap<String, FontData>, |
| 115 | +) { |
| 116 | + use fontconfig::Fontconfig; |
| 117 | + |
| 118 | + if let Some(fc) = Fontconfig::new() { |
| 119 | + platform_fonts.iter().for_each(|(language, font_names)| { |
| 120 | + for font_name in font_names { |
| 121 | + if let Some(font) = fc.find(font_name, None) { |
| 122 | + if let Ok(data) = std::fs::read(font.path) { |
| 123 | + result.insert(language.to_string(), FontData::from_owned(data)); |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + }) |
| 129 | + } else { |
| 130 | + warn!("Failed to init Fontconfig") |
| 131 | + } |
| 132 | +} |
0 commit comments