sklad/cmd/label-tool/main.go

200 lines
3.9 KiB
Go
Raw Normal View History

2019-04-06 21:31:11 +02:00
package main
import (
"errors"
"html/template"
"image"
"image/png"
"log"
"net/http"
"os"
"strconv"
"janouch.name/sklad/bdf"
2019-04-13 00:33:45 +02:00
"janouch.name/sklad/imgutil"
"janouch.name/sklad/label"
2019-04-12 02:15:28 +02:00
"janouch.name/sklad/ql"
2019-04-06 21:31:11 +02:00
)
var font *bdf.Font
var tmpl = template.Must(template.New("form").Parse(`
2019-04-14 21:38:26 +02:00
<!DOCTYPE html>
<html><body>
<h1>PT-CBP label printing tool</h1>
<table><tr>
<td valign=top>
<img border=1 src='?img&amp;scale={{.Scale}}&amp;text={{.Text}}'>
</td>
<td valign=top>
<fieldset>
{{ if .Printer }}
2019-04-14 21:38:26 +02:00
<p>Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }}
<p>Tape:
{{ if .Printer.LastStatus }}
{{ .Printer.LastStatus.MediaWidthMM }} mm &times;
{{ .Printer.LastStatus.MediaLengthMM }} mm
2019-04-14 21:38:26 +02:00
{{ if .MediaInfo }}
(offset: {{ .MediaInfo.SideMarginPins }} pt,
print area: {{ .MediaInfo.PrintAreaPins }} pt)
{{ else }}
(unknown media)
{{ end }}
2019-04-14 21:38:26 +02:00
{{ if .Printer.LastStatus.Errors }}
{{ range .Printer.LastStatus.Errors }}
<p>Error: {{ . }}
{{ end }}
{{ end }}
2019-04-14 21:38:26 +02:00
{{ end }}
{{ if .InitErr }}
{{ .InitErr }}
{{ end }}
2019-04-14 21:38:26 +02:00
{{ else }}
<p>Error: {{ .PrinterErr }}
{{ end }}
</fieldset>
<fieldset>
<p>Font: {{ .Font.Name }}
</fieldset>
<form><fieldset>
<p><label for=text>Text:</label>
<input id=text name=text value='{{.Text}}'>
<label for=scale>Scale:</label>
<input id=scale name=scale value='{{.Scale}}' size=1>
<p><input type=submit value='Update'>
<input type=submit name=print value='Update and Print'>
</fieldset></form>
</td>
</tr></table>
</body></html>
2019-04-06 21:31:11 +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 {
if err := printer.Initialize(); err != nil {
2019-04-12 21:50:36 +02:00
return err
}
2019-04-12 21:50:36 +02:00
if err := printer.UpdateStatus(); err != nil {
return err
}
2019-04-12 21:50:36 +02:00
return nil
}
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
}
var (
initErr error
mediaInfo *ql.MediaInfo
)
printer, printerErr := getPrinter()
if printerErr == nil {
defer printer.Close()
printer.StatusNotify = func(status *ql.Status) {
log.Printf("\x1b[1mreceived status\x1b[m\n%s", status)
}
if initErr = getStatus(printer); initErr == nil {
mediaInfo = ql.GetMediaInfo(
printer.LastStatus.MediaWidthMM(),
printer.LastStatus.MediaLengthMM(),
)
}
}
2019-04-06 21:31:11 +02:00
var params = struct {
Printer *ql.Printer
PrinterErr error
InitErr error
MediaInfo *ql.MediaInfo
2019-04-12 23:56:11 +02:00
Font *bdf.Font
Text string
Scale int
2019-04-06 21:31:11 +02:00
}{
Printer: printer,
PrinterErr: printerErr,
InitErr: initErr,
MediaInfo: mediaInfo,
2019-04-12 23:56:11 +02:00
Font: font,
Text: r.FormValue("text"),
2019-04-06 21:31:11 +02:00
}
var err error
params.Scale, err = strconv.Atoi(r.FormValue("scale"))
2019-04-06 21:31:11 +02:00
if err != nil {
params.Scale = 3
2019-04-06 21:31:11 +02:00
}
var img image.Image
if mediaInfo != nil {
img = &imgutil.LeftRotate{Image: label.GenLabelForHeight(
font, params.Text, mediaInfo.PrintAreaPins, params.Scale)}
if r.FormValue("print") != "" {
if err := printer.Print(img); err != nil {
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, &params)
return
}
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, img); err != nil {
2019-04-06 21:31:11 +02:00
http.Error(w, err.Error(), 500)
return
}
}
func main() {
if len(os.Args) != 3 {
log.Fatalf("usage: %s ADDRESS BDF-FILE\n", os.Args[0])
}
2019-04-13 05:38:58 +02:00
address, bdfPath := os.Args[1], os.Args[2]
2019-04-06 21:31:11 +02:00
var err error
2019-04-13 05:38:58 +02:00
fi, err := os.Open(bdfPath)
2019-04-06 21:31:11 +02:00
if err != nil {
log.Fatalln(err)
}
2019-04-06 21:31:11 +02:00
font, err = bdf.NewFromBDF(fi)
if err := fi.Close(); err != nil {
2019-04-06 21:31:11 +02:00
log.Fatalln(err)
}
if err != nil {
2019-04-06 21:31:11 +02:00
log.Fatalln(err)
}
log.Println("starting server")
2019-04-06 21:31:11 +02:00
http.HandleFunc("/", handle)
2019-04-13 05:38:58 +02:00
log.Fatalln(http.ListenAndServe(address, nil))
2019-04-06 21:31:11 +02:00
}