1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::{
    apply_rules::{ParsedFontStyle, ResolvedNodeProperties},
    rewrite_css::RewriteTargets,
};
use anyhow::Result;
use arcstr::ArcStr;
use enumset::EnumSet;
use mkwebfont_common::hashing::WyHashBuilder;
use mkwebfont_fontops::font_info::{FontStyle, FontWeight};
use std::{
    collections::{HashMap, HashSet},
    sync::Arc,
};

#[derive(Debug, Clone)]
pub struct WebrootInfo {
    pub font_stacks: Vec<FontStackInfo>,
    pub(crate) targets: RewriteTargets,
}

#[derive(Debug, Clone)]
pub struct FontStackInfo {
    pub stack: Arc<[ArcStr]>,
    pub samples: Vec<TextSample>,
}
impl FontStackInfo {
    pub fn glyphs(&self) -> String {
        let mut chars = HashSet::new();
        for sample in &self.samples {
            chars.extend(sample.glyphs().chars());
        }

        let mut chars: Vec<_> = chars.into_iter().collect();
        chars.sort();
        chars.into_iter().collect()
    }
}

#[derive(Debug, Clone)]
pub struct TextSample {
    pub used_styles: EnumSet<FontStyle>,
    pub used_weights: Arc<[FontWeight]>,
    pub content: Vec<ArcStr>,
}
impl TextSample {
    pub fn glyphs(&self) -> String {
        let chars: HashSet<_> = self
            .content
            .iter()
            .flat_map(|x| x.as_str().chars())
            .collect();
        let mut chars: Vec<_> = chars.into_iter().collect();
        chars.sort();
        chars.into_iter().collect()
    }
}

#[derive(Debug, Default)]
pub struct TextInfoBuilder {
    stacks: HashMap<
        Arc<[ArcStr]>,
        HashMap<TextSampleKey, HashSet<ArcStr, WyHashBuilder>>,
        WyHashBuilder,
    >,
    cached_strs: HashSet<ArcStr, WyHashBuilder>,
    cached_stacks: HashSet<Arc<[ArcStr]>, WyHashBuilder>,
    cached_weights: HashSet<Arc<[FontWeight]>, WyHashBuilder>,
}
impl TextInfoBuilder {
    fn intern_str(&mut self, str: &str) -> ArcStr {
        if let Some(x) = self.cached_strs.get(str) {
            x.clone()
        } else {
            let arc = ArcStr::from(str);
            self.cached_strs.insert(arc.clone());
            arc
        }
    }

    fn intern_stack(&mut self, arc: &Arc<[ArcStr]>) -> Arc<[ArcStr]> {
        if let Some(x) = self.cached_stacks.get(arc) {
            x.clone()
        } else {
            self.cached_stacks.insert(arc.clone());
            arc.clone()
        }
    }

    fn intern_weights(&mut self, weights: &HashSet<i32, WyHashBuilder>) -> Arc<[FontWeight]> {
        let mut weights = weights
            .iter()
            .map(|x| -> Result<FontWeight> { Ok(FontWeight::from_num((*x).try_into()?)) })
            .collect::<Result<Vec<_>>>()
            .expect("Negative font weights not supported.");
        weights.sort();
        let arc: Arc<[_]> = weights.into();
        if let Some(x) = self.cached_weights.get(&arc) {
            x.clone()
        } else {
            self.cached_weights.insert(arc.clone());
            arc
        }
    }

    pub fn push_sample(&mut self, properties: &ResolvedNodeProperties, additional_text: &[ArcStr]) {
        let key = TextSampleKey {
            styles: properties
                .font_style
                .iter()
                .map(|x| match x {
                    ParsedFontStyle::Normal => FontStyle::Regular,
                    ParsedFontStyle::Italic => FontStyle::Italic,
                    ParsedFontStyle::Oblique => FontStyle::Oblique,
                })
                .collect(),
            weights: self.intern_weights(&properties.font_weight),
        };
        let content: Vec<_> = additional_text
            .iter()
            .chain(additional_text.iter())
            .filter(|x| !x.is_empty())
            .map(|x| self.intern_str(&x))
            .collect();

        for stack in &properties.font_stack {
            let stack = self.intern_stack(stack);
            let texts = self
                .stacks
                .entry(stack)
                .or_default()
                .entry(key.clone())
                .or_default();
            texts.extend(content.iter().cloned());
        }
    }

    pub fn build(&self, targets: &RewriteTargets) -> WebrootInfo {
        let mut keys: Vec<_> = self.stacks.keys().collect();
        keys.sort();

        let mut out = WebrootInfo { font_stacks: vec![], targets: targets.clone() };
        for key in keys {
            let stack = self.stacks.get(key).unwrap();
            let mut stack_keys: Vec<_> = stack.keys().collect();
            stack_keys.sort();

            let mut stack_info = FontStackInfo { stack: key.clone(), samples: vec![] };
            for key in stack_keys {
                let mut content: Vec<_> = stack.get(key).unwrap().into_iter().cloned().collect();
                content.sort();
                stack_info.samples.push(TextSample {
                    used_styles: key.styles,
                    used_weights: key.weights.clone(),
                    content,
                });
            }
            out.font_stacks.push(stack_info);
        }
        out
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
struct TextSampleKey {
    styles: EnumSet<FontStyle>,
    weights: Arc<[FontWeight]>,
}