coding

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package coding implements low-level QR and Micro QR coding details.

Index

Constants

View Source
const (
	ClassM1 = iota // Micro QR version M1
	ClassM2        // Micro QR version M2
	ClassM3        // Micro QR version M3
	ClassM4        // Micro QR version M4
	Class0         // QR versions 1 to 9
	Class1         // QR versions 10 to 26
	Class2         // QR versions 27 to 40
)

Micro QR and QR version size classes.

Variables

View Source
var (
	ErrLevel   = errors.New("qr: invalid level")
	ErrVersion = errors.New("qr: invalid version")
)
View Source
var Field = gf256.NewField(0x11d, 2)

Field is the field for QR error correction.

Functions

func Is added in v0.5.0

func Is(r rune, mode Mode) bool

Is reports whether r is encodable in mode.

func IsKanji added in v0.5.0

func IsKanji(r rune) bool

IsKanji reports whether the Unicode rune r belongs to the QR Kanji subset of JIS X 0208.

Types

type AcceptsFunc added in v0.5.0

type AcceptsFunc func(rune) bool // Used in ModeEncoder.

type BitStream added in v0.5.0

type BitStream struct {
	// contains filtered or unexported fields
}

BitStream reads bits from the underlying buffer.

func NewBitStream added in v0.5.0

func NewBitStream(b []byte) BitStream

NewBitStream returns a BitStream reading from b.

func (*BitStream) Bytes added in v0.5.0

func (s *BitStream) Bytes() []byte

Bytes returns the data underlying s.

func (*BitStream) Next added in v0.5.0

func (s *BitStream) Next() byte

Next returns the next bit from s as 0 or 1. Past end of buffer Next returns 0.

type Bits

type Bits struct {
	// contains filtered or unexported fields
}

func NewBits added in v0.5.0

func NewBits(v Version, l Level) *Bits

NewBits returns Bits with enough capacity for a QR code of the given version and level.

func (*Bits) Add added in v0.5.0

func (b *Bits) Add(n int) []byte

Add adds n bytes to b and returns the added slice.

func (*Bits) AddCheckBytes

func (b *Bits) AddCheckBytes(v Version, l Level)

AddCheckBytes adds terminator, padding and checksum to b for the given QR version and level.

func (*Bits) Bits

func (b *Bits) Bits() int

func (*Bits) Bytes

func (b *Bits) Bytes() []byte

func (*Bits) Grow added in v0.5.0

func (b *Bits) Grow(n int)

func (*Bits) PadTo added in v0.5.0

func (b *Bits) PadTo(t, n int)

PadTo adds up to t terminator bits to b and pads it to n bit.

func (*Bits) Permute added in v0.5.0

func (b *Bits) Permute(v Version, l Level) BitStream

Permute returns a BitStream reading data and checksum bits in b with blocks interleaved for the given QR code version and level. The BitStream may use the same underlying buffer.

func (*Bits) Reset

func (b *Bits) Reset()

func (*Bits) Write

func (b *Bits) Write(v uint32, nbit int)

type Code

type Code struct {
	Bitmap []byte // 1 is black, 0 is white
	Size   int    // number of pixels on a side
	Stride int    // number of bytes per row
}

A Code is a square pixel grid.

func Encode added in v0.5.0

func Encode(version Version, level Level, text ...Segment) (*Code, error)

Encode encodes text using an Encoder with the given version and level.

func (*Code) Black

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

func (*Code) Penalty added in v0.5.0

func (c *Code) Penalty() int

Penalty returns the penalty value for a QR code, or the negative evaluation score for a Micro QR code. The value is used for choosing the mask.

type CompatError added in v0.7.0

type CompatError struct {
	Mode
	Version
}

CompatError represents an incompatibility between Mode and Version.

func (CompatError) Error added in v0.7.0

func (e CompatError) Error() string

type CutRuneFunc added in v0.5.0

type CutRuneFunc func(string) (rune, int) // Used in ModeEncoder.

type Encoder added in v0.5.0

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder encodes a QR code.

func NewEncoder added in v0.5.0

func NewEncoder(version Version, level Level) (*Encoder, error)

NewEncoder returns an Encoder for the given version and level.

func (*Encoder) Code added in v0.5.0

func (e *Encoder) Code() (*Code, error)

Code returns a QR code containing data written to e.

func (*Encoder) Encode added in v0.5.0

func (e *Encoder) Encode(text ...Segment) (*Code, error)

Encode is a wrapper around Write and Code.

func (*Encoder) Reset added in v0.5.0

func (e *Encoder) Reset()

func (*Encoder) Write added in v0.5.0

func (e *Encoder) Write(text ...Segment) error

Write adds text to e.

type Level

type Level int

A Level represents a QR error correction level. From least to most tolerant of errors, they are L, M, Q, H.

const (
	L Level = iota
	M
	Q
	H
)

func (Level) String

func (l Level) String() string

type Mode added in v0.5.0

type Mode int16

A Mode is a QR segment encoder.

const (
	Numeric       Mode = iota // numeric mode, ASCII-compatible text
	Alphanumeric              // alphanumeric mode, ASCII-compatible text
	Byte                      // byte mode, any data
	Kanji                     // kanji mode, UTF-8 text
	Latin1                    // byte mode, UTF-8 text encoded as ISO 8859-1
	ShiftJISKanji             // kanji mode, Shift JIS text
	ECI                       // eci mode, raw segment
	StructAppend              // structured append, raw segment
)

Predefined encoding modes.

func AddMode added in v0.5.0

func AddMode(m *ModeEncoder) Mode

AddMode registers an encoding mode, returning its number on success or -1 on failure. The number of modes is limited to 32768.

func (Mode) Length added in v0.5.0

func (mode Mode) Length(bytes, runes int, class int) int

Length returns the length in bits of a valid string of the given length in bytes and runes encoded in mode at the given QR version size class, including the header. Length returns 0 if and only if mode is invalid.

func (Mode) MinClass added in v0.7.0

func (mode Mode) MinClass() int

MinClass returns the lowest valid version size class for mode.

func (Mode) RuneFilter added in v0.5.0

func (mode Mode) RuneFilter() (CutRuneFunc, AcceptsFunc)

RuneFilter returns CutRune and Accepts functions for mode. If mode is invalid, RuneFilter returns nil and a function rejecting any rune.

func (Mode) String added in v0.6.0

func (mode Mode) String() string

type ModeEncoder added in v0.5.0

type ModeEncoder struct {
	Name      string // Name for error reporting
	Indicator byte   // 4 bit mode indicator for QR codes

	// CountLength lists lengths of the character count field in four
	// Micro QR and three QR version size classes.
	CountLength [7]byte

	// EncodedLength returns the encoded data length in bits of a valid
	// string of the given length in bytes and runes.
	EncodedLength func(bytes, runes int) int

	// Valid reports whether the string is valid for the encoding mode.
	// It is called by Segment.IsValid and by the encoder.  If nil, the
	// string is validated using CutRune and Accepts.
	Valid func(string) bool

	// CutRune returns the first rune in the string and its width in
	// bytes.  If nil, utf8.DecodeRuneInString is used.  It should be
	// set if and only if the Mode requires non-UTF-8 rune decoding.
	CutRune func(string) (rune, int)

	// Accepts reports whether the encoding mode accepts the rune.
	// If nil, any rune is accepted.  It is called by Is.
	Accepts func(rune) bool

	// Transform returns a segment of another Mode with the string
	// transformed for encoding and a boolean indicating whether the
	// transform was successful.  The target Mode must have Transform
	// unset.  If nil, the original segment is used.  It is called by
	// Segment.Transform and by the encoder.
	Transform func(string) (Segment, bool)

	// Count returns the character count of the transformed string.
	// If nil, the length of the string in bytes is used.
	Count func(string) int

	// Encode4, Encode3, Encode2 and Encode1 return the encoding of the
	// bytes and its length in bits.  The encoder calls a non-nil
	// Encode{N} repeatedly as long as N source bytes are available, in
	// descending order of N.  If all are nil, each byte is encoded as
	// 8 bits.  The encoder panics if not all bytes are consumed.
	Encode4 func([4]byte) (uint32, int)
	Encode3 func([3]byte) (uint32, int)
	Encode2 func([2]byte) (uint32, int)
	Encode1 func(byte) (uint32, int)
}

