label-tool: allow choosing from multiple fonts
This commit is contained in:
parent
5cef6b240a
commit
154f3147e3
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
"image"
|
"image"
|
||||||
|
"image/draw"
|
||||||
"image/png"
|
"image/png"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -16,15 +17,26 @@ import (
|
||||||
"janouch.name/sklad/ql"
|
"janouch.name/sklad/ql"
|
||||||
)
|
)
|
||||||
|
|
||||||
var font *bdf.Font
|
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>
|
||||||
|
`))
|
||||||
|
|
||||||
var tmpl = template.Must(template.New("form").Parse(`
|
var tmplForm = template.Must(template.New("form").Parse(`
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html><body>
|
<html><body>
|
||||||
<h1>PT-CBP label printing tool</h1>
|
<h1>PT-CBP label printing tool</h1>
|
||||||
<table><tr>
|
<table><tr>
|
||||||
<td valign=top>
|
<td valign=top>
|
||||||
<img border=1 src='?img&scale={{.Scale}}&text={{.Text}}'>
|
<img border=1 src='?font={{ .FontIndex }}&scale={{ .Scale }}{{/*
|
||||||
|
*/}}&text={{ .Text }}&render'>
|
||||||
</td>
|
</td>
|
||||||
<td valign=top>
|
<td valign=top>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
|
@ -59,9 +71,10 @@ var tmpl = template.Must(template.New("form").Parse(`
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<p>Font: {{ .Font.Name }}
|
<p>Font: {{ .Font.Name }} <a href='?'>Change</a>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<form><fieldset>
|
<form><fieldset>
|
||||||
|
<input type=hidden name=font value='{{ .FontIndex }}'>
|
||||||
<p><label for=text>Text:</label>
|
<p><label for=text>Text:</label>
|
||||||
<input id=text name=text value='{{.Text}}'>
|
<input id=text name=text value='{{.Text}}'>
|
||||||
<label for=scale>Scale:</label>
|
<label for=scale>Scale:</label>
|
||||||
|
@ -74,6 +87,14 @@ var tmpl = template.Must(template.New("form").Parse(`
|
||||||
</body></html>
|
</body></html>
|
||||||
`))
|
`))
|
||||||
|
|
||||||
|
type fontItem struct {
|
||||||
|
Path string
|
||||||
|
Font *bdf.Font
|
||||||
|
Preview image.Image
|
||||||
|
}
|
||||||
|
|
||||||
|
var fonts = []*fontItem{}
|
||||||
|
|
||||||
func getPrinter() (*ql.Printer, error) {
|
func getPrinter() (*ql.Printer, error) {
|
||||||
printer, err := ql.Open()
|
printer, err := ql.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -96,8 +117,28 @@ func getStatus(printer *ql.Printer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handle(w http.ResponseWriter, r *http.Request) {
|
func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := r.ParseForm(); err != nil {
|
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)
|
http.Error(w, err.Error(), 500)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,6 +167,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
InitErr error
|
InitErr error
|
||||||
MediaInfo *ql.MediaInfo
|
MediaInfo *ql.MediaInfo
|
||||||
Font *bdf.Font
|
Font *bdf.Font
|
||||||
|
FontIndex int
|
||||||
Text string
|
Text string
|
||||||
Scale int
|
Scale int
|
||||||
}{
|
}{
|
||||||
|
@ -133,11 +175,11 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
PrinterErr: printerErr,
|
PrinterErr: printerErr,
|
||||||
InitErr: initErr,
|
InitErr: initErr,
|
||||||
MediaInfo: mediaInfo,
|
MediaInfo: mediaInfo,
|
||||||
Font: font,
|
Font: font.Font,
|
||||||
|
FontIndex: fontIndex,
|
||||||
Text: r.FormValue("text"),
|
Text: r.FormValue("text"),
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
|
||||||
params.Scale, err = strconv.Atoi(r.FormValue("scale"))
|
params.Scale, err = strconv.Atoi(r.FormValue("scale"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
params.Scale = 3
|
params.Scale = 3
|
||||||
|
@ -146,7 +188,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
var img image.Image
|
var img image.Image
|
||||||
if mediaInfo != nil {
|
if mediaInfo != nil {
|
||||||
img = &imgutil.LeftRotate{Image: label.GenLabelForHeight(
|
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 r.FormValue("print") != "" {
|
||||||
if err := printer.Print(img); err != nil {
|
if err := printer.Print(img); err != nil {
|
||||||
log.Println("print error:", err)
|
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")
|
w.Header().Set("Content-Type", "text/html")
|
||||||
tmpl.Execute(w, ¶ms)
|
tmplForm.Execute(w, ¶ms)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,24 +215,32 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) != 3 {
|
if len(os.Args) < 3 {
|
||||||
log.Fatalf("usage: %s ADDRESS BDF-FILE\n", os.Args[0])
|
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 {
|
||||||
var err error
|
fi, err := os.Open(path)
|
||||||
fi, err := os.Open(bdfPath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
font, err := bdf.NewFromBDF(fi)
|
||||||
font, err = bdf.NewFromBDF(fi)
|
if err != nil {
|
||||||
|
log.Fatalf("%s: %s\n", path, err)
|
||||||
|
}
|
||||||
if err := fi.Close(); err != nil {
|
if err := fi.Close(); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
if 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")
|
log.Println("starting server")
|
||||||
|
|
Loading…
Reference in New Issue