#!/usr/bin/env python
import argparse
import colours
import fontdata

parser = argparse.ArgumentParser()
parser.add_argument('font', help='Text file describing the font')
parser.add_argument('start', help='First codepoint')
parser.add_argument('end', help='Last codepoint')
parsed = parser.parse_args()

with open(parsed.font) as f:
	char_width, char_height, atlas = fontdata.parse(f)

if parsed.start[0:2] == 'U+':
	codepoint_start = int(parsed.start[2:], 16)
else:
	codepoint_start = ord(parsed.start)
if parsed.end[0:2] == 'U+':
	codepoint_end = int(parsed.end[2:], 16)
else:
	codepoint_end = ord(parsed.end)


start_row = codepoint_start // 16
end_row = codepoint_end // 16

filename = f'{codepoint_start:04X}-{codepoint_end:04X}.ppm'

glyphs = {}

with open(filename, 'rb') as f:
	header = []
	while len(header) < 3:
		line = f.readline()
		if line[0:1] == b'#': continue
		header.append(line)
	assert header[0] == b'P6\n'
	width, height = map(int, header[1].split())
	assert header[2] == b'255\n'

	assert width % char_width == 0
	assert height % char_height == 0

	for row in range(height // char_height):
		for y in range(char_height):
			for column in range(width // char_width):
				codepoint = (start_row + row) * 16 + column
				if codepoint < codepoint_start or codepoint_end < codepoint:
					f.read(char_width * 3)
					continue
				pixelrow = []
				for x in range(char_width):
					colour = f.read(3)
					pixelrow.append(colour == colours.fg)
				if codepoint not in glyphs:
					glyphs[codepoint] = []
				glyphs[codepoint].append(pixelrow)

print(f'Updating range: U+{codepoint_start:04X} - U+{codepoint_end:04X}')

replaced = []
for codepoint, glyph in glyphs.items():
	if codepoint in atlas and atlas[codepoint] != glyph:
		replaced.append(codepoint)
	atlas[codepoint] = glyph

replaced = ', '.join(f'U+{c:04X}' for c in replaced)
print(f'Replacing: {replaced}')

with open(parsed.font, 'w') as f:
	fontdata.serialize(f, char_width, char_height, atlas)