encode method

  1. @override
void encode(
  1. EncoderContext context
)
override

Implementation

@override
void encode(EncoderContext context) {
  //step C
  final buffer = StringBuilder();
  while (context.hasMoreCharacters) {
    final c = context.currentChar;
    context.pos++;

    int lastCharSize = encodeChar(c, buffer);

    final unwritten = (buffer.length ~/ 3) * 2;

    final curCodewordCount = context.codewordCount + unwritten;
    context.updateSymbolInfo(curCodewordCount);
    final available = context.symbolInfo!.dataCapacity - curCodewordCount;

    if (!context.hasMoreCharacters) {
      //Avoid having a single C40 value in the last triplet
      final removed = StringBuffer();
      if ((buffer.length % 3) == 2 && available != 2) {
        lastCharSize =
            _backtrackOneCharacter(context, buffer, removed, lastCharSize);
      }
      while (
          (buffer.length % 3) == 1 && (lastCharSize > 3 || available != 1)) {
        lastCharSize =
            _backtrackOneCharacter(context, buffer, removed, lastCharSize);
      }
      break;
    }

    final count = buffer.length;
    if ((count % 3) == 0) {
      final newMode = HighLevelEncoder.lookAheadTest(
        context.message,
        context.pos,
        encodingMode,
      );
      if (newMode != encodingMode) {
        // Return to ASCII encodation, which will actually handle latch to new mode
        context.signalEncoderChange(HighLevelEncoder.asciiEncodation);
        break;
      }
    }
  }
  handleEOD(context, buffer);
}