Skip to main content

fret_render_text/
font_instance_key.rs

1use std::hash::{Hash, Hasher};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct FontFaceKey {
5    font_data_id: u64,
6    face_index: u32,
7    variation_key: u64,
8    synthesis_embolden: bool,
9    /// Faux italic/oblique skew in degrees, applied at rasterization time.
10    synthesis_skew_degrees: i8,
11}
12
13impl FontFaceKey {
14    pub fn new(
15        font_data_id: u64,
16        face_index: u32,
17        variation_key: u64,
18        synthesis_embolden: bool,
19        synthesis_skew_degrees: i8,
20    ) -> Self {
21        Self {
22            font_data_id,
23            face_index,
24            variation_key,
25            synthesis_embolden,
26            synthesis_skew_degrees,
27        }
28    }
29
30    pub fn font_data_id(&self) -> u64 {
31        self.font_data_id
32    }
33
34    pub fn face_index(&self) -> u32 {
35        self.face_index
36    }
37
38    pub fn variation_key(&self) -> u64 {
39        self.variation_key
40    }
41
42    pub fn synthesis_embolden(&self) -> bool {
43        self.synthesis_embolden
44    }
45
46    pub fn synthesis_skew_degrees(&self) -> i8 {
47        self.synthesis_skew_degrees
48    }
49
50    pub fn into_parts(self) -> (u64, u32, u64, bool, i8) {
51        (
52            self.font_data_id,
53            self.face_index,
54            self.variation_key,
55            self.synthesis_embolden,
56            self.synthesis_skew_degrees,
57        )
58    }
59}
60
61pub fn variation_key_from_normalized_coords(coords: &[i16]) -> u64 {
62    if coords.is_empty() {
63        return 0;
64    }
65    let mut hasher = std::collections::hash_map::DefaultHasher::new();
66    "fret.text.font_instance.v0".hash(&mut hasher);
67    coords.hash(&mut hasher);
68    let key = hasher.finish();
69    if key == 0 { 1 } else { key }
70}