#!/usr/bin/env python3

# Copyright (C) 2019  Max Regan

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import font
import argparse
import string

def emit_license(output):
    output.write(
"""/*
 * Copyright (C) 2019  Max Regan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

""")

def emit_include(output, header_dir):
    if header_dir:
        if header_dir[-1] == '/':
            header_dir = header_dir[:-1]
        header = header_dir + "/font.h"
    else:
        header = "font.h"

    output.write(f"#include \"{header}\"\n\n")

def ceildiv(num, den):
    return (num + (den - 1)) // den

def to_c_name(name):
    return name.replace("-","_")

def emit_bmp(output, font_name, char, glyph, bmp):

    width_bytes = ceildiv(bmp.width, 8)

    output.write(f"// Bitmap for '{char}'\n")

    name = to_c_name(f"{font_name}_{ord(char)}")
    output.write(f"static const uint8_t {name}_bitmap[] = {{\n")
    byte = 0
    for y in range(bmp.height):
        output.write("    ")
        for x in range(bmp.width):
            bit_idx = 7 - (x % 8)
            bit = bmp.pixels[y * bmp.width + x]
            byte |= bit << bit_idx
            if bit_idx == 0 or x == bmp.width - 1:
                output.write('0x{:02x}, '.format(byte))
                byte = 0

        output.write("\n")

    output.write("};\n\n")

    output.write(f"// Glyph data for '{char}'\n")

    output.write(f"static const struct glyph {name}_glyph = {{\n")
    output.write(f"    .width = {bmp.width},\n")
    output.write(f"    .width_bytes = {width_bytes},\n")
    output.write(f"    .height = {bmp.height},\n")
    output.write(f"    .top = {glyph.top},\n")
    output.write(f"    .left = {glyph.left},\n")
    output.write(f"    .bitmap = {name}_bitmap,\n")
    output.write("};\n\n")


def emit_font(output, ff, font_name, charset, height, width):
    c_font_name = to_c_name(font_name)
    output.write(f"const struct font {c_font_name} = {{\n")
    output.write(f"    .name = \"{font_name}\",\n")
    output.write(f"    .height = {height},\n")
    output.write(f"    .width = {width},\n")
    output.write(f"    .glyphs = {{\n")
    for i in range(128):
        if chr(i) in charset:
            name = to_c_name(f"{font_name}_{i}_glyph")
            output.write(f"        [{i}] = &{name},\n")
        else:
            output.write(f"        [{i}] = NULL,\n")
    output.write(f"    }}\n")
    output.write("};\n\n")


def gen_c_font_file(ttf, output, charset, width=None, size=None, header_dir=None, font_name=None):

    ff = font.FixedFont(ttf, width=width, height=size)

    if font_name is None:
        font_name = ff.face.postscript_name.decode('ASCII')

    emit_license(output)
    emit_include(output, header_dir)

    max_advance = None
    max_height = None

    for char in charset:
        bmp = ff.render_character(char)
        glyph = ff.glyph_for_character(char)
        emit_bmp(output, font_name, char, glyph, bmp)
        advance = glyph.advance_width
        if not max_advance or advance > max_advance:
            max_advance = advance
        height = glyph.top
        if not max_height or height > max_height:
            max_height = height
    emit_font(output, ff, font_name, charset, max_height, max_advance)

def gen_h_font_file(ttf, output, header_dir=None, font_name=None):

    if font_name is None:
        font_name = ff.face.postscript_name.decode('ASCII')

    c_font_name = to_c_name(font_name)
    define_name = f"_FONT_{c_font_name.upper()}_H_"

    emit_license(output)
    output.write("\n")
    output.write(f"#ifndef {define_name}\n")
    output.write(f"#define {define_name}\n")
    output.write("\n")
    emit_include(output, header_dir)
    output.write(f"extern const struct font {c_font_name};\n")
    output.write("\n")
    output.write("#endif")


def main():
    parser = argparse.ArgumentParser(description="Fixed-width TrueType C code generator")
    parser.add_argument(dest="filetype", help="The filetype to produce", action="store", type=str, choices=["c","h"])
    parser.add_argument(dest="ttf", help="TrueType font file", action="store", type=str)
    parser.add_argument(dest="output", help="Output file", action="store", type=argparse.FileType('w'))
    parser.add_argument("--chars", "-c", help="Character set. Defaults to all printable ASCII", action="store", type=str,
                        default="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -=_+{}[]:\";'<>,.?/~!@#$%^&*()|\\")
    parser.add_argument("--header-dir",  help="Directory of the font.h header", action="store", type=str)
    parser.add_argument("--name",  help="Font name to use in the generated code", action="store", type=str)

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("--size", "-s", help="Font pitch", action="store", type=int)
    group.add_argument("--width", "-w", help="Font width", action="store", type=int)

    args = parser.parse_args()
    args.chars = list(set(args.chars))
    args.chars.sort()

    if args.filetype == "c":
        gen_c_font_file(args.ttf, args.output, args.chars, width=args.width,
                        size=args.size, header_dir=args.header_dir, font_name=args.name)
    elif args.filetype == "h":
        gen_h_font_file(args.ttf, args.output, header_dir=args.header_dir, font_name=args.name)


if __name__ == "__main__":
    main()