ModeEncoder implements a QR segment encoding.

The segment is validated using either Valid or CutRune and Accepts. Text mode encoders other than Numeric, Alphanumeric, Byte and ShiftJISKanji must have a Transform function returning a segment of one of those modes. If set, it is called by Segment.Transform after validation. The encoder calls Segment.Transform and validates the returned segment before encoding.

Package split uses Indicator to determine valid Micro QR versions; CutRune, Accepts, EncodedLength and CountLength to split text into segments; and Transform for calculating the checksum when splitting input into multiple QR codes (Structured Append).

Name, Indicator and CountLength must be set.

func GetMode added in v0.5.0

func GetMode(mode Mode) *ModeEncoder

GetMode returns a copy of ModeEncoder for the mode. It can be used to base the implementation of a new mode on an existing one.

type ModeError added in v0.6.0

type ModeError Mode

ModeError represents an invalid Mode number or ModeEncoder.

func (ModeError) Error added in v0.6.0

func (e ModeError) Error() string

type Plan

type Plan struct {
	Version Version // QR code version
	Level   Level   // QR error correction Level

	DataBits int // number of data bits
	Size     int // number of pixels on a side

	Map     []byte    // pixel map: 0 is data or checksum, 1 is other
	Pattern [8][]byte // position and alignment boxes, timing, format, mask
}

A Plan describes how to construct a QR code with a specific version and level.

func NewPlan

func NewPlan(version Version, level Level) (*Plan, error)

NewPlan returns a Plan for a QR code with the given version and level.

func (*Plan) Encode

func (p *Plan) Encode(text ...Segment) (*Code, error)

func (*Plan) Serialise added in v0.5.0

func (p *Plan) Serialise(s BitStream, bitmap []byte)

Serialise writes bits from s to the bitmap in zigzag scan order.

type Segment added in v0.5.0

type Segment struct {
	Text string // data to encode
	Mode Mode   // encoding mode
}

A Segment describes a QR code segment.

func (Segment) Encode added in v0.5.0

func (seg Segment) Encode(b *Bits, class int) error

Encode writes seg encoded for the given QR version size class to b.

func (Segment) EncodedLength added in v0.5.0

func (seg Segment) EncodedLength(class int) int

EncodedLength returns the encoded length in bits of seg in the given QR version size class. EncodedLength returns 0 if and only if mode is invalid. The segment is not validated.

func (Segment) IsValid added in v0.5.0

func (seg Segment) IsValid() bool

IsValid reports whether seg is encodable.

func (Segment) Transform added in v0.5.0

func (seg Segment) Transform() (Segment, error)

Transform transforms seg for encoding. The transformed segment is not validated. The encoder calls Transform prior to encoding.

type SegmentError added in v0.5.0

type SegmentError Segment

SegmentError represents an invalid Segment.

func (SegmentError) Error added in v0.5.0

func (e SegmentError) Error() string

type Version

type Version int

A Version represents a QR version. The version specifies the size of the QR code: a QR code with version v has 4v+17 pixels on a side, a Micro QR code with version Mv 2v+9 pixels. Versions run in two sequences, from M1 to M4 and from 1 to 40: the larger the version, the more information the code can store.

const (
	// Micro QR versions
	M1 Version = MaxVersion + 1 + iota
	M2
	M3
	M4

	MinVersion Version = 1  // Minimum QR version
	MaxVersion Version = 40 // Maximum QR version
)

Code versions.

func (Version) DataBits added in v0.7.0

func (v Version) DataBits(l Level) int

DataBits returns the number of data bits that can be stored in a QR code with the given version and level.

func (Version) SizeClass added in v0.5.0

func (v Version) SizeClass() int

SizeClass returns the size class of v, as documented under ClassM1.

func (Version) String

func (v Version) String() string

Jump to

Keyboard shortcuts

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