2019-04-06 21:31:11 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"html/template"
|
|
|
|
"image"
|
2021-06-09 23:16:50 +02:00
|
|
|
"image/color"
|
2019-04-25 20:33:51 +02:00
|
|
|
"image/draw"
|
2019-04-06 21:31:11 +02:00
|
|
|
"image/png"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"janouch.name/sklad/bdf"
|
2019-04-13 00:33:45 +02:00
|
|
|
"janouch.name/sklad/imgutil"
|
2019-04-14 21:25:21 +02:00
|
|
|
"janouch.name/sklad/label"
|
2019-04-12 02:15:28 +02:00
|
|
|
"janouch.name/sklad/ql"
|
2019-04-06 21:31:11 +02:00
|
|
|
)
|
|
|
|
|
2019-04-25 20:33:51 +02:00
|
|
|
var tmplFont = template.Must(template.New("font").Parse(`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html><body>
|
|
|
|
<h1>PT-CBP label printing tool</h1>
|
|
|
|
<h2>Choose font</h2>
|
|
|
|
{{ range $i, $f := . }}
|
|
|
|
<p><a href='?font={{ $i }}'>
|
|
|
|
<img src='?font={{ $i }}&preview' title='{{ $f.Path }}'></a>
|
|
|
|
{{ end }}
|
|
|
|
</body></html>
|
|
|
|
`))
|
2019-04-06 21:31:11 +02:00
|
|
|
|
2019-04-25 20:33:51 +02:00
|
|
|
var tmplForm = 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>
|
2019-04-25 20:33:51 +02:00
|
|
|
<img border=1 src='?font={{ .FontIndex }}&scale={{ .Scale }}{{/*
|
|
|
|
*/}}&text={{ .Text }}&render'>
|
2019-04-14 21:38:26 +02:00
|
|
|
</td>
|
2019-04-25 21:30:15 +02:00
|
|
|
<td valign=top><form>
|
2019-04-14 21:38:26 +02:00
|
|
|
<fieldset>
|
|
|
|
{{ if .Printer }}
|
2019-04-12 19:22:27 +02:00
|
|
|
|
2019-04-14 21:38:26 +02:00
|
|
|
<p>Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }}
|
|
|
|
<p>Tape:
|
|
|
|
{{ if .Printer.LastStatus }}
|
|
|
|
{{ .Printer.LastStatus.MediaWidthMM }} mm ×
|
|
|
|
{{ .Printer.LastStatus.MediaLengthMM }} mm
|
2019-04-12 19:22:27 +02:00
|
|
|
|
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-12 19:22:27 +02:00
|
|
|
|
2019-04-14 21:38:26 +02:00
|
|
|
{{ if .Printer.LastStatus.Errors }}
|
|
|
|
{{ range .Printer.LastStatus.Errors }}
|
|
|
|
<p>Error: {{ . }}
|
|
|
|
{{ end }}
|
|
|
|
{{ end }}
|
2019-04-12 19:22:27 +02:00
|
|
|
|
2019-04-14 21:38:26 +02:00
|
|
|
{{ end }}
|
|
|
|
{{ if .InitErr }}
|
|
|
|
{{ .InitErr }}
|
|
|
|
{{ end }}
|
2019-04-12 19:22:27 +02:00
|
|
|
|
2019-04-14 21:38:26 +02:00
|
|
|
{{ else }}
|
|
|
|
<p>Error: {{ .PrinterErr }}
|
|
|
|
{{ end }}
|
|
|
|
</fieldset>
|
|
|
|
<fieldset>
|
2019-04-25 21:30:15 +02:00
|
|
|
<legend>Font</legend>
|
|
|
|
<p>{{ .Font.Name }} <a href='?'>Change</a>
|
|
|
|
<input type=hidden name=font value='{{ .FontIndex }}'>
|
|
|
|
<p><label for=scale>Scale:</label>
|
2019-04-14 21:38:26 +02:00
|
|
|
<input id=scale name=scale value='{{.Scale}}' size=1>
|
2019-04-25 21:30:15 +02:00
|
|
|
</fieldset>
|
|
|
|
<fieldset>
|
|
|
|
<legend>Label</legend>
|
|
|
|
<p><textarea name=text>{{.Text}}</textarea>
|
|
|
|
<p>Kind:
|
|
|
|
<input type=radio id=kind-text name=kind value=text
|
|
|
|
{{ if eq .Kind "text" }} checked{{ end }}>
|
|
|
|
<label for=kind-text>plain text (horizontal)</label>
|
|
|
|
<input type=radio id=kind-qr name=kind value=qr
|
|
|
|
{{ if eq .Kind "qr" }} checked{{ end }}>
|
|
|
|
<label for=kind-qr>QR code (vertical)</label>
|
2019-04-14 21:38:26 +02:00
|
|
|
<p><input type=submit value='Update'>
|
|
|
|
<input type=submit name=print value='Update and Print'>
|
2019-04-25 21:30:15 +02:00
|
|
|
</fieldset>
|
|
|
|
</form></td>
|
2019-04-14 21:38:26 +02:00
|
|
|
</tr></table>
|
|
|
|
</body></html>
|
2019-04-06 21:31:11 +02:00
|
|
|
`))
|
|
|
|
|
2019-04-25 20:33:51 +02:00
|
|
|
type fontItem struct {
|
|
|
|
Path string
|
|
|
|
Font *bdf.Font
|
|
|
|
Preview image.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
var fonts = []*fontItem{}
|
|
|
|
|
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) {
|
2019-04-25 20:33:51 +02:00
|
|
|
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 {
|
2019-04-25 21:33:20 +02:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2019-04-25 21:30:15 +02:00
|
|
|
if err := tmplFont.Execute(w, fonts); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
2019-04-25 20:33:51 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-04-06 21:31:11 +02:00
|
|
|
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-25 20:33:51 +02:00
|
|
|
FontIndex int
|
2019-04-12 19:22:27 +02:00
|
|
|
Text string
|
|
|
|
Scale int
|
2019-04-25 21:30:15 +02:00
|
|
|
Kind string
|
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-25 20:33:51 +02:00
|
|
|
Font: font.Font,
|
|
|
|
FontIndex: fontIndex,
|
2019-04-12 19:22:27 +02:00
|
|
|
Text: r.FormValue("text"),
|
2019-04-25 21:30:15 +02:00
|
|
|
Kind: r.FormValue("kind"),
|
2019-04-06 21:31:11 +02:00
|
|
|
}
|
|
|
|
|
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-25 21:30:15 +02:00
|
|
|
if params.Kind == "" {
|
|
|
|
params.Kind = "text"
|
|
|
|
}
|
2019-04-06 21:31:11 +02:00
|
|
|
|
2019-04-14 21:25:21 +02:00
|
|
|
var img image.Image
|
2019-04-12 19:22:27 +02:00
|
|
|
if mediaInfo != nil {
|
2019-04-25 21:30:15 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-04-12 19:22:27 +02:00
|
|
|
if r.FormValue("print") != "" {
|
2021-06-10 02:24:00 +02:00
|
|
|
if err := printer.Print(img, false); err != nil {
|
2019-04-12 19:22:27 +02:00
|
|
|
log.Println("print error:", err)
|
|
|
|
}
|
2019-04-06 21:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 20:33:51 +02:00
|
|
|
if _, ok := r.Form["render"]; !ok {
|
2019-04-25 21:33:20 +02:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2019-04-25 21:30:15 +02:00
|
|
|
if err := tmplForm.Execute(w, ¶ms); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
2019-04-06 21:31:11 +02:00
|
|
|
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")
|
2019-04-14 21:25:21 +02:00
|
|
|
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() {
|
2019-04-25 20:33:51 +02:00
|
|
|
if len(os.Args) < 3 {
|
|
|
|
log.Fatalf("usage: %s ADDRESS BDF-FILE...\n", os.Args[0])
|
2019-04-12 23:50:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 20:33:51 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-04-12 23:50:23 +02:00
|
|
|
|
2019-04-25 20:33:51 +02:00
|
|
|
r, _ := font.BoundString(font.Name)
|
|
|
|
super := r.Inset(-3)
|
2019-04-12 23:50:23 +02:00
|
|
|
|
2019-04-25 20:33:51 +02:00
|
|
|
img := image.NewRGBA(super)
|
|
|
|
draw.Draw(img, super, image.White, image.ZP, draw.Src)
|
2021-06-09 23:16:50 +02:00
|
|
|
font.DrawString(img, image.ZP, color.Black, font.Name)
|
2019-04-25 20:33:51 +02:00
|
|
|
|
|
|
|
fonts = append(fonts, &fontItem{Path: path, Font: font, Preview: img})
|
2019-04-06 21:31:11 +02:00
|
|
|
}
|
|
|
|
|
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-13 05:38:58 +02:00
|
|
|
log.Fatalln(http.ListenAndServe(address, nil))
|
2019-04-06 21:31:11 +02:00
|
|
|
}
|