How to calculate the checksumAdjustment(ms, apple) field in the head table of a TTF file? Here is my method of calculation.
package main
import (
"encoding/binary"
"os"
)
func CalcCheckSum(data []byte) uint32 {
var sum uint32
step := 3
for _, c := range data {
sum += uint32(c) << ((step & 3) * 8)
step--
}
return sum
}
func main() {
var bs []byte
bs, _ = os.ReadFile("myFont.ttf")
// Handle TableHeader
// ...
// Handle TableRecord
// ...
var headOffset uint32 // Assume it is provided by `TableRecord["head"].Offset` and it's correct.
binary.BigEndian.PutUint32(bs[headOffset+8:headOffset+12], 0) // 8~12 is checksumAdjustment: https://learn.microsoft.com/en-us/typography/opentype/spec/head
sum := CalcCheckSum(bs)
checksumAdjustment := 0xB1B0AFBA - sum
binary.BigEndian.PutUint32(bs[headOffset+8:headOffset+12], checksumAdjustment)
}
In some fonts (I am sorry for not being able to provide details due to licensing issues), I used this calculation method. When I submit the resulting font file for verification to FontVal-2.1.2
It complains that head.ChecksumAdjustment calculation is incorrect. I'm going crazy, please help me.
you can't directly read all data content for calculation.
Reason: If some tables need to be padded with zeros, then there will be a difference between doing it separately and doing it all at once. Doing it all at once will only add zeros at the end of the data, but doing it separately will check each piece of data to determine if zeros need to be added, resulting in differences. see below:
playground
so you should do it separately:
The following code for detailed process.