<commit_before>//! The [Axis Variations](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table

use font_types::Tag;

/// 'avar'
pub const TAG: Tag = Tag::new(b"avar");

include!("../../generated/generated_avar.rs");

impl<'a> Avar<'a> {
    // TODO: see comment in Post::traverse_string_data
    #[cfg(feature = "traversal")]
    fn traverse_segment_maps(&self) -> FieldType<'a> {
        FieldType::I8(-42) // meaningless value
    }
}

impl<'a> SegmentMaps<'a> {
    /// Applies the piecewise linear mapping to the specified coordinate.
    pub fn apply(&self, coord: Fixed) -> Fixed {
        let mut prev = AxisValueMap {
            from_coordinate: Default::default(),
            to_coordinate: Default::default(),
        };
        for (i, axis_value_map) in self.axis_value_maps().iter().enumerate() {
            use core::cmp::Ordering::*;
            let from = axis_value_map.from_coordinate().to_fixed();
            match from.cmp(&coord) {
                Equal => return axis_value_map.to_coordinate().to_fixed(),
                Greater => {
                    if i == 0 {
                        return coord;
                    }
                    let to = axis_value_map.to_coordinate().to_fixed();
                    let prev_from = prev.from_coordinate().to_fixed();
                    let prev_to = prev.to_coordinate().to_fixed();
                    return prev_to + (to - prev_to).mul_div(coord - prev_from, from - prev_from);
                }
                _ => {}
            }
            prev = axis_value_map.clone();
        }
        coord
    }
}

impl<'a> VarSize for SegmentMaps<'a> {
    type Size = u16;

    fn read_len_at(data: FontData, pos: usize) -> Option<usize> {
        Some(data.read_at::<u16>(pos).ok()? as usize * 4)
    }<commit_msg>replace magic number with AxisValueMap::RAW_BYTE_LEN<commit_after>//! The [Axis Variations](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table

use font_types::Tag;

/// 'avar'
pub const TAG: Tag = Tag::new(b"avar");

include!("../../generated/generated_avar.rs");

impl<'a> Avar<'a> {
    // TODO: see comment in Post::traverse_string_data
    #[cfg(feature = "traversal")]
    fn traverse_segment_maps(&self) -> FieldType<'a> {
        FieldType::I8(-42) // meaningless value
    }
}

impl<'a> SegmentMaps<'a> {
    /// Applies the piecewise linear mapping to the specified coordinate.
    pub fn apply(&self, coord: Fixed) -> Fixed {
        let mut prev = AxisValueMap {
            from_coordinate: Default::default(),
            to_coordinate: Default::default(),
        };
        for (i, axis_value_map) in self.axis_value_maps().iter().enumerate() {
            use core::cmp::Ordering::*;
            let from = axis_value_map.from_coordinate().to_fixed();
            match from.cmp(&coord) {
                Equal => return axis_value_map.to_coordinate().to_fixed(),
                Greater => {
                    if i == 0 {
                        return coord;
                    }
                    let to = axis_value_map.to_coordinate().to_fixed();
                    let prev_from = prev.from_coordinate().to_fixed();
                    let prev_to = prev.to_coordinate().to_fixed();
                    return prev_to + (to - prev_to).mul_div(coord - prev_from, from - prev_from);
                }
                _ => {}
            }
            prev = axis_value_map.clone();
        }
        coord
    }
}

impl<'a> VarSize for SegmentMaps<'a> {
    type Size = u16;

    fn read_len_at(data: FontData, pos: usize) -> Option<usize> {
        Some(data.read_at::<u16>(pos).ok()? as usize * AxisValueMap::RAW_BYTE_LEN)
    }
{'fraction_non_alphanumeric': 0.14154013015184383, 'fraction_numerical': 0.004609544468546637, 'mean_word_length': 2.016353229762878, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6513827', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1123, 'n_words': 226}