# Path: luma/core/device.py
# class dummy(device):
#     """
#     Pseudo-device that acts like a physical display, except that it does nothing
#     other than retain a copy of the displayed image. It is mostly useful for
#     testing. Supports 24-bit color depth.
#     """
# 
#     def __init__(self, width=128, height=64, rotate=0, mode="RGB", **kwargs):
#         super(dummy, self).__init__(serial_interface=noop())
#         self.capabilities(width, height, rotate, mode)
#         self.image = None
# 
#     def display(self, image):
#         """
#         Takes a :py:mod:`PIL.Image` and makes a copy of it for later
#         use/inspection.
# 
#         :param image: Image to display.
#         :type image: PIL.Image.Image
#         """
#         assert image.size == self.size
# 
#         self.image = self.preprocess(image).copy()
# 
# Path: luma/core/virtual.py
# class character(object):
#     """
#     Abstraction that wraps a device, this class provides a ``text`` property
#     which can be used to set and get a text value allowing the device to be
#     treated as a character style display such as the HD44780 LCD
# 
#     If the device is actually a character style device, be careful to provide
#     a font that adheres to the pixel dimensions of the display.
# 
#     :param device: A device instance.
#     :param font: The font to be used to paint the characters within the ``text``
#         property.  If the device contains a font (e.g. hd44780, ws0010) it will
#         be used as the default if no font is provided.
#     :type font: `PIL.ImageFont` object
#     :param undefined: The default character to substitute when an unrenderable
#         character is supplied to the text property.
#     :type undefined: char
# 
#     .. versionadded:: 1.15.0
#     """
# 
#     def __init__(self, device, font=None, undefined="_"):
#         self.device = device
#         self._undefined = undefined
# 
#         self.font = font if font else device.font if hasattr(device, 'font') else None
#         assert self.font, 'No font available'
#         self.text = ''
# 
#     @property
#     def text(self):
#         """
#         Returns the current state of the text buffer. This may not reflect
#         accurately what is displayed on the device if the font does
#         not have a symbol for a requested text value.
#         """
#         return self._text_buffer
# 
#     @text.setter
#     def text(self, value):
#         """
#         Updates the display with the given value.
# 
#         :param value: The value to render onto the device. Any characters which
#             cannot be rendered will be converted into the ``undefined``
#             character supplied in the constructor. Newline characters '\n' work
#             as expected but no other control characters (e.g. \r) are honored.
#         :type value: str
#         """
#         self._text_buffer = observable(mutable_string(value),
#             observer=self._flush)
# 
#     def _flush(self, buf):
#         # Replace any characters that are not in the font with the undefined character
#         buf = ''.join([i if i == '\n' or self.font.getsize(i)[0] > 0 else self._undefined for i in buf])
# 
#         # Draw text onto display's image using the provided font
#         with canvas(self.device) as draw:
#             # Place text
#             draw.text((0, 0), buf, fill='white', font=self.font, spacing=0)


from pathlib import Path
from PIL import Image, ImageFont
from luma.core.device import dummy
from luma.core.virtual import character
from helpers import get_reference_file, get_reference_image, assert_identical_image
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Richard Hull and contributors
# See LICENSE.rst for details.





def test_init():
    path = get_reference_file(Path('font').joinpath('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))


def test_setter_getter():
    fnt_path = get_reference_file(Path('font').joinpath('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)

# Path: luma/core/device.py
# class dummy(device):
#     """
#     Pseudo-device that acts like a physical display, except that it does nothing
#     other than retain a copy of the displayed image. It is mostly useful for
#     testing. Supports 24-bit color depth.
#     """
# 
#     def __init__(self, width=128, height=64, rotate=0, mode="RGB", **kwargs):
#         super(dummy, self).__init__(serial_interface=noop())
#         self.capabilities(width, height, rotate, mode)
#         self.image = None
# 
#     def display(self, image):
#         """
#         Takes a :py:mod:`PIL.Image` and makes a copy of it for later
#         use/inspection.
# 
#         :param image: Image to display.
#         :type image: PIL.Image.Image
#         """
#         assert image.size == self.size
# 
#         self.image = self.preprocess(image).copy()
# 
# Path: luma/core/virtual.py
# class character(object):
#     """
#     Abstraction that wraps a device, this class provides a ``text`` property
#     which can be used to set and get a text value allowing the device to be
#     treated as a character style display such as the HD44780 LCD
# 
#     If the device is actually a character style device, be careful to provide
#     a font that adheres to the pixel dimensions of the display.
# 
#     :param device: A device instance.
#     :param font: The font to be used to paint the characters within the ``text``
#         property.  If the device contains a font (e.g. hd44780, ws0010) it will
#         be used as the default if no font is provided.
#     :type font: `PIL.ImageFont` object
#     :param undefined: The default character to substitute when an unrenderable
#         character is supplied to the text property.
#     :type undefined: char
# 
#     .. versionadded:: 1.15.0
#     """
# 
#     def __init__(self, device, font=None, undefined="_"):
#         self.device = device
#         self._undefined = undefined
# 
#         self.font = font if font else device.font if hasattr(device, 'font') else None
#         assert self.font, 'No font available'
#         self.text = ''
# 
#     @property
#     def text(self):
#         """
#         Returns the current state of the text buffer. This may not reflect
#         accurately what is displayed on the device if the font does
#         not have a symbol for a requested text value.
#         """
#         return self._text_buffer
# 
#     @text.setter
#     def text(self, value):
#         """
#         Updates the display with the given value.
# 
#         :param value: The value to render onto the device. Any characters which
#             cannot be rendered will be converted into the ``undefined``
#             character supplied in the constructor. Newline characters '\n' work
#             as expected but no other control characters (e.g. \r) are honored.
#         :type value: str
#         """
#         self._text_buffer = observable(mutable_string(value),
#             observer=self._flush)
# 
#     def _flush(self, buf):
#         # Replace any characters that are not in the font with the undefined character
#         buf = ''.join([i if i == '\n' or self.font.getsize(i)[0] > 0 else self._undefined for i in buf])
# 
#         # Draw text onto display's image using the provided font
#         with canvas(self.device) as draw:
#             # Place text
#             draw.text((0, 0), buf, fill='white', font=self.font, spacing=0)


# Path: tests/test_character.py
from pathlib import Path
from PIL import Image, ImageFont
from luma.core.device import dummy
from luma.core.virtual import character
from helpers import get_reference_file, get_reference_image, assert_identical_image

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Richard Hull and contributors
# See LICENSE.rst for details.





def test_init():
    path = get_reference_file(Path('font').joinpath('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))


def test_setter_getter():
    fnt_path = get_reference_file(Path('font').joinpath('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")