| 1 | // Package qr is a follow-along code's picture (the `qr` block): the |
| 2 | // modules of the code for a follow link, which internal/slides/render |
| 3 | // draws as one SVG path on the live slide and internal/slides/layout as |
| 4 | // rectangles in the paint list, so a thumbnail and a printed page carry |
| 5 | // the same code the room scans. |
| 6 | // |
| 7 | // One function, here rather than in either renderer, so the two can |
| 8 | // never encode a link at different levels or with different quiet |
| 9 | // zones and both still look right. |
| 10 | package qr |
| 11 | |
| 12 | import ( |
| 13 | "errors" |
| 14 | "fmt" |
| 15 | |
| 16 | qrcode "github.com/skip2/go-qrcode" |
| 17 | ) |
| 18 | |
| 19 | // Modules is the code for text as rows of modules, true where dark, |
| 20 | // square, with go-qrcode's four-module quiet zone around it. Medium |
| 21 | // error correction, as presentation.go's pairing code: a follow link is |
| 22 | // short enough that a higher level only makes the modules smaller. |
| 23 | func Modules(text string) ([][]bool, error) { |
| 24 | q, err := qrcode.New(text, qrcode.Medium) |
| 25 | if err != nil { |
| 26 | return nil, fmt.Errorf("qr: build a code: %w", err) |
| 27 | } |
| 28 | bitmap := q.Bitmap() |
| 29 | if len(bitmap) == 0 { |
| 30 | return nil, errors.New("qr: empty code") |
| 31 | } |
| 32 | return bitmap, nil |
| 33 | } |
| 34 | |
| 35 | // Run is a stretch of dark modules in one row: from X, N wide, on row |
| 36 | // Y. Drawn as one shape rather than N, which is a fifth of the bytes |
| 37 | // and no hairline seams between neighbours in a row. |
| 38 | type Run struct{ X, Y, N int } |
| 39 | |
| 40 | // Runs is every row's dark runs, top to bottom, left to right. |
| 41 | func Runs(modules [][]bool) []Run { |
| 42 | var out []Run |
| 43 | for y, row := range modules { |
| 44 | for x := 0; x < len(row); { |
| 45 | if !row[x] { |
| 46 | x++ |
| 47 | continue |
| 48 | } |
| 49 | start := x |
| 50 | for x < len(row) && row[x] { |
| 51 | x++ |
| 52 | } |
| 53 | out = append(out, Run{X: start, Y: y, N: x - start}) |
| 54 | } |
| 55 | } |
| 56 | return out |
| 57 | } |
| 58 | |