Created
February 1, 2018 23:59
-
-
Save NotAFile/688791aefbfcf747c1d15b3e4f146e46 to your computer and use it in GitHub Desktop.
Quick and dirty reader for HMZK font files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fileinput | |
import sys | |
import struct | |
MAIN_SECTION_OFFSET = 0x10 | |
def bin_picture(num): | |
fmt = "{:016b}".format(num) | |
fmt = fmt.replace("0", ".") | |
fmt = fmt.replace("1", "#") | |
return fmt | |
def dump_font(f, offset, count): | |
f.seek(offset) # for english: 250 chinese: 0x3884 | |
for char in range(count): | |
for i in range(16): | |
data = f.read(2) | |
data = struct.unpack(">H", data) | |
print("0x{0:04x} {1}".format(f.tell(), bin_picture(data[0]))) | |
print() | |
print("end of file!") | |
def read_chars(f, offset, count): | |
f.seek(offset) # 15 for english | |
for i in range(count): | |
data = f.read(2) | |
char_id = struct.unpack(">H", data)[0] | |
print("0x{:04x} | 0x{:04x} {}".format(f.tell(), char_id, data.decode("utf-16-le"))) | |
with open(sys.argv[1], "rb") as f: | |
magic = f.read(4) | |
if magic != b"HMZK": | |
print("This file does not appear to be a HMZK font!") | |
sys.exit(1) | |
f.seek(MAIN_SECTION_OFFSET - 2) | |
length = struct.unpack("<H", f.read(2))[0] | |
print("font contains {} chars".format(length // 2)) | |
print() | |
print("character section:") | |
print(" start: 0x{0:04x}".format(MAIN_SECTION_OFFSET)) | |
print(" len: 0x{0:04x} ({0:d} bytes)".format(length)) | |
print() | |
print("font section:") | |
print(" start: 0x{0:04x}".format(MAIN_SECTION_OFFSET + length)) | |
print(" len: 0x{0:04x} ({0:d} bytes)".format(length * 2 * 16)) | |
print() | |
# read_chars(f, MAIN_SECTION_OFFSET, length // 2) | |
dump_font(f, MAIN_SECTION_OFFSET + length, length // 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment