Beispiel #1
0
        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)
                    {
                        // Return to ASCII encodation, which will actually handle latch to new mode
                        context.signalEncoderChange(Encodation.ASCII);
                        break;
                    }
                }
            }
            buffer.Append((char)31); //Unlatch
            handleEOD(context, buffer);
        }
Beispiel #2
0
 internal static void writeNextTriplet(EncoderContext context, StringBuilder buffer)
 {
     context.writeCodewords(encodeToCodewords(buffer, 0));
     buffer.Remove(0, 3);
 }
Beispiel #3
0
        /// <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, 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);
            }
        }