barcodes

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package barcodes encodes a value into the stripe geometry a printout carries.

See doc/printout.md#barcode. A renderer does not need to implement encoding of its own.

Index

Constants

View Source
const (
	MinHeightRatio = 0.15
	MinHeightPt    = 18 // 0.25 in
)

MinHeightRatio is a one-dimensional symbol's minimum bar height as a fraction of its length, and MinHeightPt the floor under that. Together they are the usual recommendation: fifteen per cent of the symbol length, or a quarter of an inch, whichever is greater.

View Source
const (
	// MinSymbolContrast is the reflectance difference between paper
	// and ink that a symbol needs. ISO/IEC 15416 grades this parameter,
	// and forty per cent is its grade C, the usual floor for retail.
	MinSymbolContrast = 0.40
	// MaxInkFraction is the most of the paper's own reflectance that the
	// ink may reflect, which is the standard's minimum-reflectance rule.
	MaxInkFraction = 0.5
)

Scan contrast thresholds, as fractions of full reflectance.

A barcode is read in red light -- around 660 nm for a laser scanner and for most linear imagers, and 670 nm is where ISO/IEC 15416 measures the contrast it grades. So what decides whether a symbol scans is how little red it reflects, not how dark a colour looks. Blue, green, brown and purple bars absorb red and read as bars; red, orange and yellow ones reflect it and are very nearly invisible. The same fact in reverse makes yellow, orange and pink perfectly good backgrounds, and blue or green ones unusable.

View Source
const QuietModules = 10

QuietModules is the quiet zone added to each end of a one-dimensional symbol, in modules. Ten is the usual minimum for the symbologies here.

Variables

This section is empty.

Functions

func CheckContrast

func CheckContrast(inkRed, paperRed uint8) error

CheckContrast reports whether bars of one colour on paper of another can be read, judging both by the red light a scanner uses. Only the red component of each colour is consulted, for the reason above.

The judgement is deliberately coarse. It works from the colours a template names and cannot know what the ink, the substrate, or the printer will do to them, so it catches the combinations that cannot work in principle -- yellow bars, a navy background -- and nothing subtler. A symbol going into production still wants verifying off a printed label.

Types

type Metrics

type Metrics struct {
	// Length is the extent along the coding direction, in points.
	Length float64
	// Cross is the extent across it, in points.
	Cross float64
	// Module is the narrow-element width in points, after any grow adjustment.
	Module float64
}

Metrics is a symbol placed at a module width.

func Measure

func Measure(sym *Symbol, module float64, grow bool, boxLength, boxCross float64) Metrics

Measure sizes a symbol.

The box always grows along the coding direction to at least the symbol's minimum size. grow expands the symbol to use the available box: for a two-dimensional type that recomputes the module, and for a one-dimensional one it grows the bar height.

type Symbol

type Symbol struct {
	// Type is the symbology, as the template spells it.
	Type string
	// Value is the encoded string, recorded for renderers printing
	// bar codes directly -- such as barcode printer drivers.
	Value string
	// TwoD reports whether the symbol is a matrix rather than a bar pattern.
	TwoD bool
	// Stripes for a one-dimensional symbol: alternating space and bar widths,
	// starting with a space, which is the leading quiet zone.
	Stripes []int
	// Rows for a two-dimensional symbol: per row, alternating light and dark
	// run lengths, starting with light. A row that opens dark -- which only
	// a symbology with no quiet zone can do -- opens with a zero-length
	// light run.
	Rows [][]int
	// Modules is the symbol's extent along the coding direction,
	// in modules, quiet zones included.
	Modules int
	// CrossModules is the extent across the coding direction, in modules.
	// It is meaningful only for a two-dimensional symbol.
	CrossModules int
}

Symbol is an encoded barcode, in modules.

func Encode

func Encode(kind, value string) (*Symbol, error)

Encode produces the symbol for a value.

Content a type cannot encode is an error naming the type, the value, and the reason.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL