Example #1
0
        private string getEncodedValueByChar(string data, Code128Subset subset)
        {
            DataRow[] rows = null;

            if (subset == Code128Subset.SubsetA)
            {
                rows = C128_Code.Select("A = '" + data + "'");
                if (rows.Length > 0)
                {
                    return(rows[0]["Encoding"].ToString());
                }
            }
            if (subset == Code128Subset.SubsetB)
            {
                rows = C128_Code.Select("B = '" + data + "'");
                if (rows.Length > 0)
                {
                    return(rows[0]["Encoding"].ToString());
                }
            }
            if (subset == Code128Subset.SubsetC)
            {
                rows = C128_Code.Select("C = '" + data + "'");
                if (rows.Length > 0)
                {
                    return(rows[0]["Encoding"].ToString());
                }
            }

            return(null);

            return(null);
        }
        /// <summary>
        /// Adds the supplied section and control characters to the encode.
        /// </summary>
        /// <param name="lastSubset"></param>
        /// <param name="sectionSubset"></param>
        /// <param name="sectionValue"></param>
        public void EncodeSection(Code128Subset lastSubset, Code128Subset sectionSubset, string sectionValue)
        {
            // Add the subset select or change control symbol to the encode
            string        character     = GetSubsetAsString(sectionSubset);
            int           symbol        = _symbologyCharEncode[GetSubsetAsString(lastSubset) + GetSubsetAsString(sectionSubset)];
            LinearPattern symbolPattern = _patternDictionary[symbol];

            AddSymbolToEncode(character, 1, symbol, symbolPattern);
            ZplEncode += GetZplFunction(GetSubsetAsString(lastSubset) + GetSubsetAsString(sectionSubset));

            // Add FNC1 symbol if GS1-128 Barcode
            if (lastSubset == Code128Subset.Null && _isGs1)
            {
                symbol        = _symbologyCharEncode["FNC1"];
                symbolPattern = _patternDictionary[symbol];
                AddSymbolToEncode("F1", 1, symbol, symbolPattern);
                ZplEncode += GetZplFunction("FNC1");
            }

            if (sectionSubset == Code128Subset.C)    //Subset C encoder
            {
                for (int encodePosition = 0; encodePosition <= sectionValue.Length - 1; encodePosition += 2)
                {
                    // Encode the current numeric pair symbol
                    character     = sectionValue.Substring(encodePosition, 2);
                    symbol        = Convert.ToInt32(character);
                    symbolPattern = _patternDictionary[symbol];
                    AddSymbolToEncode(character, 0, symbol, symbolPattern);
                    ZplEncode += character;
                }
            }
            else
            {
                for (int encodePosition = 0; encodePosition <= sectionValue.Length - 1; encodePosition++)
                {
                    character     = sectionValue.Substring(encodePosition, 1);
                    symbol        = _symbologyCharEncode[((int)sectionValue[encodePosition]).ToString()];
                    symbolPattern = _patternDictionary[symbol];
                    AddSymbolToEncode(character, 0, symbol, symbolPattern);
                    if (sectionSubset == Code128Subset.A)
                    {
                        string subsetACode = "0" + symbol.ToString();
                        ZplEncode += subsetACode.Substring(subsetACode.Length - 2, 2);
                    }
                    else
                    {
                        ZplEncode += character;
                    }
                }
            }
        }
        /// <summary>
        /// Get the Code 128 subset as a string
        /// </summary>
        /// <param name="subset"></param>
        /// <returns>Single character subset string</returns>
        private string GetSubsetAsString(Code128Subset subset)
        {
            switch (subset)
            {
            case Code128Subset.A:
                return("A");

            case Code128Subset.B:
                return("B");

            case Code128Subset.C:
                return("C");

            case Code128Subset.Null:
                return("");
            }

            throw new ArgumentException(subset + " is not a recognised Code128 subset", nameof(subset));
        }
        public Code128Encoder(Symbology symbology) : base(symbology)
        {
            switch (Symbology)
            {
            case Symbology.Code128ABC:
                _suppressSubsetC = false;
                StartSubset      = Code128Subset.A;
                Description      = "Code 128 - Subset A Prioritised & Auto Subset C Selected";
                break;

            case Symbology.Code128BAC:
                _suppressSubsetC = false;
                StartSubset      = Code128Subset.B;
                Description      = "Code 128 - Subset B Prioritised & Auto Subset C Selected";
                break;

            case Symbology.Code128AB:
                _suppressSubsetC = true;
                StartSubset      = Code128Subset.A;
                Description      = "Code 128 - Subset A Prioritised & Subset C Suppressed";
                break;

            case Symbology.Code128BA:
                _suppressSubsetC = true;
                StartSubset      = Code128Subset.B;
                Description      = "Code 128 - Subset B Prioritised & Subset C Suppressed";
                break;

            case Symbology.GS1128:
                _suppressSubsetC = false;
                _isGs1           = true;
                StartSubset      = Code128Subset.B;
                Description      = "GS1-128 - Subset B Prioritised & Auto Subset C Selected";
                break;

            default:
                _suppressSubsetC = false;
                StartSubset      = Code128Subset.B;
                Description      = "Code 128 - Subset B Prioritised & Auto Subset C Selected";
                break;
            }
        }
        /// <summary>
        /// Analyse the supplied section and determine the appropriate encoding.
        /// </summary>
        /// <param name="barcodeValue"></param>
        /// <param name="startPosition"></param>
        /// <param name="lastSubset"></param>
        private void AnalyseSection(string barcodeValue, int startPosition, Code128Subset lastSubset)
        {
            Code128Subset currentSubset = Code128Subset.Null;
            bool          isNewSection  = false;

            for (int position = startPosition; position <= barcodeValue.Length - 1; position++)
            {
                isNewSection = true;
                int asciiCode = barcodeValue[position];

                switch (asciiCode)
                {
                case var code when code <= 31:     // Control characters that are only supported by subset A.

                    if (currentSubset == Code128Subset.Null | currentSubset == Code128Subset.A)
                    {
                        currentSubset = Code128Subset.A;
                    }
                    else
                    {
                        EncodeSection(lastSubset, currentSubset, barcodeValue.Substring(startPosition, position - startPosition));
                        AnalyseSection(barcodeValue, position, currentSubset);
                        return;
                    }
                    break;

                case var code when code >= 96:     // Lower case characters that are only supported by subset C

                    if (currentSubset == Code128Subset.Null | currentSubset == Code128Subset.B)
                    {
                        currentSubset = Code128Subset.B;
                    }
                    else
                    {
                        EncodeSection(lastSubset, currentSubset, barcodeValue.Substring(startPosition, position - startPosition));
                        AnalyseSection(barcodeValue, position, currentSubset);
                        return;
                    }
                    break;

                case var code when code >= 48 && code <= 57:     // Numeric characters, that may benefit from using subset C.

                    if (_suppressSubsetC)
                    {
                        break;
                    }

                    int subsetCSequence = GetSubsetCSequenceCount(barcodeValue, position);

                    if (subsetCSequence >= 1)
                    {
                        if (position >= 1)
                        {
                            if (currentSubset == Code128Subset.Null)
                            {
                                currentSubset = StartSubset;
                            }
                            EncodeSection(lastSubset, currentSubset, barcodeValue.Substring(startPosition, position - startPosition));
                            lastSubset = currentSubset;
                        }

                        currentSubset = Code128Subset.C;
                        EncodeSection(lastSubset, currentSubset, barcodeValue.Substring(position, subsetCSequence));
                        AnalyseSection(barcodeValue, position + subsetCSequence, currentSubset);
                        return;
                    }

                    break;
                }
            }

            if (isNewSection)
            {
                if (currentSubset == Code128Subset.Null)
                {
                    currentSubset = StartSubset;
                }
                EncodeSection(lastSubset, currentSubset, barcodeValue.Substring(startPosition, barcodeValue.Length - startPosition));
            }

            AddCheckDigit();
            AddStopSymbol();
            SetMinXDimension();
            SetMinBarcodeHeight();
        }