package qrsvg import ( "strconv" "strings" "github.com/boombuler/barcode/qr" ) const QuietZone = 4 type Symbol struct { Size int Path string } func Encode(content string) (Symbol, error) { code, err := qr.Encode(content, qr.M, qr.Unicode) if err != nil { return Symbol{}, err } n := code.Bounds().Dx() dark := func(x, y int) bool { r, _, _, _ := code.At(x, y).RGBA() return r == 0 } var b strings.Builder cx, cy := 0, 0 for y := 0; y < n; y++ { for x := 0; x < n; { if !dark(x, y) { x++ continue } start := x for x < n && dark(x, y) { x++ } sx, sy := start+QuietZone, y+QuietZone if b.Len() == 0 { b.WriteString("M" + strconv.Itoa(sx) + " " + strconv.Itoa(sy) + ".5") } else { b.WriteString("m" + strconv.Itoa(sx-cx) + " " + strconv.Itoa(sy-cy)) } b.WriteString("h" + strconv.Itoa(x-start)) cx, cy = x+QuietZone, sy } } return Symbol{Size: n + 2*QuietZone, Path: b.String()}, nil }