def test_load_from_pillow_exceptions():
    """
    Test that exceptions are thrown as appropriate if bitmap_font is asked to
    load a pillow font that is not a PIL.ImageFont file, is damaged or does not
    include the required font metrics data.
    """

    with pytest.raises(SyntaxError) as ex:
        filename = get_reference_file(Path('font').joinpath('hd44780a02.pbm'))
        bitmap_font.load_pillow_font(filename, FONTDATA['mappings'][1])
    assert str(ex.value) == "Not a PIL.ImageFont file"

    with pytest.raises(SyntaxError) as ex:
        filename = get_reference_file(Path('font').joinpath('hd44780a02_nodata.pil'))
        bitmap_font.load_pillow_font(filename, FONTDATA['mappings'][1])
    assert str(ex.value) == "PIL.ImageFont file missing metric data"

    with pytest.raises(SyntaxError) as ex:
        filename = get_reference_file(Path('font').joinpath('hd44780a02_incomplete.pil'))
        bitmap_font.load_pillow_font(filename, FONTDATA['mappings'][1])
    assert str(ex.value) == "PIL.ImageFont file metric data incomplete"

    filename = get_reference_file(Path('font').joinpath('wrong_mode.pil'))
    with pytest.raises(OSError) as ex:
        bitmap_font.load_pillow_font(filename)
    assert str(ex.value) == 'cannot find glyph data file'
Exemple #2
0
def test_display(bits_per_pixel, bgr):
    bytes_per_pixel = bits_per_pixel // 8
    with open(get_reference_file(f"fb_{bits_per_pixel}bpp.raw"), "rb") as fp:
        reference = fp.read()
        if bgr:
            reference = swap_red_and_blue(reference, step=bytes_per_pixel)

    with patch("builtins.open",
               multi_mock_open(SCREEN_RES, str(bits_per_pixel),
                               None)) as fake_open:
        device = linux_framebuffer("/dev/fb1",
                                   framebuffer=full_frame(),
                                   bgr=bgr)

        fake_open.assert_has_calls([call("/dev/fb1", "wb")])
        fake_open.reset_mock()

        with canvas(device, dither=True) as draw:
            draw.rectangle((0, 0, 64, 32), fill="red")
            draw.rectangle((64, 0, 128, 32), fill="yellow")
            draw.rectangle((0, 32, 64, 64), fill="orange")
            draw.rectangle((64, 32, 128, 64), fill="white")

        fake_open.return_value.seek.assert_has_calls(
            [call(n * WIDTH * bytes_per_pixel) for n in range(HEIGHT)])
        fake_open.return_value.write.assert_has_calls([
            call(reference[n:n + (WIDTH * bytes_per_pixel)])
            for n in range(0, len(reference), WIDTH * bytes_per_pixel)
        ])
        fake_open.return_value.flush.assert_called_once()
Exemple #3
0
def test_load_sprite_table_exceptions():
    """
    Test that exceptions are thrown as appropriate if bitmap_font is asked to
    load from a sprite table from a filename that does not exist, is not a
    PIL.Image file, or is damaged.
    """
    with pytest.raises(FileNotFoundError) as ex:
        filename = 'badfile'
        bitmap_font.load_sprite_table(filename, range(16, 256), 5, (5, 8),
                                      (5, 8), FONTDATA['mappings'][1])
    assert str(
        ex.value) == '[Errno 2] No such file or directory: \'{0}\''.format(
            filename)

    with pytest.raises(ValueError) as ex:
        filename = get_reference_file(os.path.join('font', 'hd44780a02.pil'))
        bitmap_font.load_sprite_table(filename, range(16, 256), 5, (5, 8),
                                      (5, 8), FONTDATA['mappings'][1])
    assert str(
        ex.value) == 'File {0} not a valid sprite table'.format(filename)

    with pytest.raises(ValueError) as ex:
        bitmap_font.load_sprite_table(1, range(16, 256), 5, (5, 8), (5, 8),
                                      FONTDATA['mappings'][1])
    assert str(ex.value) == 'Provided image is not an instance of PIL.Image'
def test_load_sprite_table():
    """
    Test loading a font from a sprite_table
    """
    fnt = get_reference_pillow_font('hd44780a02.pil')
    filename = get_reference_file(Path('font').joinpath('hd44780a02.pbm'))
    bmf = bitmap_font.load_sprite_table(filename, range(16, 256), 5, (5, 8), (5, 8), FONTDATA['mappings'][1])

    img1 = make_sprite_table(fnt)
    img2 = make_sprite_table(bmf)

    assert img1 == img2
