# Path: stoqdrivers/utils.py
# def encode_text(text, encoding):
#     """ Converts the string 'text' to encoding 'encoding' and optionally
#     normalizes the string (currently only for ascii)
# 
#     @param text:       text to convert
#     @type text:        str
#     @param encoding:   encoding to use
#     @type text:        str
#     @returns:          converted text
#     """
#     if encoding == "ascii":
#         text = unicodedata.normalize("NFKD", text)
#     # If we use text.encode we will get this sometimes:
#     # TypeError: 'abicomp' encoder returned 'str' instead of 'bytes';
#     #   use codecs.encode() to encode to arbitrary types
#     text = codecs.encode(text, encoding, "ignore")
#     # Only do the bellow conversion if the encoding above worked
#     if isinstance(text, bytes):
#         # Use bytes2str instead of decode or otherwise we would encode
#         # the bytes in unicode. This way we will still keep them in the
#         # encoding that we want (since SerialBase.write will do the reversal
#         # operation, str2bytes) even though we will get some strange
#         # characters in the "unicode version" of the string
#         text = bytes2str(text)
#     return text
# 
# GRAPHICS_8BITS = 8
# 
# GRAPHICS_24BITS = 24
# 
# def matrix2graphics(graphics_api, matrix, max_cols, multiplier=1, centralized=True):
#     if graphics_api not in (GRAPHICS_8BITS, GRAPHICS_24BITS):
#         raise ValueError("Graphics api %s not supported" % (graphics_api, ))
# 
#     sub_len = int(graphics_api / multiplier)
# 
#     for i in range(0, len(matrix), sub_len):
#         bytes_ = []
#         sub = matrix[i:i + sub_len]
#         if len(sub) < sub_len:
#             sub.extend([[False] * len(matrix[0])] * (sub_len - len(sub)))
# 
#         for j in range(len(matrix[0])):
#             bits = []
#             for bit in sub:
#                 bits.extend([bit[j]] * multiplier)
# 
#             if graphics_api == GRAPHICS_8BITS:
#                 # The 3 is to compensate for the fact that each pixel is
#                 # 3x larger vertically than horizontally
#                 bytes_.extend([bits2byte(bits)] * 3 * multiplier)
#             elif graphics_api == GRAPHICS_24BITS:
#                 splitted_bytes = []
#                 for k in range(0, 24, 8):
#                     splitted_bytes.append(bits2byte(bits[k: k + 8]))
#                 bytes_.extend(splitted_bytes * multiplier)
#             else:
#                 raise AssertionError
# 
#         if centralized:
#             diff = max_cols - len(bytes_)
#             if diff:
#                 bytes_ = ([0] * int(diff / 2)) + bytes_
# 
#         divide_len_by = graphics_api / 8
#         yield ''.join(chr(b) for b in bytes_), int(len(bytes_) / divide_len_by)


from stoqdrivers.utils import encode_text, GRAPHICS_8BITS, GRAPHICS_24BITS, matrix2graphics
    CHARSET_CMD = {
        'cp850': ESC + '\x74\x02',  # Multilingual

        'cp437': ESC + '\x74\x00',  # USA: Standard Europe
        'cp932': ESC + '\x74\x01',  # Japanese Katakana
        'cp860': ESC + '\x74\x03',  # Portuguese
        'cp863': ESC + '\x74\x04',  # Canadian-French
        'cp865': ESC + '\x74\x05',  # Nordic
        'latin1': ESC + '\x74\x06',  # Simplified Kanji, Hirakana
        'cp737': ESC + '\x74\x07',  # Simplified Kanji
        'cp862': ESC + '\x74\x08',  # Simplified Kanji
        'cp1252': ESC + '\x74\x11',  # Western European Windows Code Set
        'cp866': ESC + '\x74\x12',  # Cirillic #2
        'cp852': ESC + '\x74\x13',  # Latin 2
        'cp858': ESC + '\x74\x14',  # Euro

    }

    #: How many line feeds should be done before cutting the paper
    cut_line_feeds = 4

    #: The default font
    default_font = FONT_CONDENSED

    #: The maximum number of characters that fit a line
    max_characters = 64

    #: The maximum number of characters that fit a barcode
    max_barcode_characters = 27


