From f43e9c6849a9b4cdefb871a5049b6df6c4f10f9c Mon Sep 17 00:00:00 2001 From: yeqown Date: Thu, 10 Apr 2025 22:27:13 +0800 Subject: [PATCH] feat(writer): add qart writer and implement a simple demo --- go.work | 1 + writer/qart/.gitignore | 1 + writer/qart/go.mod | 7 +++++ writer/qart/styles.go | 11 +++++++ writer/qart/writer.go | 63 ++++++++++++++++++++++++++++++++++++++ writer/qart/writer_test.go | 22 +++++++++++++ 6 files changed, 105 insertions(+) create mode 100644 writer/qart/.gitignore create mode 100644 writer/qart/go.mod create mode 100644 writer/qart/styles.go create mode 100644 writer/qart/writer.go create mode 100644 writer/qart/writer_test.go diff --git a/go.work b/go.work index a8aacd4..cd435a2 100644 --- a/go.work +++ b/go.work @@ -9,5 +9,6 @@ use ( ./writer/file ./cmd/qrcode ./cmd/wasm + ./writer/qart //example ) diff --git a/writer/qart/.gitignore b/writer/qart/.gitignore new file mode 100644 index 0000000..26f3c9e --- /dev/null +++ b/writer/qart/.gitignore @@ -0,0 +1 @@ +qart.svg \ No newline at end of file diff --git a/writer/qart/go.mod b/writer/qart/go.mod new file mode 100644 index 0000000..20672e9 --- /dev/null +++ b/writer/qart/go.mod @@ -0,0 +1,7 @@ +module github.com/yeqown/go-qrcode/writer/qart + +go 1.20 + +require github.com/yeqown/go-qrcode/v2 v2.2.5 + +require github.com/yeqown/reedsolomon v1.0.0 // indirect diff --git a/writer/qart/styles.go b/writer/qart/styles.go new file mode 100644 index 0000000..cd02a40 --- /dev/null +++ b/writer/qart/styles.go @@ -0,0 +1,11 @@ +package qart + +import "github.com/yeqown/go-qrcode/v2" + +type SVGPointExpr string + +// Styler defines the style of QArt, how to response each block +// of QRCode. +type styler interface { + Point(x, y int, v qrcode.QRValue) SVGPointExpr +} diff --git a/writer/qart/writer.go b/writer/qart/writer.go new file mode 100644 index 0000000..ec52f37 --- /dev/null +++ b/writer/qart/writer.go @@ -0,0 +1,63 @@ +package qart + +import ( + "fmt" + "io" + "os" + + "github.com/yeqown/go-qrcode/v2" +) + +var _ qrcode.Writer = (*Writer)(nil) + +// Writer is a writer that writes QR Code to SVG to support multi QArt format. +// And it's possible to customize your own QArt format. +// +// Inspired by https://github.com/latentcat/qrbtf +type Writer struct { + output io.Writer +} + +func New() (*Writer, error) { + fd, err := os.OpenFile("qart.svg", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) + if err != nil { + return nil, err + } + + return &Writer{ + output: fd, + }, nil +} + +func (w Writer) Write(mat qrcode.Matrix) error { + wn, hn := mat.Width(), mat.Height() + width, height := wn*3, hn*3 + + // + //Untitled QRCode + //The SVG of QRCode is generated by http://github.com/yeqown/go-qrcode. + + // write header + _, _ = fmt.Fprintf(w.output, ` + Untitled QRCode + The SVG of QRCode is generated by http://github.com/yeqown/go-qrcode.`, width, height, wn, hn) + + // write body + // white background + _, _ = fmt.Fprintf(w.output, ``, wn, hn) + + mat.Iterate(qrcode.IterDirection_ROW, func(x, y int, v qrcode.QRValue) { + if v.IsSet() { + _, _ = fmt.Fprintf(w.output, ``, x, y) + _, _ = fmt.Fprintln(w.output) + } + }) + + _, _ = fmt.Fprintf(w.output, "") + + return nil +} + +func (w Writer) Close() error { + return nil +} diff --git a/writer/qart/writer_test.go b/writer/qart/writer_test.go new file mode 100644 index 0000000..c8ce823 --- /dev/null +++ b/writer/qart/writer_test.go @@ -0,0 +1,22 @@ +package qart + +import ( + "testing" + + "github.com/yeqown/go-qrcode/v2" +) + +func Test_New(t *testing.T) { + qrc, err := qrcode.New("https://github.com/yeqown/go-qrcode") + if err != nil { + t.Fatal(err) + } + + w, err := New() + if err != nil { + t.Fatal(err) + } + + // save file + err = qrc.Save(w) +}