use crate::parser::{Fixed, FromData, LazyArray16, Offset, Offset16, Offset32, Stream};
#[derive(Clone, Copy, Debug)]
struct TrackTableRecord {
value: Fixed,
name_id: u16,
offset: Offset16, }
impl FromData for TrackTableRecord {
const SIZE: usize = 8;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let mut s = Stream::new(data);
Some(TrackTableRecord {
value: s.read::<Fixed>()?,
name_id: s.read::<u16>()?,
offset: s.read::<Offset16>()?,
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct Track<'a> {
pub value: f32,
pub name_index: u16,
pub values: LazyArray16<'a, i16>,
}
#[derive(Clone, Copy, Default, Debug)]
pub struct Tracks<'a> {
data: &'a [u8], records: LazyArray16<'a, TrackTableRecord>,
sizes_count: u16,
}
impl<'a> Tracks<'a> {
pub fn get(&self, index: u16) -> Option<Track<'a>> {
let record = self.records.get(index)?;
let mut s = Stream::new(self.data.get(record.offset.to_usize()..)?);
Some(Track {
value: record.value.0,
values: s.read_array16::<i16>(self.sizes_count)?,
name_index: record.name_id,
})
}
pub fn len(&self) -> u16 {
self.records.len()
}
pub fn is_empty(&self) -> bool {
self.records.is_empty()
}
}
impl<'a> IntoIterator for Tracks<'a> {
type Item = Track<'a>;
type IntoIter = TracksIter<'a>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
TracksIter {
tracks: self,
index: 0,
}
}
}
#[allow(missing_debug_implementations)]
pub struct TracksIter<'a> {
tracks: Tracks<'a>,
index: u16,
}
impl<'a> Iterator for TracksIter<'a> {
type Item = Track<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.tracks.len() {
self.index += 1;
self.tracks.get(self.index - 1)
} else {
None
}
}
}
#[derive(Clone, Copy, Default, Debug)]
pub struct TrackData<'a> {
pub tracks: Tracks<'a>,
pub sizes: LazyArray16<'a, Fixed>,
}
impl<'a> TrackData<'a> {
fn parse(offset: usize, data: &'a [u8]) -> Option<Self> {
let mut s = Stream::new_at(data, offset)?;
let tracks_count = s.read::<u16>()?;
let sizes_count = s.read::<u16>()?;
let size_table_offset = s.read::<Offset32>()?; let tracks = Tracks {
data,
records: s.read_array16::<TrackTableRecord>(tracks_count)?,
sizes_count,
};
let sizes = {
let mut s = Stream::new_at(data, size_table_offset.to_usize())?;
s.read_array16::<Fixed>(sizes_count)?
};
Some(TrackData { tracks, sizes })
}
}
#[derive(Clone, Copy, Debug)]
pub struct Table<'a> {
pub horizontal: TrackData<'a>,
pub vertical: TrackData<'a>,
}
impl<'a> Table<'a> {
pub fn parse(data: &'a [u8]) -> Option<Self> {
let mut s = Stream::new(data);
let version = s.read::<u32>()?;
if version != 0x00010000 {
return None;
}
let format = s.read::<u16>()?;
if format != 0 {
return None;
}
let hor_offset = s.read::<Option<Offset16>>()?;
let ver_offset = s.read::<Option<Offset16>>()?;
s.skip::<u16>(); let horizontal = if let Some(offset) = hor_offset {
TrackData::parse(offset.to_usize(), data)?
} else {
TrackData::default()
};
let vertical = if let Some(offset) = ver_offset {
TrackData::parse(offset.to_usize(), data)?
} else {
TrackData::default()
};
Some(Table {
horizontal,
vertical,
})
}
}