zprinter.py
def _get_width(self, string, max_width_mm, enforce_quiet_zones=True):
        """
        Try to determine the length of a barcode from the Code 128 format
        """
        index = 0
        mode = None
        barcode_length_char = len(string)
        barcode_width_dot = 2 * SYMBOL  # Checksum and Stop character widths
        max_width_dot = int(max_width_mm / DOT_MM)

        if enforce_quiet_zones is True:
            max_width_dot -= (2 * QUIET_ZONE)
        else:
            # A minimal quiet zone of 10 on each side is enforced at all
            # times. It may not be possible to scan this.
            max_width_dot -= 20

        while index < barcode_length_char:
            # Extract the next two consecutive characters
            char1 = string[index]
            try:
                char2 = string[index+1]
            except IndexError:
                # IndexError: index+1 is out of range
                char2 = ""

            # Check if the consecutive characters can be combined into
            # a Code Set C symbol.
            if char1 in ALPHA_RANGE and char2 in ALPHA_RANGE:
                pair = True
                index += 2
            else:
                pair = False
                index += 1

            # If the scanner is not using Code Set C but `char1` and
            # `char2` are a numeric pair the cost is two character
            # widths for the code set transition and character pair.
            if mode != "C" and pair is True:
                delta_width_dot = 2 * SYMBOL
                mode = "C"

            # If the scanner is using Code Set C and `char1` and `char2`
            # are a numeric pair the cost is a single character width.
            elif pair is True:
                delta_width_dot = SYMBOL

            # If the scanner is not using Code Set A or Code Set B and
            # `char1` and `char2` are not a numeric pair the cost is two
            # character widths for the code set transition and `char1`
            # only.
            elif mode != "AB":
                delta_width_dot = 2 * SYMBOL
                mode = "AB"

            # If the scanner is using Code Set A or Code Set B and
            # `char1` and `char2` are not a numeric pair the cost is a
            # single character width for `char1` only.
            else:
                delta_width_dot = SYMBOL

            # Update the determined barcode width
            new_barcode_width_dot = barcode_width_dot + delta_width_dot

            if new_barcode_width_dot == max_width_dot and index >= barcode_length_char:
                barcode_width_dot = new_barcode_width_dot
                break
            elif new_barcode_width_dot >= max_width_dot:
                # print("WARNING: '{}' is too long so has been shortened.".format(string))
                string = string[:int(index - (delta_width_dot / SYMBOL))]
                break

            barcode_width_dot = new_barcode_width_dot

        return(string, barcode_width_dot)

        Try to determine the length of a barcode from the Code 128 format
        
Try to determine the length of a barcode from the Code 128 format
Try to determine the length of a barcode from the Code 128 format
def _get_width(self, string, max_width_mm, enforce_quiet_zones=True):
        index = 0
        mode = None
        barcode_length_char = len(string)
        barcode_width_dot = 2 * SYMBOL  
        max_width_dot = int(max_width_mm / DOT_MM)
        if enforce_quiet_zones is True:
            max_width_dot -= (2 * QUIET_ZONE)
        else:
            max_width_dot -= 20
        while index < barcode_length_char:
            char1 = string[index]
            try:
                char2 = string[index+1]
            except IndexError:
                char2 = ""
            if char1 in ALPHA_RANGE and char2 in ALPHA_RANGE:
                pair = True
                index += 2
            else:
                pair = False
                index += 1
            if mode != "C" and pair is True:
                delta_width_dot = 2 * SYMBOL
                mode = "C"
            elif pair is True:
                delta_width_dot = SYMBOL
            elif mode != "AB":
                delta_width_dot = 2 * SYMBOL
                mode = "AB"
            else:
                delta_width_dot = SYMBOL
            new_barcode_width_dot = barcode_width_dot + delta_width_dot
            if new_barcode_width_dot == max_width_dot and index >= barcode_length_char:
                barcode_width_dot = new_barcode_width_dot
                break
            elif new_barcode_width_dot >= max_width_dot:
                string = string[:int(index - (delta_width_dot / SYMBOL))]
                break
            barcode_width_dot = new_barcode_width_dot
        return(string, barcode_width_dot)
def _get_width ( self , string , max_width_mm , enforce_quiet_zones = True ) : index = 0 mode = None barcode_length_char = len ( string ) barcode_width_dot = 2 * SYMBOL max_width_dot = int ( max_width_mm / DOT_MM ) if enforce_quiet_zones is True : max_width_dot -= ( 2 * QUIET_ZONE ) else : max_width_dot -= 20 while index < barcode_length_char : char1 = string [ index ] try : char2 = string [ index + 1 ] except IndexError : char2 = "" if char1 in ALPHA_RANGE and char2 in ALPHA_RANGE : pair = True index += 2 else : pair = False index += 1 if mode != "C" and pair is True : delta_width_dot = 2 * SYMBOL mode = "C" elif pair is True : delta_width_dot = SYMBOL elif mode != "AB" : delta_width_dot = 2 * SYMBOL mode = "AB" else : delta_width_dot = SYMBOL new_barcode_width_dot = barcode_width_dot + delta_width_dot if new_barcode_width_dot == max_width_dot and index >= barcode_length_char : barcode_width_dot = new_barcode_width_dot break elif new_barcode_width_dot >= max_width_dot : string = string [ : int ( index - ( delta_width_dot / SYMBOL ) ) ] break barcode_width_dot = new_barcode_width_dot return ( string , barcode_width_dot )
Try to determine the length of a barcode from the Code 128 format
Try to determine the length of a barcode from the Code 128 format
"""
        Try to determine the length of a barcode from the Code 128 format
        """ # Checksum and Stop character widths # A minimal quiet zone of 10 on each side is enforced at all # times. It may not be possible to scan this. # Extract the next two consecutive characters # IndexError: index+1 is out of range # Check if the consecutive characters can be combined into # a Code Set C symbol. # If the scanner is not using Code Set C but `char1` and # `char2` are a numeric pair the cost is two character # widths for the code set transition and character pair. # If the scanner is using Code Set C and `char1` and `char2` # are a numeric pair the cost is a single character width. # If the scanner is not using Code Set A or Code Set B and # `char1` and `char2` are not a numeric pair the cost is two # character widths for the code set transition and `char1` # only. # If the scanner is using Code Set A or Code Set B and # `char1` and `char2` are not a numeric pair the cost is a # single character width for `char1` only. # Update the determined barcode width # print("WARNING: '{}' is too long so has been shortened.".format(string))
self
string
max_width_mm
enforce_quiet_zones