def split_font_file(filename, offset_count=97, subtract_value=18226):  # 19439 for font1
    with open(filename, "rb") as f:
        # Read all offsets
        offsets_raw = f.read(offset_count * 2)
        offsets = []
        for i in range(offset_count):
            # Extract 2 bytes for offset
            two_bytes = offsets_raw[i*2:(i+1)*2]
            # Convert to integer (little-endian)
            val = int.from_bytes(two_bytes, byteorder='little')
            # Subtract base value
            val -= subtract_value
            offsets.append(val)

        # Add file end as the final fragment boundary
        f.seek(0, 2)
        file_size = f.tell()
        offsets.append(file_size)

        # Now cut the fragments
        for i in range(offset_count):
            start = offsets[i]
            end = offsets[i+1]
            if start == end:
                # Ignore empty fragment
                continue
            if start < 0 or end > file_size or start > end:
                print(f"Skipping invalid fragment {i}: start={start}, end={end}")
                continue
            f.seek(start)
            data = f.read(end - start)
            out_filename = f"fragment_{i+1:03}.bin"
            with open(out_filename, "wb") as out_f:
                out_f.write(data)
            print(f"Saved {out_filename} ({end - start} bytes)")

if __name__ == "__main__":
    split_font_file("font0")