Add the dependency

In your project’s ziran.toml; pin ref to a commit for reproducible builds.

[dependencies.qrcode]
git = "https://github.com/ziranlang/qrcode.git"
ref = "master"

Overview

It is a port of Project Nayuki's QR Code generator library (MIT License), and its tests match that library module for module.

Use it

ziran add https://github.com/ziranlang/qrcode.git
#import "qrcode/qrcode"

using QrEcc;

code: QrCode;

Draw :: (text: string) {
    if !QrEncodeText(text, cast(QrEcc)QrEccMedium, *code) { return }
    for y: 0..code.size - 1 {
        for x: 0..code.size - 1 {
            if QrModule(*code, x, y) {
                // Paint a dark module at (x, y), inside a light border of
                // at least four modules.
            }
        }
    }
}

A QrCode is about 70 KB, so keep it in a global or on the heap rather than on a small thread stack.

Check it

make

qrcode/src/qrcode.zi

qrcode

QR Code Model 2 encoding of text as one byte-mode segment, for versions 1 to 40, the four error correction levels and an automatically chosen mask. A port of Project Nayuki's QR Code generator library (MIT License, see LICENSE). It allocates nothing: a QrCode holds its modules and the working space the encoder needs.

Import with #import "qrcode/qrcode".

QrEcc :: enum { QrEccLow = 0; QrEccMedium = 1; QrEccQuartile = 2; QrEccHigh = 3; }
QR_VERSION_MIN :: 1
QR_VERSION_MAX :: 40
QR_SIZE_MAX :: 177
QR_CODEWORDS_MAX :: 3706
QR_BLOCK_MAX :: 256
QR_ECC_MAX :: 30
QrCode :: struct { version: s32; size: s32; ecc: QrEcc; mask: s32; modules: [31329]u8; function: [31329]u8; data: [3706]u8; codewords: [3706]u8; blocks: [3962]u8; divisor: [30]u8; remainder: [30]u8; bits: s32; }
qr_ecc_codewords_per_block: [164]s32
qr_error_correction_blocks: [164]s32
QR_PENALTY_N1 :: 3
QR_PENALTY_N2 :: 3
QR_PENALTY_N3 :: 40
QR_PENALTY_N4 :: 10
QrEccCodewords :: (ecc: QrEcc, version: s32) -> s32
QrEccBlocks :: (ecc: QrEcc, version: s32) -> s32
QrRawDataModules :: (version: s32) -> s32

Data bits a version holds once function modules are excluded, including remainder bits.

QrDataCodewords :: (version: s32, ecc: QrEcc) -> s32
QrFormatLevel :: (ecc: QrEcc) -> s32
QrBit :: (value: s32, index: s32) -> bool
QrModule :: (qr: *QrCode, x: s32, y: s32) -> bool
QrSetFunction :: (qr: *QrCode, x: s32, y: s32, dark: bool)
QrAppendBits :: (qr: *QrCode, value: s32, count: s32)
QrMultiply :: (x: s32, y: s32) -> s32
QrComputeDivisor :: (qr: *QrCode, degree: s32)
QrComputeRemainder :: (qr: *QrCode, start: s32, count: s32, degree: s32)

The Reed-Solomon remainder of qr.blocks[start:start + count].

QrAddEccAndInterleave :: (qr: *QrCode)

Splits the data into blocks, appends each block's error correction and interleaves the blocks into qr.codewords.

QrDrawFormatBits :: (qr: *QrCode, mask: s32)
QrDrawVersion :: (qr: *QrCode)
QrDrawFinder :: (qr: *QrCode, x: s32, y: s32)
QrDrawAlignment :: (qr: *QrCode, x: s32, y: s32)
QrDrawFunctionPatterns :: (qr: *QrCode)
QrDrawCodewords :: (qr: *QrCode)
QrApplyMask :: (qr: *QrCode, mask: s32)
QrPenaltyAddHistory :: (qr: *QrCode, length: s32, history: []s32)
QrPenaltyCountPatterns :: (history: []s32) -> s32
QrPenaltyTerminate :: (qr: *QrCode, dark: bool, length: s32, history: []s32) -> s32
QrPenaltyScore :: (qr: *QrCode) -> s32
QrEncodeText :: (text: string, ecc: QrEcc, qr: *QrCode) -> bool

Encodes text in byte mode at the smallest version that holds it, raising the error correction level while the version stays the same. Returns false when the text does not fit in version 40 at the requested level.

Source ↗