prehnite_core/font/
mod.rs

1#![doc = "アプリ全体のフォントマネージャ"]
2use crate::i18n::get_locale_language;
3use iced::{Application, Daemon, Program};
4use iced_graphics::text::font_system;
5use std::sync::OnceLock;
6use tracing::error;
7
8pub mod fonts;
9pub mod material_symbol;
10pub mod widget;
11
12#[tracing::instrument]
13fn font_list() -> Vec<String> {
14    let mut font_list: Vec<String> = match font_system().write() {
15        Ok(mut v) => v
16            .raw()
17            .db()
18            .faces()
19            .filter_map(|v| v.families.first().map(|v| v.0.clone()))
20            .collect(),
21        Err(e) => {
22            error!("Failed to lock font_system. {e:#?}");
23            Default::default()
24        }
25    };
26    font_list.sort();
27    font_list.dedup();
28    font_list
29}
30
31/// 読み込まれているフォントのリストを取得します。
32///
33/// 最初に呼び出した時点で読み込まれているフォントのみが含まれます。
34pub fn get_global_font_list() -> &'static Vec<String> {
35    static FONT_LIST: OnceLock<Vec<String>> = OnceLock::new();
36    FONT_LIST.get_or_init(|| font_list())
37}
38
39/// デフォルトのフォントファミリ名を取得します。
40pub fn get_default_font_family() -> &'static str {
41    static EN_DEFAULT_FONT: &&str = &fonts::noto_sans::NAME;
42    static JA_DEFAULT_FONT: &&str = &fonts::sawarabi_gothic::NAME;
43    match get_locale_language().as_str() {
44        "ja" => *JA_DEFAULT_FONT,
45        _ => *EN_DEFAULT_FONT,
46    }
47}
48
49/// フォントを読み込ませるために必要な実装。
50pub trait FontLoader {
51    fn load_all_prehnite_bundled_font(self) -> Self;
52}
53
54impl<P: Program> FontLoader for Daemon<P> {
55    fn load_all_prehnite_bundled_font(self) -> Self {
56        self.font(fonts::noto_sans::FONT)
57            .font(fonts::sawarabi_gothic::FONT)
58            .font(fonts::material_symbols_outlined::FONT)
59    }
60}
61
62impl<P: Program> FontLoader for Application<P> {
63    fn load_all_prehnite_bundled_font(self) -> Self {
64        self.font(fonts::noto_sans::FONT)
65            .font(fonts::sawarabi_gothic::FONT)
66            .font(fonts::material_symbols_outlined::FONT)
67    }
68}