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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use app_units::Au;
use atomic_refcell::AtomicRefCell;
use log::warn;
use parking_lot::RwLock;
use style::stylesheets::DocumentStyleSheet;
use style::values::computed::{FontStyle, FontWeight};
use webrender_api::{FontInstanceFlags, FontInstanceKey, FontKey};

use crate::font::FontDescriptor;
use crate::font_cache_thread::{FontIdentifier, FontSource, LowercaseFontFamilyName};
use crate::font_context::WebFontDownloadState;
use crate::font_template::{FontTemplate, FontTemplateRef, FontTemplateRefMethods, IsOblique};

#[derive(Default)]
pub struct FontStore {
    pub(crate) families: HashMap<LowercaseFontFamilyName, FontTemplates>,
    web_fonts_loading: Vec<(DocumentStyleSheet, usize)>,
}
pub(crate) type CrossThreadFontStore = Arc<RwLock<FontStore>>;

impl FontStore {
    pub(crate) fn clear(&mut self) {
        self.families.clear();
    }

    pub(crate) fn font_load_cancelled_for_stylesheet(
        &self,
        stylesheet: &DocumentStyleSheet,
    ) -> bool {
        !self
            .web_fonts_loading
            .iter()
            .any(|(loading_stylesheet, _)| loading_stylesheet == stylesheet)
    }

    pub(crate) fn handle_stylesheet_removed(&mut self, stylesheet: &DocumentStyleSheet) {
        self.web_fonts_loading
            .retain(|(loading_stylesheet, _)| loading_stylesheet != stylesheet);
    }

    pub(crate) fn handle_web_font_load_started_for_stylesheet(
        &mut self,
        stylesheet: &DocumentStyleSheet,
    ) {
        if let Some((_, count)) = self
            .web_fonts_loading
            .iter_mut()
            .find(|(loading_stylesheet, _)| loading_stylesheet == stylesheet)
        {
            *count += 1;
        } else {
            self.web_fonts_loading.push((stylesheet.clone(), 1))
        }
    }

    fn remove_one_web_font_loading_for_stylesheet(&mut self, stylesheet: &DocumentStyleSheet) {
        if let Some((_, count)) = self
            .web_fonts_loading
            .iter_mut()
            .find(|(loading_stylesheet, _)| loading_stylesheet == stylesheet)
        {
            *count -= 1;
        }
        self.web_fonts_loading.retain(|(_, count)| *count != 0);
    }

    pub(crate) fn handle_web_font_failed_to_load(&mut self, state: &WebFontDownloadState) {
        self.remove_one_web_font_loading_for_stylesheet(&state.stylesheet);
    }

    /// Handle a web font load finishing, adding the new font to the [`FontStore`]. If the web font
    /// load was canceled (for instance, if the stylesheet was removed), then do nothing and return
    /// false.
    pub(crate) fn handle_web_font_loaded(
        &mut self,
        state: &WebFontDownloadState,
        new_template: FontTemplate,
    ) -> bool {
        // Abort processing this web font if the originating stylesheet was removed.
        if self.font_load_cancelled_for_stylesheet(&state.stylesheet) {
            return false;
        }

        let family_name = state.css_font_face_descriptors.family_name.clone();
        self.families
            .entry(family_name)
            .or_default()
            .add_template(new_template);
        self.remove_one_web_font_loading_for_stylesheet(&state.stylesheet);
        true
    }

    pub(crate) fn number_of_fonts_still_loading(&self) -> usize {
        self.web_fonts_loading.iter().map(|(_, count)| count).sum()
    }
}

#[derive(Default)]
pub struct WebRenderFontStore {
    pub(crate) webrender_font_key_map: HashMap<FontIdentifier, FontKey>,
    pub(crate) webrender_font_instance_map: HashMap<(FontKey, Au), FontInstanceKey>,
}
pub(crate) type CrossThreadWebRenderFontStore = Arc<RwLock<WebRenderFontStore>>;

impl WebRenderFontStore {
    pub(crate) fn get_font_instance<FCT: FontSource>(
        &mut self,
        font_cache_thread: &FCT,
        font_template: FontTemplateRef,
        pt_size: Au,
        flags: FontInstanceFlags,
    ) -> FontInstanceKey {
        let webrender_font_key_map = &mut self.webrender_font_key_map;
        let identifier = font_template.identifier().clone();
        let font_key = *webrender_font_key_map
            .entry(identifier.clone())
            .or_insert_with(|| {
                font_cache_thread.get_web_font(font_template.data(), identifier.index())
            });

        *self
            .webrender_font_instance_map
            .entry((font_key, pt_size))
            .or_insert_with(|| {
                font_cache_thread.get_web_font_instance(font_key, pt_size.to_f32_px(), flags)
            })
    }

