read_fonts/
tables.rs

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