Example #1
0
        /// <summary>
        /// インスタンスを初期化します。
        /// </summary>
        /// <param name="ecLevel">誤り訂正レベル</param>
        /// <param name="maxVersion">型番の上限</param>
        /// <param name="allowStructuredAppend">複数シンボルへの分割を許可するには true を指定します。</param>
        /// <param name="charsetName">文字セット名</param>
        public Symbols(ErrorCorrectionLevel ecLevel = ErrorCorrectionLevel.M,
                       int maxVersion             = Constants.MAX_VERSION,
                       bool allowStructuredAppend = false,
                       string charsetName         = Charset.SHIFT_JIS)
        {
            if (!(Constants.MIN_VERSION <= maxVersion && maxVersion <= Constants.MAX_VERSION))
            {
                throw new ArgumentOutOfRangeException(nameof(maxVersion));
            }

            _items = new List <Symbol>();

            _minVersion = Constants.MIN_VERSION;

            _maxVersion              = maxVersion;
            _errorCorrectionLevel    = ecLevel;
            _structuredAppendAllowed = allowStructuredAppend;
            _encoding = Encoding.GetEncoding(charsetName);

            _parity = 0;

            _currSymbol = new Symbol(this);
            _items.Add(_currSymbol);

            _encNumeric = new NumericEncoder(_encoding);
            _encAlpha   = new AlphanumericEncoder(_encoding);

            if (Charset.IsJP(charsetName))
            {
                _encKanji = new KanjiEncoder(_encoding);
            }

            _encByte = new ByteEncoder(_encoding);
        }
Example #2
0
        /// <summary>
        /// 符号化モードを設定します。
        /// </summary>
        /// <param name="encMode">符号化モード</param>
        /// <param name="c">符号化する最初の文字。この文字はシンボルに追加されません。</param>
        /// <returns>シンボル容量が不足している場合は false を返します。</returns>
        internal bool TrySetEncodingMode(EncodingMode encMode, char c)
        {
            QRCodeEncoder encoder;
            Encoding      encoding = _parent.Encoding;

            switch (encMode)
            {
            case EncodingMode.NUMERIC:
                encoder = new NumericEncoder(encoding);
                break;

            case EncodingMode.ALPHA_NUMERIC:
                encoder = new AlphanumericEncoder(encoding);
                break;

            case EncodingMode.EIGHT_BIT_BYTE:
                encoder = new ByteEncoder(encoding);
                break;

            case EncodingMode.KANJI:
                if (Charset.IsJP(encoding.WebName))
                {
                    encoder = new KanjiEncoder(encoding);
                }
                else
                {
                    throw new InvalidOperationException();
                }
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(encMode));
            }

            int bitLength = encoder.GetCodewordBitLength(c);

            while (_dataBitCapacity <
                   _dataBitCounter
                   + ModeIndicator.LENGTH
                   + CharCountIndicator.GetLength(_currVersion, encMode)
                   + bitLength)
            {
                if (_currVersion >= _parent.MaxVersion)
                {
                    return(false);
                }

                SelectVersion();
            }

            _dataBitCounter += ModeIndicator.LENGTH
                               + CharCountIndicator.GetLength(_currVersion, encMode);
            _currEncoder = encoder;
            _segments.Add(_currEncoder);
            _segmentCounter[encMode]++;
            _currEncodingMode = encMode;

            return(true);
        }