use std::hash::{Hash, Hasher};
use super::{AnnotatedString, layout_input::TextUnits, layout_options::TextLayoutOptions};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct TextKey {
text_hash: u64,
style_hash: u64,
options: TextLayoutOptions,
units: (u32, u32, u32),
}
impl TextKey {
pub(super) fn of(
text: &AnnotatedString,
style_hash: u64,
options: TextLayoutOptions,
units: TextUnits,
) -> Option<Self> {
(text.string_annotations.is_empty() && text.link_annotations.is_empty()).then(|| Self {
text_hash: text.render_hash(),
style_hash,
options: options.normalized(),
units: (
units.density.density().to_bits(),
units.density.font_scale_curve().fingerprint(),
units.factor.to_bits(),
),
})
}
pub(super) fn mixed_hash(&self) -> u64 {
(self.text_hash ^ self.style_hash.rotate_left(32)).wrapping_mul(0x9e37_79b9_7f4a_7c15)
}
}
impl Hash for TextKey {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self.mixed_hash());
}
}