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

valk-qrcode

Pure-Valk QR code generator implementing ISO/IEC 18004 (versions 1–40,
error correction levels L/M/Q/H), with built-in SVG, PNG, terminal, ASCII, and raw pixel renderers.
Zero external dependencies.

Requires Valk 0.9.1 or newer.

Install

vman install github.com/ctxcode/valk-qrcode

Example

use qrcode
use valk.fs

fn main() {
    let qr = qrcode.encode("https://valk-lang.dev", qrcode.ErrorCorrection.medium) ! panic(E.message)

    // Print directly to the terminal (scannable with a phone camera)
    print(qr.to_terminal(qrcode.TerminalOptions { quiet_zone: 2, invert: true }))

    // Write crisp SVG and 1-bit PNG files with no extra dependencies
    fs.write("qr.svg", qr.to_svg()) ! panic("Cannot write qr.svg")
    fs.write("qr.png", qr.to_png(8, 4)) ! panic("Cannot write qr.png")
}

QR code for https://valk-lang.dev

Encoding

  • qrcode.encode(text, ec) encodes UTF-8 text, automatically choosing the most compact ISO/IEC
    18004 mode (Numeric, Alphanumeric, or Byte) and the smallest version (1–40) that fits.
  • qrcode.encode_bytes(data, ec) encodes arbitrary byte slices (local &[u8]).
  • qrcode.encode_version(data, version, ec) encodes into an explicit version (1..40).

Four error correction levels are available via qrcode.ErrorCorrection:

  • low (~7% recovery, up to 7,089 digits / 4,296 alphanumeric / 2,953 bytes)
  • medium (~15% recovery, default)
  • quartile (~25% recovery)
  • high (~30% recovery)

Rendering

  • SVG: qr.to_svg(SvgOptions { module_size: 8, quiet_zone: 4, dark: "#000000", light: "#ffffff" })
    emits a compact single-<path> SVG with shape-rendering="crispEdges". Pass light: "" for a
    transparent background.
  • PNG: qr.to_png(scale, quiet_zone) emits a valid 1-bit grayscale PNG using valk.compress
    directly, with no external image library needed.
  • Terminal: qr.to_terminal(TerminalOptions { quiet_zone: 2, invert: false }) packs two module
    rows per line using UTF-8 half-block characters (▀, ▄, █, ). Set invert: true on
    dark-background terminals so the quiet zone appears bright.
  • ASCII: qr.to_ascii(quiet_zone, dark, light) renders plain text (defaults to "##" / " ").
  • Raw pixels: qr.to_pixels(scale, quiet_zone, channels) returns an 8-bit pixel buffer
    (1 grayscale, 3 RGB, or 4 RGBA) of size qr.image_size(scale, quiet_zone) squared, ready
    for valk-image or valk-pdf.
  • Direct module inspection: qr.version, qr.size, qr.ec, qr.mask, qr.mode, and
    qr.at(x, y).

Errors

qrcode.Error has two variants with a message payload:

  • data_too_long: the input exceeds the capacity of version 40 (or the requested version) at the
    chosen error correction level.
  • invalid_version: the version passed to encode_version is outside 1..40.

Development

make lint checks the sources (--deny-warnings), make test runs the test suite, make example
runs the example, and make docs regenerates docs/api.md and
docs/api-full.md.