text_example/
text_example.rs

1use spottedcat::{Context, Spot, Text, DrawOption, load_font_from_bytes};
2
3struct TextApp {}
4
5impl Spot for TextApp {
6    fn initialize(_context: &mut Context) -> Self {
7        Self {}
8    }
9
10    fn draw(&mut self, context: &mut Context) {
11        // 示例:优先尝试 macOS 自带中文字体(多数为 .ttc,可能不被当前字体解析器支持)
12        // 如果加载失败则回退到仓库内的 DejaVuSans.ttf(仅用于演示,中文会变方块)。
13        const FALLBACK_FONT: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");
14
15        let font_opts = [
16            "/System/Library/Fonts/PingFang.ttc",
17            "/System/Library/Fonts/STHeiti Medium.ttc",
18            "/System/Library/Fonts/Supplemental/Songti.ttc",
19        ];
20
21        let mut font_data: Option<Vec<u8>> = None;
22        let mut chosen_path: Option<&str> = None;
23        for p in font_opts {
24            if let Ok(data) = spottedcat::load_font_from_file(p) {
25                font_data = Some(data);
26                chosen_path = Some(p);
27                break;
28            }
29        }
30
31        let font_data = font_data.unwrap_or_else(|| load_font_from_bytes(FALLBACK_FONT));
32
33        // 使用 Text::draw() - 传入字体数据
34        let mut opts = DrawOption::new();
35        opts.position = [spottedcat::Pt::from(100.0), spottedcat::Pt::from(100.0)];
36        Text::new("Provided Font", font_data.clone())
37            .with_font_size(spottedcat::Pt::from(32.0))
38            .with_color([1.0, 1.0, 1.0, 1.0])
39            .draw(context, opts);
40        
41        let mut custom_opts = DrawOption::new();
42        custom_opts.position = [spottedcat::Pt::from(100.0), spottedcat::Pt::from(150.0)];
43        Text::new("使用嵌入字体 - Embedded Font", font_data.clone())
44            .with_font_size(spottedcat::Pt::from(28.0))
45            .with_color([1.0, 0.5, 0.0, 1.0])
46            .with_stroke_width(spottedcat::Pt::from(2.0))
47            .with_stroke_color([1.0, 1.0, 1.0, 1.0])
48            .draw(context, custom_opts);
49
50        // 从文件加载也可以(同样会写入 font_data)
51        let file_font = match chosen_path {
52            Some(p) => spottedcat::load_font_from_file(p).unwrap_or_else(|_| font_data.clone()),
53            None => spottedcat::load_font_from_file("assets/DejaVuSans.ttf")
54                .unwrap_or_else(|_| font_data.clone()),
55        };
56        let mut file_opts = DrawOption::new();
57        file_opts.position = [spottedcat::Pt::from(100.0), spottedcat::Pt::from(200.0)];
58        Text::new("从文件加载字体 - Loaded from File", file_font)
59            .with_font_size(spottedcat::Pt::from(24.0))
60            .with_color([0.0, 1.0, 0.5, 1.0])
61            .draw(context, file_opts);
62
63        // 使用 Text::draw() - 不同颜色和大小
64        let mut small_opts = DrawOption::new();
65        small_opts.position = [spottedcat::Pt::from(100.0), spottedcat::Pt::from(250.0)];
66        Text::new("小字体 - Small Font Size", font_data.clone())
67            .with_font_size(spottedcat::Pt::from(18.0))
68            .with_color([0.5, 0.5, 1.0, 1.0])
69            .draw(context, small_opts);
70
71        let mut large_opts = DrawOption::new();
72        large_opts.position = [spottedcat::Pt::from(100.0), spottedcat::Pt::from(300.0)];
73        Text::new("大字体 - Large Font", font_data)
74            .with_font_size(spottedcat::Pt::from(48.0))
75            .with_color([1.0, 0.0, 0.5, 1.0])
76            .draw(context, large_opts);
77    }
78
79    fn update(&mut self, _context: &mut Context, _dt: std::time::Duration) {}
80    fn remove(&self) {}
81}
82
83fn main() {
84    spottedcat::run::<TextApp>(spottedcat::WindowConfig::default());
85}