From 1331f3b5642f521236fcb1ec21ee43d5b76c0b91 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sun, 14 Apr 2019 22:30:40 +0200
Subject: Move commands under cmd/
---
cmd/label-tool/main.go | 199 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 199 insertions(+)
create mode 100644 cmd/label-tool/main.go
(limited to 'cmd/label-tool')
diff --git a/cmd/label-tool/main.go b/cmd/label-tool/main.go
new file mode 100644
index 0000000..39a840c
--- /dev/null
+++ b/cmd/label-tool/main.go
@@ -0,0 +1,199 @@
+package main
+
+import (
+ "errors"
+ "html/template"
+ "image"
+ "image/png"
+ "log"
+ "net/http"
+ "os"
+ "strconv"
+
+ "janouch.name/sklad/bdf"
+ "janouch.name/sklad/imgutil"
+ "janouch.name/sklad/label"
+ "janouch.name/sklad/ql"
+)
+
+var font *bdf.Font
+
+var tmpl = template.Must(template.New("form").Parse(`
+
+
+PT-CBP label printing tool
+
+
+
+ |
+
+
+
+
+ |
+
+
+`))
+
+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
+}
+
+func getStatus(printer *ql.Printer) error {
+ if err := printer.Initialize(); err != nil {
+ return err
+ }
+ if err := printer.UpdateStatus(); err != nil {
+ return err
+ }
+ return nil
+}
+
+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(),
+ )
+ }
+ }
+
+ var params = struct {
+ Printer *ql.Printer
+ PrinterErr error
+ InitErr error
+ MediaInfo *ql.MediaInfo
+ Font *bdf.Font
+ Text string
+ Scale int
+ }{
+ Printer: printer,
+ PrinterErr: printerErr,
+ InitErr: initErr,
+ MediaInfo: mediaInfo,
+ Font: font,
+ Text: r.FormValue("text"),
+ }
+
+ var err error
+ params.Scale, err = strconv.Atoi(r.FormValue("scale"))
+ if err != nil {
+ params.Scale = 3
+ }
+
+ 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)
+ }
+ }
+ }
+
+ if _, ok := r.Form["img"]; !ok {
+ w.Header().Set("Content-Type", "text/html")
+ tmpl.Execute(w, ¶ms)
+ return
+ }
+
+ if mediaInfo == nil {
+ http.Error(w, "unknown media", 500)
+ return
+ }
+
+ w.Header().Set("Content-Type", "image/png")
+ if err := png.Encode(w, img); err != nil {
+ 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])
+ }
+
+ address, bdfPath := os.Args[1], os.Args[2]
+
+ var err error
+ fi, err := os.Open(bdfPath)
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ font, err = bdf.NewFromBDF(fi)
+ if err := fi.Close(); err != nil {
+ log.Fatalln(err)
+ }
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ log.Println("starting server")
+ http.HandleFunc("/", handle)
+ log.Fatalln(http.ListenAndServe(address, nil))
+}
--
cgit v1.2.3-70-g09d2