generated from VLADIMIR/template
up
This commit is contained in:
@@ -152,8 +152,8 @@ func renderReceipt(card team_qr_pdf.Card) (*image.Gray, error) {
|
||||
return nil, ErrInvalidReceipt
|
||||
}
|
||||
qrSize := len(bitmap) * scale
|
||||
qrY := 16 + len(lines)*nameLineHeight + 16
|
||||
height := qrY + qrSize + 24 + len(instructions)*26
|
||||
qrY := 16 + len(instructions)*26 + 16
|
||||
height := qrY + qrSize + 24 + len(lines)*nameLineHeight
|
||||
for card.WiFiName != "" && height > receiptMaxHeight && nameSize > 16 {
|
||||
_ = face.Close()
|
||||
nameSize -= 2
|
||||
@@ -163,8 +163,7 @@ func renderReceipt(card team_qr_pdf.Card) (*image.Gray, error) {
|
||||
return nil, fmt.Errorf("resize receipt font: %w", err)
|
||||
}
|
||||
lines = wrapReceiptName(name, face)
|
||||
qrY = 16 + len(lines)*nameLineHeight + 16
|
||||
height = qrY + qrSize + 24 + len(instructions)*26
|
||||
height = qrY + qrSize + 24 + len(lines)*nameLineHeight
|
||||
}
|
||||
if height > receiptMaxHeight {
|
||||
return nil, ErrInvalidReceipt
|
||||
@@ -174,7 +173,7 @@ func renderReceipt(card team_qr_pdf.Card) (*image.Gray, error) {
|
||||
drawer := font.Drawer{Dst: img, Src: image.Black, Face: face}
|
||||
for i, line := range lines {
|
||||
width := drawer.MeasureString(line).Ceil()
|
||||
drawer.Dot = fixed.P((receiptWidth-width)/2, 16+face.Metrics().Ascent.Ceil()+i*nameLineHeight)
|
||||
drawer.Dot = fixed.P((receiptWidth-width)/2, qrY+qrSize+face.Metrics().Ascent.Ceil()+i*nameLineHeight)
|
||||
drawer.DrawString(line)
|
||||
}
|
||||
x := (receiptWidth - qrSize) / 2
|
||||
@@ -187,7 +186,7 @@ func renderReceipt(card team_qr_pdf.Card) (*image.Gray, error) {
|
||||
}
|
||||
drawer.Face = instructionFace
|
||||
for i, line := range instructions {
|
||||
drawer.Dot = fixed.P(16, qrY+qrSize+instructionFace.Metrics().Ascent.Ceil()+i*26)
|
||||
drawer.Dot = fixed.P(16, 16+instructionFace.Metrics().Ascent.Ceil()+i*26)
|
||||
drawer.DrawString(line)
|
||||
}
|
||||
for i, pixel := range img.Pix {
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"evening_detective_server/internal/repos"
|
||||
"evening_detective_server/internal/repos/games_repo"
|
||||
"evening_detective_server/internal/repos/teams_repo"
|
||||
"github.com/skip2/go-qrcode"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/font/opentype"
|
||||
)
|
||||
@@ -198,3 +199,26 @@ func TestWrapReceiptTextPreservesCredentials(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReceiptQRCodeBeforeTeamName(t *testing.T) {
|
||||
card := team_qr_pdf.Card{Name: "Вечерние сыщики", URL: "https://example.org/team"}
|
||||
img, err := renderReceipt(card)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
qr, err := qrcode.New(card.URL, qrcode.Medium)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bitmap := qr.Bitmap()
|
||||
scale := (receiptWidth - 32) / len(bitmap)
|
||||
x := (receiptWidth - len(bitmap)*scale) / 2
|
||||
// With no instructions the QR starts at the top, before the team name.
|
||||
for row, cells := range bitmap {
|
||||
for col, black := range cells {
|
||||
if got := img.GrayAt(x+col*scale, 32+row*scale).Y == 0; got != black {
|
||||
t.Fatal("QR must precede the team name")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user