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