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 :: 1QR_VERSION_MAX :: 40QR_SIZE_MAX :: 177QR_CODEWORDS_MAX :: 3706QR_BLOCK_MAX :: 256QR_ECC_MAX :: 30QrCode :: 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]s32qr_error_correction_blocks: [164]s32QR_PENALTY_N1 :: 3QR_PENALTY_N2 :: 3QR_PENALTY_N3 :: 40QR_PENALTY_N4 :: 10QrEccCodewords :: (ecc: QrEcc, version: s32) -> s32QrEccBlocks :: (ecc: QrEcc, version: s32) -> s32QrRawDataModules :: (version: s32) -> s32Data bits a version holds once function modules are excluded, including remainder bits.
QrDataCodewords :: (version: s32, ecc: QrEcc) -> s32QrFormatLevel :: (ecc: QrEcc) -> s32QrBit :: (value: s32, index: s32) -> boolQrModule :: (qr: *QrCode, x: s32, y: s32) -> boolQrSetFunction :: (qr: *QrCode, x: s32, y: s32, dark: bool)QrAppendBits :: (qr: *QrCode, value: s32, count: s32)QrMultiply :: (x: s32, y: s32) -> s32QrComputeDivisor :: (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) -> s32QrPenaltyTerminate :: (qr: *QrCode, dark: bool, length: s32, history: []s32) -> s32QrPenaltyScore :: (qr: *QrCode) -> s32QrEncodeText :: (text: string, ecc: QrEcc, qr: *QrCode) -> boolEncodes 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.