text
stringlengths
1
93.6k
assert f.tell() == 2 + 128 + 256
write_madspack(ff_name, [f])
def write_glyph(f, ff_name, h, n):
iname = "{}.{:03}.png".format(ff_name, n)
if not os.path.exists(iname):
h.char_widths[n] = 0
h.char_offsets[n] = f.tell()
else:
img = Image.open(iname)
width,height = img.size
if h.max_width < width:
h.max_width = width
if h.max_height < height:
h.max_height = height
h.char_widths[n] = width
h.char_offsets[n] = f.tell()
y = 0
while y < height:
x = 0
eol = False
while not eol:
byte = 0b00000000
for shift in (6,4,2,0):
ind = get_col_index(img, img.getpixel((x,y)))
byte = byte | (ind << shift)
x += 1
if x == width:
eol = True
break
write_uint8(f, byte)
y += 1
def write_ff_header(f, h):
write_uint8(f, h.max_height)
write_uint8(f, h.max_width)
assert f.tell() == 2
for b in h.char_widths[1:]:
write_uint8(f, b)
write_uint8(f, 0)
assert f.tell() == 2 + 128
for o in h.char_offsets[1:]:
write_uint16(f, o)
write_uint16(f, 0)
assert f.tell() == 2 + 128 + 256
def read_ff(ff_name):
"""
FF file: 1 part MADSPACK
header
max_height: 1 byte
max_width: 1 byte
glyphs widths: 128 bytes
glyphs offsets: 2 x 128 byte
glyphs
...
"""
check_ext(ff_name, '.FF')
parts = read_madspack(ff_name)
save_madspack(ff_name, parts)
f = parts[0]
# Header
h = Record()
h.max_height = read_uint8(f)
h.max_width = read_uint8(f)
# glyphs width in pixels
# glyph height is max_height
# null has width 0
h.char_widths = [0] + [read_uint8(f) for _ in range(127)]
read_uint8(f) # alignment to 128
# space occupied by glyph is
# math.ceil((glyph_width * max_height) / 4.0) # or is is rounded per line ?