encode method

List<bool> encode(
  1. String contents
)

Implementation

List<bool> encode(String contents) {
  memoizedCost = List.generate(4, (index) => List.filled(contents.length, 0));
  minPath =
      List.generate(4, (index) => List.filled(contents.length, Latch.none));

  encodeCharset(contents, Charset.none, 0);

  final patterns = <List<int>>[];
  final checkSum = [0];
  final checkWeight = [1];
  final length = contents.length;
  Charset charset = Charset.none;
  for (int i = 0; i < length; i++) {
    final latch = minPath![charset.index][i];
    switch (latch) {
      case Latch.A:
        charset = Charset.A;
        addPattern(
          patterns,
          i == 0 ? Code128Writer._codeStartA : Code128Writer._codeCodeA,
          checkSum,
          checkWeight,
          i,
        );
        break;
      case Latch.B:
        charset = Charset.B;
        addPattern(
          patterns,
          i == 0 ? Code128Writer._codeStartB : Code128Writer._codeCodeB,
          checkSum,
          checkWeight,
          i,
        );
        break;
      case Latch.C:
        charset = Charset.C;
        addPattern(
          patterns,
          i == 0 ? Code128Writer._codeStartC : Code128Writer._codeCodeC,
          checkSum,
          checkWeight,
          i,
        );
        break;
      case Latch.shift:
        addPattern(patterns, codeShift, checkSum, checkWeight, i);
        break;
      default:
        break;
    }
    if (charset == Charset.C) {
      if (contents.codeUnitAt(i) == Code128Writer._escapeFnc1) {
        addPattern(
          patterns,
          Code128Writer._codeFnc1,
          checkSum,
          checkWeight,
          i,
        );
      } else {
        addPattern(
          patterns,
          int.parse(contents.substring(i, i + 2)),
          checkSum,
          checkWeight,
          i,
        );

        //the algorithm never leads to a single trailing digit in character set C
        assert(i + 1 < length);

        if (i + 1 < length) {
          i++;
        }
      }
    } else {
      // charset A or B
      int patternIndex;
      switch (contents.codeUnitAt(i)) {
        case Code128Writer._escapeFnc1:
          patternIndex = Code128Writer._codeFnc1;
          break;
        case Code128Writer._escapeFnc2:
          patternIndex = Code128Writer._codeFnc2;
          break;
        case Code128Writer._escapeFnc3:
          patternIndex = Code128Writer._codeFnc3;
          break;
        case Code128Writer._escapeFnc4:
          if (charset == Charset.A && latch != Latch.shift ||
              charset == Charset.B && latch == Latch.shift) {
            patternIndex = Code128Writer._codeFnc4A;
          } else {
            patternIndex = Code128Writer._codeFnc4B;
          }
          break;
        default:
          patternIndex = contents.codeUnitAt(i) - 32 /*' '*/;
      }
      if ((charset == Charset.A && latch != Latch.shift ||
              charset == Charset.B && latch == Latch.shift) &&
          patternIndex < 0) {
        patternIndex += 96 /*'`'*/;
      }
      addPattern(patterns, patternIndex, checkSum, checkWeight, i);
    }
  }
  memoizedCost = null;
  minPath = null;
  return Code128Writer.produceResult(patterns, checkSum[0]);
}