docs: def add_codewords(matrix, codewords, version):
      """\
      Adds the codewords (data and error correction) to the provided matrix.
  
      ISO/IEC 18004:2015(E) -- 7.7.3 Symbol character placement (page 46)
  
      :param matrix: The matrix to add the codewords into.
      :param codewords: Sequence of ints
      """
      matrix_size = len(matrix)
      is_micro = version < 1
      # Necessary for M1 and M3: The algorithm would start at the upper right
      # corner, see <https://github.com/heuer/segno/issues/36>
      inc = 0 if version not in (consts.VERSION_M1, consts.VERSION_M3) else 2
      idx = 0  # Pointer to the current codeword
      # ISO/IEC 18004:2015(E) - page 48
      # [...] An alternative method for placement in the symbol [...] is to regard
      # the interleaved codeword sequence as a single bit stream, which is placed
      # (starting with the most significant bit) in the two-module wide columns
      # alternately upwards and downwards from the right to left of the symbol.
      # [...]
      for right in range(matrix_size - 1, 0, -2):
          if not is_micro and right <= 6:
              right -= 1
          for vertical in range(matrix_size):
              for z in range(2):
                  j = right - z
                  upwards = ((right + inc) & 2) == 0
                  if not is_micro:
                      upwards ^= j < 6
                  i = (matrix_size - 1 - vertical) if upwards else vertical
                  if matrix[i][j] == 0x2 and idx < len(codewords):
                      matrix[i][j] = codewords[idx]
                      idx += 1
      if idx != len(codewords):  # pragma: no cover
          raise QRCodeError('Internal error: Adding codewords to matrix failed. '
                            'Added {0} of {1} codewords'.format(idx, len(codewords)))