getC40Words method

Uint8List getC40Words(
  1. bool c40,
  2. int fnc1
)

Implementation

Uint8List getC40Words(bool c40, int fnc1) {
  final c40Values = <int>[];
  for (int i = 0; i < characterLength; i++) {
    final ci = input.charAt(fromPosition + i);
    if (c40 && HighLevelEncoder.isNativeC40(ci) ||
        !c40 && HighLevelEncoder.isNativeText(ci)) {
      c40Values.add(getC40Value(c40, 0, ci, fnc1));
    } else if (!MinimalEncoder.isExtendedASCII(ci, fnc1)) {
      final shiftValue = getShiftValue(ci, c40, fnc1);
      c40Values.add(shiftValue); //Shift[123]
      c40Values.add(getC40Value(c40, shiftValue, ci, fnc1));
    } else {
      final asciiValue = ((ci & 0xff) - 128);
      if (c40 && HighLevelEncoder.isNativeC40(asciiValue) ||
          !c40 && HighLevelEncoder.isNativeText(asciiValue)) {
        c40Values.add(1); //Shift 2
        c40Values.add(30); //Upper Shift
        c40Values.add(getC40Value(c40, 0, asciiValue, fnc1));
      } else {
        c40Values.add(1); //Shift 2
        c40Values.add(30); //Upper Shift
        final shiftValue = getShiftValue(asciiValue, c40, fnc1);
        c40Values.add(shiftValue); // Shift[123]
        c40Values.add(getC40Value(c40, shiftValue, asciiValue, fnc1));
      }
    }
  }

  if ((c40Values.length % 3) != 0) {
    assert(
      (c40Values.length - 2) % 3 == 0 &&
          fromPosition + characterLength == input.length,
    );
    c40Values.add(0); // pad with 0 (Shift 1)
  }

  final result = Uint8List(c40Values.length ~/ 3 * 2);
  int byteIndex = 0;
  for (int i = 0; i < c40Values.length; i += 3) {
    setC40Word(
      result,
      byteIndex,
      c40Values[i] & 0xff,
      c40Values[i + 1] & 0xff,
      c40Values[i + 2] & 0xff,
    );
    byteIndex += 2;
  }
  return result;
}