pub const FONT_GLYPH_WIDTH: usize = 8;
pub const FONT_GLYPH_HEIGHT: usize = 16;
pub const FONT_8X16: [[u8; 16]; 95] = [
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x40, 0x40, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x50, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0x7f, 0x24, 0x24, 0xff, 0x24, 0x2c, 0x68, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x10, 0x3c, 0x50, 0x50, 0x70, 0x1c, 0x16, 0x16, 0x7c, 0x10, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x51, 0x5a, 0x5f, 0x57, 0x7a, 0x0a, 0x13, 0x11, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x24, 0x24, 0x38, 0x78, 0x4d, 0x47, 0x47, 0x3d, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x40, 0x40, 0xc0, 0xc0, 0xc0, 0x40, 0x40, 0x60,
0x20,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x60, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x60,
0x40,
],
[
0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x7e, 0x38, 0x28, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x7e, 0x10, 0x10, 0x10, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40,
0x40,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x10, 0x10, 0x20, 0x20, 0x60, 0x40, 0x40, 0xc0, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x64, 0x46, 0x46, 0x42, 0x46, 0x46, 0x64, 0x38, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x44, 0x04, 0x04, 0x0c, 0x18, 0x30, 0x60, 0x7e, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x44, 0x04, 0x0c, 0x38, 0x04, 0x06, 0x06, 0x7c, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x1c, 0x14, 0x24, 0x64, 0x44, 0xfe, 0x04, 0x04, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x40, 0x40, 0x7c, 0x04, 0x06, 0x06, 0x04, 0x78, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x20, 0x40, 0x5c, 0x66, 0x42, 0x46, 0x66, 0x3c, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x06, 0x04, 0x0c, 0x08, 0x18, 0x10, 0x30, 0x20, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x64, 0x44, 0x6c, 0x38, 0x44, 0x42, 0x46, 0x3c, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x64, 0x46, 0x46, 0x46, 0x3e, 0x04, 0x0c, 0x78, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40,
0xc0,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0c, 0x30, 0x60, 0x38, 0x0e, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x70, 0x1c, 0x06, 0x18, 0x60, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x08, 0x08, 0x18, 0x30, 0x20, 0x00, 0x20, 0x20, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x30, 0x6f, 0x49, 0x59, 0x59, 0x5b, 0x4e, 0x30, 0x1f,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x1c, 0x24, 0x26, 0x7e, 0x42, 0xc3, 0x81, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x62, 0x62, 0x62, 0x7c, 0x63, 0x63, 0x63, 0x7e, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x30, 0x60, 0x40, 0x40, 0x40, 0x60, 0x30, 0x1e, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x63, 0x61, 0x61, 0x61, 0x61, 0x61, 0x63, 0x7e, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x60, 0x60, 0x60, 0x7c, 0x60, 0x60, 0x60, 0x7e, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x60, 0x60, 0x60, 0x7c, 0x60, 0x60, 0x60, 0x60, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x30, 0x60, 0x40, 0x47, 0x41, 0x61, 0x31, 0x1f, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x61, 0x61, 0x61, 0x7f, 0x61, 0x61, 0x61, 0x61, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x66, 0x6c, 0x78, 0x78, 0x68, 0x64, 0x66, 0x63, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7e, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x70, 0x70, 0x51, 0x59, 0x49, 0x4e, 0x46, 0x46, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x71, 0x71, 0x59, 0x4d, 0x45, 0x47, 0x43, 0x41, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x21, 0x60, 0x40, 0x40, 0x40, 0x60, 0x21, 0x1e, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x62, 0x66, 0x7c, 0x60, 0x60, 0x60, 0x60, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x21, 0x60, 0x40, 0x40, 0x40, 0x60, 0x21, 0x1e, 0x02,
0x01,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x62, 0x66, 0x7c, 0x6c, 0x66, 0x62, 0x63, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x40, 0x40, 0x60, 0x38, 0x0c, 0x06, 0x04, 0x78, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x61, 0x23, 0x1e, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0xc2, 0x42, 0x66, 0x24, 0x2c, 0x3c, 0x18, 0x18, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0x46, 0x4f, 0x69, 0x29, 0x39, 0x30, 0x30, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x64, 0x2c, 0x38, 0x18, 0x38, 0x24, 0x46, 0xc2, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x46, 0x64, 0x2c, 0x38, 0x10, 0x10, 0x10, 0x10, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x04, 0x0c, 0x08, 0x10, 0x30, 0x20, 0x40, 0xfe, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x70,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x40, 0x40, 0x60, 0x20, 0x20, 0x10, 0x10, 0x18, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0xe0,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x38, 0x28, 0x24, 0x44, 0x42, 0x00, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xfc,
],
[
0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x04, 0x04, 0x7c, 0x44, 0x4c, 0x7c, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x5c, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x60, 0x40, 0x40, 0x40, 0x60, 0x3c, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x3e, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3a, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x64, 0x46, 0x7e, 0x40, 0x60, 0x3c, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x38, 0x60, 0x60, 0xf0, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3e, 0x02,
0x06,
],
[
0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7c, 0x66, 0x42, 0x42, 0x42, 0x42, 0x42, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
0x40,
],
[
0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x44, 0x48, 0x50, 0x70, 0x58, 0x4c, 0x46, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, 0x66, 0x46, 0x46, 0x46, 0x46, 0x46, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x66, 0x42, 0x42, 0x42, 0x42, 0x42, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3c, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x7c, 0x40,
0x40,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3e, 0x02,
0x02,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x40, 0x60, 0x38, 0x0c, 0x0c, 0x78, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, 0x40, 0x40, 0x40, 0x40, 0x60, 0x30, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x66, 0x3a, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc4, 0x4c, 0x68, 0x28, 0x38, 0x30, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0x4e, 0x5a, 0x72, 0x33, 0x31, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x6c, 0x38, 0x30, 0x38, 0x6c, 0xc4, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x44, 0x4c, 0x68, 0x28, 0x38, 0x10, 0x30,
0x20,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x08, 0x10, 0x30, 0x20, 0x40, 0xfc, 0x00,
0x00,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x20, 0x20, 0x20, 0x20, 0xc0, 0x20, 0x20, 0x20, 0x20,
0x18,
],
[
0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
0x10,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x20, 0x20, 0x20, 0x20, 0x18, 0x20, 0x20, 0x20, 0x60,
0xc0,
],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x72, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
],
];
#[inline]
pub fn get_glyph(c: char) -> Option<&'static [u8; 16]> {
let code = c as u32;
if (32..=126).contains(&code) {
Some(&FONT_8X16[(code - 32) as usize])
} else {
None
}
}
pub const TOFU_GLYPH: [u8; 16] = [
0x00, 0x00, 0x7e, 0x42, 0x42, 0x5a, 0x5a, 0x42, 0x5a, 0x5a, 0x42, 0x42, 0x7e, 0x00, 0x00, 0x00,
];
pub fn map_common_unicode(c: char) -> Option<&'static [u8; 16]> {
match c {
'\u{00A0}' => get_glyph(' '), '\u{2018}' | '\u{2019}' | '\u{201A}' | '\u{201B}' => get_glyph('\''), '\u{201C}' | '\u{201D}' | '\u{201E}' | '\u{201F}' => get_glyph('"'), '\u{2013}' | '\u{2014}' | '\u{2212}' => get_glyph('-'), '\u{2026}' => get_glyph('.'), '\u{2022}' => get_glyph('*'), '\u{00B7}' => get_glyph('.'), '\u{00AB}' => get_glyph('<'), '\u{00BB}' => get_glyph('>'), '\u{00D7}' => get_glyph('x'), '\u{00F7}' => get_glyph('/'), '\u{00B1}' => get_glyph('+'), '\u{00B0}' => get_glyph('o'), _ => None,
}
}
#[inline]
pub fn get_glyph_or_fallback(c: char) -> &'static [u8; 16] {
if let Some(glyph) = get_glyph(c) {
glyph
} else if let Some(mapped) = map_common_unicode(c) {
mapped
} else {
&TOFU_GLYPH
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OsPlatform {
Linux,
MacOS,
Windows,
Other,
}
impl OsPlatform {
pub const fn current() -> Self {
if cfg!(target_os = "macos") {
Self::MacOS
} else if cfg!(target_os = "windows") {
Self::Windows
} else if cfg!(target_os = "linux") {
Self::Linux
} else {
Self::Other
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GenericFontFamily {
SansSerif,
Serif,
Monospace,
Cursive,
Fantasy,
SystemUi,
}
#[derive(Debug, Clone)]
pub struct SystemFontResolver;
impl SystemFontResolver {
pub fn default_chain_for_generic(family: GenericFontFamily) -> Vec<String> {
Self::chain_for_platform(family, OsPlatform::current())
}
pub fn chain_for_platform(family: GenericFontFamily, platform: OsPlatform) -> Vec<String> {
match platform {
OsPlatform::MacOS => match family {
GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
"-apple-system".into(),
"BlinkMacSystemFont".into(),
"SF Pro Text".into(),
"Helvetica Neue".into(),
"Helvetica".into(),
"Arial".into(),
"PingFang SC".into(),
"Hiragino Sans".into(),
"Apple Color Emoji".into(),
"sans-serif".into(),
],
GenericFontFamily::Serif => vec![
"New York".into(),
"Times New Roman".into(),
"Times".into(),
"Songti SC".into(),
"serif".into(),
],
GenericFontFamily::Monospace => vec![
"SF Mono".into(),
"Menlo".into(),
"Monaco".into(),
"Courier New".into(),
"monospace".into(),
],
_ => vec!["-apple-system".into(), "sans-serif".into()],
},
OsPlatform::Windows => match family {
GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
"Segoe UI".into(),
"Calibri".into(),
"Arial".into(),
"Microsoft YaHei".into(),
"Meiryo".into(),
"Segoe UI Emoji".into(),
"Segoe UI Symbol".into(),
"sans-serif".into(),
],
GenericFontFamily::Serif => vec![
"Georgia".into(),
"Times New Roman".into(),
"Cambria".into(),
"SimSun".into(),
"serif".into(),
],
GenericFontFamily::Monospace => vec![
"Consolas".into(),
"Cascadia Code".into(),
"Courier New".into(),
"monospace".into(),
],
_ => vec!["Segoe UI".into(), "sans-serif".into()],
},
OsPlatform::Linux | OsPlatform::Other => match family {
GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
"Ubuntu".into(),
"DejaVu Sans".into(),
"Liberation Sans".into(),
"Noto Sans".into(),
"Noto Sans CJK SC".into(),
"Noto Color Emoji".into(),
"sans-serif".into(),
],
GenericFontFamily::Serif => vec![
"DejaVu Serif".into(),
"Liberation Serif".into(),
"Noto Serif".into(),
"serif".into(),
],
GenericFontFamily::Monospace => vec![
"Ubuntu Mono".into(),
"DejaVu Sans Mono".into(),
"Liberation Mono".into(),
"Noto Sans Mono".into(),
"monospace".into(),
],
_ => vec!["Ubuntu".into(), "sans-serif".into()],
},
}
}
pub fn parse_font_family_cascade(css_value: &str) -> Vec<String> {
let mut results = Vec::new();
for item in css_value.split(',') {
let trimmed = item.trim().trim_matches(|c| c == '\'' || c == '"');
if trimmed.is_empty() {
continue;
}
match trimmed.to_ascii_lowercase().as_str() {
"sans-serif" => {
for f in Self::default_chain_for_generic(GenericFontFamily::SansSerif) {
if !results.contains(&f) {
results.push(f);
}
}
}
"serif" => {
for f in Self::default_chain_for_generic(GenericFontFamily::Serif) {
if !results.contains(&f) {
results.push(f);
}
}
}
"monospace" => {
for f in Self::default_chain_for_generic(GenericFontFamily::Monospace) {
if !results.contains(&f) {
results.push(f);
}
}
}
"system-ui" | "-apple-system" | "blinkmacsystemfont" => {
let token = trimmed.to_string();
if !results.contains(&token) {
results.push(token);
}
for f in Self::default_chain_for_generic(GenericFontFamily::SystemUi) {
if !results.contains(&f) {
results.push(f);
}
}
}
_ => {
let owned = trimmed.to_string();
if !results.contains(&owned) {
results.push(owned);
}
}
}
}
if results.is_empty() {
results = Self::default_chain_for_generic(GenericFontFamily::SansSerif);
}
results
}
}
#[derive(Debug, Clone)]
pub struct FontFallbackChain {
pub families: Vec<String>,
}
impl FontFallbackChain {
pub fn new(families: Vec<String>) -> Self {
Self { families }
}
pub fn from_css(css_value: &str) -> Self {
Self {
families: SystemFontResolver::parse_font_family_cascade(css_value),
}
}
pub fn primary_font(&self) -> Option<&str> {
self.families.first().map(|s| s.as_str())
}
pub fn can_render(&self, c: char) -> bool {
get_glyph(c).is_some() || map_common_unicode(c).is_some()
}
}