    pub(crate) fn remove_all_fonts(&mut self) -> (Vec<FontKey>, Vec<FontInstanceKey>) {
        (
            self.webrender_font_key_map
                .drain()
                .map(|(_, key)| key)
                .collect(),
            self.webrender_font_instance_map
                .drain()
                .map(|(_, key)| key)
                .collect(),
        )
    }

    pub(crate) fn remove_all_fonts_for_identifiers(
        &mut self,
        identifiers: HashSet<FontIdentifier>,
    ) -> (Vec<FontKey>, Vec<FontInstanceKey>) {
        let mut removed_keys: HashSet<FontKey> = HashSet::new();
        self.webrender_font_key_map.retain(|identifier, font_key| {
            if identifiers.contains(identifier) {
                removed_keys.insert(*font_key);
                false
            } else {
                true
            }
        });

        let mut removed_instance_keys: HashSet<FontInstanceKey> = HashSet::new();
        self.webrender_font_instance_map
            .retain(|(font_key, _), instance_key| {
                if removed_keys.contains(font_key) {
                    removed_instance_keys.insert(*instance_key);
                    false
                } else {
                    true
                }
            });

        (
            removed_keys.into_iter().collect(),
            removed_instance_keys.into_iter().collect(),
        )
    }
}

/// A struct that represents the available templates in a "simple family." A simple family
/// is one that contains <= 4 available faces: regular, bold, italic, and bold italic. Having
/// this simple family abstraction makes font matching much faster for families that don't
/// have a complex set of fonts.
///
/// This optimization is taken from:
/// <https://searchfox.org/mozilla-central/source/gfx/thebes/gfxFontEntry.cpp>.
#[derive(Clone, Debug, Default)]
struct SimpleFamily {
    regular: Option<FontTemplateRef>,
    bold: Option<FontTemplateRef>,
    italic: Option<FontTemplateRef>,
    bold_italic: Option<FontTemplateRef>,
}

impl SimpleFamily {
    /// Find a font in this family that matches a given descriptor.
    fn find_for_descriptor(&self, descriptor_to_match: &FontDescriptor) -> Option<FontTemplateRef> {
        let want_bold = descriptor_to_match.weight >= FontWeight::BOLD_THRESHOLD;
        let want_italic = descriptor_to_match.style != FontStyle::NORMAL;

        // This represents the preference of which font to return from the [`SimpleFamily`],
        // given what kind of font we are requesting.
        let preference = match (want_bold, want_italic) {
            (true, true) => [&self.bold_italic, &self.italic, &self.bold, &self.regular],
            (true, false) => [&self.bold, &self.regular, &self.bold_italic, &self.italic],
            (false, true) => [&self.italic, &self.bold_italic, &self.regular, &self.bold],
            (false, false) => [&self.regular, &self.bold, &self.italic, &self.bold_italic],
        };
        preference
            .iter()
            .filter_map(|template| (*template).clone())
            .next()
    }

    fn remove_templates_for_stylesheet(&mut self, stylesheet: &DocumentStyleSheet) {
        let remove_if_template_matches = |template: &mut Option<FontTemplateRef>| {
            if template
                .as_ref()
                .is_some_and(|template| template.borrow().stylesheet.as_ref() == Some(stylesheet))
            {
                *template = None;
            }
        };
        remove_if_template_matches(&mut self.regular);
        remove_if_template_matches(&mut self.bold);
        remove_if_template_matches(&mut self.italic);
        remove_if_template_matches(&mut self.bold_italic);
    }

    pub(crate) fn for_all_identifiers(&self, mut callback: impl FnMut(&FontIdentifier)) {
        let mut call_if_not_none = |template: &Option<FontTemplateRef>| {
            if let Some(template) = template {
                callback(&template.identifier())
            }
        };
        call_if_not_none(&self.regular);
        call_if_not_none(&self.bold);
        call_if_not_none(&self.italic);
        call_if_not_none(&self.bold_italic);
    }
}
/// A list of font templates that make up a given font family.
#[derive(Clone, Debug)]
pub struct FontTemplates {
    pub(crate) templates: Vec<FontTemplateRef>,
    simple_family: Option<SimpleFamily>,
}

