read_fonts/
table_ref.rs

1//! Typed font tables
2
3use super::read::{FontRead, Format, ReadError};
4use crate::{
5    font_data::FontData,
6    offset::{Offset, ResolveOffset},
7};
8use std::ops::Range;
9/// Return the minimum range of the table bytes
10///
11/// This trait is implemented in generated code, and we use this to get the minimum length/bytes of a table  
12pub trait MinByteRange {
13    fn min_byte_range(&self) -> Range<usize>;
14}
15
16#[derive(Clone)]
17/// Typed access to raw table data.
18pub struct TableRef<'a, T> {
19    pub(crate) shape: T,
20    pub(crate) data: FontData<'a>,
21}
22
23impl<'a, T> TableRef<'a, T> {
24    /// Resolve the provided offset from the start of this table.
25    pub fn resolve_offset<O: Offset, R: FontRead<'a>>(&self, offset: O) -> Result<R, ReadError> {
26        offset.resolve(self.data)
27    }
28
29    /// Return a reference to this table's raw data.
30    ///
31    /// We use this in the compile crate to resolve offsets.
32    pub fn offset_data(&self) -> FontData<'a> {
33        self.data
34    }
35
36    /// Return a reference to the table's 'Shape' struct.
37    ///
38    /// This is a low level implementation detail, but it can be useful in
39    /// some cases where you want to know things about a table's layout, such
40    /// as the byte offsets of specific fields.
41    pub fn shape(&self) -> &T {
42        &self.shape
43    }
44}
45
46// a blanket impl so that the format is available through a TableRef
47impl<U, T: Format<U>> Format<U> for TableRef<'_, T> {
48    const FORMAT: U = T::FORMAT;
49}
50
51impl<'a, T: MinByteRange> TableRef<'a, T> {
52    /// Return the minimum byte range of this table
53    pub fn min_byte_range(&self) -> Range<usize> {
54        self.shape.min_byte_range()
55    }
56
57    /// Return the minimum bytes of this table
58    pub fn min_table_bytes(&self) -> &'a [u8] {
59        self.offset_data()
60            .as_bytes()
61            .get(self.shape.min_byte_range())
62            .unwrap_or_default()
63    }
64}