# Path: stoqdrivers/utils.py
# def encode_text(text, encoding):
#     """ Converts the string 'text' to encoding 'encoding' and optionally
#     normalizes the string (currently only for ascii)
# 
#     @param text:       text to convert
#     @type text:        str
#     @param encoding:   encoding to use
#     @type text:        str
#     @returns:          converted text
#     """
#     if encoding == "ascii":
#         text = unicodedata.normalize("NFKD", text)
#     # If we use text.encode we will get this sometimes:
#     # TypeError: 'abicomp' encoder returned 'str' instead of 'bytes';
#     #   use codecs.encode() to encode to arbitrary types
#     text = codecs.encode(text, encoding, "ignore")
#     # Only do the bellow conversion if the encoding above worked
#     if isinstance(text, bytes):
#         # Use bytes2str instead of decode or otherwise we would encode
#         # the bytes in unicode. This way we will still keep them in the
#         # encoding that we want (since SerialBase.write will do the reversal
#         # operation, str2bytes) even though we will get some strange
#         # characters in the "unicode version" of the string
#         text = bytes2str(text)
#     return text
# 
# GRAPHICS_8BITS = 8
# 
# GRAPHICS_24BITS = 24
# 
# def matrix2graphics(graphics_api, matrix, max_cols, multiplier=1, centralized=True):
#     if graphics_api not in (GRAPHICS_8BITS, GRAPHICS_24BITS):
#         raise ValueError("Graphics api %s not supported" % (graphics_api, ))
# 
#     sub_len = int(graphics_api / multiplier)
# 
#     for i in range(0, len(matrix), sub_len):
#         bytes_ = []
#         sub = matrix[i:i + sub_len]
#         if len(sub) < sub_len:
#             sub.extend([[False] * len(matrix[0])] * (sub_len - len(sub)))
# 
#         for j in range(len(matrix[0])):
#             bits = []
#             for bit in sub:
#                 bits.extend([bit[j]] * multiplier)
# 
#             if graphics_api == GRAPHICS_8BITS:
#                 # The 3 is to compensate for the fact that each pixel is
#                 # 3x larger vertically than horizontally
#                 bytes_.extend([bits2byte(bits)] * 3 * multiplier)
#             elif graphics_api == GRAPHICS_24BITS:
#                 splitted_bytes = []
#                 for k in range(0, 24, 8):
#                     splitted_bytes.append(bits2byte(bits[k: k + 8]))
#                 bytes_.extend(splitted_bytes * multiplier)
#             else:
#                 raise AssertionError
# 
#         if centralized:
#             diff = max_cols - len(bytes_)
#             if diff:
#                 bytes_ = ([0] * int(diff / 2)) + bytes_
# 
#         divide_len_by = graphics_api / 8
#         yield ''.join(chr(b) for b in bytes_), int(len(bytes_) / divide_len_by)


# Path: stoqdrivers/escpos.py
from stoqdrivers.utils import encode_text, GRAPHICS_8BITS, GRAPHICS_24BITS, matrix2graphics

    CHARSET_CMD = {
        'cp850': ESC + '\x74\x02',  # Multilingual

        'cp437': ESC + '\x74\x00',  # USA: Standard Europe
        'cp932': ESC + '\x74\x01',  # Japanese Katakana
        'cp860': ESC + '\x74\x03',  # Portuguese
        'cp863': ESC + '\x74\x04',  # Canadian-French
        'cp865': ESC + '\x74\x05',  # Nordic
        'latin1': ESC + '\x74\x06',  # Simplified Kanji, Hirakana
        'cp737': ESC + '\x74\x07',  # Simplified Kanji
        'cp862': ESC + '\x74\x08',  # Simplified Kanji
        'cp1252': ESC + '\x74\x11',  # Western European Windows Code Set
        'cp866': ESC + '\x74\x12',  # Cirillic #2
        'cp852': ESC + '\x74\x13',  # Latin 2
        'cp858': ESC + '\x74\x14',  # Euro

    }

    #: How many line feeds should be done before cutting the paper
    cut_line_feeds = 4

    #: The default font
    default_font = FONT_CONDENSED

    #: The maximum number of characters that fit a line
    max_characters = 64

    #: The maximum number of characters that fit a barcode
    max_barcode_characters = 27


    GRAPHICS_API = GRAPHICS_8BITS