Skip to main content

read_fonts/
tables.rs

1//! The various font tables
2
3// Getters shared by the selected views of original and extended tables.
4macro_rules! selected_table_getters {
5    ($($method:ident -> $result:ty),* $(,)?) => {
6        $(#[doc = concat!("Returns `", stringify!($method), "` from the selected table.")]
7        pub fn $method(&self) -> $result {
8            match self {
9                Self::Original(table) => table.$method(),
10                Self::Extended(table) => table.$method(),
11            }
12        })*
13    };
14}
15
16pub mod aat;
17pub mod ankr;
18pub mod avar;
19pub mod base;
20pub mod bitmap;
21pub mod cbdt;
22pub mod cblc;
23pub mod cff;
24pub mod cff2;
25pub mod cmap;
26pub mod colr;
27pub mod cpal;
28pub mod cvar;
29pub mod dsig;
30pub mod ebdt;
31pub mod eblc;
32pub mod feat;
33pub mod fvar;
34pub mod gasp;
35pub mod gdef;
36pub mod glyf;
37pub mod gpos;
38pub mod gsub;
39pub mod gvar;
40pub mod hdmx;
41pub mod head;
42pub mod hhea;
43pub mod hmtx;
44pub mod hvar;
45pub mod kern;
46pub mod kerx;
47pub mod layout;
48pub mod loca;
49pub mod ltag;
50pub mod math;
51pub mod maxp;
52pub mod meta;
53pub mod mort;
54pub mod morx;
55pub mod mvar;
56pub mod name;
57pub mod os2;
58pub mod post;
59pub mod sbix;
60pub mod stat;
61pub mod svg;
62pub mod trak;
63pub mod varc;
64pub mod variations;
65pub mod vhea;
66pub mod vmtx;
67pub mod vorg;
68pub mod vvar;
69
70#[cfg(feature = "ift")]
71pub mod ift;
72
73/// Computes the table checksum for the given data.
74///
75/// See the OpenType [specification](https://learn.microsoft.com/en-us/typography/opentype/spec/otff#calculating-checksums)
76/// for details.
77pub fn compute_checksum(table: &[u8]) -> u32 {
78    let mut sum = 0u32;
79    let mut iter = table.chunks_exact(4);
80    for quad in &mut iter {
81        // this can't fail, and we trust the compiler to avoid a branch
82        let array: [u8; 4] = quad.try_into().unwrap_or_default();
83        sum = sum.wrapping_add(u32::from_be_bytes(array));
84    }
85
86    let rem = match *iter.remainder() {
87        [a] => u32::from_be_bytes([a, 0, 0, 0]),
88        [a, b] => u32::from_be_bytes([a, b, 0, 0]),
89        [a, b, c] => u32::from_be_bytes([a, b, c, 0]),
90        _ => 0,
91    };
92
93    sum.wrapping_add(rem)
94}