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,
]) {
  int length = contents.length;
  if (length > 80) {
    throw ArgumentError(
      'Requested contents should be less than 80 digits long, but got $length',
    );
  }

  for (int i = 0; i < length; i++) {
    final indexInString = Code39Reader.alphabetString.indexOf(contents[i]);
    if (indexInString < 0) {
      contents = _tryToConvertToExtendedMode(contents);
      length = contents.length;
      if (length > 80) {
        throw ArgumentError(
          'Requested contents should be less than 80 digits long, '
          'but got $length (extended full ASCII mode)',
        );
      }
      break;
    }
  }

  final widths = List.filled(9, 0);
  final codeWidth = 24 + 1 + (13 * length);
  final result = List.filled(codeWidth, false);
  _toIntArray(Code39Reader.asteriskEncoding, widths);
  int pos = OneDimensionalCodeWriter.appendPattern(result, 0, widths, true);
  final narrowWhite = <int>[1];
  pos +=
      OneDimensionalCodeWriter.appendPattern(result, pos, narrowWhite, false);
  //append next character to byte matrix
  for (int i = 0; i < length; i++) {
    final indexInString = Code39Reader.alphabetString.indexOf(contents[i]);
    _toIntArray(Code39Reader.characterEnncodings[indexInString], widths);
    pos += OneDimensionalCodeWriter.appendPattern(result, pos, widths, true);
    pos += OneDimensionalCodeWriter.appendPattern(
      result,
      pos,
      narrowWhite,
      false,
    );
  }
  _toIntArray(Code39Reader.asteriskEncoding, widths);
  OneDimensionalCodeWriter.appendPattern(result, pos, widths, true);
  return result;
}