Documentation
¶
Overview ¶
Package rqrcode is a pure-Go (CGO-free) faithful reimplementation of Ruby's rqrcode_core / rqrcode gems (MRI 4.0.5). It generates the QR-code module matrix deterministically and renders it (SVG / ANSI / HTML / PNG) exactly as the gems do, so a host such as go-embedded-ruby can bind RQRCode::QRCode without any Ruby runtime or a C QR library.
Value model ¶
The generated matrix is a [][]bool where true is a dark module. A QRCode is built with New; renderers are methods that mirror rqrcode's `as_svg`, `as_ansi`, `as_html` and `as_png`. Everything here is fully deterministic given input + version (size) + level + mode, so it matches the gems' `to_a` / module layout module-for-module.
Index ¶
- Variables
- type ANSIOptions
- type Level
- type Mode
- type Option
- type Options
- type PNGOptions
- type QRCode
- func (q *QRCode) AsANSI(opts ANSIOptions) string
- func (q *QRCode) AsANSIDefault() string
- func (q *QRCode) AsHTML() string
- func (q *QRCode) AsPNG(opts PNGOptions) ([]byte, error)
- func (q *QRCode) AsSVG(opts SVGOptions) string
- func (q *QRCode) Checked(row, col int) (bool, error)
- func (q *QRCode) ErrorCorrectionLevel() Level
- func (q *QRCode) Mode() Mode
- func (q *QRCode) String() string
- func (q *QRCode) ToString(opts StringOptions) string
- type SVGOptions
- type Segment
- type StringOptions
Constants ¶
This section is empty.
Variables ¶
var ( // ErrArgument corresponds to RQRCodeCore::QRCodeArgumentError. ErrArgument = errors.New("rqrcode: argument error") // ErrRunTime corresponds to RQRCodeCore::QRCodeRunTimeError. ErrRunTime = errors.New("rqrcode: runtime error") )
Sentinel errors mirroring the gem's QRCodeArgumentError / QRCodeRunTimeError.
Functions ¶
This section is empty.
Types ¶
type ANSIOptions ¶
type ANSIOptions struct {
Light string // foreground escape (default "\033[47m")
Dark string // background escape (default "\033[40m")
FillCharacter string // written cell (default " ")
QuietZoneSize int // quiet-zone width (default 4); negative selects the default
}
ANSIOptions configures AsANSI, mirroring rqrcode's as_ansi option hash. Empty strings fall back to the gem defaults, and QuietZoneSize < 0 means "unset" so the default of 4 applies (0 is a meaningful value: no quiet zone).
type Level ¶
type Level int
Level is a QR error-correction level (rqrcode's :l/:m/:q/:h symbols).
type Option ¶
type Option func(*Options)
Option mutates Options, matching rqrcode's keyword arguments.
func WithLevelSymbol ¶
WithLevelSymbol sets the level from a symbol string (:l/:m/:q/:h).
func WithMaxSize ¶
WithMaxSize sets the maximum auto-selected version (:max_size).
func WithModeSymbol ¶
WithModeSymbol forces a mode from a symbol (:number/:alphanumeric/:byte_8bit).
type Options ¶
type Options struct {
// Size is the QR version (1..40). Zero means auto-select the smallest that fits.
Size int
// MaxSize caps auto-selection (default qrMaxSize, i.e. 40).
MaxSize int
// Level is the error-correction level; the zero value LevelM matches Ruby's
// numeric m=0, so callers should use WithLevel / set it explicitly to get :h.
Level Level
// Mode forces an encoding mode; zero means auto-detect from the data.
Mode Mode
// contains filtered or unexported fields
}
Options configures New, mirroring RQRCodeCore::QRCode.new's option hash.
type PNGOptions ¶
type PNGOptions struct {
ModulePxSize int // pixels per module (default 6)
BorderModules int // quiet-zone width in modules (default 4)
Color color.Color // foreground (default black)
Fill color.Color // background (default white)
}
PNGOptions configures AsPNG. This renderer is a pure-Go (stdlib image/png, CGO=0) implementation of rqrcode's "Original" as_png sizing: each module is ModulePxSize pixels square and BorderModules modules of quiet zone surround the code. (The gem's chunky_png "Google" resize mode is intentionally not reproduced byte-for-byte, as it depends on chunky_png's own zlib encoder; the module geometry here is deterministic and standard.)
type QRCode ¶
type QRCode struct {
Modules [][]bool // the dark/light module matrix (Modules[row][col])
ModuleCount int // side length in modules
Version int // QR version (1..40)
// contains filtered or unexported fields
}
QRCode is a generated QR code: the module matrix plus its metadata. It mirrors RQRCodeCore::QRCode's public attributes (modules, module_count, version).
func New ¶
New builds a QR code from a string, matching RQRCode::QRCode.new(string, ...) and RQRCodeCore::QRCode.new. The default level is :h (as in the gems).
func NewMulti ¶
NewMulti builds a multi-segment QR code, matching passing an array of {data:, mode:} hashes to RQRCodeCore::QRCode.new.
func (*QRCode) AsANSI ¶
func (q *QRCode) AsANSI(opts ANSIOptions) string
AsANSI renders the QR code with ANSI background colors, matching RQRCode's as_ansi. The zero-value options reproduce the gem defaults (note: a zero QuietZoneSize means no quiet zone; use -1 or leave DefaultANSIOptions to get 4).
func (*QRCode) AsANSIDefault ¶
AsANSIDefault renders with the gem's default options (quiet zone 4).
func (*QRCode) AsPNG ¶
func (q *QRCode) AsPNG(opts PNGOptions) ([]byte, error)
AsPNG renders the QR code to PNG bytes with the given options, using the deterministic module-per-pixel geometry.
func (*QRCode) AsSVG ¶
func (q *QRCode) AsSVG(opts SVGOptions) string
AsSVG renders the QR code as an SVG document, matching RQRCode's as_svg. The zero-value options reproduce the gem defaults.
func (*QRCode) Checked ¶
Checked reports whether the module at (row, col) is dark, matching RQRCodeCore::QRCode#checked? (note the row, col order).
func (*QRCode) ErrorCorrectionLevel ¶
ErrorCorrectionLevel returns the QR code's level.
func (*QRCode) Mode ¶
Mode returns the effective mode of the (first) data writer, mirroring RQRCodeCore::QRCode#mode.
func (*QRCode) ToString ¶
func (q *QRCode) ToString(opts StringOptions) string
ToString renders the matrix as text, matching RQRCodeCore::QRCode#to_s. The zero-value options reproduce the gem's defaults (dark "x", light " ", no quiet zone).
type SVGOptions ¶
type SVGOptions struct {
Offset int // padding around the QR in pixels (default 0)
OffsetX int // X padding; falls back to Offset when OffsetXSet is false
OffsetXSet bool // whether OffsetX was explicitly provided
OffsetY int // Y padding; falls back to Offset when OffsetYSet is false
OffsetYSet bool // whether OffsetY was explicitly provided
Fill string // background color hex (no leading #); empty = none
FillNamed bool // treat Fill as a named color (no "#" prefix)
Color string // foreground color hex (default "000")
ColorNamed bool // treat Color as a named color (no "#" prefix)
ShapeRendering string // default "crispEdges"
ModuleSize int // pixel size of each module (default 11)
Standalone *bool // full SVG file when true/nil; embeddable svg when false
Viewbox bool // use viewBox instead of width/height
UsePath bool // render with <path> instead of <rect>
SVGAttributes [][2]string
}
SVGOptions configures AsSVG, mirroring rqrcode's as_svg option hash.
type Segment ¶
type Segment struct {
Data string
// Mode forces this segment's mode; zero auto-detects.
Mode Mode
// contains filtered or unexported fields
}
Segment is one piece of multi-segment data (rqrcode's {data:, mode:} hash).
type StringOptions ¶
type StringOptions struct {
Dark string // character(s) for a dark module (default "x")
Light string // character(s) for a light module (default " ")
QuietZoneSize int // quiet-zone width in modules (default 0)
}
StringOptions configures ToString, mirroring RQRCodeCore::QRCode#to_s options.
