team-ljyzipelo

공개
65줄1.7 KB
1use std::io;
2
3pub(super) struct RuntimeFont {
4 pub bytes: Vec<u8>,
5 pub face_index: u32,
6}
7
8pub(super) fn load() -> io::Result<RuntimeFont> {
9 platform::load()
10}
11
12#[cfg(target_os = "linux")]
13mod platform {
14 use super::RuntimeFont;
15 use std::io;
16
17 pub(super) fn load() -> io::Result<RuntimeFont> {
18 Ok(RuntimeFont {
19 bytes: std::fs::read("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc")?,
20 face_index: 1,
21 })
22 }
23}
24
25#[cfg(target_os = "windows")]
26mod platform {
27 use super::RuntimeFont;
28 use std::{io, path::PathBuf};
29
30 pub(super) fn load() -> io::Result<RuntimeFont> {
31 let root = std::env::var_os("SystemRoot").ok_or_else(|| {
32 io::Error::new(
33 io::ErrorKind::NotFound,
34 "Windows system directory is unavailable",
35 )
36 })?;
37 let root = PathBuf::from(root);
38 if !root.is_absolute()
39 || !matches!(root.components().next(), Some(std::path::Component::Prefix(prefix))
40 if matches!(prefix.kind(), std::path::Prefix::Disk(_)))
41 {
42 return Err(io::Error::new(
43 io::ErrorKind::InvalidInput,
44 "Windows system directory must be a local absolute drive path",
45 ));
46 }
47 Ok(RuntimeFont {
48 bytes: std::fs::read(root.join("Fonts").join("malgun.ttf"))?,
49 face_index: 0,
50 })
51 }
52}
53
54#[cfg(not(any(target_os = "linux", target_os = "windows")))]
55mod platform {
56 use super::RuntimeFont;
57 use std::io;
58
59 pub(super) fn load() -> io::Result<RuntimeFont> {
60 Err(io::Error::new(
61 io::ErrorKind::Unsupported,
62 "No Korean system font provider is available for this platform",
63 ))
64 }
65}