# 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
        # Levels = (L, M, Q, H) => (48, 49, 50, 51)
        self.write(GS + '(k\x03\x00%s%s%s' % (chr(49), chr(69), chr(48)))

        # Store data in symbols storage area:
        # 1D 28 6B pl ph cn(49) fn(80) m(48) data
        bytes_len = 3 + len(code)
        pl = chr(bytes_len & 0xff)
        ph = chr((bytes_len >> 8 & 0xff))
        cmd = GS + '(k%s%s%s%s%s%s' % (pl, ph, chr(49), chr(80), chr(48), code)
        self.write(cmd)

        # Print - 1D 28 6B pl(3) ph(0) cn(49) fn(81) m(48)
        self.write(GS + '(k\x03\x00%s%s%s' % (chr(49), chr(81), chr(48)))

    def cut_paper(self):
        """ Performs a paper cutting. """
        # FIXME: Ensure the paper is safely out of the paper-cutter before
        #        executing the cut.
        self.print_inline(b'\n' * self.cut_line_feeds)
        self.write(self.PAPER_FULL_CUT)

    def print_matrix(self, matrix, api=None, linefeed=True, multiplier=None):
        multiplier = multiplier or self.GRAPHICS_MULTIPLIER
        if api is None:
            api = self.GRAPHICS_API

        max_cols = self.GRAPHICS_MAX_COLS[api]
        cmd = self.GRAPHICS_CMD[api]

        # Check that the given image fits properly with the api, and if not, try to increase the api

# 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

        # Levels = (L, M, Q, H) => (48, 49, 50, 51)
        self.write(GS + '(k\x03\x00%s%s%s' % (chr(49), chr(69), chr(48)))

        # Store data in symbols storage area:
        # 1D 28 6B pl ph cn(49) fn(80) m(48) data
        bytes_len = 3 + len(code)
        pl = chr(bytes_len & 0xff)
        ph = chr((bytes_len >> 8 & 0xff))
        cmd = GS + '(k%s%s%s%s%s%s' % (pl, ph, chr(49), chr(80), chr(48), code)
        self.write(cmd)

        # Print - 1D 28 6B pl(3) ph(0) cn(49) fn(81) m(48)
        self.write(GS + '(k\x03\x00%s%s%s' % (chr(49), chr(81), chr(48)))

    def cut_paper(self):
        """ Performs a paper cutting. """
        # FIXME: Ensure the paper is safely out of the paper-cutter before
        #        executing the cut.
        self.print_inline(b'\n' * self.cut_line_feeds)
        self.write(self.PAPER_FULL_CUT)

    def print_matrix(self, matrix, api=None, linefeed=True, multiplier=None):
        multiplier = multiplier or self.GRAPHICS_MULTIPLIER
        if api is None:
            api = self.GRAPHICS_API

        max_cols = self.GRAPHICS_MAX_COLS[api]
        cmd = self.GRAPHICS_CMD[api]

        # Check that the given image fits properly with the api, and if not, try to increase the api

        line, line_len = next(matrix2graphics(api, matrix, max_cols, multiplier))