Skip to main content

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 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}