Skip to content

Commit 86741e1

Browse files
committed
add macOS custom fonts as a fallback
1 parent 20d1250 commit 86741e1

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

asm_server/src/ui/font.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ impl FontFallbacks {
1616
#[cfg(target_os = "macos")]
1717
pub const MONO: &'static str = "Menlo";
1818

19+
#[cfg(not(target_os = "macos"))]
20+
pub const FALLBACKS: &'static [&'static str] = &[
21+
// some special characters.
22+
"Segoe UI Emoji", "Segoe UI Symbol", "Segoe UI Historic",
23+
];
24+
25+
#[cfg(target_os = "macos")]
26+
pub const FALLBACKS: &'static [&'static str] = &[
27+
// some special characters.
28+
"Apple Color Emoji",
29+
];
30+
1931
#[cfg(not(target_os = "macos"))]
2032
pub const NORMAL: &'static [&'static str] = &[
2133
"Segoe UI",
2234
// CJK
2335
"Microsoft YaHei UI", "Microsoft JhengHei UI", "Yu Gothic UI", "Malgun Gothic",
24-
// some special characters.
25-
"Segoe UI Emoji", "Segoe UI Symbol", "Segoe UI Historic",
2636
];
2737

2838
#[cfg(target_os = "macos")]
@@ -62,6 +72,7 @@ impl FontFallbacks {
6272
}
6373

6474
pub fn load_all(&self, db: &fontdb::Database) -> Vec<FontData> {
75+
// load normal
6576
let mut fonts: Vec<FontData> = FontFallbacks::NORMAL.iter().filter_map(|name| {
6677
match self.load_font(db, name) {
6778
None => {
@@ -71,11 +82,20 @@ impl FontFallbacks {
7182
Some(data) => Some(data)
7283
}
7384
}).collect();
85+
// load customized
7486
if cfg!(target_os = "macos") {
75-
if let Some(emoji) = self.load_macos_emoji() {
87+
if let Some(emoji) = load_macos_emoji() {
7688
fonts.push(emoji);
7789
}
7890
}
91+
// load fallback
92+
for name in FontFallbacks::FALLBACKS {
93+
if let Some(font) = self.load_font(db, name) {
94+
fonts.push(font);
95+
} else {
96+
warn!("Failed to find system font family as a fallback: {}", name);
97+
}
98+
}
7999
fonts
80100
}
81101

@@ -85,9 +105,8 @@ impl FontFallbacks {
85105
let font_data = db.with_face_data(*id, |font_data, _| font_data.to_vec())?;
86106
Some((name, font_data))
87107
}
88-
89-
pub fn load_macos_emoji(&self) -> Option<FontData> {
90-
let bytes = include_bytes!("../fonts/NotoEmoji-Regular.ttf");
91-
Some(("NotoEmoji", bytes.to_vec()))
92-
}
108+
}
109+
fn load_macos_emoji() -> Option<FontData> {
110+
let bytes = include_bytes!("../fonts/NotoEmoji-Regular.ttf");
111+
Some(("NotoEmoji", bytes.to_vec()))
93112
}

0 commit comments

Comments
 (0)