barcode

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package barcode makes QR codes and barcodes as drawings: the symbols that forms carry for the machines that read them (slips, labels, invoices).

Encode makes a symbol of a text: a QR code (model 2, versions 1 to 40, the four levels of error correction, in the numeric, alphanumeric or byte mode that the text needs), or a linear barcode: JAN/EAN-13, EAN-8, UPC-A, Code 39, Code 128, GS1-128, ITF (interleaved 2 of 5, ITF-14) and NW-7 (Codabar). A symbol is a grid of modules with its quiet zone, and Path draws its dark modules in a box, with human-readable digits for JAN/EAN and UPC-A: as vectors, which stay sharp at any size and in print.

The package writes symbols; it does not read them. It does not check what a text means (that the digits of a JAN code are those of a product), only that the symbology can hold it, and the check digits that a symbology has.

Digit outlines derived from M PLUS 1p Regular 1.062. Copyright 2016 The M+ Project Authors. Licensed under the SIL Open Font License 1.1; see OFL-MPLUS.txt. The outlines use font coordinates (y up), with an advance of 620.

Index

Constants

View Source
const (
	QR      = "qr"
	EAN13   = "ean13" // JAN (13 digits)
	EAN8    = "ean8"  // JAN (8 digits)
	UPCA    = "upca"
	Code39  = "code39"
	Code128 = "code128"
	GS1128  = "gs1-128"
	ITF     = "itf"
	ITF14   = "itf14"
	NW7     = "nw7" // Codabar
)

The symbologies.

View Source
const MaxText = 8192

MaxText bounds the text of a symbol, in bytes: more than a QR code holds.

View Source
const NoQuiet = -1

NoQuiet is the Options.Quiet of a symbol without a quiet zone.

Variables

This section is empty.

Functions

func Kind

func Kind(name string) string

Kind returns the symbology of a name ("jan", "JAN-13", "Codabar" …), or "" for a name that is none.

func Kinds

func Kinds() []string

Kinds lists the symbologies by the names Kind takes for them.

Types

type Mark added in v0.2.0

type Mark struct {
	// Kind is the symbology, and Text what the symbol says (with the
	// check digit Encode adds).
	Kind string `json:"kind"`
	Text string `json:"text"`
	// Level is the level of error correction of a QR code.
	Level string `json:"level,omitempty"`
	// Quiet is Options.Quiet: 0 the quiet zone of the symbology, -1
	// none, else its modules.
	Quiet int `json:"quiet,omitempty"`
	// NoText is set when the drawing has no digits under the bars.
	NoText bool `json:"noText,omitempty"`
	// Box is the box the symbol and its quiet zone are drawn in (x, y,
	// width, height), in the units of the drawing after the mark: the box
	// of Symbol.Path.
	Box [4]float32 `json:"box"`
}

Mark is what a BARCODE mark of a drawing says (bdf.MarkBarcode, docs/spec.md §7.8): the symbol that the drawing after it draws, and the box it is drawn in. A printer of labels prints such a symbol with its own barcode commands, and a raster of few dots an inch draws its modules on whole dots; anything else draws the drawing as it is.

func ParseMark added in v0.2.0

func ParseMark(payload string) (*Mark, error)

ParseMark reads the payload of a BARCODE mark.

func (*Mark) Symbol added in v0.2.0

func (m *Mark) Symbol() (*Symbol, error)

Symbol makes the symbol of a mark again.

type Options

type Options struct {
	// Level is the level of error correction of a QR code: "L" (7% of
	// the symbol may be lost), "M" (15%, the default), "Q" (25%) or "H"
	// (30%).
	Level string
	// Quiet is the width of the quiet zone around the symbol in modules:
	// the white margin readers need. 0 is that of the symbology (4
	// modules around a QR code, 10 at the sides of most barcodes), and
	// NoQuiet none: the margin is then for what is around the symbol to
	// leave.
	Quiet int
	// NoText leaves out the human-readable digits of JAN/EAN and UPC-A.
	// The digits are drawn by Path and Object by default, including a
	// check digit added by Encode. The modules are unchanged either way.
	NoText bool
}

Options are the options of a symbol.

type Symbol

type Symbol struct {
	// Kind is the symbology.
	Kind string
	// Text is what the symbol says: the text, with the check digit of
	// the symbology when the text came without it.
	Text string
	// W and H are the size of the grid with its quiet zone, in modules.
	// A linear barcode is a module high; Path leaves room for the digits
	// of JAN/EAN and UPC-A inside the box where it draws the bars.
	W, H int
	// Linear is set for a barcode of bars.
	Linear bool
	// contains filtered or unexported fields
}

Symbol is a symbol: a grid of modules, dark or light.

func Encode

func Encode(kind, text string, opts *Options) (*Symbol, error)

Encode makes the symbol of a text in a symbology (a name Kind knows).

func (*Symbol) At

func (s *Symbol) At(x, y int) bool

At reports whether the module at column x and row y is dark.

func (*Symbol) BarHeight added in v0.2.0

func (s *Symbol) BarHeight(w, h float32) float32

BarHeight returns the height of the bars of the symbol drawn in a box w × h by Path: h less the room of the digits.

func (*Symbol) Bars added in v0.2.0

func (s *Symbol) Bars() (x0, x1 int)

Bars returns the columns of the symbol's first and last dark modules (x1 is past the last): the symbol without its quiet zone.

func (*Symbol) Fill added in v0.2.0

func (s *Symbol) Fill(o *bdf.Object, x, y, w, h float32)

Fill draws the symbol in the box at (x, y) of the size w × h on an object, in its fill colour, after the BARCODE mark that says what it is.

func (*Symbol) HasText added in v0.2.0

func (s *Symbol) HasText() bool

HasText reports whether Path draws digits under the bars.

func (*Symbol) Mark added in v0.2.0

func (s *Symbol) Mark(x, y, w, h float32) string

Mark returns the payload of the BARCODE mark of the symbol drawn in the box at (x, y) of the size w × h, as Path draws it.

func (*Symbol) Object

func (s *Symbol) Object(w, h float32) *bdf.Object

Object returns the symbol as an object of the size w × h: its dark modules and, for JAN/EAN and UPC-A, its digits, in black on nothing. Options.NoText leaves out the digits. The object needs no font.

func (*Symbol) Path

func (s *Symbol) Path(x, y, w, h float32) *bdf.Path

Path returns the symbol (and its quiet zone) in the box at (x, y) of the size w × h. Its dark modules are rectangles. JAN/EAN and UPC-A also have their digits below the bars unless Options.NoText was set; the bars are shortened to leave room inside the box. The digits are outlines, so no font is needed. Fill the path with the nonzero rule.

func (*Symbol) Rects

func (s *Symbol) Rects() [][4]int

Rects returns the dark modules as rectangles of modules (x, y, width, height): the runs of dark modules of the rows, with those that are the same in rows that follow each other as one.

Jump to

Keyboard shortcuts

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