2019-04-06 21:31:11 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"html/template"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"image/draw"
|
|
|
|
"image/png"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/boombuler/barcode"
|
|
|
|
"github.com/boombuler/barcode/qr"
|
|
|
|
|
|
|
|
"janouch.name/sklad/bdf"
|
2019-04-12 02:15:28 +02:00
|
|
|
"janouch.name/sklad/ql"
|
2019-04-06 21:31:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// scaler is a scaling image.Image wrapper.
|
|
|
|
type scaler struct {
|
|
|
|
image image.Image
|
|
|
|
scale int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ColorModel implements image.Image.
|
|
|
|
func (s *scaler) ColorModel() color.Model {
|
|
|
|
return s.image.ColorModel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds implements image.Image.
|
|
|
|
func (s *scaler) Bounds() image.Rectangle {
|
|
|
|
r := s.image.Bounds()
|
|
|
|
return image.Rect(r.Min.X*s.scale, r.Min.Y*s.scale,
|
|
|
|
r.Max.X*s.scale, r.Max.Y*s.scale)
|
|
|
|
}
|
|
|
|
|
|
|
|
// At implements image.Image.
|
|
|
|
func (s *scaler) At(x, y int) color.Color {
|
|
|
|
if x < 0 {
|
|
|
|
x = x - s.scale + 1
|
|
|
|
}
|
|
|
|
if y < 0 {
|
|
|
|
y = y - s.scale + 1
|
|
|
|
}
|
|
|
|
return s.image.At(x/s.scale, y/s.scale)
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:45:56 +02:00
|
|
|
// leftRotate is a 90 degree rotating image.Image wrapper.
|
|
|
|
type leftRotate struct {
|
|
|
|
image image.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
// ColorModel implements image.Image.
|
|
|
|
func (lr *leftRotate) ColorModel() color.Model {
|
|
|
|
return lr.image.ColorModel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds implements image.Image.
|
|
|
|
func (lr *leftRotate) Bounds() image.Rectangle {
|
|
|
|
r := lr.image.Bounds()
|
|
|
|
// Min is inclusive, Max is exclusive.
|
|
|
|
return image.Rect(r.Min.Y, -(r.Max.X - 1), r.Max.Y, -(r.Min.X - 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
// At implements image.Image.
|
|
|
|
func (lr *leftRotate) At(x, y int) color.Color {
|
|
|
|
return lr.image.At(-y, x)
|
|
|
|
}
|
|
|
|
|
2019-04-06 21:31:11 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
var font *bdf.Font
|
|
|
|
|
2019-04-12 19:22:27 +02:00
|
|
|
func genLabelForHeight(text string, height, scale int) image.Image {
|
2019-04-11 21:45:56 +02:00
|
|
|
// Create a scaled bitmap of the text label.
|
|
|
|
textRect, _ := font.BoundString(text)
|
|
|
|
textImg := image.NewRGBA(textRect)
|
|
|
|
draw.Draw(textImg, textRect, image.White, image.ZP, draw.Src)
|
|
|
|
font.DrawString(textImg, image.ZP, text)
|
|
|
|
|
2019-04-12 19:22:27 +02:00
|
|
|
scaledTextImg := scaler{image: textImg, scale: scale}
|
2019-04-11 21:45:56 +02:00
|
|
|
scaledTextRect := scaledTextImg.Bounds()
|
|
|
|
|
|
|
|
remains := height - scaledTextRect.Dy() - 20
|
|
|
|
|
|
|
|
width := scaledTextRect.Dx()
|
|
|
|
if remains > width {
|
|
|
|
width = remains
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a scaled bitmap of the QR code.
|
|
|
|
qrImg, _ := qr.Encode(text, qr.H, qr.Auto)
|
|
|
|
qrImg, _ = barcode.Scale(qrImg, remains, remains)
|
|
|
|
qrRect := qrImg.Bounds()
|
|
|
|
|
|
|
|
// Combine.
|
|
|
|
combinedRect := image.Rect(0, 0, width, height)
|
|
|
|
combinedImg := image.NewRGBA(combinedRect)
|
|
|
|
draw.Draw(combinedImg, combinedRect, image.White, image.ZP, draw.Src)
|
|
|
|
draw.Draw(combinedImg,
|
|
|
|
combinedRect.Add(image.Point{X: (width - qrRect.Dx()) / 2, Y: 0}),
|
|
|
|
qrImg, image.ZP, draw.Src)
|
|
|
|
|
|
|
|
target := image.Rect(
|
|
|
|
(width-scaledTextRect.Dx())/2, qrRect.Dy()+20,
|
|
|
|
combinedRect.Max.X, combinedRect.Max.Y)
|
|
|
|
draw.Draw(combinedImg, target, &scaledTextImg, scaledTextRect.Min, draw.Src)
|
|
|
|
return combinedImg
|
|
|
|
}
|
|
|
|
|
2019-04-06 21:31:11 +02:00
|
|
|
var tmpl = template.Must(template.New("form").Parse(`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html><body>
|
2019-04-12 19:22:27 +02:00
|
|
|
<h1>PT-CBP label printing tool</h1>
|
2019-04-06 21:31:11 +02:00
|
|
|
<table><tr>
|
|
|
|
<td valign=top>
|
2019-04-12 19:22:27 +02:00
|
|
|
<img border=1 src='?img&scale={{.Scale}}&text={{.Text}}'>
|
2019-04-06 21:31:11 +02:00
|
|
|
</td>
|
|
|
|
<td valign=top>
|
2019-04-12 19:22:27 +02:00
|
|
|
<fieldset>
|
|
|
|
{{ if .Printer }}
|
|
|
|
|
|
|
|
<p>Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }}
|
|
|
|
<p>Tape:
|
2019-04-12 21:50:36 +02:00
|
|
|
{{ if .Printer.LastStatus }}
|
|
|
|
{{ .Printer.LastStatus.MediaWidthMM }} mm ×
|
|
|
|
{{ .Printer.LastStatus.MediaLengthMM }} mm
|
2019-04-12 19:22:27 +02:00
|
|
|
|
|
|
|
{{ if .MediaInfo }}
|
|
|
|
(offset: {{ .MediaInfo.SideMarginPins }} pt,
|
|
|
|
print area: {{ .MediaInfo.PrintAreaPins }} pt)
|
|
|
|
{{ else }}
|
|
|
|
(unknown media)
|
|
|
|
{{ end }}
|
|
|
|
|
2019-04-12 21:50:36 +02:00
|
|
|
{{ if .Printer.LastStatus.Errors }}
|
|
|
|
{{ range .Printer.LastStatus.Errors }}
|
2019-04-12 19:22:27 +02:00
|
|
|
<p>Error: {{ . }}
|
|
|
|
{{ end }}
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
{{ end }}
|
|
|
|
{{ if .InitErr }}
|
|
|
|
{{ .InitErr }}
|
|
|
|
{{ end }}
|
|
|
|
|
|
|
|
{{ else }}
|
|
|
|
<p>Error: {{ .PrinterErr }}
|
|
|
|
{{ end }}
|
|
|
|
</fieldset>
|
2019-04-12 23:56:11 +02:00
|
|
|
<fieldset>
|
|
|
|
<p>Font: {{ .Font.Name }}
|
|
|
|
</fieldset>
|
2019-04-06 21:31:11 +02:00
|
|
|
<form><fieldset>
|
|
|
|
<p><label for=text>Text:</label>
|
|
|
|
<input id=text name=text value='{{.Text}}'>
|
2019-04-12 19:22:27 +02:00
|
|
|
<label for=scale>Scale:</label>
|
|
|
|
<input id=scale name=scale value='{{.Scale}}' size=1>
|
2019-04-06 21:31:11 +02:00
|
|
|
<p><input type=submit value='Update'>
|
2019-04-11 19:16:40 +02:00
|
|
|
<input type=submit name=print value='Update and Print'>
|
2019-04-06 21:31:11 +02:00
|
|
|
</fieldset></form>
|
|
|
|
</td>
|
|
|
|
</tr></table>
|
|
|
|
</body></html>
|
|
|
|
`))
|
|
|
|
|
2019-04-12 19:22:27 +02:00
|
|
|
func getPrinter() (*ql.Printer, error) {
|
|
|
|
printer, err := ql.Open()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if printer == nil {
|
|
|
|
return nil, errors.New("no suitable printer found")
|
|
|
|
}
|
|
|
|
return printer, nil
|
|
|
|
}
|
|
|
|
|
2019-04-12 21:50:36 +02:00
|
|
|
func getStatus(printer *ql.Printer) error {
|
2019-04-12 19:22:27 +02:00
|
|
|
if err := printer.Initialize(); err != nil {
|
2019-04-12 21:50:36 +02:00
|
|
|
return err
|
2019-04-12 19:22:27 +02:00
|
|
|
}
|
2019-04-12 21:50:36 +02:00
|
|
|
if err := printer.UpdateStatus(); err != nil {
|
|
|
|
return err
|
2019-04-12 19:22:27 +02:00
|
|
|
}
|
2019-04-12 21:50:36 +02:00
|
|
|
return nil
|
2019-04-12 19:22:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-06 21:31:11 +02:00
|
|
|
func handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-12 23:10:42 +02:00
|
|
|
var (
|
|
|
|
initErr error
|
|
|
|
mediaInfo *ql.MediaInfo
|
|
|
|
)
|
2019-04-12 19:22:27 +02:00
|
|
|
printer, printerErr := getPrinter()
|
|
|
|
if printerErr == nil {
|
|
|
|
defer printer.Close()
|
2019-04-12 23:10:42 +02:00
|
|
|
printer.StatusNotify = func(status *ql.Status) {
|
|
|
|
log.Printf("\x1b[1mreceived status\x1b[m\n%s", status)
|
|
|
|
}
|
2019-04-12 19:22:27 +02:00
|
|
|
|
2019-04-12 23:10:42 +02:00
|
|
|
if initErr = getStatus(printer); initErr == nil {
|
|
|
|
mediaInfo = ql.GetMediaInfo(
|
|
|
|
printer.LastStatus.MediaWidthMM(),
|
|
|
|
printer.LastStatus.MediaLengthMM(),
|
|
|
|
)
|
|
|
|
}
|
2019-04-12 19:22:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-06 21:31:11 +02:00
|
|
|
var params = struct {
|
2019-04-12 19:22:27 +02:00
|
|
|
Printer *ql.Printer
|
|
|
|
PrinterErr error
|
|
|
|
InitErr error
|
|
|
|
MediaInfo *ql.MediaInfo
|
2019-04-12 23:56:11 +02:00
|
|
|
Font *bdf.Font
|
2019-04-12 19:22:27 +02:00
|
|
|
Text string
|
|
|
|
Scale int
|
2019-04-06 21:31:11 +02:00
|
|
|
}{
|
2019-04-12 19:22:27 +02:00
|
|
|
Printer: printer,
|
|
|
|
PrinterErr: printerErr,
|
|
|
|
InitErr: initErr,
|
|
|
|
MediaInfo: mediaInfo,
|
2019-04-12 23:56:11 +02:00
|
|
|
Font: font,
|
2019-04-12 19:22:27 +02:00
|
|
|
Text: r.FormValue("text"),
|
2019-04-06 21:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2019-04-12 19:22:27 +02:00
|
|
|
params.Scale, err = strconv.Atoi(r.FormValue("scale"))
|
2019-04-06 21:31:11 +02:00
|
|
|
if err != nil {
|
2019-04-12 19:22:27 +02:00
|
|
|
params.Scale = 3
|
2019-04-06 21:31:11 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 19:22:27 +02:00
|
|
|
var label image.Image
|
|
|
|
if mediaInfo != nil {
|
|
|
|
label = &leftRotate{image: genLabelForHeight(
|
|
|
|
params.Text, mediaInfo.PrintAreaPins, params.Scale)}
|
|
|
|
if r.FormValue("print") != "" {
|
2019-04-12 21:50:36 +02:00
|
|
|
if err := printer.Print(label); err != nil {
|
2019-04-12 19:22:27 +02:00
|
|
|
log.Println("print error:", err)
|
|
|
|
}
|
2019-04-06 21:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := r.Form["img"]; !ok {
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
tmpl.Execute(w, ¶ms)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:22:27 +02:00
|
|
|
if mediaInfo == nil {
|
|
|
|
http.Error(w, "unknown media", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-06 21:31:11 +02:00
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
if err := png.Encode(w, label); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-04-12 23:50:23 +02:00
|
|
|
if len(os.Args) != 3 {
|
|
|
|
log.Fatalf("usage: %s ADDRESS BDF-FILE\n", os.Args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
address, bdf_path := os.Args[1], os.Args[2]
|
|
|
|
|
2019-04-06 21:31:11 +02:00
|
|
|
var err error
|
2019-04-12 23:50:23 +02:00
|
|
|
fi, err := os.Open(bdf_path)
|
2019-04-06 21:31:11 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2019-04-12 23:50:23 +02:00
|
|
|
|
2019-04-06 21:31:11 +02:00
|
|
|
font, err = bdf.NewFromBDF(fi)
|
2019-04-12 23:50:23 +02:00
|
|
|
if err := fi.Close(); err != nil {
|
2019-04-06 21:31:11 +02:00
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
2019-04-12 23:50:23 +02:00
|
|
|
if err != nil {
|
2019-04-06 21:31:11 +02:00
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2019-04-12 23:10:42 +02:00
|
|
|
log.Println("starting server")
|
2019-04-06 21:31:11 +02:00
|
|
|
http.HandleFunc("/", handle)
|
2019-04-12 23:50:23 +02:00
|
|
|
log.Fatal(http.ListenAndServe(address, nil))
|
2019-04-06 21:31:11 +02:00
|
|
|
}
|