Documentation ¶
Overview ¶
Package coding implements low-level QR coding details.
Index ¶
- Constants
- Variables
- func Is(r rune, mode Mode) bool
- func IsKanji(r rune) bool
- type AcceptsFunc
- type BitStream
- type Bits
- func (b *Bits) Add(n int) []byte
- func (b *Bits) AddCheckBytes(v Version, l Level)
- func (b *Bits) Bits() int
- func (b *Bits) Bytes() []byte
- func (b *Bits) Grow(n int)
- func (b *Bits) PadTo(n int)
- func (b *Bits) Permute(v Version, l Level) BitStream
- func (b *Bits) Reset()
- func (b *Bits) Write(v uint32, nbit int)
- type Code
- type CutRuneFunc
- type Encoder
- type Level
- type Mode
- type ModeEncoder
- type ModeError
- type Plan
- type Segment
- type SegmentError
- type Version
Constants ¶
const MaxVersion = 40
const MinVersion = 1
Variables ¶
var ( ErrLevel = errors.New("qr: invalid level") ErrVersion = errors.New("qr: invalid version") )
var Field = gf256.NewField(0x11d, 2)
Field is the field for QR error correction.
Functions ¶
Types ¶
type AcceptsFunc ¶ added in v0.5.0
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
NewBitStream returns a BitStream reading from b.
type Bits ¶
type Bits struct {
// contains filtered or unexported fields
}
func NewBits ¶ added in v0.5.0
NewBits returns Bits with enough capacity for a QR code of the given version and level.
func (*Bits) AddCheckBytes ¶
AddCheckBytes adds padding and checksum to b for the given QR version and level.
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.
type CutRuneFunc ¶ added in v0.5.0
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
NewEncoder returns an Encoder for the given version and level.
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.
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
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) 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.
type ModeEncoder ¶ added in v0.5.0
type ModeEncoder struct { Name string // Name for error reporting Indicator byte // 4 bit mode indicator // CountLength lists lengths of the character count field in three // QR version size classes. CountLength [3]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 must have a Transform function returning a Numeric, Alphanumeric, Byte or ShiftJISKanji mode segment. 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 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).
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.
type Plan ¶
type Plan struct { Version Version // QR code version Level Level // QR error correction Level DataBytes int // number of data bytes 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.
type Segment ¶ added in v0.5.0
A Segment describes a QR code segment.
func (Segment) Encode ¶ added in v0.5.0
Encode writes seg encoded for the given QR version size class to b.
func (Segment) EncodedLength ¶ added in v0.5.0
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.
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. Versions number from 1 to 40: the larger the version, the more information the code can store.
func (Version) DataBytes ¶
DataBytes returns the number of data bytes that can be stored in a QR code with the given version and level.