C#バーコード生成(四)----Code 128 Auto


Code 128 Autoは、GBT 18347-2001 128バーコードの最小長の原則に完全に従って作成されたバーコードです.
/// <summary>
    /// Code128Auto   ,        
    /// </summary>
    public class Code128Auto : absCode128
    {
        public Code128Auto(string rawData)
            : base(rawData)
        {
        }

        protected override bool RawDataCheck()
        {
            //Code128   ASCII    0~127
            foreach (char c in this._rawData)
            {
                if ((byte)c > 127)
                {
                    return false;
                }
            }
            return true;
        }

        protected override string GetEncodedData()
        {
            StringBuilder tempBuilder = new StringBuilder();

            CharacterSet nowCharacterSet = Code128.GetCharacterSet(this._rawData, 0);

            int checkNum;//    
            switch (nowCharacterSet)
            {
                case CharacterSet.A:
                    tempBuilder.Append(Code128.BSList[Code128.StartA]);//     StartA
                    checkNum = Code128.StartA;
                    break;
                case CharacterSet.B:
                    tempBuilder.Append(Code128.BSList[Code128.StartB]);//     StartB
                    checkNum = Code128.StartB;
                    break;
                default:
                    tempBuilder.Append(Code128.BSList[Code128.StartC]);//     StartC
                    checkNum = Code128.StartC;
                    break;
            }
            int nowWeight = 1, nowIndex = 0;
            this.GetEncodedData(tempBuilder, nowCharacterSet, ref nowIndex, ref nowWeight, ref checkNum);

            checkNum %= 103;
            tempBuilder.Append(Code128.BSList[checkNum]);//     
            tempBuilder.Append(Code128.BSList[Code128.Stop]);//     
            return tempBuilder.ToString();
        }
        /// <summary>
        ///     
        /// </summary>
        /// <param name="tempBuilder"></param>
        /// <param name="sIndex"></param>
        /// <param name="nowWeight"></param>
        /// <param name="checkNum"></param>
        private void EncodingCommon(StringBuilder tempBuilder, byte sIndex, ref int nowWeight, ref int checkNum)
        {
            tempBuilder.Append(Code128.BSList[sIndex]);
            checkNum += nowWeight * sIndex;
            nowWeight++;
        }
        /// <summary>
        ///         
        /// </summary>
        /// <param name="tempBuilder">      </param>
        /// <param name="nowCharacterSet">     </param>
        /// <param name="i">     </param>
        /// <param name="nowWeight">    </param>
        /// <param name="checkNum">       </param>
        private void GetEncodedData(StringBuilder tempBuilder, CharacterSet nowCharacterSet,ref int i, ref int nowWeight, ref int checkNum)
        {//         C,  i nowWeight         ,       
            byte sIndex;
            switch (nowCharacterSet)
            {
                case CharacterSet.A:
                case CharacterSet.B:
                    for (; i < this._rawData.Length; i++)
                    {
                        if (char.IsDigit(this._rawData[i]))
                        {
                            //  
                            int digitLength = Code128.GetDigitLength(this._rawData, i);
                            if (digitLength >= 4)
                            {
                                //  CodeC
                                if (digitLength % 2 != 0)
                                {//     ,          CodeC  
                                    sIndex = Code128.GetSIndex(nowCharacterSet, (this._rawData[i]));
                                    this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
                                    i++;
                                }
                                nowCharacterSet = CharacterSet.C;
                                sIndex = Code128.GetCodeXIndex(nowCharacterSet);//  CodeC    
                                this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
                                this.GetEncodedData(tempBuilder, nowCharacterSet, ref i, ref nowWeight, ref checkNum);
                                return;
                            }
                            else
                            {
                                //    4   ,         
                                for (int j = 0; j < digitLength; j++)
                                {
                                    sIndex = Code128.GetSIndex(nowCharacterSet, (this._rawData[i]));
                                    this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
                                    i++;
                                }
                                i--;//                  i   1,     1
                                continue;
                            }
                        }
                        else if (Code128.CharNotBelongsTo(nowCharacterSet, this._rawData[i]))
                        {//             
                            byte tempByte = Code128.GetProprietaryChar(this._rawData, i + 1);//            A, B    
                            CharacterSet tempCharacterSet = Code128.GetShiftCharacterSet(nowCharacterSet);
                            if (tempByte != byte.MaxValue && Code128.CharOnlyBelongsTo(nowCharacterSet, (char)tempByte))
                            {
                                //     
                                sIndex = Code128.SHIFT_AB;
                                this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);

                                sIndex = Code128.GetSIndex(tempCharacterSet, this._rawData[i]);
                                this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
                                continue;
                            }
                            else
                            {
                                //     
                                nowCharacterSet = tempCharacterSet;
                                sIndex = Code128.GetCodeXIndex(nowCharacterSet);
                                this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
                                this.GetEncodedData(tempBuilder, nowCharacterSet, ref i, ref nowWeight, ref checkNum);
                                return;
                            }
                        }
                        else
                        {
                            sIndex = Code128.GetSIndex(nowCharacterSet, this._rawData[i]);
                            this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
                        }
                    }
                    break;
                default:
                    for (; i < this._rawData.Length; i += 2)
                    {
                        if (i != this._rawData.Length - 1 && char.IsDigit(this._rawData, i) && char.IsDigit(this._rawData, i + 1))
                        {
                            sIndex = byte.Parse(this._rawData.Substring(i, 2));
                            this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
                        }
                        else
                        {
                            nowCharacterSet = Code128.GetCharacterSet(this._rawData, i);
                            //      
                            sIndex = Code128.GetCodeXIndex(nowCharacterSet);
                            this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
                            this.GetEncodedData(tempBuilder, nowCharacterSet, ref i, ref nowWeight, ref checkNum);
                            return;
                        }
                    }
                    break;
            }
        }

        #region   
        //private void GetEncodedData(StringBuilder tempBuilder, string data, CharacterSet nowCharacterSet, ref int nowWeight, ref int checkNum)
        //{
        //    byte sIndex, nowChar;
        //    switch (nowCharacterSet)
        //    {
        //        case CharacterSet.A:
        //            for (int i = 0; i < data.Length; i++)
        //            {
        //                nowChar = (byte)data[i];
        //                if (nowChar > 95)
        //                {
        //                    //   B
        //                    byte tempByte = Code128.GetProprietaryChar(data, i + 1);
        //                    if (tempByte < 32)
        //                    {
        //                        //     
        //                        sIndex = Code128.SHIFT_AB;
        //                        this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);

        //                        sIndex = Code128.GetSIndexFromB(data[i]);
        //                        this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                        continue;
        //                    }
        //                    else
        //                    {
        //                        //     
        //                        sIndex = Code128.CODEB_AC;
        //                        this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                        this.GetEncodedData(tempBuilder, data.Substring(i), CharacterSet.B, ref nowWeight, ref checkNum);
        //                        return;
        //                    }
        //                }
        //                else if (char.IsDigit(data[i]))
        //                {
        //                    //  
        //                    int digitLength = Code128.GetDigitLength(data, i);
        //                    if (digitLength >= 4)
        //                    {
        //                        //  CodeC
        //                        if (digitLength % 2 != 0)
        //                        {//     ,          CodeC  
        //                            sIndex = Code128.GetSIndexFromA(data[i]);
        //                            this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                            i++;
        //                        }
        //                        sIndex = Code128.CODEC_AB;//  CodeC    
        //                        this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                        this.GetEncodedData(tempBuilder, data.Substring(i), CharacterSet.C, ref nowWeight, ref checkNum);
        //                        return;
        //                    }
        //                    else
        //                    {
        //                        for (int j = 0; j < digitLength; j++)
        //                        {
        //                            sIndex = Code128.GetSIndexFromA(data[i]);
        //                            this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);i++;
        //                        }i--;
        //                        continue;
        //                    }
        //                }

        //                sIndex = Code128.GetSIndexFromA(data[i]);
        //                this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //            }
        //            break;
        //        case CharacterSet.B:
        //            for (int i = 0; i < data.Length; i++)
        //            {
        //                nowChar = (byte)data[i];
        //                if (nowChar < 32)
        //                {
        //                    //   A
        //                    byte tempByte = Code128.GetProprietaryChar(data, i + 1);
        //                    if (tempByte > 95)
        //                    {
        //                        //     
        //                        sIndex = Code128.SHIFT_AB;
        //                        this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);

        //                        sIndex = Code128.GetSIndexFromA(data[i]);
        //                        this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                        continue;
        //                    }
        //                    else
        //                    {
        //                        //     
        //                        sIndex = Code128.CODEA_BC;
        //                        this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                        this.GetEncodedData(tempBuilder, data.Substring(i), CharacterSet.B, ref nowWeight, ref checkNum);
        //                        return;
        //                    }
        //                }
        //                else if (char.IsDigit(data[i]))
        //                {
        //                    //  
        //                    int digitLength = Code128.GetDigitLength(data, i);
        //                    if (digitLength >= 4)
        //                    {
        //                        //  CodeC
        //                        if (digitLength % 2 != 0)
        //                        {//     ,          CodeC  
        //                            sIndex = Code128.GetSIndexFromB(data[i]);
        //                            this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                            i++;
        //                        }
        //                        sIndex = Code128.CODEC_AB;//  CodeC    
        //                        this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                        this.GetEncodedData(tempBuilder, data.Substring(i), CharacterSet.C, ref nowWeight, ref checkNum);
        //                        return;
        //                    }
        //                    else
        //                    {
        //                        for (int j = 0; j < digitLength; j++)
        //                        {
        //                            sIndex = Code128.GetSIndexFromB(data[i]);
        //                            this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);i++;
        //                        }i--;
        //                        continue;
        //                    }
        //                }

        //                sIndex = Code128.GetSIndexFromB(data[i]);
        //                this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //            }
        //            break;
        //        default:
        //            for (int i = 0; i < data.Length; i += 2)
        //            {
        //                if (i != data.Length - 1 && char.IsDigit(data, i) && char.IsDigit(data, i + 1))
        //                {
        //                    sIndex = byte.Parse(data.Substring(i, 2));
        //                    this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                }
        //                else
        //                {
        //                    nowCharacterSet = Code128.GetCharacterSet(data, i);
        //                    //      
        //                    sIndex = Code128.GetCodeXIndex(nowCharacterSet);
        //                    this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
        //                    this.GetEncodedData(tempBuilder, data.Substring(i), nowCharacterSet, ref nowWeight, ref checkNum);
        //                    return;
        //                }
        //            }
        //            break;
        //    }
        //}
        #endregion
    }

ログアウト部分は最初の取得符号化の書き方です