Skip to content

Latest commit

 

History

History
561 lines (408 loc) · 12.3 KB

File metadata and controls

561 lines (408 loc) · 12.3 KB

BarcodeOptions Class

The BarcodeOptions class provides configuration options for customizing barcode appearance, behavior, and font settings. Options are configured using an action delegate pattern.


Options Locking Mechanism

Important: Once a Barcode instance is created, the options are automatically locked and cannot be modified. This ensures thread safety and prevents configuration changes after the encoder has been initialized.

var options = new BarcodeOptions();
options.Type = BarcodeTypes.Ean13; // This works

using var barcode = new Barcode(opt => opt.Type = BarcodeTypes.Ean13);
// Options are now locked - any attempt to modify them will be ignored

Class Information

Namespace

TyKonKet.BarcodeGenerator

Inheritance

public sealed class BarcodeOptions

Static Fields

DefaultTypeface

The default typeface used for rendering text in barcodes when no custom typeface is specified.

public static readonly SKTypeface DefaultTypeface

Example Usage:

// Check if using default typeface
if (options.Typeface == BarcodeOptions.DefaultTypeface)
{
    Console.WriteLine("Using default system font");
}

Properties

Type

Gets or sets the type of barcode to generate.

public BarcodeTypes Type { get; set; }
Attribute Value
Default Value BarcodeTypes.Ean8
Available Types EAN-13, UPC-A, ISBN-13, EAN-8, CODE-93

Example:

var options = new BarcodeOptions
{
    Type = BarcodeTypes.Ean13
};

Height

Gets or sets the height of the barcode bars in pixels.

public int Height { get; set; }
Attribute Value
Default Value 30 pixels
Recommended Range 20-100 pixels
Use Case Larger values improve scanning reliability

Example:

options.Height = 50; // Taller bars for better scanning

Scaling

Gets or sets the scaling factor for the barcode. Minimum value is 1.

public int Scaling { get; set; }
Attribute Value
Default Value 5
Minimum Value 1
Behavior Values < 1 adjusted to 1, negative values converted to absolute

Example:

options.Scaling = 3; // 3x scale for larger output

Margins

Gets or sets the margins around the barcode in pixels.

public int Margins { get; set; }
Attribute Value
Default Value 2 pixels
Purpose Provides white space around barcode for better scanning
Recommended 5-20 pixels for most applications

Example:

options.Margins = 10; // More white space around barcode

BackgroundColor

Gets or sets the background color of the barcode.

public SKColor BackgroundColor { get; set; }
Attribute Value
Default Value SKColors.White
Common Options White, Transparent, Light Gray
Best Practice High contrast with foreground color

Examples:

options.BackgroundColor = SKColors.Transparent; // Transparent background
options.BackgroundColor = SKColor.Parse("#F0F0F0"); // Light gray

ForegroundColor

Gets or sets the color of the barcode bars and text.

public SKColor ForegroundColor { get; set; }
Attribute Value
Default Value SKColors.Black
Best Practice Dark colors for better contrast
Scanner Compatibility Black provides best scanning results

Examples:

options.ForegroundColor = SKColors.DarkBlue;
options.ForegroundColor = SKColor.Parse("#FF0000"); // Red bars

RenderText

Gets or sets a value indicating whether to render text below the barcode.

public bool RenderText { get; set; }
Attribute Value
Default Value true
When to Disable Space-constrained layouts, purely machine-readable codes
When to Enable Human verification needed, retail applications

Example:

options.RenderText = false; // Bars only, no text

Typeface

Gets the typeface used for rendering text in the barcode. This is read-only; use the UseTypeface methods to set it.

public SKTypeface Typeface { get; }
Attribute Value
Access Read-only property
Modification Use UseTypeface() methods to change
Default System default font

Example:

Console.WriteLine($"Current font: {options.Typeface.FamilyName}");

public SKTypeface Typeface { get; }

Example

Console.WriteLine($"Current font: {options.Typeface.FamilyName}");

Methods

UseTypeface(SKTypeface)

Sets the typeface used for rendering text in the barcode.

public void UseTypeface(SKTypeface typeface)

Parameters

  • typeface (SKTypeface) - The typeface to use.

Exceptions

  • InvalidOperationException - Thrown when the options are locked and cannot be modified.

Example

var customTypeface = SKTypeface.FromFamilyName("Arial");
options.UseTypeface(customTypeface);

UseTypeface(FontFamily)

Sets the typeface used for rendering text using a FontFamily.

public void UseTypeface(FontFamily fontFamily)

Parameters

  • fontFamily (FontFamily) - The font family to use. Can be a string or FontFamilies enum value.

Examples

Using string:

options.UseTypeface("Times New Roman");

Using FontFamilies enum:

options.UseTypeface(FontFamilies.Arial);

Implicit conversion from string:

FontFamily family = "Verdana"; // Implicit conversion
options.UseTypeface(family);

UseTypeface(FontFamily, SKFontStyle)

Sets the typeface with a specific font style.

public void UseTypeface(FontFamily fontFamily, SKFontStyle fontStyle)

Parameters

  • fontFamily (FontFamily) - The font family to use.
  • fontStyle (SKFontStyle) - The font style to apply.

Example

options.UseTypeface("Arial", SKFontStyle.Bold);
options.UseTypeface(FontFamilies.TimesNewRoman, SKFontStyle.Italic);

UseTypeface(FontFamily, int, int, SKFontStyleSlant)

Sets the typeface with custom weight, width, and slant.

public void UseTypeface(FontFamily fontFamily, int weight, int width, SKFontStyleSlant fontStyleSlant)

