cn_font_split/
lib.rs

1pub mod link_subset;
2pub mod pre_subset;
3pub mod run_subset;
4pub mod runner;
5pub use runner::font_split;
6mod loader;
7mod message;
8
9#[test]
10fn main_test() {
11    use std::env;
12    env::set_var("RUST_LOG", "info");
13    env_logger::init();
14
15    fn test_on(path: &str, dist_dir: &str) {
16        use cn_font_proto::api_interface::input_template::CssProperties;
17        use cn_font_proto::api_interface::input_template::PreviewImage;
18        use cn_font_proto::api_interface::InputTemplate;
19        use cn_font_utils::{output_file, read_binary_file};
20        use log::info;
21
22        let font_file = read_binary_file(&path).expect("Failed to read file");
23        let input = InputTemplate {
24            input: font_file,
25            preview_image: Some(PreviewImage {
26                name: "preview".to_string(),
27                text: "中文网字计划\nThe Chinese Web Font Project".to_string(),
28            }),
29            css: Some(CssProperties {
30                // font_family: Some("New".to_string()),
31                // font_weight: Some("200".to_string()),
32                // font_style: Some("italic".to_string()),
33                // font_display: Some("auto".to_string()),
34                // local_family: vec!["New2".to_string()],
35                // polyfill: vec![],
36                // comment_base: Some(true),
37                // comment_name_table: Some(true),
38                // comment_unicodes: Some(true),
39                // compress: Some(true),
40                // file_name: Some("input.css".to_string()),
41                ..Default::default()
42            }),
43            chunk_size: Some(20 * 1024),
44            // 精确控制
45            // subsets: vec![[65]].iter().map(|x| u32_array_to_u8_array(x)).collect(),
46            // language_areas: Some(false),
47            // auto_subset: Some(false),
48            // font_feature: Some(false),
49            // reduce_mins: Some(false),
50            // rename_output_font: Some("font_[hash:6].[ext]".to_string()),
51            ..Default::default()
52        };
53
54        let start = std::time::Instant::now();
55        font_split(input, |m| {
56            // println!("{}  {}", m.event, m.message.unwrap_or("".to_owned()));
57            // 打开一个文件以供写入,如果文件不存在,则创建它
58            match m.data {
59                Some(data) => {
60                    output_file(
61                        &format!("dist/{}/{}", dist_dir, m.message),
62                        &data,
63                    )
64                    .expect("write file error");
65                }
66                _ => (),
67            }
68        });
69
70        let duration = start.elapsed();
71        println!("Time: {:?}", duration);
72    }
73
74    test_on("./packages/demo/public/SmileySans-Oblique.ttf", "ttf");
75    test_on("./packages/demo/public/SmileySans-Oblique.ttf.woff2", "woff2");
76}