package main import ( "errors" "html/template" "image" "image/draw" "image/png" "log" "net/http" "os" "strconv" "janouch.name/sklad/bdf" "janouch.name/sklad/imgutil" "janouch.name/sklad/label" "janouch.name/sklad/ql" ) var tmplFont = template.Must(template.New("font").Parse(`

PT-CBP label printing tool

Choose font

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

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

PT-CBP label printing tool

{{ if .Printer }}

Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }}

Tape: {{ if .Printer.LastStatus }} {{ .Printer.LastStatus.MediaWidthMM }} mm × {{ .Printer.LastStatus.MediaLengthMM }} mm {{ if .MediaInfo }} (offset: {{ .MediaInfo.SideMarginPins }} pt, print area: {{ .MediaInfo.PrintAreaPins }} pt) {{ else }} (unknown media) {{ end }} {{ if .Printer.LastStatus.Errors }} {{ range .Printer.LastStatus.Errors }}

Error: {{ . }} {{ end }} {{ end }} {{ end }} {{ if .InitErr }} {{ .InitErr }} {{ end }} {{ else }}

Error: {{ .PrinterErr }} {{ end }}

Font

{{ .Font.Name }} Change

Label

Kind:

`)) 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 { 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 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") if err := tmplFont.Execute(w, fonts); err != nil { http.Error(w, err.Error(), 500) } 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 } 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 FontIndex int Text string Scale int Kind string }{ Printer: printer, PrinterErr: printerErr, InitErr: initErr, MediaInfo: mediaInfo, Font: font.Font, FontIndex: fontIndex, Text: r.FormValue("text"), Kind: r.FormValue("kind"), } params.Scale, err = strconv.Atoi(r.FormValue("scale")) if err != nil { params.Scale = 3 } if params.Kind == "" { params.Kind = "text" } var img image.Image if mediaInfo != nil { if params.Kind == "qr" { img = &imgutil.LeftRotate{Image: label.GenLabelForHeight( font.Font, params.Text, mediaInfo.PrintAreaPins, params.Scale)} } else { img = label.GenLabelForWidth( font.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["render"]; !ok { w.Header().Set("Content-Type", "text/html") if err := tmplForm.Execute(w, ¶ms); err != nil { http.Error(w, err.Error(), 500) } 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, 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) } r, _ := font.BoundString(font.Name) super := r.Inset(-3) 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") http.HandleFunc("/", handle) log.Fatalln(http.ListenAndServe(address, nil)) }