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 layout;
32pub mod loca;
33pub mod ltag;
34pub mod maxp;
35pub mod meta;
36pub mod mvar;
37pub mod name;
38pub mod os2;
39pub mod post;
40pub mod postscript;
41pub mod sbix;
42pub mod stat;
43pub mod svg;
44pub mod varc;
45pub mod variations;
46pub mod vhea;
47pub mod vmtx;
48pub mod vorg;
49pub mod vvar;
50
51#[cfg(feature = "ift")]
52pub mod ift;
53
54/// Computes the table checksum for the given data.
55///
56/// See the OpenType [specification](https://learn.microsoft.com/en-us/typography/opentype/spec/otff#calculating-checksums)
57/// for details.
58pub fn compute_checksum(table: &[u8]) -> u32 {
59    let mut sum = 0u32;
60    let mut iter = table.chunks_exact(4);
61    for quad in &mut iter {
62        // this can't fail, and we trust the compiler to avoid a branch
63        let array: [u8; 4] = quad.try_into().unwrap_or_default();
64        sum = sum.wrapping_add(u32::from_be_bytes(array));
65    }
66
67    let rem = match *iter.remainder() {
68        [a] => u32::from_be_bytes([a, 0, 0, 0]),
69        [a, b] => u32::from_be_bytes([a, b, 0, 0]),
70        [a, b, c] => u32::from_be_bytes([a, b, c, 0]),
71        _ => 0,
72    };
73
74    sum.wrapping_add(rem)
75}