implement a native EPS writer

This commit is contained in:
Michael Stapelberg
2020-11-14 11:12:02 +01:00
parent f39a23b8dc
commit 31bda30724
5 changed files with 162 additions and 4 deletions

View File

@@ -105,7 +105,8 @@ func logic() error {
format != "svg" && format != "svg" &&
format != "txt" && format != "txt" &&
format != "html" && format != "html" &&
format != "wv" { format != "wv" &&
format != "eps" {
msg := fmt.Sprintf("format (%q) must be one of png, svg, txt or html", format) msg := fmt.Sprintf("format (%q) must be one of png, svg, txt or html", format)
log.Printf("%s %s", prefix, msg) log.Printf("%s %s", prefix, msg)
http.Error(w, msg, http.StatusBadRequest) http.Error(w, msg, http.StatusBadRequest)
@@ -157,6 +158,18 @@ func logic() error {
w.Header().Add("Content-Type", "image/svg+xml") w.Header().Add("Content-Type", "image/svg+xml")
case "eps":
var err error
b, err = bill.EncodeToEPS()
if err != nil {
log.Printf("%s %s", prefix, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "image/eps")
w.Header().Add("Content-Disposition", `attachment; filename="qr.eps"`)
case "txt": case "txt":
w.Header().Add("Content-Type", "text/plain; charset=utf-8") w.Header().Add("Content-Type", "text/plain; charset=utf-8")
spew.Fdump(w, qrch.Validate()) spew.Fdump(w, qrch.Validate())

3
go.mod
View File

@@ -3,10 +3,11 @@ module github.com/stapelberg/qrbill
go 1.14 go 1.14
require ( require (
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb
github.com/davecgh/go-spew v1.1.1 github.com/davecgh/go-spew v1.1.1
github.com/makiuchi-d/gozxing v0.0.0-20200903113411-25f730ed83da github.com/makiuchi-d/gozxing v0.0.0-20200903113411-25f730ed83da
github.com/mattn/go-isatty v0.0.12 github.com/mattn/go-isatty v0.0.12
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c // indirect
golang.org/x/text v0.3.4 // indirect golang.org/x/text v0.3.4 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
) )

6
go.sum
View File

@@ -1,5 +1,5 @@
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca h1:kWzLcty5V2rzOqJM7Tp/MfSX0RMSI1x4IOLApEefYxA= github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb h1:EVl3FJLQCzSbgBezKo/1A4ADnJ4mtJZ0RvnNzDJ44nY=
github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= github.com/ajstarks/svgo v0.0.0-20200725142600-7a3c8b57fecb/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/makiuchi-d/gozxing v0.0.0-20200903113411-25f730ed83da h1:OgNu1PPD9EvZckyKDAc8DA4KymNXuc6vaCLsdOGyjOE= github.com/makiuchi-d/gozxing v0.0.0-20200903113411-25f730ed83da h1:OgNu1PPD9EvZckyKDAc8DA4KymNXuc6vaCLsdOGyjOE=
@@ -8,6 +8,8 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c h1:UIcGWL6/wpCfyGuJnRFJRurA+yj8RrW7Q6x2YMCXt6c=
golang.org/x/sys v0.0.0-20200724161237-0e2f3a69832c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc= golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View File

@@ -287,6 +287,21 @@ func (b *Bill) EncodeToSVG() ([]byte, error) {
return bytes.ReplaceAll(qrCodeSVG, []byte(`</g>`), append(cross, []byte("</g>")...)), nil return bytes.ReplaceAll(qrCodeSVG, []byte(`</g>`), append(cross, []byte("</g>")...)), nil
} }
func (b *Bill) EncodeToEPS() ([]byte, error) {
var err error
code, err := encoder.Encoder_encode(b.qrcontents, decoder.ErrorCorrectionLevel_M, qrEncodeHints())
if err != nil {
return nil, err
}
const quietzone = 4
qrCodeEPS, err := renderResultEPS(code, qrCodeEdgeSidePx, qrCodeEdgeSidePx, quietzone)
if err != nil {
return nil, err
}
return qrCodeEPS, nil
}
func (b *Bill) EncodeToImage() (image.Image, error) { func (b *Bill) EncodeToImage() (image.Image, error) {
return generateSwissQrCode(b.qrcontents) return generateSwissQrCode(b.qrcontents)
} }

127
qrcodegeneratoreps.go Normal file
View File

@@ -0,0 +1,127 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package qrbill
import (
"fmt"
"strings"
"time"
"github.com/makiuchi-d/gozxing"
"github.com/makiuchi-d/gozxing/qrcode/encoder"
)
// renderResultEPS is a copy of renderResult from
// gozxing/qrcode/qrcode_writer.go, adapted to output to SVG.
func renderResultEPS(code *encoder.QRCode, width, height, quietZone int) ([]byte, error) {
input := code.GetMatrix()
if input == nil {
return nil, gozxing.NewWriterException("IllegalStateException")
}
inputWidth := input.GetWidth()
inputHeight := input.GetHeight()
qrWidth := inputWidth + (quietZone * 2)
qrHeight := inputHeight + (quietZone * 2)
outputWidth := qrWidth
if outputWidth < width {
outputWidth = width
}
outputHeight := qrHeight
if outputHeight < height {
outputHeight = height
}
multiple := outputWidth / qrWidth
if h := outputHeight / qrHeight; multiple > h {
multiple = h
}
// Padding includes both the quiet zone and the extra white pixels to accommodate the requested
// dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
// If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
// handle all the padding from 100x100 (the actual QR) up to 200x160.
leftPadding := (outputWidth - (inputWidth * multiple)) / 2
topPadding := (outputHeight - (inputHeight * multiple)) / 2
// --------------------------------------------------------------------------------
var eps strings.Builder
// See postscript language document structuring conventions specification version 3.0
// https://www-cdf.fnal.gov/offline/PostScript/5001.PDF
// See Encapsulated PostScript File Format Specification
// https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/5002.EPSF_Spec.pdf
// EPS files must not have lines of ASCII text that exceed 255 characters,
// excluding line-termination characters.
// Lines must be terminated with one of the following combinations:
// - CR (carriage return, ASCII decimal 13)
// - LF (line feed, ASCII decimal 10)
// - CR LF
// - LF CR
// BoundingBox parameters are lower-left (llx, lly) and upper-right (urx, ury)
eps.WriteString("%!PS-Adobe-3.0 EPSF-3.0\n")
eps.WriteString("%%Creator: https://github.com/stapelberg/qrbill\n")
// TODO: summarize message and recipient in title
// TODO: is there a max length for the title?
eps.WriteString("%%Title: QR-Bill\n")
eps.WriteString("%%CreationDate: " + time.Now().Format("2006-01-02") + "\n")
eps.WriteString("%%BoundingBox: 0 0 1265 1265\n")
eps.WriteString("%%EndComments\n")
eps.WriteString("/F { rectfill } def\n")
// Change the application coordinate system to work like the SVG one does,
// for consistency between the different code paths. See also General
// Coordinate System Transformation, Page 18, Encapsulated PostScript File
// Format Specification:
// https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/5002.EPSF_Spec.pdf
eps.WriteString("0 1265 translate\n")
eps.WriteString("1 -1 scale\n")
// Explicitly fill the background with white:
eps.WriteString("1 1 1 setrgbcolor\n")
// or 1 setgray?
eps.WriteString("0 0 1265 1265 F\n")
// Explicitly set color to black:
eps.WriteString("0 0 0 setrgbcolor\n")
// or 0 setgray?
for inputY, outputY := 0, topPadding; inputY < inputHeight; inputY, outputY = inputY+1, outputY+multiple {
// Write the contents of this row of the barcode
for inputX, outputX := 0, leftPadding; inputX < inputWidth; inputX, outputX = inputX+1, outputX+multiple {
if input.Get(inputX, inputY) == 1 {
eps.WriteString(fmt.Sprintf("%d %d %d %d F\n", outputX, outputY, multiple, multiple))
}
}
}
eps.WriteString("549 549 translate\n")
// overlay an EPS version of the swiss cross
eps.WriteString("1 1 1 setrgbcolor\n")
eps.WriteString("0 0 166 166 F\n")
eps.WriteString("0 0 0 setrgbcolor\n")
eps.WriteString("12 12 142 142 F\n")
eps.WriteString("1 1 1 setrgbcolor\n")
eps.WriteString("36 66 94 28 F\n")
eps.WriteString("68 34 30 92 F\n")
eps.WriteString("%%EOF")
return []byte(eps.String()), nil
}