barcode
stableEncode and validate EAN-13, EAN-8, Code 128B, and Code 39 barcodes, with SVG rendering for EAN-13 and Code 128.
use plugin barcode::{encode_ean13, ean13_checksum, encode_code128_b, …} Functions (12)
- encode_ean13 Encode 12 digits into EAN-13 bar pattern
- ean13_checksum Compute EAN-13 check digit
- encode_code128_b Encode text into Code 128B bar pattern
- code128_to_svg Render Code 128B barcode as SVG
- validate_ean13 Validate a 13-digit EAN-13 string
- ean8_checksum Compute EAN-8 check digit
- encode_ean8 Encode 7 digits into EAN-8 bar pattern
- validate_ean8 Validate an 8-digit EAN-8 string
- ean13_to_svg Render EAN-13 barcode as SVG
- validate_code128 Check all chars are valid Code 128B
- encode_code39 Encode text into Code 39 bar pattern
- validate_code39 Check all chars are valid Code 39
Overview
The barcode plugin generates and validates retail and industrial barcodes
entirely in pure Zolo, with no external image dependencies. It supports four
common symbologies: EAN-13 and EAN-8 (the numeric product codes printed on
consumer goods), Code 128B (full ASCII, ideal for shipping and SKUs), and
Code 39 (alphanumeric, common in logistics). Functions come in three flavors —
checksum helpers that compute the trailing check digit, encoders that return a
bars_str module pattern, and validators that confirm a code is well-formed.
EAN-13 and Code 128 additionally offer one-call SVG rendering so you can write
a scannable image straight to disk. The plugin is stateless: every function
takes a string and returns a value, so you can call them in any order.
Common patterns
Build a complete EAN-13 from a 12-digit payload, then confirm it scans:
use plugin barcode::{ean13_checksum, validate_ean13}
let payload = "590123412345"
let check = ean13_checksum(payload)
let full = "{payload}{check}"
print("full code: {full}")
print("valid: {validate_ean13(full)}")
Validate text before encoding to avoid an error on out-of-range characters:
use plugin barcode::{validate_code128, encode_code128_b}
let sku = "ORDER-9001"
if validate_code128(sku) {
let result = encode_code128_b(sku)
print(result["bars_str"])
} else {
print("unsupported characters in {sku}")
}
Render a barcode to an SVG file in a single call:
use plugin barcode::{ean13_to_svg}
use std::fs
let svg = ean13_to_svg("590123412345", 100.0, 2.0)
fs.write("product.svg", svg)
print("wrote product.svg")
Encode 12 digits into EAN-13 bar pattern
Encodes a 12-digit string into an EAN-13 bar pattern. Returns a table with bars_str (a binary string of 0/1 module values) and checksum (the computed 13th digit).
use plugin barcode::{encode_ean13, validate_ean13}
let result = encode_ean13("590123412345")
print(result["checksum"])
print(result["bars_str"])
Compute EAN-13 check digit
Computes the EAN-13 check digit for a 12-digit string. The check digit should be appended to form the full 13-digit barcode.
use plugin barcode::{ean13_checksum}
let check = ean13_checksum("590123412345")
print("check digit: {check}")
Combine it with validate_ean13 to round-trip a payload into a verified code:
use plugin barcode::{ean13_checksum, validate_ean13}
let payload = "400638133393"
let full = "{payload}{ean13_checksum(payload)}"
print("{full} ok? {validate_ean13(full)}")
Encode text into Code 128B bar pattern
Encodes an ASCII text string (characters 32–127) into a Code 128B bar pattern. Returns a table with bars_str where each character represents a module width. Includes start, checksum, and stop.
use plugin barcode::{encode_code128_b, validate_code128}
let text = "HELLO-123"
if validate_code128(text) {
let result = encode_code128_b(text)
print(result["bars_str"])
}
The bars_str is a run-length string: each character is a module count, and
positions alternate bar/space. You can inspect its length to gauge the printed
width:
use plugin barcode::{encode_code128_b}
let result = encode_code128_b("SKU-42")
let pattern = result["bars_str"]
print("pattern length: {pattern.len()}")
Render Code 128B barcode as SVG
Encodes text as Code 128B and renders it directly to an SVG string. height is in pixels, module_width is the width of a single module unit in pixels.
use plugin barcode::{code128_to_svg}
use std::fs
let svg = code128_to_svg("ORDER-9001", 80.0, 2.0)
fs.write("barcode.svg", svg)
Validate a 13-digit EAN-13 string
Returns true if the 13-character string is a valid EAN-13 barcode (correct length, all digits, valid check digit).
use plugin barcode::{validate_ean13}
print(validate_ean13("5901234123457"))
print(validate_ean13("0000000000000"))
Compute EAN-8 check digit
Computes the EAN-8 check digit for a 7-digit string.
use plugin barcode::{ean8_checksum}
let check = ean8_checksum("9638507")
print("check digit: {check}")
Encode 7 digits into EAN-8 bar pattern
Encodes a 7-digit string into an EAN-8 bar pattern. Returns a table with bars_str (binary 0/1 string) and checksum.
use plugin barcode::{encode_ean8}
let result = encode_ean8("9638507")
print(result["checksum"])
print(result["bars_str"])
Validate an 8-digit EAN-8 string
Returns true if the 8-character string is a valid EAN-8 barcode (correct length, all digits, valid check digit).
use plugin barcode::{validate_ean8, ean8_checksum}
let digits7 = "9638507"
let check = ean8_checksum(digits7)
let full = "{digits7}{check}"
print(validate_ean8(full))
Render EAN-13 barcode as SVG
Encodes a 12-digit EAN-13 payload (the checksum is computed automatically) and renders it as an SVG barcode. height and module_width are floating-point pixel values.
use plugin barcode::{ean13_to_svg}
use std::fs
let svg = ean13_to_svg("590123412345", 100.0, 2.0)
fs.write("ean13.svg", svg)
Check all chars are valid Code 128B
Returns true if all characters in text are within the Code 128B printable ASCII range (32–127). Use this before encoding to avoid errors.
use plugin barcode::{validate_code128, encode_code128_b}
let text = "Product-ABC"
if validate_code128(text) {
let encoded = encode_code128_b(text)
print(encoded["bars_str"])
}
Encode text into Code 39 bar pattern
Encodes text into a Code 39 bar pattern string. Input is automatically uppercased. Valid characters are A-Z, 0-9, and the symbols - . $ / + % *. Returns a table with bars_str where each character is n (narrow) or w (wide).
use plugin barcode::{encode_code39, validate_code39}
let text = "ABC-123"
if validate_code39(text) {
let result = encode_code39(text)
print(result["bars_str"])
}
Lowercase input is uppercased automatically, so you can pass mixed-case labels
directly. The bars_str uses n for narrow and w for wide elements:
use plugin barcode::{encode_code39}
let result = encode_code39("part-7")
print(result["bars_str"])
Check all chars are valid Code 39
Returns true if all characters in text are valid Code 39 characters (letters, digits, and allowed symbols). Input is uppercased before checking.
use plugin barcode::{validate_code39}
print(validate_code39("HELLO 123"))
print(validate_code39("invalid!"))