#! /usr/bin/python3
# -*- coding: utf-8 -*-

# The MIT License (MIT)
#
# Copyright (c) 2016 Peter Hinch
# Copyright (c) 2017 Brian Cappello
#
# 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.

# Test programs for the utility font_to_py and for font files created by it.
# The test of most general use is test_font which enables a string to be
# output to the REPL using a font file created by this utility.

import sys
import os
from importlib import import_module
from font_to_py import Font, write_font


# Utility functions

def render_string(myfont, string):
    height = myfont.HEIGHT
    for row in range(height):
        for char in string:
            is_lhmap, data, _, width = myfont.get_char(char)
            if is_lhmap:
                render_row_lhmap(data, row, height, width)
            else:
                render_row_lvmap(data, row, height, width)
        print()


def render_row_lhmap(data, row, height, width):
    lines = []
    y = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            y = data[data_i + 1]
            while len(lines) <= y:
                lines.append([])
            for i in range(num_lines):
                lstart = data_i + 2 + (i * 2)
                x = data[lstart]
                length = data[lstart + 1]
                lines[y].append((x, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            y += 1
            data_i += 1
        if y == row:
            break
    while len(lines) < height:
        lines.append([])

    x = 0
    for line in lines[row]:
        while x < line[0]:
            print('.', end='')
            x += 1
        while x < line[0] + line[1]:
            print('#', end='')
            x += 1
    while x < width:
        print('.', end='')
        x += 1


def render_row_lvmap(data, row, height, width):
    lines = []
    x = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            lines.append([])
            x = data[data_i + 1]
            for i in range(num_lines):
                lstart = data_i + 2 + (i * 2)
                y = data[lstart]
                length = data[lstart + 1]
                lines[x].append((y, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            x += 1
            data_i += 1
    while len(lines) < width:
        lines.append([])

    for x in range(width):
        char = '.'
        for line in lines[x]:
            if line[0] <= row < line[0] + line[1]:
                char = '#'
        print(char, end='')


# Render a string to REPL using a specified Python font file
def test_font(fontfile, string):
    if fontfile in sys.modules:
        del sys.modules[fontfile]  # force reload
    myfont = import_module(fontfile)
    render_string(myfont, string)


# Create font file, render a string to REPL using it
def test_file(fontfile, height, string, *, minchar=32, maxchar=126, fixed=False):
    charset = [chr(x) for x in range(minchar, maxchar+1)]
    if not write_font('myfont.py', fontfile, height, fixed, charset):
        print('Failed to create font file.')
        return

    if 'myfont' in sys.modules:
        del sys.modules['myfont']  # force reload
    import myfont

    render_string(myfont, string)

    os.unlink('myfont.py')


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Test font_to_py')
    parser.add_argument('fontfile', type=str, help='path to Python font module generated by font_to_py, or path to a TTF/OTF font file')
    parser.add_argument('-s', '--size', dest='size', default=20, help='font size to generate (only applies for TTF/OTF files)')
    parser.add_argument('test_string', nargs='?', default='ABCD.efghij 123!', help='string to render with the given font')

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()

    fontfile = args.fontfile.lower()
    is_system_font_file = fontfile.endswith('.ttf') or fontfile.endswith('.otf')

    if is_system_font_file:
        if not os.path.exists(args.fontfile):
            exit('Sorry, could not find a font file at {}.'.format(args.fontfile))
        print('Running test_file with source font {}:'.format(args.fontfile))
        test_file(args.fontfile, int(args.size), args.test_string)
    else:
        if '.py' in args.fontfile:
            args.fontfile = args.fontfile[:-3]
        print('Running test_font with py font module {}:'.format(args.fontfile))
        test_font(args.fontfile, args.test_string)

1cb12f7536261ddad9e47e321661e3ebdbb09bf0
<mask token>


def render_row_lhmap(data, row, height, width):
    lines = []
    y = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            y = data[data_i + 1]
            while len(lines) <= y:
                lines.append([])
            for i in range(num_lines):
                lstart = data_i + 2 + i * 2
                x = data[lstart]
                length = data[lstart + 1]
                lines[y].append((x, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            y += 1
            data_i += 1
        if y == row:
            break
    while len(lines) < height:
        lines.append([])
    x = 0
    for line in lines[row]:
        while x < line[0]:
            print('.', end='')
            x += 1
        while x < line[0] + line[1]:
            print('#', end='')
            x += 1
    while x < width:
        print('.', end='')
        x += 1


def render_row_lvmap(data, row, height, width):
    lines = []
    x = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            lines.append([])
            x = data[data_i + 1]
            for i in range(num_lines):
                lstart = data_i + 2 + i * 2
                y = data[lstart]
                length = data[lstart + 1]
                lines[x].append((y, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            x += 1
            data_i += 1
    while len(lines) < width:
        lines.append([])
    for x in range(width):
        char = '.'
        for line in lines[x]:
            if line[0] <= row < line[0] + line[1]:
                char = '#'
        print(char, end='')


def test_font(fontfile, string):
    if fontfile in sys.modules:
        del sys.modules[fontfile]
    myfont = import_module(fontfile)
    render_string(myfont, string)


<mask token>

<mask token>


def render_string(myfont, string):
    height = myfont.HEIGHT
    for row in range(height):
        for char in string:
            is_lhmap, data, _, width = myfont.get_char(char)
            if is_lhmap:
                render_row_lhmap(data, row, height, width)
            else:
                render_row_lvmap(data, row, height, width)
        print()


def render_row_lhmap(data, row, height, width):
    lines = []
    y = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            y = data[data_i + 1]
            while len(lines) <= y:
                lines.append([])
            for i in range(num_lines):
                lstart = data_i + 2 + i * 2
                x = data[lstart]
                length = data[lstart + 1]
                lines[y].append((x, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            y += 1
            data_i += 1
        if y == row:
            break
    while len(lines) < height:
        lines.append([])
    x = 0
    for line in lines[row]:
        while x < line[0]:
            print('.', end='')
            x += 1
        while x < line[0] + line[1]:
            print('#', end='')
            x += 1
    while x < width:
        print('.', end='')
        x += 1


def render_row_lvmap(data, row, height, width):
    lines = []
    x = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            lines.append([])
            x = data[data_i + 1]
            for i in range(num_lines):
                lstart = data_i + 2 + i * 2
                y = data[lstart]
                length = data[lstart + 1]
                lines[x].append((y, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            x += 1
            data_i += 1
    while len(lines) < width:
        lines.append([])
    for x in range(width):
        char = '.'
        for line in lines[x]:
            if line[0] <= row < line[0] + line[1]:
                char = '#'
        print(char, end='')


def test_font(fontfile, string):
    if fontfile in sys.modules:
        del sys.modules[fontfile]
    myfont = import_module(fontfile)
    render_string(myfont, string)


def test_file(fontfile, height, string, *, minchar=32, maxchar=126, fixed=False
    ):
    charset = [chr(x) for x in range(minchar, maxchar + 1)]
    if not write_font('myfont.py', fontfile, height, fixed, charset):
        print('Failed to create font file.')
        return
    if 'myfont' in sys.modules:
        del sys.modules['myfont']
    import myfont
    render_string(myfont, string)
    os.unlink('myfont.py')


<mask token>

<mask token>


def render_string(myfont, string):
    height = myfont.HEIGHT
    for row in range(height):
        for char in string:
            is_lhmap, data, _, width = myfont.get_char(char)
            if is_lhmap:
                render_row_lhmap(data, row, height, width)
            else:
                render_row_lvmap(data, row, height, width)
        print()


def render_row_lhmap(data, row, height, width):
    lines = []
    y = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            y = data[data_i + 1]
            while len(lines) <= y:
                lines.append([])
            for i in range(num_lines):
                lstart = data_i + 2 + i * 2
                x = data[lstart]
                length = data[lstart + 1]
                lines[y].append((x, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            y += 1
            data_i += 1
        if y == row:
            break
    while len(lines) < height:
        lines.append([])
    x = 0
    for line in lines[row]:
        while x < line[0]:
            print('.', end='')
            x += 1
        while x < line[0] + line[1]:
            print('#', end='')
            x += 1
    while x < width:
        print('.', end='')
        x += 1


def render_row_lvmap(data, row, height, width):
    lines = []
    x = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            lines.append([])
            x = data[data_i + 1]
            for i in range(num_lines):
                lstart = data_i + 2 + i * 2
                y = data[lstart]
                length = data[lstart + 1]
                lines[x].append((y, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            x += 1
            data_i += 1
    while len(lines) < width:
        lines.append([])
    for x in range(width):
        char = '.'
        for line in lines[x]:
            if line[0] <= row < line[0] + line[1]:
                char = '#'
        print(char, end='')


def test_font(fontfile, string):
    if fontfile in sys.modules:
        del sys.modules[fontfile]
    myfont = import_module(fontfile)
    render_string(myfont, string)


def test_file(fontfile, height, string, *, minchar=32, maxchar=126, fixed=False
    ):
    charset = [chr(x) for x in range(minchar, maxchar + 1)]
    if not write_font('myfont.py', fontfile, height, fixed, charset):
        print('Failed to create font file.')
        return
    if 'myfont' in sys.modules:
        del sys.modules['myfont']
    import myfont
    render_string(myfont, string)
    os.unlink('myfont.py')


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Test font_to_py')
    parser.add_argument('fontfile', type=str, help=
        'path to Python font module generated by font_to_py, or path to a TTF/OTF font file'
        )
    parser.add_argument('-s', '--size', dest='size', default=20, help=
        'font size to generate (only applies for TTF/OTF files)')
    parser.add_argument('test_string', nargs='?', default=
        'ABCD.efghij 123!', help='string to render with the given font')
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)
    args = parser.parse_args()
    fontfile = args.fontfile.lower()
    is_system_font_file = fontfile.endswith('.ttf') or fontfile.endswith('.otf'
        )
    if is_system_font_file:
        if not os.path.exists(args.fontfile):
            exit('Sorry, could not find a font file at {}.'.format(args.
                fontfile))
        print('Running test_file with source font {}:'.format(args.fontfile))
        test_file(args.fontfile, int(args.size), args.test_string)
    else:
        if '.py' in args.fontfile:
            args.fontfile = args.fontfile[:-3]
        print('Running test_font with py font module {}:'.format(args.fontfile)
            )
        test_font(args.fontfile, args.test_string)

import sys
import os
from importlib import import_module
from font_to_py import Font, write_font


def render_string(myfont, string):
    height = myfont.HEIGHT
    for row in range(height):
        for char in string:
            is_lhmap, data, _, width = myfont.get_char(char)
            if is_lhmap:
                render_row_lhmap(data, row, height, width)
            else:
                render_row_lvmap(data, row, height, width)
        print()


def render_row_lhmap(data, row, height, width):
    lines = []
    y = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            y = data[data_i + 1]
            while len(lines) <= y:
                lines.append([])
            for i in range(num_lines):
                lstart = data_i + 2 + i * 2
                x = data[lstart]
                length = data[lstart + 1]
                lines[y].append((x, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            y += 1
            data_i += 1
        if y == row:
            break
    while len(lines) < height:
        lines.append([])
    x = 0
    for line in lines[row]:
        while x < line[0]:
            print('.', end='')
            x += 1
        while x < line[0] + line[1]:
            print('#', end='')
            x += 1
    while x < width:
        print('.', end='')
        x += 1


def render_row_lvmap(data, row, height, width):
    lines = []
    x = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            lines.append([])
            x = data[data_i + 1]
            for i in range(num_lines):
                lstart = data_i + 2 + i * 2
                y = data[lstart]
                length = data[lstart + 1]
                lines[x].append((y, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            x += 1
            data_i += 1
    while len(lines) < width:
        lines.append([])
    for x in range(width):
        char = '.'
        for line in lines[x]:
            if line[0] <= row < line[0] + line[1]:
                char = '#'
        print(char, end='')


def test_font(fontfile, string):
    if fontfile in sys.modules:
        del sys.modules[fontfile]
    myfont = import_module(fontfile)
    render_string(myfont, string)


def test_file(fontfile, height, string, *, minchar=32, maxchar=126, fixed=False
    ):
    charset = [chr(x) for x in range(minchar, maxchar + 1)]
    if not write_font('myfont.py', fontfile, height, fixed, charset):
        print('Failed to create font file.')
        return
    if 'myfont' in sys.modules:
        del sys.modules['myfont']
    import myfont
    render_string(myfont, string)
    os.unlink('myfont.py')


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Test font_to_py')
    parser.add_argument('fontfile', type=str, help=
        'path to Python font module generated by font_to_py, or path to a TTF/OTF font file'
        )
    parser.add_argument('-s', '--size', dest='size', default=20, help=
        'font size to generate (only applies for TTF/OTF files)')
    parser.add_argument('test_string', nargs='?', default=
        'ABCD.efghij 123!', help='string to render with the given font')
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)
    args = parser.parse_args()
    fontfile = args.fontfile.lower()
    is_system_font_file = fontfile.endswith('.ttf') or fontfile.endswith('.otf'
        )
    if is_system_font_file:
        if not os.path.exists(args.fontfile):
            exit('Sorry, could not find a font file at {}.'.format(args.
                fontfile))
        print('Running test_file with source font {}:'.format(args.fontfile))
        test_file(args.fontfile, int(args.size), args.test_string)
    else:
        if '.py' in args.fontfile:
            args.fontfile = args.fontfile[:-3]
        print('Running test_font with py font module {}:'.format(args.fontfile)
            )
        test_font(args.fontfile, args.test_string)

#! /usr/bin/python3
# -*- coding: utf-8 -*-

# The MIT License (MIT)
#
# Copyright (c) 2016 Peter Hinch
# Copyright (c) 2017 Brian Cappello
#
# 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.

# Test programs for the utility font_to_py and for font files created by it.
# The test of most general use is test_font which enables a string to be
# output to the REPL using a font file created by this utility.

import sys
import os
from importlib import import_module
from font_to_py import Font, write_font


# Utility functions

def render_string(myfont, string):
    height = myfont.HEIGHT
    for row in range(height):
        for char in string:
            is_lhmap, data, _, width = myfont.get_char(char)
            if is_lhmap:
                render_row_lhmap(data, row, height, width)
            else:
                render_row_lvmap(data, row, height, width)
        print()


def render_row_lhmap(data, row, height, width):
    lines = []
    y = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            y = data[data_i + 1]
            while len(lines) <= y:
                lines.append([])
            for i in range(num_lines):
                lstart = data_i + 2 + (i * 2)
                x = data[lstart]
                length = data[lstart + 1]
                lines[y].append((x, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            y += 1
            data_i += 1
        if y == row:
            break
    while len(lines) < height:
        lines.append([])

    x = 0
    for line in lines[row]:
        while x < line[0]:
            print('.', end='')
            x += 1
        while x < line[0] + line[1]:
            print('#', end='')
            x += 1
    while x < width:
        print('.', end='')
        x += 1


def render_row_lvmap(data, row, height, width):
    lines = []
    x = 0
    data_i = 0
    while data_i < len(data):
        num_lines = data[data_i]
        if num_lines:
            lines.append([])
            x = data[data_i + 1]
            for i in range(num_lines):
                lstart = data_i + 2 + (i * 2)
                y = data[lstart]
                length = data[lstart + 1]
                lines[x].append((y, length))
            data_i = lstart + 2
        else:
            lines.append(lines[-1])
            x += 1
            data_i += 1
    while len(lines) < width:
        lines.append([])

    for x in range(width):
        char = '.'
        for line in lines[x]:
            if line[0] <= row < line[0] + line[1]:
                char = '#'
        print(char, end='')


# Render a string to REPL using a specified Python font file
def test_font(fontfile, string):
    if fontfile in sys.modules:
        del sys.modules[fontfile]  # force reload
    myfont = import_module(fontfile)
    render_string(myfont, string)


# Create font file, render a string to REPL using it
def test_file(fontfile, height, string, *, minchar=32, maxchar=126, fixed=False):
    charset = [chr(x) for x in range(minchar, maxchar+1)]
    if not write_font('myfont.py', fontfile, height, fixed, charset):
        print('Failed to create font file.')
        return

    if 'myfont' in sys.modules:
        del sys.modules['myfont']  # force reload
    import myfont

    render_string(myfont, string)

    os.unlink('myfont.py')


if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Test font_to_py')
    parser.add_argument('fontfile', type=str, help='path to Python font module generated by font_to_py, or path to a TTF/OTF font file')
    parser.add_argument('-s', '--size', dest='size', default=20, help='font size to generate (only applies for TTF/OTF files)')
    parser.add_argument('test_string', nargs='?', default='ABCD.efghij 123!', help='string to render with the given font')

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()

    fontfile = args.fontfile.lower()
    is_system_font_file = fontfile.endswith('.ttf') or fontfile.endswith('.otf')

    if is_system_font_file:
        if not os.path.exists(args.fontfile):
            exit('Sorry, could not find a font file at {}.'.format(args.fontfile))
        print('Running test_file with source font {}:'.format(args.fontfile))
        test_file(args.fontfile, int(args.size), args.test_string)
    else:
        if '.py' in args.fontfile:
            args.fontfile = args.fontfile[:-3]
        print('Running test_font with py font module {}:'.format(args.fontfile))
        test_font(args.fontfile, args.test_string)
