Files
skills/docs/foundation/barcode/barcode/src/symbologies/itf.md
2026-05-29 08:20:56 +01:00

2.6 KiB
Raw Blame History

itf

Purpose

Implements ITF (Interleaved 2 of 5) barcode encoding for numeric-only data. ITF is widely used in logistics, shipping, and inventory applications because it achieves high density by interleaving two digit streams — one encoded in bars and one in spaces — simultaneously. This module encodes digit pairs into alternating bar/space elements following the ISO/IEC 16390 standard, with start and stop guards.

Features

  • encode(data) — encodes an even-length digit string into a binary bar pattern.
  • validate(data) — verifies digit-only content and even length requirement.
  • Narrow element = 1 module, wide element = 3 modules (3:1 ratio).
  • Start guard 0000, stop guard 100.
  • Each digit pair interleaved: 5 bar elements (first digit) × 5 space elements (second digit).
  • Throws BarcodeError with stable codes on invalid input.

Usage: Basic Example

import { encode, validate } from "@suaveplan/barcode/symbologies";

console.log(validate("12345678")); // true
console.log(validate("1234567"));  // false — odd length

const bits = encode("12345678");

Usage: Advanced Example

import { encode } from "@suaveplan/barcode/symbologies";

// Encode a 14-digit GTIN (common ITF-14 use case)
const bits = encode("12345678901231");

// ITF structure: 0000 (start) + pairs + 100 (stop)
console.log(bits.startsWith("0000")); // true
console.log(bits.endsWith("100"));    // true

API Reference

validate(data: string): boolean

Returns true if data matches /^\d+$/ and data.length is even and greater than zero. Returns false without throwing.

encode(data: string): string

Encodes ITF data into a binary string. Processes digits in pairs: the first digit of each pair drives bar widths and the second drives space widths.

Segment Description
Start 0000 (4 narrow modules)
Data Each pair → 5 interleaved bar+space segments (1 or 3 modules each)
Stop 100 (wide bar + 2 narrow)

Throws BarcodeError(BARCODE_CODES.INVALID_DATA) for non-digit input or odd-length data.

Implementation Notes

The ITF_DIGIT table uses boolean arrays (false = narrow, true = wide) for each digit 0–9, with exactly 2 wide elements per digit. The interleaving algorithm iterates through the 5 element positions and alternates: barWidth repeated 1 characters then spaceWidth repeated 0 characters. This produces a continuous binary string where 1 represents a bar and 0 represents a space, directly usable by the SVG renderer. The 3:1 wide-to-narrow ratio is standard for ITF; some implementations use 2.5:1 but 3:1 provides better scanner compatibility.