The ecosystem · packages/qr
QR codes, in plain Roc
Text in, a code out, drawn as SVG. A page shows one with a component that follows its text; the server draws one into an email; rocx prints one in the terminal.
Use it when a phone should open something from a screen: a link to join a room, a ticket, a page's address. The encoder is plain Roc on no platform, with no JavaScript library or service to call, so the browser, the server, rocx and a test all run the same code.
Installing#
Name the package in the app's header:
app [program] { pf: platform "../server/platform/main.roc", qr: "../packages/qr/main.roc" }In a .rocx file, import qr.QrCode for the component and import qr.Qr for the encoder and its drawing functions. rocx generate (which rocx dev and rocx build run first) copies the component from the package's components/ into the app, as Generated/qr/QrCode.roc (as for the charts): a package can't import the platform's Html for the server and the browser at once.
A code that follows#
Type some text and pick how much damage the code must survive. The line under the buttons says what it came to:
import qr.Qr
import qr.QrCode
<QrCode text={text!()} level={level!()} label="A QR code of the text" />The demo keeps the text and level in two signals; <QrCode> encodes again whenever either changes.
Its props#
| Prop | What it is |
|---|---|
text | What the code says: usually a URL. Like every value prop of an effectful component, it is a getter, so the code follows it as it changes. |
level | How much damage the code survives: Low (about 7%), Medium (15%, the default), Quartile (25%) or High (30%). The higher the level, the bigger the code for the same text. |
label | What the code is, for screen readers. "QR code" by default. |
class | More classes for the <svg>, besides qr-code. |
The component draws a square <svg> that fills its box's width, so give the box a width. It is black on white, with the four-module quiet zone readers need. Its colours are CSS (.qr-light and .qr-dark); keep them dark on light, as most readers can read nothing else.
Text too long for any code at the chosen level draws an empty code, labelled "…: the text is too long for a QR code".
Anywhere else#
Outside a page, call the encoder: Qr.encode(text, level) returns the code, or Err(TooLong). A code is a record: size, its side in modules, and dark, its modules row by row (Bool.True for dark). Draw it with:
Qr.svg(code): a whole SVG document, for an image in an email or a fileQr.path(code)andQr.view_box(code): every dark module as one SVG path, and the box around it, for markup of your own (this is what the component draws)Qr.terminal(code): text for a terminal, two rows of modules per line, black on white whatever the terminal's coloursQr.pbm(code): a PBM image, one pixel per moduleQr.is_dark(code, x, y): one module;Qr.capacity(level): the most bytes a code holds at that level
match Qr.encode("https://example.com/tickets/17", Medium) {
Ok(code) => Qr.svg(code)
Err(TooLong) => ""
}rocx uses the terminal drawing: rocx dev --android --lan prints a code for a phone on the same Wi-Fi to scan, and rocx qr <text> prints any text's code.
How big#
QR codes come in 40 sizes, called versions: version 1 is 21 modules a side, and each adds 4, up to 177 at version 40. The package picks the smallest version that holds the text's UTF-8 bytes at your level. The most each level holds:
| Level | Survives | Version 1 holds | Version 40 holds |
|---|---|---|---|
Low | about 7% damaged | 17 bytes | 2,953 bytes |
Medium | 15% | 14 bytes | 2,331 bytes |
Quartile | 25% | 11 bytes | 1,663 bytes |
High | 30% | 7 bytes | 1,273 bytes |
A link to a page fits a small code: a 60-byte URL at Medium is version 4, 33 modules a side.
How it is made#
The encoder follows the standard (ISO/IEC 18004), step by step as Project Nayuki's reference encoder takes them:
- The text's bytes are encoded in byte mode, with the smallest version that holds them.
- Reed–Solomon error correction is added, in the blocks the standard sets for each version and level.
- The finder, timing and alignment patterns are placed, and version information from version 7 on.
- Each of the eight masks is tried, and the one the standard's penalty rules score lowest is kept.
roc test packages/qr/main.roc checks the standard's numbers: format and version bits, capacities, where alignment patterns go, and the error-correction generator. packages/qr/test/decode.sh has a real reader (zbar) read back a code at every version and level, filled to capacity: all 160 read back as their text.
Not yet#
- Byte mode is the only mode. Digits alone, or upper-case letters and digits, would fit in smaller codes in the standard's numeric and alphanumeric modes.
- The level is never raised for you, even when the chosen version could hold the text at a higher one.
- No logo in the middle, and no Kanji mode, ECI, structured append or micro QR codes.
Each is sketched in the package's PROPOSALS.md.