def test_type_of_object_passed_to_image_function(img_function):
    """
    Test the type of object that is passed to the image function during non-native qr-printing.

    The type should be PIL.Image
    """
    d = Dummy()
    d.qr("LoremIpsum")
    args, kwargs = img_function.call_args
    assert isinstance(args[0], Image.Image)
Exemplo n.º 2
0
class EscPosPrint(BasePrint):
    '''
    print data to a ESC/POS printer.
    '''
    font = 'A'

    def __init__(self, encode=None, **kwargs):
        BasePrint.__init__(self, **kwargs)
        from escpos.printer import Dummy
        self.dummy = Dummy()
        if encode:
            self.dummy.codepage = encode
        self.dummy.hw('init')
        self.texts = []

    def _get_char_width(self, c):
        w = BasePrint._get_char_width(self, c)
        if self.font == 'A':
            w = w * 12
        elif self.font == 'B':
            if w == 1:
                w = 9
            else:
                w = 16
        return w

    def print_text(self, text):
        self.texts.append(text)
        self.dummy.text(text)

    def _set_with_node(self, node):
        font = get_node_attr(node, 'font', '')
        align = get_node_attr(node, 'align', '')
        size = get_node_attr_int(node, 'size', 0)
        text_type = 'B' if get_node_attr(node, 'bold', '') else '' + 'U' if get_node_attr(node, 'underline', '') else ''
        setd = {}
        if font:
            setd['font'] = font
            self.font = font
        if align:
            setd['align'] = align
        if size:
            setd['width'] = size
            setd['height'] = size
        if text_type:
            setd['text_type'] = text_type
        if setd:
            self.dummy.set(**setd)

    def _handle_node(self, node):
        tag = node.tagName.lower()
        if tag in ['table', 'text', 'td', 'tr']:
            self._set_with_node(node)
        BasePrint._handle_node(self, node)

    def handle_cut(self, node):
        self.dummy.cut()

    def handle_image(self, node):
        src = get_node_attr(node, 'src', '')
        path = self.get_url_to_path(src)
        if path:
            from PIL import Image
            img = Image.open(path)
            img = align_image(img, self.width, get_node_attr(node, 'align', 'left'))
            self.dummy.image(img)

    def handle_img(self, node):
        self.handle_image(node)

    def handle_qrcode(self, node):
        native = get_node_attr(node, 'native', 'False').upper() == 'TRUE'
        code = get_node_text(node)
        size = get_node_attr_int(node, 'size', 5)
        if not native:
            img = get_qrcode_image(code, size=size)
            img = align_image(img, self.width, get_node_attr(node, 'align', 'left'))
            self.dummy.image(img)
        else:
            self.dummy.qr(code, size=size, native=native)

    def handle_barcode(self, node):
        native = get_node_attr(node, 'native', 'False').upper() == 'TRUE'
        codetype = get_node_attr(node, 'type', 'EAN13').lower()
        code = get_node_text(node)
        if not native:
            import qrcode
            img = get_barcode_image(code, codetype=codetype)
            img = align_image(img, self.width, get_node_attr(node, 'align', 'left'))
            self.dummy.image(img)
        else:
            self.dummy.barcode(code, codetype.upper(), font=get_node_attr(node, 'font', 'A').upper(), pos=get_node_attr(node, 'pos', 'BELOW').upper(), function_type=get_node_attr(node, 'function', 'B').upper())

    def handle_beep(self, node):
        self.dummy._raw(b'\x1B\x42\x01\x01')

    def raw_send(self, msg):
        self.dummy._raw(msg)

    def get_data(self):
        return self.dummy.output

    def get_text(self):
        return ''.join(self.texts)