/// <summary> /// Gets the length in pixels needed to render the specified barcode. /// </summary> /// <param name="barcode">Barcode glyphs to be analysed.</param> /// <param name="interGlyphSpace">Amount of inter-glyph space.</param> /// <param name="barMinWidth">Minimum barcode width.</param> /// <param name="barMaxWidth">Maximum barcode width.</param> /// <returns>The barcode width in pixels.</returns> /// <remarks> /// Currently this method does not account for any "quiet space" /// around the barcode as dictated by each symbology standard. /// </remarks> protected virtual int GetBarcodeLength( Glyph[] barcode, int interGlyphSpace, int barMinWidth, int barMaxWidth) { // Determine bar code length in pixels int totalImageWidth = GetBarcodeInterGlyphLength(barcode, interGlyphSpace); foreach (BarGlyph glyph in barcode) { // Determine encoding bit-width for this character int encodingBitCount = GetEncodingBitCount(glyph); if (glyph is IBinaryPitchGlyph) { IBinaryPitchGlyph binaryGlyph = (IBinaryPitchGlyph)glyph; int widthIndex = WidthBitCount - 1; bool lastBitState = false; for (int bitIndex = encodingBitCount - 1; bitIndex >= 0; --bitIndex) { // Determine whether the bit state is changing int bitmask = (1 << bitIndex); bool currentBitState = false; if ((bitmask & binaryGlyph.BitEncoding) != 0) { currentBitState = true; } // Adjust the width bit checker if (bitIndex < (encodingBitCount - 1) && lastBitState != currentBitState) { --widthIndex; } lastBitState = currentBitState; // Determine width encoding bit mask bitmask = (1 << widthIndex); if ((bitmask & binaryGlyph.WidthEncoding) != 0) { totalImageWidth += barMaxWidth; } else { totalImageWidth += barMinWidth; } } } else { totalImageWidth += (encodingBitCount * barMinWidth); } } return(totalImageWidth); }
/// <summary> /// Renders the bar-code glyph. /// </summary> /// <param name="glyphIndex">Index of the glyph.</param> /// <param name="glyph">A <see cref="T:Zen.Barcode.Glyph"/> object to be rendered.</param> /// <param name="dc">A <see cref="T:System.Drawing.Graphics"/> representing the draw context.</param> /// <param name="bounds">The bounding rectangle.</param> /// <param name="barOffset">The bar offset.</param> /// <param name="barMinHeight">Minimum bar height in pixels.</param> /// <param name="barMinWidth">Small bar width in pixels.</param> /// <param name="barMaxWidth">Large bar width in pixels.</param> /// <exception cref="T:System.InvalidOperationException"> /// Thrown if the encoding bit count is zero or variable-pitch /// bar rendering is attempted. /// </exception> protected virtual void RenderBar( int glyphIndex, BarGlyph glyph, Graphics dc, Rectangle bounds, ref int barOffset, int barMinHeight, int barMinWidth, int barMaxWidth) { // Sanity check int encodingBitCount = GetEncodingBitCount(glyph); if (encodingBitCount == 0) { throw new InvalidOperationException( "Encoding bit width must be greater than zero."); } // Allow derived classes to modify the glyph bits int glyphBits = GetGlyphEncoding(glyphIndex, glyph); // Get glyph height int height = GetGlyphHeight(glyph, barMinHeight, bounds.Height); if (glyph is IBinaryPitchGlyph) { IBinaryPitchGlyph binGlyph = (IBinaryPitchGlyph)glyph; // Render glyph int widthIndex = WidthBitCount - 1; bool lastBitState = false; for (int bitIndex = encodingBitCount - 1; bitIndex >= 0; --bitIndex) { int bitMask = 1 << bitIndex; int barWidth = barMinWidth; bool currentBitState = false; if ((bitMask & glyphBits) != 0) { currentBitState = true; } // Adjust the width bit checker if (bitIndex < (encodingBitCount - 1) && lastBitState != currentBitState) { --widthIndex; } lastBitState = currentBitState; // Determine width encoding bit mask int widthMask = (1 << widthIndex); if ((widthMask & binGlyph.WidthEncoding) != 0) { barWidth = barMaxWidth; } if ((binGlyph.BitEncoding & bitMask) != 0) { dc.FillRectangle(Brushes.Black, barOffset, bounds.Top, barWidth, height); } // Update offset barOffset += barWidth; } } else { for (int bitIndex = encodingBitCount - 1; bitIndex >= 0; --bitIndex) { int bitMask = (1 << bitIndex); if ((glyphBits & bitMask) != 0) { dc.FillRectangle(Brushes.Black, barOffset, bounds.Top, barMinWidth, height); } // Update offset barOffset += barMinWidth; } } }