Exemple #5
0
def test_setter_getter():
    fnt_path = get_reference_file(os.path.join('font', 'hd44780a02.pil'))
    img_path = get_reference_image('character_golden_ratio.png')

    with open(img_path, 'rb') as img:
        fnt = ImageFont.load(fnt_path)
        reference = Image.open(img)
        device = dummy(width=80, height=16, mode="1")
        c = character(device, font=fnt)
        c.text = "1.61803398875\n1.61803398875"
        assert str(c.text) == "1.61803398875\n1.61803398875"

        assert_identical_image(reference, device.image, img_path)
def test_load_sprite_table_exceptions_2():
    """
    Test that exceptions are thrown as appropriate if bitmap_font is asked to
    load from a sprite table from a filename that does not exist, is not a
    PIL.Image file, or is damaged.
    """
    with pytest.raises(ValueError) as ex:
        filename = get_reference_file(Path('font').joinpath('hd44780a02.pil'))
        bitmap_font.load_sprite_table(filename, range(16, 256), 5, (5, 8), (5, 8), FONTDATA['mappings'][1])
    assert str(ex.value) == f'File {filename} not a valid sprite table'

    with pytest.raises(ValueError) as ex:
        bitmap_font.load_sprite_table(1, range(16, 256), 5, (5, 8), (5, 8), FONTDATA['mappings'][1])
    assert str(ex.value) == 'Provided image is not an instance of PIL.Image'
def test_load_from_pillow_font():
    """
    Test the loading of a pillow font from disk by loading the font from bitmap_font
    and PIL.ImageFont, rendering a page of glyphs which each and testing to make
    sure the results are the same.
    """
    fnt = get_reference_pillow_font('hd44780a02.pil')
    filename = get_reference_file(Path('font').joinpath('hd44780a02.pil'))
    bmf = bitmap_font.load_pillow_font(filename)

    img1 = make_sprite_table(fnt)
    img2 = make_sprite_table(bmf)

    assert img1 == img2
def bm_font(request):
    """
    Fixture which loads a ``bitmap_font`` persists it to disk
    The fixture removes the file when it is finished.
    """
    filename = get_reference_file(Path('font').joinpath('hd44780a02.pbm'))
    bmf = bitmap_font.load_sprite_table(filename, range(16, 256), 5, (5, 8),
        (5, 8), FONTDATA['mappings'][1])
    bmf.save('test.bmf')

    def tear_down():
        os.remove('test.bmf')
    request.addfinalizer(tear_down)

    return bmf
def test_mapping():
    """
    Test to make sure that values that have unicode mappings work correctly
    """
    fnt = get_reference_pillow_font('hd44780a02.pil')
    filename = get_reference_file(Path('font').joinpath('hd44780a02.pil'))
    bmf = bitmap_font.load_pillow_font(filename, FONTDATA['mappings'][1])

    size = bmf.getsize('\u0010')
    img1 = Image.new('1', size, 0)
    img2 = Image.new('1', (5, 8), 0)

    drw = ImageDraw.Draw(img1)
    drw.text((0, 0), '\u0010', font=fnt, fill='white')
    drw = ImageDraw.Draw(img2)
    drw.text((0, 0), '\u25b6', font=bmf, fill='white')

    assert img1 == img2
def test_dumps_loads_saves_load(bm_font):
    """
    Test which verifies the loading and restoring of bitmap_fonts
    """
    bmf = bm_font

    data = bmf.dumps()
    bmf2 = bitmap_font.loads(data)
    bmf3 = bitmap_font.load('test.bmf')

    img1 = make_sprite_table(bmf)
    img2 = make_sprite_table(bmf2)
    img3 = make_sprite_table(bmf3)

    assert img1 == img2
    assert img1 == img3

    with pytest.raises(SyntaxError) as ex:
        filename = get_reference_file(Path('font').joinpath('hd44780a02.pil'))
        bitmap_font.load(filename)
    assert str(ex.value) == 'Not a luma.core.bitmap_font file'
Exemple #11
0
def test_init():
    path = get_reference_file(os.path.join('font', 'hd44780a02.pil'))
    fnt = ImageFont.load(path)
    device = dummy(width=80, height=16, mode="1")
    character(device, font=fnt)
    assert device.image == Image.new("1", (80, 16))
Exemple #12
0
# See LICENSE.rst for details.
"""
Tests for the :py:mod:`luma.core.cmdline` module.
"""

import pytest
import errno
import sys

from luma.core import cmdline, error
from luma.core.interface.serial import __all__ as iface_types

from helpers import (get_reference_file, patch, Mock, i2c_error,
                     rpi_gpio_missing, spidev_missing, pyftdi_missing)

test_config_file = get_reference_file('config-test.txt')


class test_spi_opts(object):
    spi_port = 0
    spi_device = 0
    spi_bus_speed = 8000000
    spi_cs_high = False
    spi_transfer_size = 4096
    gpio_data_command = 24
    gpio_reset = 25
    gpio_backlight = 18

    interface = 'spi'