Compare commits
No commits in common. "32ad36a287f1eded549e42ad1bed77acb148be3b" and "4dade55387515e3ed153a5901672abdfb7f3654f" have entirely different histories.
32ad36a287
...
4dade55387
|
@ -4,74 +4,114 @@ import (
|
|||
"errors"
|
||||
"html/template"
|
||||
"image"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/qr"
|
||||
|
||||
"janouch.name/sklad/bdf"
|
||||
"janouch.name/sklad/imgutil"
|
||||
"janouch.name/sklad/label"
|
||||
"janouch.name/sklad/ql"
|
||||
)
|
||||
|
||||
var font *bdf.Font
|
||||
|
||||
func genLabelForHeight(text string, height, scale int) image.Image {
|
||||
// Create a scaled bitmap of the text label.
|
||||
textRect, _ := font.BoundString(text)
|
||||
textImg := image.NewRGBA(textRect)
|
||||
draw.Draw(textImg, textRect, image.White, image.ZP, draw.Src)
|
||||
font.DrawString(textImg, image.ZP, text)
|
||||
|
||||
scaledTextImg := imgutil.Scale{Image: textImg, Scale: scale}
|
||||
scaledTextRect := scaledTextImg.Bounds()
|
||||
|
||||
remains := height - scaledTextRect.Dy() - 20
|
||||
|
||||
width := scaledTextRect.Dx()
|
||||
if remains > width {
|
||||
width = remains
|
||||
}
|
||||
|
||||
// Create a scaled bitmap of the QR code.
|
||||
qrImg, _ := qr.Encode(text, qr.H, qr.Auto)
|
||||
qrImg, _ = barcode.Scale(qrImg, remains, remains)
|
||||
qrRect := qrImg.Bounds()
|
||||
|
||||
// Combine.
|
||||
combinedRect := image.Rect(0, 0, width, height)
|
||||
combinedImg := image.NewRGBA(combinedRect)
|
||||
draw.Draw(combinedImg, combinedRect, image.White, image.ZP, draw.Src)
|
||||
draw.Draw(combinedImg,
|
||||
combinedRect.Add(image.Point{X: (width - qrRect.Dx()) / 2, Y: 0}),
|
||||
qrImg, image.ZP, draw.Src)
|
||||
|
||||
target := image.Rect(
|
||||
(width-scaledTextRect.Dx())/2, qrRect.Dy()+20,
|
||||
combinedRect.Max.X, combinedRect.Max.Y)
|
||||
draw.Draw(combinedImg, target, &scaledTextImg, scaledTextRect.Min, draw.Src)
|
||||
return combinedImg
|
||||
}
|
||||
|
||||
var tmpl = template.Must(template.New("form").Parse(`
|
||||
<!DOCTYPE html>
|
||||
<html><body>
|
||||
<h1>PT-CBP label printing tool</h1>
|
||||
<table><tr>
|
||||
<td valign=top>
|
||||
<img border=1 src='?img&scale={{.Scale}}&text={{.Text}}'>
|
||||
</td>
|
||||
<td valign=top>
|
||||
<fieldset>
|
||||
{{ if .Printer }}
|
||||
<!DOCTYPE html>
|
||||
<html><body>
|
||||
<h1>PT-CBP label printing tool</h1>
|
||||
<table><tr>
|
||||
<td valign=top>
|
||||
<img border=1 src='?img&scale={{.Scale}}&text={{.Text}}'>
|
||||
</td>
|
||||
<td valign=top>
|
||||
<fieldset>
|
||||
{{ if .Printer }}
|
||||
|
||||
<p>Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }}
|
||||
<p>Tape:
|
||||
{{ if .Printer.LastStatus }}
|
||||
{{ .Printer.LastStatus.MediaWidthMM }} mm ×
|
||||
{{ .Printer.LastStatus.MediaLengthMM }} mm
|
||||
<p>Printer: {{ .Printer.Manufacturer }} {{ .Printer.Model }}
|
||||
<p>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 .MediaInfo }}
|
||||
(offset: {{ .MediaInfo.SideMarginPins }} pt,
|
||||
print area: {{ .MediaInfo.PrintAreaPins }} pt)
|
||||
{{ else }}
|
||||
(unknown media)
|
||||
{{ end }}
|
||||
|
||||
{{ if .Printer.LastStatus.Errors }}
|
||||
{{ range .Printer.LastStatus.Errors }}
|
||||
<p>Error: {{ . }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ if .Printer.LastStatus.Errors }}
|
||||
{{ range .Printer.LastStatus.Errors }}
|
||||
<p>Error: {{ . }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
||||
{{ if .InitErr }}
|
||||
{{ .InitErr }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ if .InitErr }}
|
||||
{{ .InitErr }}
|
||||
{{ end }}
|
||||
|
||||
{{ else }}
|
||||
<p>Error: {{ .PrinterErr }}
|
||||
{{ end }}
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<p>Font: {{ .Font.Name }}
|
||||
</fieldset>
|
||||
<form><fieldset>
|
||||
<p><label for=text>Text:</label>
|
||||
<input id=text name=text value='{{.Text}}'>
|
||||
<label for=scale>Scale:</label>
|
||||
<input id=scale name=scale value='{{.Scale}}' size=1>
|
||||
<p><input type=submit value='Update'>
|
||||
<input type=submit name=print value='Update and Print'>
|
||||
</fieldset></form>
|
||||
</td>
|
||||
</tr></table>
|
||||
</body></html>
|
||||
{{ else }}
|
||||
<p>Error: {{ .PrinterErr }}
|
||||
{{ end }}
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<p>Font: {{ .Font.Name }}
|
||||
</fieldset>
|
||||
<form><fieldset>
|
||||
<p><label for=text>Text:</label>
|
||||
<input id=text name=text value='{{.Text}}'>
|
||||
<label for=scale>Scale:</label>
|
||||
<input id=scale name=scale value='{{.Scale}}' size=1>
|
||||
<p><input type=submit value='Update'>
|
||||
<input type=submit name=print value='Update and Print'>
|
||||
</fieldset></form>
|
||||
</td>
|
||||
</tr></table>
|
||||
</body></html>
|
||||
`))
|
||||
|
||||
func getPrinter() (*ql.Printer, error) {
|
||||
|
@ -143,12 +183,12 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
|||
params.Scale = 3
|
||||
}
|
||||
|
||||
var img image.Image
|
||||
var label image.Image
|
||||
if mediaInfo != nil {
|
||||
img = &imgutil.LeftRotate{Image: label.GenLabelForHeight(
|
||||
font, params.Text, mediaInfo.PrintAreaPins, params.Scale)}
|
||||
label = &imgutil.LeftRotate{Image: genLabelForHeight(
|
||||
params.Text, mediaInfo.PrintAreaPins, params.Scale)}
|
||||
if r.FormValue("print") != "" {
|
||||
if err := printer.Print(img); err != nil {
|
||||
if err := printer.Print(label); err != nil {
|
||||
log.Println("print error:", err)
|
||||
}
|
||||
}
|
||||
|
@ -166,7 +206,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
if err := png.Encode(w, img); err != nil {
|
||||
if err := png.Encode(w, label); err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -108,14 +108,16 @@ func (p *Printer) Initialize() error {
|
|||
|
||||
// Flush any former responses in the printer's queue.
|
||||
//
|
||||
// I haven't checked if this is the kernel driver or the printer doing
|
||||
// the buffering that causes data to be returned at this point.
|
||||
var dummy [32]byte
|
||||
for {
|
||||
if _, err := p.File.Read(dummy[:]); err == io.EOF {
|
||||
break
|
||||
// I'm not sure if this is necessary, or rather whether the kernel driver
|
||||
// does any buffering that could cause data to be returned at this point.
|
||||
/*
|
||||
var dummy [32]byte
|
||||
for {
|
||||
if _, err := f.Read(dummy[:]); err == io.EOF {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
21
sklad/db.go
21
sklad/db.go
|
@ -7,8 +7,6 @@ import (
|
|||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"janouch.name/sklad/bdf"
|
||||
)
|
||||
|
||||
type Series struct {
|
||||
|
@ -48,9 +46,6 @@ type Database struct {
|
|||
Prefix string // prefix for all container IDs
|
||||
Series []*Series // all known series
|
||||
Containers []*Container // all known containers
|
||||
|
||||
BDFPath string // path to bitmap font file
|
||||
BDFScale int // integer scaling for the bitmap font
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -62,8 +57,6 @@ var (
|
|||
indexSeries = map[string]*Series{}
|
||||
indexContainer = map[ContainerId]*Container{}
|
||||
indexChildren = map[ContainerId][]*Container{}
|
||||
|
||||
labelFont *bdf.Font
|
||||
)
|
||||
|
||||
// TODO: Some functions to add, remove and change things in the database.
|
||||
|
@ -199,20 +192,6 @@ func loadDatabase() error {
|
|||
}
|
||||
}
|
||||
|
||||
// Prepare label printing.
|
||||
if db.BDFScale <= 0 {
|
||||
db.BDFScale = 1
|
||||
}
|
||||
|
||||
if f, err := os.Open(db.BDFPath); err != nil {
|
||||
return fmt.Errorf("cannot load label font: %s", err)
|
||||
} else {
|
||||
defer f.Close()
|
||||
if labelFont, err = bdf.NewFromBDF(f); err != nil {
|
||||
return fmt.Errorf("cannot load label font: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Open database log file for appending.
|
||||
if dbLog, err = os.OpenFile(dbPath+".log",
|
||||
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil {
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
{{ define "Title" }}Tisk štĂtku{{ end }}
|
||||
{{ define "Content" }}
|
||||
<h2>Tisk štĂtku pro {{ .Id }}</h2>
|
||||
|
||||
{{ if .UnknownId }}
|
||||
<p>Neznámý obal.
|
||||
{{ else if .Error }}
|
||||
<p>Tisk selhal: {{ .Error }}
|
||||
{{ else }}
|
||||
<p>Tisk proběhl úspěšně.
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
|
@ -1,7 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
|
@ -10,10 +9,6 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"janouch.name/sklad/imgutil"
|
||||
"janouch.name/sklad/label"
|
||||
"janouch.name/sklad/ql"
|
||||
)
|
||||
|
||||
var templates = map[string]*template.Template{}
|
||||
|
@ -175,61 +170,18 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
|||
executeTemplate("search.tmpl", w, ¶ms)
|
||||
}
|
||||
|
||||
func printLabel(id string) error {
|
||||
printer, err := ql.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if printer == nil {
|
||||
return errors.New("no suitable printer found")
|
||||
}
|
||||
defer printer.Close()
|
||||
|
||||
/*
|
||||
printer.StatusNotify = func(status *ql.Status) {
|
||||
log.Printf("\x1b[1mreceived status\x1b[m\n%+v\n%s",
|
||||
status[:], status)
|
||||
}
|
||||
*/
|
||||
|
||||
if err := printer.Initialize(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := printer.UpdateStatus(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mediaInfo := ql.GetMediaInfo(
|
||||
printer.LastStatus.MediaWidthMM(),
|
||||
printer.LastStatus.MediaLengthMM(),
|
||||
)
|
||||
if mediaInfo == nil {
|
||||
return errors.New("unknown media")
|
||||
}
|
||||
|
||||
return printer.Print(&imgutil.LeftRotate{Image: label.GenLabelForHeight(
|
||||
labelFont, id, mediaInfo.PrintAreaPins, db.BDFScale)})
|
||||
}
|
||||
|
||||
func handleLabel(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
params := struct {
|
||||
Id string
|
||||
UnknownId bool
|
||||
Error error
|
||||
}{
|
||||
Id: r.FormValue("id"),
|
||||
}
|
||||
id := r.FormValue("id")
|
||||
_ = id
|
||||
|
||||
if c := indexContainer[ContainerId(params.Id)]; c == nil {
|
||||
params.UnknownId = true
|
||||
} else {
|
||||
params.Error = printLabel(params.Id)
|
||||
}
|
||||
// TODO: See if such a container exists, print a label on the printer.
|
||||
|
||||
params := struct{}{}
|
||||
|
||||
executeTemplate("label.tmpl", w, ¶ms)
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ func sessionWrap(inner func(http.ResponseWriter, *http.Request)) func(
|
|||
w.Header().Set("Cache-Control", "no-store")
|
||||
|
||||
redirect := "/login"
|
||||
if r.RequestURI != "/" && r.Method == http.MethodGet {
|
||||
if r.RequestURI != "/" {
|
||||
redirect += "?redirect=" + url.QueryEscape(r.RequestURI)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue