qr

package
v0.2.2-dev.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package qr is a small QR Code encoder (ISO/IEC 18004, Model 2): byte mode, versions 1-40, error correction levels L/M/Q/H. It exists so the CLI can print a QR code to a terminal and the panel can draw one as an SVG (svg.go) without a dependency.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrWindowLevel means a window was asked for at a level below Q: a logo takes error correction that L and M cannot spare.
	ErrWindowLevel = errors.New("qr: a window for a logo needs error correction level Q or H")
	// ErrWindowPatterns means the window would cover a pattern the readers find the code by.
	ErrWindowPatterns = errors.New("qr: the window would cover a finder, timing, format, version or the last alignment pattern")
	// ErrWindowBudget means the window would use up too much of the error correction of this symbol.
	ErrWindowBudget = errors.New("qr: the window leaves too little error correction for reading")
	// ErrOptions means an option is out of range (a negative or not-a-number window, a quiet zone over 32).
	ErrOptions = errors.New("qr: invalid SVG options")
)
View Source
var ErrTooLong = errors.New("qr: data too long for a QR code")

ErrTooLong means the data does not fit in a version 40 symbol at the chosen level.

Functions

func LogoWindow

func LogoWindow(size int) float64

LogoWindow is the size, in modules, the board's page gives the logo window of a code `size` modules wide: 26.9 % of a 29-module code, 22.3 % of a 53-module one.

Types

type Code

type Code struct {
	Size int // modules per side, 4*version+17
	// contains filtered or unexported fields
}

Code is an encoded symbol, without the quiet zone.

func Encode

func Encode(data []byte, level Level) (*Code, error)

Encode encodes data in byte mode in the smallest version that fits at level.

func (*Code) Black

func (c *Code) Black(x, y int) bool

Black reports whether the module at column x, row y is dark. Outside the symbol it is false.

func (*Code) FitWindow

func (c *Code) FitWindow(want float64) float64

FitWindow is the largest window not over want (in steps of half a module) that SVG accepts for this code, or 0 when even the smallest does not fit (a level below Q, a version 1 symbol, ...). A caller lays its logo at the size this returns.

func (*Code) Packed

func (c *Code) Packed() []byte

Packed is the modules row by row, eight to a byte with the first module in the high bit, a dark module a 1 bit; the last byte is padded with zeros. A caller that draws the code itself takes this: it is Size*Size/8 bytes where an SVG of the same code is about thirty times that.

func (*Code) SVG

func (c *Code) SVG(o SVGOptions) ([]byte, error)

SVG draws the code as an SVG image: one path, in module units, so the viewBox is the size of the symbol and the quiet zone and the image scales to its box. Nothing of the encoded data is in it, only coordinates; the colours are written from three bytes each, so no text of the caller's reaches the markup.

func (*Code) Terminal

func (c *Code) Terminal(invert bool) string

Terminal draws the code with Unicode half blocks, two modules per text row, inside the 4-module quiet zone (ISO/IEC 18004 "Quiet zone"). Every row ends with a newline.

By default the light modules are the blocks, which scans on a dark terminal (light text on a dark background). With invert the dark modules are the blocks, for a light terminal.

type Color

type Color struct{ R, G, B uint8 }

Color is an sRGB colour. The SVG only ever contains it as #rrggbb, never as text from the caller.

type Level

type Level int

Level is the error correction level.

const (
	L Level = iota // recovers about 7% of the codewords
	M              // about 15%
	Q              // about 25%
	H              // about 30%
)

The order matches the rows of the tables.

type SVGOptions

type SVGOptions struct {
	// Rounded draws every connected figure of dark modules as one shape along its outline, with a corner that sticks out
	// rounded by half a module: the shape of the board's page (roundedPath in web/src/lib/qr-path.ts). Without it the modules
	// are squares, a run in a row one rectangle.
	Rounded bool
	// Fillet, with Rounded, fills a corner that goes in (a light module with a dark one on each of two sides) with a quarter
	// of a circle of 0.3 of a module, which makes the figures look cast. Modules that touch only at a corner stay two figures,
	// and the light module on each side of that point gets a fillet of its own.
	Fillet bool
	// Eyes draws the three finder patterns as rounded squares (a 7x7 ring of radius 2, its 5x5 cut-out of radius 1.2 and the
	// 3x3 centre of radius 0.9) after the data, and leaves the modules in those squares out of the data, as the board's path
	// does (eyes in web/src/lib/qr-path.ts).
	Eyes bool
	// Window is the side, in modules, of a clear square in the middle for a logo the caller lays over it (the library
	// does not draw the logo). Every module that touches the square widened by half a module on each side is left out.
	// 0 means no window. A window needs level Q or H, must leave the finder, timing, format and version patterns and the
	// bottom-right alignment pattern alone, and must not use up more error correction than windowShare allows; see
	// FitWindow for the largest window that does.
	Window float64
	// Quiet is the quiet zone around the code, in modules, 0 to 32 (ISO/IEC 18004 asks for 4; a page that draws the code on
	// a white card of its own uses 0).
	Quiet int
	// Dark is the colour of the modules; nil leaves the fill out, so the page's CSS (fill: currentColor) or black decides.
	Dark *Color
	// Light draws a background of this colour over the symbol and the quiet zone; nil draws none.
	Light *Color
}

SVGOptions is how SVG draws a code. The zero value is square black modules, no quiet zone, no background.

Jump to

Keyboard shortcuts

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