// unifont.bin:       character-packed bitmap (16*16, 8*16 chars with the back half with zero)
// unifont_width.bin: bitfield starting from highest bit, representing width of character (0-narrow, 1-wide)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

const (
	Count = 0xffff + 1
	SizeX = 8
	SizeY = 16
)

var (
	Filename = "unifont-13.0.06.hex"

	Data      [Count * SizeX * SizeY * 2]byte
	WidthData [Count / 8]byte
)

func main() {

	if len(os.Args) > 1 {
		Filename = os.Args[1]
	}

	file, err := os.Open(Filename)
	if err != nil {
		panic(err)
	}

	sc := bufio.NewScanner(file)
	for sc.Scan() {

		line := sc.Text()
		parts := strings.Split(line, ":")

		var code int
		fmt.Sscanf(parts[0], "%X", &code)

		//fmt.Printf("Codepoint %02d(%02X): ",code,code)

		if len(parts[1]) == 64 { // Full-Width (16*16)
			WidthData[code/8] |= 1 << (7 - code%8)
		}

		basis := code * SizeX * SizeY * 2 / 8

		for i := 0; i < len(parts[1]); i += 2 {
			var num int
			fmt.Sscanf(parts[1][i:i+2], "%X", &num)

			//fmt.Printf("%02X",byte(num))

			Data[basis+i/2] = byte(num)
		}

		//fmt.Printf("\n")
	}

	os.WriteFile("unifont.bin", Data[:], 0644)
	os.WriteFile("unifont_width.bin", WidthData[:], 0644)
}
