This commit is contained in:
Zachary Levy
2026-04-21 16:16:51 -07:00
parent 7650b90d91
commit ea19b83ba4
5 changed files with 211 additions and 271 deletions

View File

@@ -73,57 +73,32 @@ main :: proc() {
}
}
// -------------------------------------------------------------------------------------------------
// Utilities
// -------------------------------------------------------------------------------------------------
// Prints the given QR Code to the console.
print_qr :: proc(qrcode: []u8) {
size := qr.get_size(qrcode)
border :: 4
for y in -border ..< size + border {
for x in -border ..< size + border {
fmt.print("##" if qr.get_module(qrcode, x, y) else " ")
}
fmt.println()
}
fmt.println()
}
// -------------------------------------------------------------------------------------------------
// Demo: Basic
// -------------------------------------------------------------------------------------------------
// Creates a single QR Code, then prints it to the console.
basic :: proc() {
text :: "Hello, world!"
ecl :: qr.Ecc.Low
qrcode: [qr.BUFFER_LEN_MAX]u8
ok := qr.encode(text, qrcode[:], ecl)
ok := qr.encode_auto(text, qrcode[:], ecl)
if ok do print_qr(qrcode[:])
}
// -------------------------------------------------------------------------------------------------
// Demo: Variety
// -------------------------------------------------------------------------------------------------
// Creates a variety of QR Codes that exercise different features of the library.
variety :: proc() {
qrcode: [qr.BUFFER_LEN_MAX]u8
{ // Numeric mode encoding (3.33 bits per digit)
ok := qr.encode("314159265358979323846264338327950288419716939937510", qrcode[:], qr.Ecc.Medium)
ok := qr.encode_auto("314159265358979323846264338327950288419716939937510", qrcode[:], qr.Ecc.Medium)
if ok do print_qr(qrcode[:])
}
{ // Alphanumeric mode encoding (5.5 bits per character)
ok := qr.encode("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", qrcode[:], qr.Ecc.High)
ok := qr.encode_auto("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", qrcode[:], qr.Ecc.High)
if ok do print_qr(qrcode[:])
}
{ // Unicode text as UTF-8
ok := qr.encode(
ok := qr.encode_auto(
"\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1wa\xE3\x80\x81" +
"\xE4\xB8\x96\xE7\x95\x8C\xEF\xBC\x81\x20\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4",
qrcode[:],
@@ -133,7 +108,7 @@ variety :: proc() {
}
{ // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
ok := qr.encode(
ok := qr.encode_auto(
"Alice was beginning to get very tired of sitting by her sister on the bank, " +
"and of having nothing to do: once or twice she had peeped into the book her sister was reading, " +
"but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " +
@@ -148,10 +123,6 @@ variety :: proc() {
}
}
// -------------------------------------------------------------------------------------------------
// Demo: Segment
// -------------------------------------------------------------------------------------------------
// Creates QR Codes with manually specified segments for better compactness.
segment :: proc() {
qrcode: [qr.BUFFER_LEN_MAX]u8
@@ -163,7 +134,7 @@ segment :: proc() {
// Encode as single text (auto mode selection)
{
concat :: silver0 + silver1
ok := qr.encode(concat, qrcode[:], qr.Ecc.Low)
ok := qr.encode_auto(concat, qrcode[:], qr.Ecc.Low)
if ok do print_qr(qrcode[:])
}
@@ -172,7 +143,7 @@ segment :: proc() {
seg_buf0: [qr.BUFFER_LEN_MAX]u8
seg_buf1: [qr.BUFFER_LEN_MAX]u8
segs := [2]qr.Segment{qr.make_alphanumeric(silver0, seg_buf0[:]), qr.make_numeric(silver1, seg_buf1[:])}
ok := qr.encode(segs[:], qr.Ecc.Low, qrcode[:])
ok := qr.encode_auto(segs[:], qr.Ecc.Low, qrcode[:])
if ok do print_qr(qrcode[:])
}
}
@@ -185,7 +156,7 @@ segment :: proc() {
// Encode as single text (auto mode selection)
{
concat :: golden0 + golden1 + golden2
ok := qr.encode(concat, qrcode[:], qr.Ecc.Low)
ok := qr.encode_auto(concat, qrcode[:], qr.Ecc.Low)
if ok do print_qr(qrcode[:])
}
@@ -201,7 +172,7 @@ segment :: proc() {
qr.make_numeric(golden1, seg_buf1[:]),
qr.make_alphanumeric(golden2, seg_buf2[:]),
}
ok := qr.encode(segs[:], qr.Ecc.Low, qrcode[:])
ok := qr.encode_auto(segs[:], qr.Ecc.Low, qrcode[:])
if ok do print_qr(qrcode[:])
}
}
@@ -219,7 +190,7 @@ segment :: proc() {
"\xEF\xBD\x84\xEF\xBD\x85\xEF\xBD\x93\xEF" +
"\xBD\x95\xE3\x80\x80\xCE\xBA\xCE\xB1\xEF" +
"\xBC\x9F"
ok := qr.encode(madoka, qrcode[:], qr.Ecc.Low)
ok := qr.encode_auto(madoka, qrcode[:], qr.Ecc.Low)
if ok do print_qr(qrcode[:])
}
@@ -254,16 +225,12 @@ segment :: proc() {
seg.data = seg_buf[:(seg.bit_length + 7) / 8]
segs := [1]qr.Segment{seg}
ok := qr.encode(segs[:], qr.Ecc.Low, qrcode[:])
ok := qr.encode_auto(segs[:], qr.Ecc.Low, qrcode[:])
if ok do print_qr(qrcode[:])
}
}
}
// -------------------------------------------------------------------------------------------------
// Demo: Mask
// -------------------------------------------------------------------------------------------------
// Creates QR Codes with the same size and contents but different mask patterns.
mask :: proc() {
qrcode: [qr.BUFFER_LEN_MAX]u8
@@ -271,10 +238,10 @@ mask :: proc() {
{ // Project Nayuki URL
ok: bool
ok = qr.encode("https://www.nayuki.io/", qrcode[:], qr.Ecc.High)
ok = qr.encode_auto("https://www.nayuki.io/", qrcode[:], qr.Ecc.High)
if ok do print_qr(qrcode[:])
ok = qr.encode("https://www.nayuki.io/", qrcode[:], qr.Ecc.High, mask = qr.Mask.M3)
ok = qr.encode_auto("https://www.nayuki.io/", qrcode[:], qr.Ecc.High, mask = qr.Mask.M3)
if ok do print_qr(qrcode[:])
}
@@ -290,16 +257,29 @@ mask :: proc() {
ok: bool
ok = qr.encode(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M0)
ok = qr.encode_auto(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M0)
if ok do print_qr(qrcode[:])
ok = qr.encode(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M1)
ok = qr.encode_auto(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M1)
if ok do print_qr(qrcode[:])
ok = qr.encode(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M5)
ok = qr.encode_auto(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M5)
if ok do print_qr(qrcode[:])
ok = qr.encode(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M7)
ok = qr.encode_auto(text, qrcode[:], qr.Ecc.Medium, mask = qr.Mask.M7)
if ok do print_qr(qrcode[:])
}
}
// Prints the given QR Code to the console.
print_qr :: proc(qrcode: []u8) {
size := qr.get_size(qrcode)
border :: 4
for y in -border ..< size + border {
for x in -border ..< size + border {
fmt.print("##" if qr.get_module(qrcode, x, y) else " ")
}
fmt.println()
}
fmt.println()
}