encodeMaximal method

void encodeMaximal(
  1. EncoderContext context
)

Implementation

void encodeMaximal(EncoderContext context) {
  final buffer = StringBuilder();
  int lastCharSize = 0;
  int backtrackStartPosition = context.pos;
  int backtrackBufferLength = 0;
  while (context.hasMoreCharacters) {
    final c = context.currentChar;
    context.pos++;
    lastCharSize = encodeChar(c, buffer);
    if (buffer.length % 3 == 0) {
      backtrackStartPosition = context.pos;
      backtrackBufferLength = buffer.length;
    }
  }
  if (backtrackBufferLength != buffer.length) {
    final unwritten = (buffer.length ~/ 3) * 2;

    // +1 for the latch to C40
    final curCodewordCount = context.codewordCount + unwritten + 1;
    context.updateSymbolInfo(curCodewordCount);
    final available = context.symbolInfo!.dataCapacity - curCodewordCount;
    final rest = buffer.length % 3;
    if ((rest == 2 && available != 2) ||
        (rest == 1 && (lastCharSize > 3 || available != 1))) {
      buffer.setLength(backtrackBufferLength);
      context.pos = backtrackStartPosition;
    }
  }
  if (buffer.length > 0) {
    context.writeCodeword(HighLevelEncoder.latchToC40);
  }

  handleEOD(context, buffer);
}