예제 #1
0
        /// <summary>
        /// Overridden. Gets the glyphs needed to render a full barcode.
        /// </summary>
        /// <param name="text">Text to convert into bar-code.</param>
        /// <returns>A collection of <see cref="T:Glyph"/> objects.</returns>
        protected override Glyph[] GetFullBarcode(string text)
        {
            // Run proposed barcode through validator
            Match m = Regex.Match(text, @"^\s*(?<barcode>[0-9]{12})\s*$");

            if (!m.Success)
            {
                throw new ArgumentException("Invalid barcode.");
            }
            string barcodeText = m.Groups["barcode"].Value;

            // Determine parity to use
            byte[] parityTable = new byte[]
            {
                0x00,                 // OOOOOO
                0x0B,                 // OOEOEE
                0x0D,                 // OOEEOE
                0x0E,                 // OOEEEO
                0x13,                 // OEOOEE
                0x19,                 // OEEOOE
                0x1D,                 // OEEEOE
                0x15,                 // OEOEOE
                0x16,                 // OEOEEO
                0x1A,                 // OEEOEO
            };

            // Determine parity value for remaining encoding then strip
            //	after calculating the checksum
            char parityDigit = barcodeText[0];
            byte parity      = parityTable[parityDigit - '0'];

            if (Checksum != null)
            {
                barcodeText += Checksum.GetChecksumChar(barcodeText);
            }
            barcodeText = barcodeText.Substring(1);

            // Build result with composite glyphs
            List <Glyph> result = new List <Glyph>();

            result.AddRange(Factory.GetGlyphs(barcodeText, true));

            // Now translate each composite glyph using the parity
            //	in the second digit.
            // The first two digits are encoded without parity.
            int parityIndex = 32;

            for (int index = 0; index < result.Count; ++index)
            {
                // Fetch next glyph
                if (result[index] is CompositeGlyph)
                {
                    // Determine effective parity arrangement
                    byte effectiveParity = (index < 6) ? parity : (byte)0;

                    CompositeGlyph composite = (CompositeGlyph)result[index];
                    if ((parityIndex & effectiveParity) == 0)
                    {
                        result[index] = composite.First;
                    }
                    else
                    {
                        result[index] = composite.Second;
                    }
                    if (parityIndex > 1)
                    {
                        parityIndex /= 2;
                    }
                }
            }

            // Add start/stop and seperator glyphs
            result.Insert(6, Factory.GetRawGlyph('|'));
            result.Insert(0, Factory.GetRawGlyph('*'));
            result.Add(Factory.GetRawGlyph('*'));
            return(result.ToArray());
        }