Skip to main content

read_fonts/tables/
instance_record.rs

1//! An fvar InstanceRecord
2
3use types::{BigEndian, Fixed, FixedSize, NameId};
4
5use crate::{ComputeSize, FontData, FontRead, ReadArgs, ReadError};
6
7/// The [InstanceRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#instancerecord)
8#[derive(Clone, Debug)]
9pub struct InstanceRecord<'a> {
10    /// The name ID for entries in the 'name' table that provide subfamily names for this instance.
11    pub subfamily_name_id: NameId,
12    /// Reserved for future use — set to 0.
13    pub flags: u16,
14    /// The coordinates array for this instance.
15    pub coordinates: &'a [BigEndian<Fixed>],
16    /// Optional. The name ID for entries in the 'name' table that provide PostScript names for this instance.
17    pub post_script_name_id: Option<NameId>,
18}
19
20crate::impl_font_read_at!(InstanceRecord<'a>);
21
22impl ReadArgs for InstanceRecord<'_> {
23    type Args = (u16, u16);
24}
25
26impl<'a> InstanceRecord<'a> {
27    /// Parse an instance record with a known axis_count and instance_size
28    pub fn read(
29        data: FontData<'a>,
30        axis_count: u16,
31        instance_size: u16,
32    ) -> Result<Self, ReadError> {
33        let args = (axis_count, instance_size);
34        Self::read_with_args(data, args)
35    }
36}
37
38impl<'a> FontRead<'a> for InstanceRecord<'a> {
39    fn read_with_args(data: FontData<'a>, args: Self::Args) -> Result<Self, ReadError> {
40        let axis_count = args.0 as usize;
41        let instance_size = args.1 as usize;
42        let mut cursor = data.cursor();
43        let subfamily_name_id = cursor.read()?;
44        let flags = cursor.read()?;
45        let coordinates = cursor.read_array(axis_count)?;
46        // Size of common fields (subfamily_name_id and flags) plus axis coordinates.
47        let common_byte_len = u16::RAW_BYTE_LEN * 2 + (axis_count * Fixed::RAW_BYTE_LEN);
48        // The instance contains a post_script_name_id field if the instance size is greater than
49        // or equal to the common size plus the 2 bytes for the optional field.
50        let has_post_script_name_id = instance_size >= common_byte_len + u16::RAW_BYTE_LEN;
51        let post_script_name_id = if has_post_script_name_id {
52            let id: NameId = cursor.read()?;
53            // From <https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#instancerecord>:
54            // "If the value is 0xFFFF, then the value is ignored, and no
55            // PostScript name equivalent is provided for the instance."
56            (id.to_u16() != 0xFFFF).then_some(id)
57        } else {
58            None
59        };
60        Ok(InstanceRecord {
61            subfamily_name_id,
62            flags,
63            coordinates,
64            post_script_name_id,
65        })
66    }
67}
68
69impl ComputeSize for InstanceRecord<'_> {
70    #[inline]
71    fn compute_size(args: (u16, u16)) -> Result<usize, ReadError> {
72        Ok(args.1 as usize)
73    }
74}