Class: Fontisan::Utilities::ChecksumCalculator
- Inherits:
-
Object
- Object
- Fontisan::Utilities::ChecksumCalculator
- Defined in:
- lib/fontisan/utilities/checksum_calculator.rb
Overview
ChecksumCalculator provides stateless utility methods for calculating font file checksums.
This class implements the TrueType/OpenType checksum algorithm which sums all uint32 values in a file. The checksum is used to verify file integrity and calculate the checksumAdjustment value stored in the ‘head’ table.
Class Method Summary collapse
-
.calculate_adjustment(file_checksum) ⇒ Integer
Calculate the checksum adjustment value for the ‘head’ table.
-
.calculate_checksum_from_io(io) ⇒ Integer
private
Calculate checksum from an IO object.
-
.calculate_file_checksum(file_path) ⇒ Integer
Calculate the checksum of an entire font file.
-
.calculate_table_checksum(data) ⇒ Integer
Calculate checksum for raw table data.
Class Method Details
.calculate_adjustment(file_checksum) ⇒ Integer
Calculate the checksum adjustment value for the ‘head’ table.
The checksum adjustment is stored at offset 8 in the ‘head’ table and is calculated as: CHECKSUM_ADJUSTMENT_MAGIC - file_checksum. This value ensures that the checksum of the entire font file equals the magic number.
55 56 57 |
# File 'lib/fontisan/utilities/checksum_calculator.rb', line 55 def self.calculate_adjustment(file_checksum) (Constants::CHECKSUM_ADJUSTMENT_MAGIC - file_checksum) & 0xFFFFFFFF end |
.calculate_checksum_from_io(io) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Calculate checksum from an IO object.
Reads the IO stream in 4-byte chunks and calculates the uint32 checksum. This is the core checksum algorithm implementation.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/fontisan/utilities/checksum_calculator.rb', line 84 def self.calculate_checksum_from_io(io) io.rewind sum = 0 until io.eof? # Read 4 bytes at a time bytes = io.read(4) break if bytes.nil? || bytes.empty? # Pad with zeros if less than 4 bytes bytes += "\0" * (4 - bytes.length) if bytes.length < 4 # Convert to uint32 (network byte order, big-endian) value = bytes.unpack1("N") sum = (sum + value) & 0xFFFFFFFF end sum end |
.calculate_file_checksum(file_path) ⇒ Integer
Calculate the checksum of an entire font file.
The checksum is calculated by summing all uint32 (4-byte) values in the file. Files that are not multiples of 4 bytes are padded with zeros. The sum is masked to 32 bits to prevent overflow.
36 37 38 39 40 |
# File 'lib/fontisan/utilities/checksum_calculator.rb', line 36 def self.calculate_file_checksum(file_path) File.open(file_path, "rb") do |file| calculate_checksum_from_io(file) end end |
.calculate_table_checksum(data) ⇒ Integer
Calculate checksum for raw table data.
This method calculates the checksum for a binary string of table data. Used when creating WOFF files or validating table integrity.
70 71 72 73 74 |
# File 'lib/fontisan/utilities/checksum_calculator.rb', line 70 def self.calculate_table_checksum(data) io = StringIO.new(data) io.set_encoding(Encoding::BINARY) calculate_checksum_from_io(io) end |