/// <summary> /// Gets the index of this glyph. /// </summary> /// <param name="glyph"> /// The <see cref="T:Barcode.BarGlyph"/> to be located. /// </param> /// <returns> /// The ordinal index of the glyph or -1 if not found. /// </returns> public virtual int GetRawGlyphIndex(BarGlyph glyph) { int foundIndex = -1; BarGlyph[] glyphs = GetGlyphs(); for (int index = 0; index < glyphs.Length; ++index) { if (glyphs[index] == glyph) { foundIndex = index; break; } } return(foundIndex); }
/// <summary> /// Renders the barcode bars. /// </summary> /// <param name="barcode">A collection of <see cref="T:Barcode.Glyph"/> objects representing the /// barcode 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="interGlyphSpace">The inter glyph space in pixels.</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> /// <remarks> /// By default this method renders each glyph by calling the /// <see cref="M:RenderBar"/> method, applying the specified /// inter-glyph spacing as necessary. /// </remarks> protected virtual void RenderBars( Glyph[] barcode, Graphics dc, Rectangle bounds, int interGlyphSpace, int barMinHeight, int barMinWidth, int barMaxWidth) { int barOffset = 0; for (int index = 0; index < barcode.Length; ++index) { BarGlyph glyph = (BarGlyph)barcode[index]; RenderBar(index, glyph, dc, bounds, ref barOffset, barMinHeight, barMinWidth, barMaxWidth); // Account for inter glyph spacing barOffset += interGlyphSpace; } }
/// <summary> /// Gets the glyph encoding. /// </summary> /// <param name="glyphIndex">Index of the glyph.</param> /// <param name="glyph">The glyph.</param> /// <returns></returns> /// <remarks> /// By default this method simply returns the glyph bit encoding /// however some algorithms may chose to modify the encoding /// based on positional information. /// </remarks> protected virtual int GetGlyphEncoding(int glyphIndex, BarGlyph glyph) { return(glyph.BitEncoding); }
/// <summary> /// Renders the bar-code glyph. /// </summary> /// <param name="glyphIndex">Index of the glyph.</param> /// <param name="glyph">A <see cref="T: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; } } }
/// <summary> /// Overridden. Renders the specified glyph. /// </summary> /// <param name="glyphIndex">Index of the glyph.</param> /// <param name="glyph">A <see cref="T:Glyph"/> to be rendered.</param> /// <param name="dc">A <see cref="T:Graphics"/> representing the device context.</param> /// <param name="bounds">The bounding rectangle.</param> /// <param name="barOffset">The bar offset in pixels.</param> /// <param name="barMinHeight">Minimum bar height in pixels.</param> /// <param name="barMinWidth">Minimum bar width.</param> /// <param name="barMaxWidth">Maximum bar width.</param> /// <exception cref="T:InvalidOperationException"> /// Thrown if the encoding bit count is zero or variable-pitch /// bar rendering is attempted. /// </exception> protected override void RenderBar(int glyphIndex, BarGlyph glyph, Graphics dc, Rectangle bounds, ref int barOffset, int barMinHeight, int barMinWidth, int barMaxWidth) { // Sanity check BinaryPitchGlyph binGlyph = glyph as BinaryPitchGlyph; if (binGlyph == null) { throw new InvalidOperationException("Glyph must be derived from BinaryPitchGlyph."); } if (barMinWidth == barMaxWidth) { throw new InvalidOperationException("Only variable-pitch drawing supported."); } if (WidthBitCount == 0) { throw new InvalidOperationException("Must have width bit information."); } // Get glyph encoding width int encodingBitCount = GetGlyphEncodingBitCount(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 & binGlyph.BitEncoding) != 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, bounds.Height); } // Update offset barOffset += barWidth; } }