pub fn load_font_from_bytes(bytes: &[u8]) -> Vec<u8> ⓘExamples found in repository?
examples/text_input_example.rs (line 32)
11 fn initialize(_: &mut Context) -> Self {
12 // Prefer a system font that contains CJK glyphs.
13 // Fallback to the bundled DejaVuSans.ttf.
14 const FALLBACK_FONT: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");
15
16 let font_candidates = [
17 "/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
18 "/System/Library/Fonts/PingFang.ttc",
19 "/System/Library/Fonts/Supplemental/Songti.ttc",
20 "/System/Library/Fonts/Supplemental/Heiti TC.ttc",
21 "/System/Library/Fonts/Supplemental/STHeiti Medium.ttc",
22 ];
23
24 let mut font_data = None;
25 for path in font_candidates {
26 if let Ok(data) = spottedcat::load_font_from_file(path) {
27 font_data = Some(data);
28 break;
29 }
30 }
31
32 let font_data = font_data.unwrap_or_else(|| spottedcat::load_font_from_bytes(FALLBACK_FONT));
33 Self {
34 committed: String::new(),
35 preedit: String::new(),
36 font_data,
37 capture_enabled: false,
38 }
39 }More examples
examples/mouse_click_example.rs (line 20)
18 fn draw(&mut self, context: &mut Context) {
19 const FONT: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");
20 let font_data = spottedcat::load_font_from_bytes(FONT);
21
22 let mut title_opts = DrawOption::new();
23 title_opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(40.0)];
24 Text::new(
25 "Mouse Click Example (Left click to record position)",
26 font_data.clone(),
27 )
28 .with_font_size(spottedcat::Pt::from(24.0))
29 .with_color([1.0, 1.0, 1.0, 1.0])
30 .draw(context, title_opts);
31
32 let mut pos_opts = DrawOption::new();
33 pos_opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(90.0)];
34
35 let text = match self.last_click {
36 Some((x, y)) => format!("Last left click: ({:.1}, {:.1})", x.as_f32(), y.as_f32()),
37 None => "Last left click: (none)".to_string(),
38 };
39
40 Text::new(text, font_data)
41 .with_font_size(spottedcat::Pt::from(20.0))
42 .with_color([0.7, 0.9, 1.0, 1.0])
43 .draw(context, pos_opts);
44 }examples/input_example.rs (line 42)
40 fn draw(&mut self, context: &mut Context) {
41 const FONT: &[u8] = include_bytes!("../assets/DejaVuSans.ttf");
42 let font_data = spottedcat::load_font_from_bytes(FONT);
43
44 let mut opts = DrawOption::new();
45 opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(40.0)];
46 Text::new(
47 "Input Example (WASD / Arrow Keys to move, ESC to reset)",
48 font_data.clone(),
49 )
50 .with_font_size(spottedcat::Pt::from(24.0))
51 .with_color([1.0, 1.0, 1.0, 1.0])
52 .draw(context, opts);
53
54 let mut opts = DrawOption::new();
55 opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(80.0)];
56 Text::new(format!("Position: ({:.1}, {:.1})", self.x, self.y), font_data.clone())
57 .with_font_size(spottedcat::Pt::from(20.0))
58 .with_color([0.7, 0.9, 1.0, 1.0])
59 .draw(context, opts);
60
61 let mut opts = DrawOption::new();
62 opts.position = [spottedcat::Pt::from(20.0), spottedcat::Pt::from(120.0)];
63 Text::new(
64 "Tip: hold keys for continuous movement; press ESC to reset.",
65 font_data,
66 )
67 .with_font_size(spottedcat::Pt::from(18.0))
68 .with_color([0.9, 0.9, 0.9, 1.0])
69 .draw(context, opts);
70 }examples/text_example.rs (line 31)
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 }