1//! Typed font tables
23use 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 {
13fn min_byte_range(&self) -> Range<usize>;
14}
1516#[derive(Clone)]
17/// Typed access to raw table data.
18pub struct TableRef<'a, T> {
19pub(crate) shape: T,
20pub(crate) data: FontData<'a>,
21}
2223impl<'a, T> TableRef<'a, T> {
24/// Resolve the provided offset from the start of this table.
25pub fn resolve_offset<O: Offset, R: FontRead<'a>>(&self, offset: O) -> Result<R, ReadError> {
26 offset.resolve(self.data)
27 }
2829/// Return a reference to this table's raw data.
30 ///
31 /// We use this in the compile crate to resolve offsets.
32pub fn offset_data(&self) -> FontData<'a> {
33self.data
34 }
3536/// 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.
41pub fn shape(&self) -> &T {
42&self.shape
43 }
44}
4546// a blanket impl so that the format is available through a TableRef
47impl<U, T: Format<U>> Format<U> for TableRef<'_, T> {
48const FORMAT: U = T::FORMAT;
49}
5051impl<'a, T: MinByteRange> TableRef<'a, T> {
52/// Return the minimum byte range of this table
53pub fn min_byte_range(&self) -> Range<usize> {
54self.shape.min_byte_range()
55 }
5657/// Return the minimum bytes of this table
58pub fn min_table_bytes(&self) -> &'a [u8] {
59self.offset_data()
60 .as_bytes()
61 .get(self.shape.min_byte_range())
62 .unwrap_or_default()
63 }
64}