Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

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:

main.roc'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:

Version 3: 29 × 29 modules, the text 40 of the 42 bytes it holds
site/Demos/QrDemo.rocx, in part
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#

PropWhat it is
textWhat 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.
levelHow 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.
labelWhat the code is, for screen readers. "QR code" by default.
classMore 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:

A code for an email
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:

LevelSurvivesVersion 1 holdsVersion 40 holds
Lowabout 7% damaged17 bytes2,953 bytes
Medium15%14 bytes2,331 bytes
Quartile25%11 bytes1,663 bytes
High30%7 bytes1,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:

  1. The text's bytes are encoded in byte mode, with the smallest version that holds them.
  2. Reed–Solomon error correction is added, in the blocks the standard sets for each version and level.
  3. The finder, timing and alignment patterns are placed, and version information from version 7 on.
  4. 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#

Each is sketched in the package's PROPOSALS.md.