-
Notifications
You must be signed in to change notification settings - Fork 10
add font loader #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CrazyHPi
wants to merge
6
commits into
OrnitheMC:main
Choose a base branch
from
CrazyHPi:font-loader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+224
−2
Open
add font loader #16
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
61be919
add font loader to load system fonts
CrazyHPi ed7684a
suppress unused warning
CrazyHPi 52bde3e
add chinese and japanese fonts from # 14 and #15
CrazyHPi c99ee0d
add feature gate to mod font_loader
CrazyHPi bf9e104
proper err handle
CrazyHPi 513a99d
fix build warnings
CrazyHPi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| { | ||
| "windows": { | ||
| "zh-CN": [ | ||
| "msyh.ttc", | ||
| "msyhbd.ttc", | ||
| "simsun.ttc", | ||
| "simhei.ttf", | ||
| "simkai.ttf", | ||
| "simfang.ttf", | ||
| "msjh.ttc", | ||
| "msjhbd.ttc" | ||
| ], | ||
| "ja": [ | ||
| "meiryo.ttc", | ||
| "meiryob.ttc", | ||
| "msgothic.ttc", | ||
| "msmincho.ttc", | ||
| "YuGothM.ttc", | ||
| "YuGothB.ttc", | ||
| "YuMincho.ttc" | ||
| ] | ||
| }, | ||
| "linux": { | ||
| "zh-CN": [ | ||
| "Noto Sans CJK SC", | ||
| "WenQuanYi Micro Hei", | ||
| "WenQuanYi Zen Hei", | ||
| "AR PL UMing CN", | ||
| "AR PL UKai CN" | ||
| ], | ||
| "ja": [ | ||
| "Noto Sans CJK JP", | ||
| "Noto Sans CJK SC", | ||
| "Noto Sans CJK TC", | ||
| "Noto Sans JP", | ||
| "IPAGothic", | ||
| "IPA Gothic", | ||
| "VL Gothic" | ||
| ] | ||
| }, | ||
| "macos": { | ||
| "zh-CN": [ | ||
| "PingFang.ttc", | ||
| "STHeiti Light.ttc", | ||
| "STHeiti Medium.ttc", | ||
| "Hiragino Sans GB.ttc", | ||
| "Arial Unicode.ttf" | ||
| ], | ||
| "ja": [ | ||
| "ヒラギノ角ゴシック W3.ttc", | ||
| "ヒラギノ明朝 ProN.ttc", | ||
| "ヒラギノ丸ゴ ProN W4.ttc", | ||
| "Hiragino Sans GB.ttc" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| use egui::FontData; | ||
| use egui::FontFamily::{Monospace, Proportional}; | ||
| use egui::epaint::text::FontPriority::Lowest; | ||
| use egui::epaint::text::{FontInsert, InsertFontFamily}; | ||
| use serde::Deserialize; | ||
| use std::collections::HashMap; | ||
| use log::warn; | ||
|
|
||
| const FONT_LIST: &str = include_str!("../../res/font/fonts.json"); | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| const WINDOWS_FONT_PATH: &str = r"C:\Windows\Fonts\"; | ||
| #[cfg(target_os = "macos")] | ||
| const MACOS_FONT_PATH: &str = "/System/Library/Fonts/"; | ||
| #[cfg(target_os = "macos")] | ||
| const MACOS_FONT_PATH_SHARED: &str = "/Library/Fonts/"; | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct SystemFontList { | ||
| #[cfg(target_os = "windows")] | ||
| windows: PlatformFonts, | ||
| #[cfg(target_os = "linux")] | ||
| linux: PlatformFonts, | ||
| #[cfg(target_os = "macos")] | ||
| macos: PlatformFonts, | ||
| } | ||
|
|
||
| type PlatformFonts = HashMap<String, Vec<String>>; | ||
|
|
||
| pub fn load_system_font_to_egui(ctx: &egui::Context) { | ||
| let system_font = find_system_font(); | ||
|
|
||
| if system_font.is_empty() { | ||
| warn!("No system font found, some languages may not display properly."); | ||
| return; | ||
| } | ||
|
|
||
| for font in system_font { | ||
| let font_insert = FontInsert::new( | ||
| &font.0, | ||
| font.1, | ||
| vec![ | ||
| InsertFontFamily { | ||
| family: Proportional, | ||
| priority: Lowest, // low priority to not override existing fonts | ||
| }, | ||
| InsertFontFamily { | ||
| family: Monospace, | ||
| priority: Lowest, | ||
| }, | ||
| ], | ||
| ); | ||
|
|
||
| ctx.add_font(font_insert); | ||
| } | ||
| } | ||
|
|
||
| fn find_system_font() -> HashMap<String, FontData> { | ||
| let sys_font_list: SystemFontList = | ||
| serde_json::from_str(FONT_LIST).expect("failed to parse font list"); | ||
|
|
||
| let mut result: HashMap<String, FontData> = HashMap::new(); | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| { | ||
| load_fonts_from_paths(&sys_font_list.windows, &[WINDOWS_FONT_PATH], &mut result); | ||
| } | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| { | ||
| load_fonts_from_paths( | ||
| &sys_font_list.macos, | ||
| &[MACOS_FONT_PATH, MACOS_FONT_PATH_SHARED], | ||
| &mut result, | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| { | ||
| // use fontconfig for linux fo find fonts | ||
| load_fonts_from_fontconfig(&sys_font_list.linux, &mut result); | ||
| } | ||
|
|
||
| result | ||
| } | ||
|
|
||
| #[cfg(any(target_os = "windows", target_os = "macos"))] | ||
| fn load_fonts_from_paths( | ||
| platform_fonts: &PlatformFonts, | ||
| search_paths: &[&str], | ||
| result: &mut HashMap<String, FontData>, | ||
| ) { | ||
| for (language, font_files) in platform_fonts { | ||
| let mut loaded = false; | ||
| for font_file in font_files { | ||
| for search_path in search_paths { | ||
| let font_path = format!("{}{}", search_path, font_file); | ||
| if let Ok(font_data) = std::fs::read(&font_path) { | ||
| result.insert(language.to_string(), FontData::from_owned(font_data)); | ||
| loaded = true; | ||
| break; | ||
| } | ||
| } | ||
| if loaded { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(target_os = "linux", feature = "gui"))] | ||
| fn load_fonts_from_fontconfig( | ||
| platform_fonts: &PlatformFonts, | ||
| result: &mut HashMap<String, FontData>, | ||
| ) { | ||
| use fontconfig::Fontconfig; | ||
|
|
||
| if let Some(fc) = Fontconfig::new() { | ||
| platform_fonts.iter().for_each(|(language, font_names)| { | ||
| for font_name in font_names { | ||
| if let Some(font) = fc.find(font_name, None) { | ||
| if let Ok(data) = std::fs::read(font.path) { | ||
| result.insert(language.to_string(), FontData::from_owned(data)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } else { | ||
| warn!("Failed to init Fontconfig") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.