Parameters

  • fontFamily (FontFamily) - The font family to use.
  • weight (int) - The font weight (100-900, where 400 is normal, 700 is bold).
  • width (int) - The font width (1-9, where 5 is normal).
  • fontStyleSlant (SKFontStyleSlant) - The font slant (upright, italic, or oblique).

Example

options.UseTypeface("Roboto", 600, 5, SKFontStyleSlant.Upright); // Semi-bold
options.UseTypeface("Open Sans", 300, 3, SKFontStyleSlant.Italic); // Light, condensed, italic

UseTypeface(FontFamily, SKFontStyleWeight, SKFontStyleWidth, SKFontStyleSlant)

Sets the typeface using strongly-typed weight and width enums.

public void UseTypeface(FontFamily fontFamily, SKFontStyleWeight weight, SKFontStyleWidth width, SKFontStyleSlant fontStyleSlant)

Parameters

  • fontFamily (FontFamily) - The font family to use.
  • weight (SKFontStyleWeight) - The font weight enum value.
  • width (SKFontStyleWidth) - The font width enum value.
  • fontStyleSlant (SKFontStyleSlant) - The font slant.

Example

options.UseTypeface("Helvetica", 
    SKFontStyleWeight.SemiBold, 
    SKFontStyleWidth.Condensed, 
    SKFontStyleSlant.Upright);

UseTypefaceFromFile(string, int)

Sets the typeface from a font file.

public void UseTypefaceFromFile(string path, int index = 0)

Parameters

  • path (string) - The path to the font file (TTF, OTF, etc.).
  • index (int) - The index of the font in the file (for font collections). Default is 0.

Example

options.UseTypefaceFromFile("fonts/custom-font.ttf");
options.UseTypefaceFromFile("fonts/font-collection.ttc", 2); // Third font in collection

UseTypefaceFromData(SKData, int)

Sets the typeface from font data in memory.

public void UseTypefaceFromData(SKData data, int index = 0)

Parameters

  • data (SKData) - The font data.
  • index (int) - The index of the font in the data. Default is 0.

Example

byte[] fontBytes = File.ReadAllBytes("custom-font.ttf");
using var fontData = SKData.CreateCopy(fontBytes);
options.UseTypefaceFromData(fontData);

UseTypefaceFromStream(SKStreamAsset, int)

Sets the typeface from an SKStreamAsset.

public void UseTypefaceFromStream(SKStreamAsset stream, int index = 0)

Parameters

  • stream (SKStreamAsset) - The font stream.
  • index (int) - The index of the font in the stream. Default is 0.

Example

using var fontStream = new SKFileStream("fonts/custom.ttf");
options.UseTypefaceFromStream(fontStream);

UseTypefaceFromStream(Stream, int)

Sets the typeface from a .NET Stream.

public void UseTypefaceFromStream(Stream stream, int index = 0)

Parameters

  • stream (Stream) - The font stream.
  • index (int) - The index of the font in the stream. Default is 0.

Example

using var fileStream = File.OpenRead("fonts/custom.ttf");
options.UseTypefaceFromStream(fileStream);

Configuration Examples

Basic Configuration

var options = new BarcodeOptions
{
    Type = BarcodeTypes.Ean13,
    Height = 50,
    Scaling = 3,
    RenderText = true
};

Custom Colors and Margins

options.BackgroundColor = SKColors.White;
options.ForegroundColor = SKColor.Parse("#2C3E50"); // Dark blue-gray
options.Margins = 15; // Generous margins for printing

Font Configuration

// Simple font family
options.UseTypeface("Arial");

// Font with style
options.UseTypeface("Times New Roman", SKFontStyle.Bold);

// Custom font file
options.UseTypefaceFromFile("assets/fonts/barcode-font.ttf");

High-Quality Print Configuration

var printOptions = new BarcodeOptions
{
    Type = BarcodeTypes.Ean13,
    Height = 100,           // Taller for better scanning
    Scaling = 6,            // Larger scale for high DPI
    Margins = 20,           // Adequate white space
    BackgroundColor = SKColors.White,
    ForegroundColor = SKColors.Black,
    RenderText = true
};
printOptions.UseTypeface("Arial", SKFontStyle.Normal);

Minimal Barcode Configuration

var minimalOptions = new BarcodeOptions
{
    Type = BarcodeTypes.Code93,
    Height = 25,            // Compact height
    Scaling = 1,            // Minimal scale
    Margins = 0,            // No margins
    RenderText = false,     // No text
    BackgroundColor = SKColors.Transparent
};

Web-Optimized Configuration

var webOptions = new BarcodeOptions
{
    Type = BarcodeTypes.Ean13,
    Height = 40,
    Scaling = 2,
    Margins = 5,
    BackgroundColor = SKColors.Transparent,
    ForegroundColor = SKColors.Black,
    RenderText = true
};
webOptions.UseTypeface("Arial", SKFontStyle.Normal);

Usage Patterns

Configuration with Action Delegate

using var barcode = new Barcode(options =>
{
    options.Type = BarcodeTypes.Code93;
    options.Height = 60;
    options.Scaling = 4;
    options.UseTypeface("Consolas", SKFontStyle.Bold);
});

Reusable Configuration

public static void ConfigureForPrint(BarcodeOptions options)
{
    options.Height = 80;
    options.Scaling = 5;
    options.Margins = 20;
    options.BackgroundColor = SKColors.White;
    options.ForegroundColor = SKColors.Black;
    options.UseTypeface("Arial", SKFontStyle.Normal);
}

// Use the configuration
using var barcode = new Barcode(ConfigureForPrint);

Options Validation

public static bool ValidateOptions(BarcodeOptions options)
{
    return options.Height > 0 
        && options.Scaling > 0 
        && options.Margins >= 0;
}

Related Documentation