pkg

ctxcode/valk-qrcode

Pure-Valk QR code generator with SVG, PNG and terminal renderers

github.com/ctxcode/valk-qrcode ↗
0.2.0 latest Valk ≥ 0.9.1

Install

vman install github.com/ctxcode/valk-qrcode

Adds the package to your valk.json. To allow only some versions, add them: vman install github.com/ctxcode/valk-qrcode 0.2.x

Documentation

Namespaces: main


main

Enums for 'main'

// Error correction level (ISO/IEC 18004 Table 12).
+ enum ErrorCorrection { low, medium, quartile, high }
// Data encoding mode selected automatically for the input.
+ enum Mode { numeric, alphanumeric, byte }

ErrorCorrection

Error correction level (ISO/IEC 18004 Table 12).

Higher levels recover more damaged modules at the cost of lower data capacity.

Mode

Data encoding mode selected automatically for the input.

Functions for 'main'

// Encodes UTF-8 `text` into a QR code at error correction level `ec`.
+ fn encode(text: String, ec: ErrorCorrection (ErrorCorrection.medium)) QrCode !Error
// Encodes raw bytes `data` into a QR code at error correction level `ec`.
+ fn encode_bytes(data: local &[u8], ec: ErrorCorrection (ErrorCorrection.medium)) QrCode !Error
// Encodes raw bytes `data` into a QR code of an explicit `version` (1..40) at error correction level `ec`.
+ fn encode_version(data: local &[u8], version: uint, ec: ErrorCorrection (ErrorCorrection.medium)) QrCode !Error

encode

Encodes UTF-8 text into a QR code at error correction level ec.

Automatically picks the most compact encoding mode (numeric, alphanumeric, or byte)
and the smallest QR version (1..40) that fits the input.

encode_bytes

Encodes raw bytes data into a QR code at error correction level ec.

Automatically picks the most compact encoding mode (numeric, alphanumeric, or byte)
and the smallest QR version (1..40) that fits the input.

encode_version

Encodes raw bytes data into a QR code of an explicit version (1..40) at error
correction level ec.

Throws invalid_version when version is outside 1..40 and data_too_long when
data does not fit in that version.

Classes for 'main'

// Thrown when input cannot be encoded as a QR code.
+ union Error {
    data_too_long(String)
    invalid_version(String)

    + get message: String
    + fn to_string() String
}

Error

Thrown when input cannot be encoded as a QR code.

  • data_too_long(message): the input exceeds the capacity of version 40 (or the requested version)
    at the chosen error correction level.
  • invalid_version(message): the requested QR version is outside 1..40.
// A generated QR code symbol (ISO/IEC 18004).
+ class QrCode {
    // Error correction level used to encode this symbol.
    ~ ec: ErrorCorrection
    // Mask pattern selected by ISO penalty scoring (`0..7`).
    ~ mask: u8
    // Encoding mode selected for the payload.
    ~ mode: Mode
    // Width and height in modules (`21..177`, equal to `17 + 4 * version`).
    ~ size: uint
    // Symbol version (`1..40`).
    ~ version: uint

    // Returns `true` if the module at (`x`, `y`) is dark, or `false` if light or out of bounds.
    + fn at(x: uint, y: uint) bool
    // Width and height in pixels of the output produced by `to_png` or `to_pixels`.
    + fn image_size(scale: uint (1), quiet_zone: uint (4)) uint
    // Renders the QR code as plain ASCII art using `dark` and `light` strings per module.
    + fn to_ascii(quiet_zone: uint (2), dark: String ("##"), light: String ("  ")) String
    // Renders raw 8-bit pixels (top row first) for use with `valk-image` or `valk-pdf`.
    + fn to_pixels(scale: uint (1), quiet_zone: uint (4), channels: u8 (1)) mut &[u8]
    // Encodes the QR code directly as a 1-bit grayscale PNG image with zero external dependencies.
    + fn to_png(scale: uint (8), quiet_zone: uint (4)) String
    // Renders the QR code as a compact, crisp SVG string.
    + fn to_svg(options: SvgOptions (SvgOptions{})) String
    // Renders the QR code using UTF-8 half-block characters (`▀`, `▄`, `█`, ` `), packing two module rows into each terminal line so the symbol appears square.
    + fn to_terminal(options: TerminalOptions (TerminalOptions{})) String
}

QrCode

A generated QR code symbol (ISO/IEC 18004).

Inspect individual modules with at(x, y) or render to SVG, PNG, terminal UTF-8,
ASCII, or raw pixels.

ec

Error correction level used to encode this symbol.

mask

Mask pattern selected by ISO penalty scoring (0..7).

mode

Encoding mode selected for the payload.

size

Width and height in modules (21..177, equal to 17 + 4 * version).

version

Symbol version (1..40).

at

Returns true if the module at (x, y) is dark, or false if light or out of bounds.

image_size

Width and height in pixels of the output produced by to_png or to_pixels.

to_ascii

Renders the QR code as plain ASCII art using dark and light strings per module.

to_pixels

Renders raw 8-bit pixels (top row first) for use with valk-image or valk-pdf.

channels selects the pixel layout: 1 for grayscale, 3 for RGB, or 4 for RGBA.
Dark modules are black (0), light modules are white (255), and alpha is opaque (255).

to_png

Encodes the QR code directly as a 1-bit grayscale PNG image with zero external dependencies.

to_svg

Renders the QR code as a compact, crisp SVG string.

to_terminal

Renders the QR code using UTF-8 half-block characters (▀, ▄, █, ), packing
two module rows into each terminal line so the symbol appears square.

// Options for rendering a QR code as an SVG document.
+ class SvgOptions {
    // Fill color for dark modules.
    + dark: String
    // Fill color for the background; set to `""` for a transparent background.
    + light: String
    // Pixel size of a single module in the SVG `width` and `height` attributes.
    + module_size: uint
    // Border width in modules around the QR code (ISO standard recommends 4).
    + quiet_zone: uint
}

SvgOptions

Options for rendering a QR code as an SVG document.

dark

Fill color for dark modules.

light

Fill color for the background; set to "" for a transparent background.

module_size

Pixel size of a single module in the SVG width and height attributes.

quiet_zone

Border width in modules around the QR code (ISO standard recommends 4).

// Options for rendering a QR code to a UTF-8 terminal using half-block characters.
+ class TerminalOptions {
    // Swap foreground and background blocks (set `true` for dark-background terminals so the quiet zone appears bright and phone cameras scan it cleanly).
    + invert: bool
    // Border width in modules around the QR code.
    + quiet_zone: uint
}

TerminalOptions

Options for rendering a QR code to a UTF-8 terminal using half-block characters.

invert

Swap foreground and background blocks (set true for dark-background terminals
so the quiet zone appears bright and phone cameras scan it cleanly).

quiet_zone

Border width in modules around the QR code.