impl Default for FontTemplates {
    fn default() -> Self {
        Self {
            templates: Default::default(),
            simple_family: Some(SimpleFamily::default()),
        }
    }
}

impl FontTemplates {
    /// Find a font in this family that matches a given descriptor.
    pub fn find_for_descriptor(
        &self,
        descriptor_to_match: Option<&FontDescriptor>,
    ) -> Vec<FontTemplateRef> {
        let Some(descriptor_to_match) = descriptor_to_match else {
            return self.templates.clone();
        };

        if self.templates.len() == 1 {
            return vec![self.templates[0].clone()];
        }

        if let Some(template) = self
            .simple_family
            .as_ref()
            .and_then(|simple_family| simple_family.find_for_descriptor(descriptor_to_match))
        {
            return vec![template];
        }

        let mut best_templates = Vec::new();
        let mut best_distance = f32::MAX;
        for template in self.templates.iter() {
            let distance = template.descriptor_distance(descriptor_to_match);
            if distance < best_distance {
                best_templates = vec![template.clone()];
                best_distance = distance
            } else if distance == best_distance {
                best_templates.push(template.clone());
            }
        }

        if !best_templates.is_empty() {
            return best_templates;
        }

        // If a request is made for a font family that exists,
        // pick the first valid font in the family if we failed
        // to find an exact match for the descriptor.
        if let Some(template) = self.templates.first() {
            return vec![template.clone()];
        }

        Vec::new()
    }

    pub fn add_template(&mut self, new_template: FontTemplate) {
        for existing_template in &self.templates {
            let existing_template = existing_template.borrow();
            if *existing_template.identifier() == new_template.identifier &&
                existing_template.descriptor == new_template.descriptor
            {
                return;
            }
        }

        let new_template = Arc::new(AtomicRefCell::new(new_template));
        self.templates.push(new_template.clone());
        self.update_simple_family(new_template);
    }

    fn update_simple_family(&mut self, added_template: FontTemplateRef) {
        // If this was detected to not be a simple family before, it cannot ever be one
        // in the future.
        let Some(simple_family) = self.simple_family.as_mut() else {
            return;
        };

        if self.templates.len() > 4 {
            self.simple_family = None;
            return;
        }

        // Variation fonts are never simple families.
        if added_template.descriptor().is_variation_font() {
            self.simple_family = None;
            return;
        }

        let Some(first) = self.templates.first() else {
            warn!("Called before adding any templates.");
            return;
        };

        // If the stretch between any of these fonts differ, it cannot be a simple family nor if this
        // font is oblique.
        let stretch = added_template.descriptor().stretch.0;
        let style = added_template.descriptor().style.0;
        if first.descriptor().stretch.0 != stretch || style.is_oblique() {
            self.simple_family = None;
            return;
        }

        let weight = added_template.descriptor().weight.0;
        let supports_bold = weight >= FontWeight::BOLD_THRESHOLD;
        let is_italic = style == FontStyle::ITALIC;
        let slot = match (supports_bold, is_italic) {
            (true, true) => &mut simple_family.bold_italic,
            (true, false) => &mut simple_family.bold,
            (false, true) => &mut simple_family.italic,
            (false, false) => &mut simple_family.regular,
        };

        // If the slot was already filled there are two fonts with the same simple properties
        // and this isn't a simple family.
        if slot.is_some() {
            self.simple_family = None;
            return;
        }

        slot.replace(added_template);
    }

    pub(crate) fn remove_templates_for_stylesheet(
        &mut self,
        stylesheet: &DocumentStyleSheet,
    ) -> bool {
        let length_before = self.templates.len();
        self.templates
            .retain(|template| template.borrow().stylesheet.as_ref() != Some(stylesheet));

        if let Some(simple_family) = self.simple_family.as_mut() {
            simple_family.remove_templates_for_stylesheet(stylesheet);
        }

        length_before != self.templates.len()
    }

    pub(crate) fn for_all_identifiers(&self, mut callback: impl FnMut(&FontIdentifier)) {
        for template in self.templates.iter() {
            callback(&template.borrow().identifier);
        }
        if let Some(ref simple_family) = self.simple_family {
            simple_family.for_all_identifiers(callback)
        }
    }
}