Raw
 1package qrsvg
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"github.com/boombuler/barcode/qr"
 8)
 9
10const QuietZone = 4
11
12type Symbol struct {
13	Size int
14	Path string
15}
16
17func Encode(content string) (Symbol, error) {
18	code, err := qr.Encode(content, qr.M, qr.Unicode)
19	if err != nil {
20		return Symbol{}, err
21	}
22	n := code.Bounds().Dx()
23	dark := func(x, y int) bool {
24		r, _, _, _ := code.At(x, y).RGBA()
25		return r == 0
26	}
27	var b strings.Builder
28	cx, cy := 0, 0
29	for y := 0; y < n; y++ {
30		for x := 0; x < n; {
31			if !dark(x, y) {
32				x++
33				continue
34			}
35			start := x
36			for x < n && dark(x, y) {
37				x++
38			}
39			sx, sy := start+QuietZone, y+QuietZone
40			if b.Len() == 0 {
41				b.WriteString("M" + strconv.Itoa(sx) + " " + strconv.Itoa(sy) + ".5")
42			} else {
43				b.WriteString("m" + strconv.Itoa(sx-cx) + " " + strconv.Itoa(sy-cy))
44			}
45			b.WriteString("h" + strconv.Itoa(x-start))
46			cx, cy = x+QuietZone, sy
47		}
48	}
49	return Symbol{Size: n + 2*QuietZone, Path: b.String()}, nil
50}