From 154f3147e3bb6acc1170316fb8c83d1476bbdf3f Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Thu, 25 Apr 2019 20:33:51 +0200 Subject: label-tool: allow choosing from multiple fonts --- cmd/label-tool/main.go | 100 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 25 deletions(-) (limited to 'cmd') diff --git a/cmd/label-tool/main.go b/cmd/label-tool/main.go index 39a840c..15fb68d 100644 --- a/cmd/label-tool/main.go +++ b/cmd/label-tool/main.go @@ -4,6 +4,7 @@ import ( "errors" "html/template" "image" + "image/draw" "image/png" "log" "net/http" @@ -16,15 +17,26 @@ import ( "janouch.name/sklad/ql" ) -var font *bdf.Font +var tmplFont = template.Must(template.New("font").Parse(` + + +

PT-CBP label printing tool

+

Choose font

+{{ range $i, $f := . }} +

+ +{{ end }} + +`)) -var tmpl = template.Must(template.New("form").Parse(` +var tmplForm = template.Must(template.New("form").Parse(`

PT-CBP label printing tool

- +
@@ -59,9 +71,10 @@ var tmpl = template.Must(template.New("form").Parse(` {{ end }}
-

Font: {{ .Font.Name }} +

Font: {{ .Font.Name }} Change

+

@@ -74,6 +87,14 @@ var tmpl = template.Must(template.New("form").Parse(` `)) +type fontItem struct { + Path string + Font *bdf.Font + Preview image.Image +} + +var fonts = []*fontItem{} + func getPrinter() (*ql.Printer, error) { printer, err := ql.Open() if err != nil { @@ -96,8 +117,28 @@ func getStatus(printer *ql.Printer) error { } func handle(w http.ResponseWriter, r *http.Request) { - if err := r.ParseForm(); err != nil { - http.Error(w, err.Error(), 500) + if r.Method == http.MethodGet { + w.Header().Set("Cache-Control", "no-store") + } + + var ( + font *fontItem + fontIndex int + err error + ) + if fontIndex, err = strconv.Atoi(r.FormValue("font")); err == nil { + font = fonts[fontIndex] + } else { + w.Header().Set("Content-Type", "text/html") + tmplFont.Execute(w, fonts) + return + } + + if _, ok := r.Form["preview"]; ok { + w.Header().Set("Content-Type", "image/png") + if err := png.Encode(w, font.Preview); err != nil { + http.Error(w, err.Error(), 500) + } return } @@ -126,6 +167,7 @@ func handle(w http.ResponseWriter, r *http.Request) { InitErr error MediaInfo *ql.MediaInfo Font *bdf.Font + FontIndex int Text string Scale int }{ @@ -133,11 +175,11 @@ func handle(w http.ResponseWriter, r *http.Request) { PrinterErr: printerErr, InitErr: initErr, MediaInfo: mediaInfo, - Font: font, + Font: font.Font, + FontIndex: fontIndex, Text: r.FormValue("text"), } - var err error params.Scale, err = strconv.Atoi(r.FormValue("scale")) if err != nil { params.Scale = 3 @@ -146,7 +188,7 @@ func handle(w http.ResponseWriter, r *http.Request) { var img image.Image if mediaInfo != nil { img = &imgutil.LeftRotate{Image: label.GenLabelForHeight( - font, params.Text, mediaInfo.PrintAreaPins, params.Scale)} + font.Font, params.Text, mediaInfo.PrintAreaPins, params.Scale)} if r.FormValue("print") != "" { if err := printer.Print(img); err != nil { log.Println("print error:", err) @@ -154,9 +196,9 @@ func handle(w http.ResponseWriter, r *http.Request) { } } - if _, ok := r.Form["img"]; !ok { + if _, ok := r.Form["render"]; !ok { w.Header().Set("Content-Type", "text/html") - tmpl.Execute(w, ¶ms) + tmplForm.Execute(w, ¶ms) return } @@ -173,24 +215,32 @@ func handle(w http.ResponseWriter, r *http.Request) { } func main() { - if len(os.Args) != 3 { - log.Fatalf("usage: %s ADDRESS BDF-FILE\n", os.Args[0]) + if len(os.Args) < 3 { + log.Fatalf("usage: %s ADDRESS BDF-FILE...\n", os.Args[0]) } - address, bdfPath := os.Args[1], os.Args[2] + address, bdfPaths := os.Args[1], os.Args[2:] + for _, path := range bdfPaths { + fi, err := os.Open(path) + if err != nil { + log.Fatalln(err) + } + font, err := bdf.NewFromBDF(fi) + if err != nil { + log.Fatalf("%s: %s\n", path, err) + } + if err := fi.Close(); err != nil { + log.Fatalln(err) + } - var err error - fi, err := os.Open(bdfPath) - if err != nil { - log.Fatalln(err) - } + r, _ := font.BoundString(font.Name) + super := r.Inset(-3) - font, err = bdf.NewFromBDF(fi) - if err := fi.Close(); err != nil { - log.Fatalln(err) - } - if err != nil { - log.Fatalln(err) + img := image.NewRGBA(super) + draw.Draw(img, super, image.White, image.ZP, draw.Src) + font.DrawString(img, image.ZP, font.Name) + + fonts = append(fonts, &fontItem{Path: path, Font: font, Preview: img}) } log.Println("starting server") -- cgit v1.2.3-70-g09d2