encodeContent method

  1. @override
List<bool> encodeContent(
  1. String contents, [
  2. EncodeHint? hints
])
override

Encode the contents to bool array expression of one-dimensional barcode. Start code and end code should be included in result, and side margins should not be included.

@param contents barcode contents to encode @return a {@code List

Implementation

@override
List<bool> encodeContent(
  String contents, [
  EncodeHint? hints,
]) {
  if (contents.length < 2) {
    // Can't have a start/end guard, so tentatively add default guards
    contents = _defaultGuard + contents + _defaultGuard;
  } else {
    // Verify input and calculate decoded length.
    final firstChar = contents[0].toUpperCase();
    final lastChar = contents[contents.length - 1].toUpperCase();
    final startsNormal = _startEndChars.contains(firstChar);
    final endsNormal = _startEndChars.contains(lastChar);
    final startsAlt = _altStartEndChars.contains(firstChar);
    final endsAlt = _altStartEndChars.contains(lastChar);
    if (startsNormal) {
      if (!endsNormal) {
        throw ArgumentError('Invalid start/end guards: $contents');
      }
      // else already has valid start/end
    } else if (startsAlt) {
      if (!endsAlt) {
        throw ArgumentError('Invalid start/end guards: $contents');
      }
      // else already has valid start/end
    } else {
      // Doesn't start with a guard
      if (endsNormal || endsAlt) {
        throw ArgumentError('Invalid start/end guards: $contents');
      }
      // else doesn't end with guard either, so add a default
      contents = _defaultGuard + contents + _defaultGuard;
    }
  }

  // The start character and the end character are decoded to 10 length each.
  int resultLength = 20;
  for (int i = 1; i < contents.length - 1; i++) {
    if (MathUtils.isDigit(contents.codeUnitAt(i)) ||
        contents[i] == '-' ||
        contents[i] == r'$') {
      resultLength += 9;
    } else if (_charsWhichAreTenLengthEachAfterDecoded
        .contains(contents[i])) {
      resultLength += 10;
    } else {
      throw ArgumentError("Cannot encode : '${contents[i]}'");
    }
  }
  // A blank is placed between each character.
  resultLength += contents.length - 1;

  final result = List.generate(resultLength, (index) => false);
  int position = 0;
  for (int index = 0; index < contents.length; index++) {
    String c = contents[index].toUpperCase();
    if (index == 0 || index == contents.length - 1) {
      // The start/end chars are not in the CodaBarReader.ALPHABET.
      switch (c) {
        case 'T':
          c = 'A';
          break;
        case 'N':
          c = 'B';
          break;
        case '*':
          c = 'C';
          break;
        case 'E':
          c = 'D';
          break;
      }
    }
    int code = 0;
    for (int i = 0; i < CodaBarReader.alphaBet.length; i++) {
      // Found any, because I checked above.
      if (c.codeUnitAt(0) == CodaBarReader.alphaBet[i]) {
        code = CodaBarReader.characterEncodings[i];
        break;
      }
    }
    bool color = true;
    int counter = 0;
    int bit = 0;
    while (bit < 7) {
      // A character consists of 7 digit.
      result[position] = color;
      position++;
      if (((code >> (6 - bit)) & 1) == 0 || counter == 1) {
        color = !color; // Flip the color.
        bit++;
        counter = 0;
      } else {
        counter++;
      }
    }
    if (index < contents.length - 1) {
      result[position] = false;
      position++;
    }
  }
  return result;
}