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,
]) {
  final length = contents.length;
  switch (length) {
    case 7:
      // No check digit present, calculate it and add it
      int check;
      try {
        check = UPCEANReader.getStandardUPCEANChecksum(
          UPCEReader.convertUPCEtoUPCA(contents),
        );
      } on FormatsException catch (fe) {
        //
        throw ArgumentError(fe);
      }
      contents += check.toString();
      break;
    case 8:
      try {
        if (!UPCEANReader.checkStandardUPCEANChecksum(
          UPCEReader.convertUPCEtoUPCA(contents),
        )) {
          throw ArgumentError('Contents do not pass checksum');
        }
      } on FormatsException catch (_) {
        //
        throw ArgumentError('Illegal contents');
      }
      break;
    default:
      throw ArgumentError(
        'Requested contents should be 7 or 8 digits long, but got $length',
      );
  }

  OneDimensionalCodeWriter.checkNumeric(contents);

  final firstDigit = int.parse(contents[0]);
  if (firstDigit != 0 && firstDigit != 1) {
    throw ArgumentError('Number system must be 0 or 1');
  }

  final checkDigit = int.parse(contents[7]);
  final parities =
      UPCEReader.numsysAndCheckDigitPatterns[firstDigit][checkDigit];
  final result = List.filled(_codeWidth, false);

  int pos = OneDimensionalCodeWriter.appendPattern(
    result,
    0,
    UPCEANReader.startEndPattern,
    true,
  );

  for (int i = 1; i <= 6; i++) {
    int digit = int.parse(contents[i]);
    if ((parities >> (6 - i) & 1) == 1) {
      digit += 10;
    }
    pos += OneDimensionalCodeWriter.appendPattern(
      result,
      pos,
      UPCEANReader.lAndGPatterns[digit],
      false,
    );
  }

  OneDimensionalCodeWriter.appendPattern(
    result,
    pos,
    UPCEANReader.endPattern,
    false,
  );

  return result;
}