public void encode(EncoderContext context) { //step F var buffer = new StringBuilder(); while (context.HasMoreCharacters) { char c = context.CurrentChar; encodeChar(c, buffer); context.Pos++; int count = buffer.Length; if (count >= 4) { context.writeCodewords(encodeToCodewords(buffer, 0)); buffer.Remove(0, 4); int newMode = HighLevelEncoder.lookAheadTest(context.Message, context.Pos, EncodingMode); if (newMode != EncodingMode) { context.signalEncoderChange(Encodation.ASCII); break; } } } buffer.Append((char)31); //Unlatch handleEOD(context, buffer); }
public void encode(EncoderContext context) { //step F var buffer = new StringBuilder(); while (context.HasMoreCharacters) { char c = context.CurrentChar; encodeChar(c, buffer); context.Pos++; int count = buffer.Length; if (count >= 4) { context.writeCodewords(encodeToCodewords(buffer, 0)); buffer.Remove(0, 4); int newMode = HighLevelEncoder.lookAheadTest(context.Message, context.Pos, EncodingMode); if (newMode != EncodingMode) { context.signalEncoderChange(HighLevelEncoder.ASCII_ENCODATION); break; } } } buffer.Append((char)31); //Unlatch handleEOD(context, buffer); }
/// <summary> /// Handle "end of data" situations /// </summary> /// <param name="context">the encoder context</param> /// <param name="buffer">the buffer with the remaining encoded characters</param> private static void handleEOD(EncoderContext context, StringBuilder buffer) { try { int count = buffer.Length; if (count == 0) { return; //Already finished } if (count == 1) { //Only an unlatch at the end context.updateSymbolInfo(); int available = context.SymbolInfo.dataCapacity - context.CodewordCount; int remaining = context.RemainingCharacters; if (remaining == 0 && available <= 2) { return; //No unlatch } } if (count > 4) { throw new InvalidOperationException("Count must not exceed 4"); } int restChars = count - 1; String encoded = encodeToCodewords(buffer, 0); bool endOfSymbolReached = !context.HasMoreCharacters; bool restInAscii = endOfSymbolReached && restChars <= 2; if (restChars <= 2) { context.updateSymbolInfo(context.CodewordCount + restChars); int available = context.SymbolInfo.dataCapacity - context.CodewordCount; if (available >= 3) { restInAscii = false; context.updateSymbolInfo(context.CodewordCount + encoded.Length); //available = context.symbolInfo.dataCapacity - context.getCodewordCount(); } } if (restInAscii) { context.resetSymbolInfo(); context.Pos -= restChars; } else { context.writeCodewords(encoded); } } finally { context.signalEncoderChange(Encodation.ASCII); } }
internal static void writeNextTriplet(EncoderContext context, StringBuilder buffer) { context.writeCodewords(encodeToCodewords(buffer, 0)); buffer.Remove(0, 3); }
/// <summary> /// Handle "end of data" situations /// </summary> /// <param name="context">the encoder context</param> /// <param name="buffer">the buffer with the remaining encoded characters</param> private static void handleEOD(EncoderContext context, StringBuilder buffer) { try { int count = buffer.Length; if (count == 0) { return; //Already finished } if (count == 1) { //Only an unlatch at the end context.updateSymbolInfo(); int available = context.SymbolInfo.dataCapacity - context.CodewordCount; int remaining = context.RemainingCharacters; // The following two lines are a hack inspired by the 'fix' from https://sourceforge.net/p/barcode4j/svn/221/ if (remaining > available) { context.updateSymbolInfo(context.CodewordCount + 1); available = context.SymbolInfo.dataCapacity - context.CodewordCount; } if (remaining <= available && available <= 2) { return; //No unlatch } } if (count > 4) { throw new InvalidOperationException("Count must not exceed 4"); } int restChars = count - 1; String encoded = encodeToCodewords(buffer); bool endOfSymbolReached = !context.HasMoreCharacters; bool restInAscii = endOfSymbolReached && restChars <= 2; if (restChars <= 2) { context.updateSymbolInfo(context.CodewordCount + restChars); int available = context.SymbolInfo.dataCapacity - context.CodewordCount; if (available >= 3) { restInAscii = false; context.updateSymbolInfo(context.CodewordCount + encoded.Length); //available = context.symbolInfo.dataCapacity - context.getCodewordCount(); } } if (restInAscii) { context.resetSymbolInfo(); context.Pos -= restChars; } else { context.writeCodewords(encoded); } } finally { context.signalEncoderChange((int)Encodation.ASCII); } }
/// <summary> /// Handle "end of data" situations /// </summary> /// <param name="context">the encoder context</param> /// <param name="buffer">the buffer with the remaining encoded characters</param> private static void handleEOD(EncoderContext context, StringBuilder buffer) { try { int count = buffer.Length; if (count == 0) { return; //Already finished } if (count == 1) { //Only an unlatch at the end context.updateSymbolInfo(); int available = context.SymbolInfo.dataCapacity - context.CodewordCount; int remaining = context.RemainingCharacters; if (remaining == 0 && available <= 2) { return; //No unlatch } } if (count > 4) { throw new InvalidOperationException("Count must not exceed 4"); } int restChars = count - 1; String encoded = encodeToCodewords(buffer, 0); bool endOfSymbolReached = !context.HasMoreCharacters; bool restInAscii = endOfSymbolReached && restChars <= 2; if (restChars <= 2) { context.updateSymbolInfo(context.CodewordCount + restChars); int available = context.SymbolInfo.dataCapacity - context.CodewordCount; if (available >= 3) { restInAscii = false; context.updateSymbolInfo(context.CodewordCount + encoded.Length); //available = context.symbolInfo.dataCapacity - context.getCodewordCount(); } } if (restInAscii) { context.resetSymbolInfo(); context.Pos -= restChars; } else { context.writeCodewords(encoded); } } finally { context.signalEncoderChange(HighLevelEncoder.ASCII_ENCODATION); } }