diff --git a/main.go b/main.go index fb1fe97..4b8cd14 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,9 @@ import ( // 1 var cols = 4 -var rows = 7 -var qrSize = 35 -var offset = qrSize/2 + 5 +var rows = 5 +var qrSize = 40 +var fontSize = 20 func main() { qrc, err := qrcode.New("https://nanocat.net") @@ -25,6 +25,8 @@ func main() { b := newBWC() qrBytes := standard.NewWithWriter(&b, + // standard.WithQRWidth(uint8(qrSize)), + standard.WithBorderWidth(0), standard.WithBuiltinImageEncoder(standard.PNG_FORMAT), standard.WithCircleShape(), standard.WithFgGradient(&standard.LinearGradient{ @@ -53,8 +55,10 @@ func main() { if err = qrc.Save(qrBytes); err != nil { fmt.Printf("could not save image: %v", err) } + // dim := qrc.Dimension() pdf := fpdf.New("P", "mm", "A4", "") + pdf.SetMargins(0, 0, 0) pageWidth, pageHeight := pdf.GetPageSize() // register the qr @@ -64,8 +68,11 @@ func main() { AllowNegativePosition: false, }, b) + info := pdf.GetImageInfo("qr") + // panic(info.Height()) + pdf.AddPage() - pdf.SetFont("Arial", "B", 16) + pdf.SetFont("Arial", "B", float64(fontSize)) text := "foobar" textWidth := pdf.GetStringWidth(text) @@ -73,16 +80,39 @@ func main() { colWidth := pageWidth / float64(cols) rowHeight := pageHeight / float64(rows) - for i := 0; i < rows; i++ { - for j := 0; j < cols; j++ { - pdf.Text(colWidth*float64(j)+colWidth/2-textWidth/2, - rowHeight*float64(i)+rowHeight/2-float64(offset), - text) + for rowIdx := range rows { + for colIdx := range cols { + topLeftX := colWidth * float64(colIdx) + topLeftY := rowHeight * float64(rowIdx) - pdf.Image("qr", - colWidth*float64(j)+colWidth/2-float64(qrSize)/2, rowHeight*float64(i)+rowHeight/2+float64(offset), + // if rowHeight*0.8 < float64(qrSize) { + // panic("row too small for qr") + // } + // if colWidth*0.6 < float64(qrSize) { + // panic("col too small for qr") + // } + + // label at the top + pdf.Text( + topLeftX+colWidth/2-textWidth/2, + topLeftY+float64(fontSize), + text, // +fmt.Sprintf("%d/%d", rowIdx, colIdx) + ) + + info.Height() + // pdf at the bottom + pdf.ImageOptions("qr", + topLeftX+info.Width()-float64(qrSize), //+float64(dim)/2, // centre + topLeftY+float64(fontSize)*1.3, // centre float64(qrSize), float64(qrSize), - false, "tp", 0, "") + false, + fpdf.ImageOptions{AllowNegativePosition: true}, 0, "") + // pdf.Image("qr", + // topLeftX+5, // centre + // topLeftY+float64(fontSize)*1.3, // centre + // float64(qrSize), + // float64(qrSize), + // false, "tp", 0, "") } }