cn_font_split/pre_subset/
mod.rs

1pub mod features;
2pub mod fvar;
3pub mod gen_svg;
4pub mod name_table;
5pub mod plugin;
6mod plugin_auto_subset;
7
8use crate::runner::Context;
9mod plugin_add_user_subset;
10use cn_font_utils::u8_array_to_u32_array;
11use features::features_plugin;
12use gen_svg::gen_svg_from_ctx;
13use harfbuzz_rs_now::{Face, Owned};
14use indexmap::IndexSet;
15use plugin::{
16    add_remain_chars_plugin, language_area_plugin, reduce_min_plugin,
17};
18use plugin_auto_subset::plugin_auto_subset;
19use std::io::Cursor;
20
21pub struct PreSubsetContext<'a, 'b, 'c>
22where
23    'b: 'a,
24    'c: 'a,
25{
26    all_unicodes: IndexSet<u32>,
27    face: &'a mut Owned<Face<'b>>,
28    predict_bytes_pre_subset: u32,
29    font: &'a opentype::Font,
30    font_file: &'a mut Cursor<&'c [u8]>,
31    subsets: &'c Vec<Vec<u32>>,
32}
33
34pub fn pre_subset(ctx: &mut Context) {
35    let file_binary = &*ctx.binary;
36    let mut all_unicodes: IndexSet<u32> =
37        IndexSet::from_iter(ctx.face.collect_unicodes());
38
39    let mut font_file = Cursor::new(file_binary);
40    let font = opentype::Font::read(&mut font_file)
41        .expect("cn-font-split | pre_subset | read font file error");
42
43    gen_svg_from_ctx(ctx);
44
45    let mut subsets: Vec<IndexSet<u32>> = vec![];
46    let user_subsets: Vec<Vec<u32>> =
47        ctx.input.subsets.iter().map(|x| u8_array_to_u32_array(x)).collect();
48    let mut context = PreSubsetContext {
49        all_unicodes: all_unicodes.clone(),
50        face: &mut ctx.face,
51        // 每个包的大小
52        predict_bytes_pre_subset: ctx.input.chunk_size.unwrap_or(1024 * 70)
53            as u32,
54        font: &font,
55        subsets: &user_subsets,
56        font_file: &mut font_file,
57    };
58
59    let mut process: Vec<
60        fn(
61            &mut Vec<IndexSet<u32>>,
62            &mut IndexSet<u32>,
63            &mut PreSubsetContext<'_, '_, '_>,
64        ),
65    > = vec![];
66    process.push(plugin_add_user_subset::plugin_add_user_subset);
67    if ctx.input.language_areas.unwrap_or(true) {
68        process.push(language_area_plugin);
69    }
70    if ctx.input.subset_remain_chars.unwrap_or(true) {
71        process.push(add_remain_chars_plugin);
72    }
73    if ctx.input.auto_subset.unwrap_or(true) {
74        process.push(plugin_auto_subset);
75    }
76    if ctx.input.font_feature.unwrap_or(true) {
77        process.push(features_plugin);
78    }
79    if ctx.input.reduce_mins.unwrap_or(true) {
80        process.push(reduce_min_plugin);
81    }
82    for p in process {
83        p(&mut subsets, &mut all_unicodes, &mut context);
84    }
85
86    // let set = analyze_gsub(&font, &mut font_file);
87    ctx.pre_subset_result = subsets
88        .iter()
89        .filter(|v| v.len() > 0)
90        .map(|v| v.iter().map(|i| i.clone()).collect::<Vec<u32>>())
91        .collect();
92    ctx.name_table = name_table::analyze_name_table(&font, &mut font_file);
93}