encodeFast static method

List<bool> encodeFast(
  1. String contents,
  2. EncodeHint? hints,
  3. int forcedCodeSet
)

Implementation

static List<bool> encodeFast(
  String contents,
  EncodeHint? hints,
  int forcedCodeSet,
) {
  final length = contents.length;
  final patterns = <List<int>>[]; // temporary storage for patterns
  int checkSum = 0;
  int checkWeight = 1;
  int codeSet = 0; // selected code (CODE_CODE_B or CODE_CODE_C)
  int position = 0; // position in contents

  while (position < length) {
    //Select code to use
    int newCodeSet;
    if (forcedCodeSet == -1) {
      newCodeSet = _chooseCode(contents, position, codeSet);
    } else {
      newCodeSet = forcedCodeSet;
    }
    //Get the pattern index
    int patternIndex;
    if (newCodeSet == codeSet) {
      // Encode the current character
      // First handle escapes
      switch (contents.codeUnitAt(position)) {
        case _escapeFnc1:
          patternIndex = _codeFnc1;
          break;
        case _escapeFnc2:
          patternIndex = _codeFnc2;
          break;
        case _escapeFnc3:
          patternIndex = _codeFnc3;
          break;
        case _escapeFnc4:
          if (codeSet == _codeCodeA) {
            patternIndex = _codeFnc4A;
          } else {
            patternIndex = _codeFnc4B;
          }
          break;
        default:
          // Then handle normal characters otherwise
          switch (codeSet) {
            case _codeCodeA:
              patternIndex = contents.codeUnitAt(position) - 32 /*   */;
              if (patternIndex < 0) {
                // everything below a space character comes behind the underscore in the code patterns table
                patternIndex += 96 /* ` */;
              }
              break;
            case _codeCodeB:
              patternIndex = contents.codeUnitAt(position) - 32 /*   */;
              break;
            default:
              // CODE_CODE_C
              if (position + 1 == length) {
                // this is the last character, but the encoding is C, which always encodes two characers
                throw ArgumentError(
                  'Bad number of characters for digit only encoding.',
                );
              }
              patternIndex =
                  int.parse(contents.substring(position, position + 2));
              position++; // Also incremented below
              break;
          }
      }
      position++;
    } else {
      // Should we change the current code?
      // Do we have a code set?
      if (codeSet == 0) {
        // No, we don't have a code set
        switch (newCodeSet) {
          case _codeCodeA:
            patternIndex = _codeStartA;
            break;
          case _codeCodeB:
            patternIndex = _codeStartB;
            break;
          default:
            patternIndex = _codeStartC;
            break;
        }
      } else {
        // Yes, we have a code set
        patternIndex = newCodeSet;
      }
      codeSet = newCodeSet;
    }

    // Get the pattern
    patterns.add(Code128Reader.codePatterns[patternIndex]);

    // Compute checksum
    checkSum += patternIndex * checkWeight;
    if (position != 0) {
      checkWeight++;
    }
  }
  return produceResult(patterns, checkSum);
}