Skip to content

Commit

Permalink
add WithWidth option
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Apr 19, 2024
1 parent 30fd79d commit afd7d22
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,68 +417,74 @@ func skipTimingPattern(n int) int {
return n + 1
}

type EncodeOptions interface {
apply(opts *encodeOptions)
}
type EncodeOptions func(opts *encodeOptions)

type encodeOptions struct {
QuietZone int
ModuleSize float64
Level Level
Kanji bool
Width int
}

func newEncodeOptions(opts ...EncodeOptions) encodeOptions {
myopts := encodeOptions{
QuietZone: 4,
ModuleSize: 1,
Level: LevelQ,
Level: LevelM,
Kanji: true,
Width: 0,
}
for _, o := range opts {
o.apply(&myopts)
o(&myopts)
}
return myopts
}

type withModuleSize float64

func (opt withModuleSize) apply(opts *encodeOptions) {
opts.ModuleSize = float64(opt)
}

// WithModuleSize sets the module size.
// The default size is 1.
func WithModuleSize(size float64) EncodeOptions {
return withModuleSize(size)
}

type withQuietZone int

func (opt withQuietZone) apply(opts *encodeOptions) {
opts.QuietZone = int(opt)
return func(opts *encodeOptions) {
opts.ModuleSize = size
}
}

// WithQuietZone sets the quiet zone size.
// The default size is 4.
func WithQuietZone(n int) EncodeOptions {
return withQuietZone(n)
}

type withLevel Level

func (opt withLevel) apply(opts *encodeOptions) {
opts.Level = Level(opt)
return func(opts *encodeOptions) {
opts.QuietZone = n
}
}

// WithLevel sets the error correction level.
// The default level is LevelM.
// If the level is invalid, it panics.
func WithLevel(lv Level) EncodeOptions {
return withLevel(lv)
if !lv.IsValid() {
panic(fmt.Sprintf("qrcode: invalid level: %d", lv))
}
return func(opts *encodeOptions) {
opts.Level = lv
}
}

type withKanji bool

func (opt withKanji) apply(opts *encodeOptions) {
opts.Kanji = bool(opt)
// WithKanji sets the kanji mode.
// The default mode is true.
// If it's enabled, Shift-JIS encoding is used for kanji mode.
func WithKanji(use bool) EncodeOptions {
return func(opts *encodeOptions) {
opts.Kanji = use
}
}

func WithKanji(use bool) EncodeOptions {
return withKanji(use)
// WithWidth sets the width of the image.
// The larger of the image width calculated from [WithModuleSize]
// and the image width specified with WithWidth is used.
func WithWidth(width int) EncodeOptions {
return func(opts *encodeOptions) {
opts.Width = width
}
}

func Encode(data []byte, opts ...EncodeOptions) (image.Image, error) {
Expand Down

0 comments on commit afd7d22

Please sign in to comment.